World.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. require('MouseInput');
  2. require('Camera');
  3. require('HumanController');
  4. require('Player');
  5. require('HeadsUpDisplay');
  6. require('Sheep');
  7. require('SheepController');
  8. class World {
  9. constructor() {
  10. this.game = null;
  11. this.system = null;
  12. this.player = null;
  13. this.mouse = null;
  14. this.camera = null;
  15. this.dialog = null;
  16. this.ui = null;
  17. this.curtain = null;
  18. this.isStarted = false;
  19. this.sheeps = [];
  20. }
  21. init(systemObject, canvas) {
  22. this.game = this;
  23. this.canvas = canvas;
  24. this.canvas.style.cursor = "auto";
  25. this.system = systemObject;
  26. this.mouse = new MouseInput();
  27. this.mouse.attach(canvas, this);
  28. this.camera = new Camera();
  29. this.camera.init(canvas);
  30. this.player = new Player(this.game, new HumanController());
  31. this.player.id = 1;
  32. this.player.init(systemObject);
  33. this.sheeps = [];
  34. for(let i = 0; i < 4; i++) {
  35. let sheep = new Sheep(this.game, new SheepController());
  36. sheep.init(systemObject);
  37. this.sheeps.push(sheep);
  38. }
  39. this.ui = new HeadsUpDisplay(this.game, this.camera);
  40. this.ui.init(this.player, canvas);
  41. var stage = this;
  42. this.curtain = new Curtain();
  43. this.curtain.init(systemObject, canvas);
  44. this.curtain.open(700, function () {
  45. stage.isStarted = true;
  46. });
  47. }
  48. end() {
  49. this.mouse.detach();
  50. }
  51. updateDelta(delta, canvas) {
  52. if (this.system.keyboard.isDown(Keys.Escape)) {
  53. this.curtain.close(1000, function () {
  54. this.system.theater.changeStage("mainmenu");
  55. });
  56. }
  57. this.update(canvas);
  58. this.player.update(delta);
  59. for(let index in this.sheeps) {
  60. this.sheeps[index].update(delta);
  61. }
  62. this.camera.update(canvas, this.player, delta);
  63. this.ui.update(delta);
  64. this.curtain.updateDelta(delta);
  65. }
  66. update(canvas) {
  67. this.player.control();
  68. for(let index in this.sheeps) {
  69. this.sheeps[index].control(this.player);
  70. }
  71. }
  72. draw(context) {
  73. context.save();
  74. context.translate(this.camera.position.x, this.camera.position.y);
  75. for(let index in this.sheeps) {
  76. this.sheeps[index].draw(context);
  77. }
  78. this.player.draw(context);
  79. context.restore();
  80. this.ui.draw(context);
  81. this.curtain.draw(context);
  82. }
  83. handleResize(canvas) {
  84. this.camera.handleResize(canvas);
  85. }
  86. mouseMove(canvas, x, y) {
  87. this.player.controller.mouseMove(this.camera, x, y);
  88. }
  89. mouseDown(canvas, button, x, y) {
  90. this.player.controller.mouseDown(this.camera, button, x, y);
  91. }
  92. mouseUp(canvas, button, x, y) {
  93. this.player.controller.mouseUp(this.camera, button, x, y);
  94. }
  95. touchStart(canvas, x, y) {
  96. this.player.controller.touchStart(this.camera, x, y);
  97. }
  98. touchMove(canvas, x, y) {
  99. this.player.controller.touchMove(this.camera, x, y);
  100. }
  101. touchEnd(canvas, x, y) {
  102. this.player.controller.touchEnd(this.camera, x, y);
  103. }
  104. contextMenu(canvas, event) {
  105. this.player.controller.contextMenu(this.camera, event);
  106. }
  107. };