main.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {entity_manager} from './entity-manager.js';
  2. import {entity} from './entity.js';
  3. import {load_controller} from './load-controller.js';
  4. import {spawners} from './spawners.js';
  5. import {spatial_hash_grid} from './spatial-hash-grid.js';
  6. import {threejs_component} from './threejs-component.js';
  7. import {ammojs_component} from './ammojs-component.js';
  8. import {blaster} from './fx/blaster.js';
  9. import {ui_controller} from './ui-controller.js';
  10. class QuickFPS1 {
  11. constructor() {
  12. this._Initialize();
  13. }
  14. _Initialize() {
  15. this.entityManager_ = new entity_manager.EntityManager();
  16. this.OnGameStarted_();
  17. }
  18. OnGameStarted_() {
  19. this.grid_ = new spatial_hash_grid.SpatialHashGrid(
  20. [[-5000, -5000], [5000, 5000]], [100, 100]);
  21. this.LoadControllers_();
  22. this.previousRAF_ = null;
  23. this.RAF_();
  24. }
  25. LoadControllers_() {
  26. const threejs = new entity.Entity();
  27. threejs.AddComponent(new threejs_component.ThreeJSController());
  28. this.entityManager_.Add(threejs, 'threejs');
  29. const ammojs = new entity.Entity();
  30. ammojs.AddComponent(new ammojs_component.AmmoJSController());
  31. this.entityManager_.Add(ammojs, 'physics');
  32. // Hack
  33. this.ammojs_ = ammojs.GetComponent('AmmoJSController');
  34. this.scene_ = threejs.GetComponent('ThreeJSController').scene_;
  35. this.decals_ = threejs.GetComponent('ThreeJSController').sceneDecals_;
  36. this.camera_ = threejs.GetComponent('ThreeJSController').camera_;
  37. this.threejs_ = threejs.GetComponent('ThreeJSController');
  38. const fx = new entity.Entity();
  39. fx.AddComponent(new blaster.BlasterSystem({
  40. scene: this.scene_,
  41. camera: this.camera_,
  42. texture: 'resources/textures/fx/tracer.png',
  43. }));
  44. this.entityManager_.Add(fx, 'fx');
  45. const l = new entity.Entity();
  46. l.AddComponent(new load_controller.LoadController());
  47. this.entityManager_.Add(l, 'loader');
  48. const u = new entity.Entity();
  49. u.AddComponent(new ui_controller.UIController());
  50. this.entityManager_.Add(u, 'ui');
  51. const basicParams = {
  52. grid: this.grid_,
  53. scene: this.scene_,
  54. decals: this.decals_,
  55. camera: this.camera_,
  56. };
  57. const spawner = new entity.Entity();
  58. spawner.AddComponent(new spawners.PlayerSpawner(basicParams));
  59. spawner.AddComponent(new spawners.Level1Spawner(basicParams));
  60. spawner.AddComponent(new spawners.TargetSpawner(basicParams));
  61. this.entityManager_.Add(spawner, 'spawners');
  62. spawner.GetComponent('PlayerSpawner').Spawn();
  63. spawner.GetComponent('Level1Spawner').Spawn();
  64. }
  65. RAF_() {
  66. requestAnimationFrame((t) => {
  67. if (this.previousRAF_ === null) {
  68. this.previousRAF_ = t;
  69. } else {
  70. this.Step_(t - this.previousRAF_);
  71. this.previousRAF_ = t;
  72. }
  73. setTimeout(() => {
  74. this.RAF_();
  75. }, 1);
  76. });
  77. }
  78. Step_(timeElapsed) {
  79. const timeElapsedS = Math.min(1.0 / 30.0, timeElapsed * 0.001);
  80. this.entityManager_.Update(timeElapsedS);
  81. this.ammojs_.StepSimulation(timeElapsedS);
  82. this.threejs_.Render(timeElapsedS);
  83. }
  84. }
  85. let _APP = null;
  86. window.addEventListener('DOMContentLoaded', async () => {
  87. // const _Setup = () => {
  88. // Ammo().then(function(AmmoLib) {
  89. // Ammo = AmmoLib;
  90. // _APP = new QuickFPS1();
  91. // });
  92. // document.body.removeEventListener('click', _Setup);
  93. // };
  94. // document.body.addEventListener('click', _Setup);
  95. const AmmoLib = await Ammo();
  96. Ammo = AmmoLib;
  97. _APP = new QuickFPS1();
  98. });