Game.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. require("Paddle");
  2. require("Ball");
  3. class Game {
  4. engine = null;
  5. paddles = {};
  6. gameActive = true;
  7. constructor() {
  8. this.paddles['left'] = new Paddle('left');
  9. this.paddles['right'] = new Paddle('right');
  10. this.ball = new Ball();
  11. }
  12. init(engine) {
  13. this.engine = engine;
  14. this.rounds = 11;
  15. for(let index in this.paddles) {
  16. this.paddles[index].init(this);
  17. }
  18. this.ball.init(this);
  19. this.gameActive = true;
  20. this.winner = "";
  21. this.finalScore = 0;
  22. }
  23. update(delta) {
  24. if(this.ball.isDead() && this.rounds <= 0) {
  25. this.gameActive = false;
  26. this.finalScore = 0;
  27. this.winner = "";
  28. for(let index in this.paddles) {
  29. let paddleScore = this.paddles[index].getScore();
  30. if(paddleScore > this.finalScore) {
  31. this.finalScore = paddleScore;
  32. this.winner = index;
  33. }
  34. }
  35. return;
  36. }
  37. for(let index in this.paddles) {
  38. this.paddles[index].update(delta);
  39. }
  40. this.ball.update(delta);
  41. if(this.ball.isDead()) {
  42. this.rounds--;
  43. if(this.rounds > 0) {
  44. this.ball.init(this);
  45. }
  46. }
  47. }
  48. draw(context) {
  49. if(!this.gameActive) {
  50. context.fillStyle = "#000000";
  51. context.beginPath();
  52. context.rect(0, 0, this.getWidth(), this.getHeight());
  53. context.fill();
  54. context.fillStyle = "#FFFFFF";
  55. context.textAlign = "center";
  56. context.font = "18px Arial";
  57. context.fillText("winner:", this.getWidth() / 2, this.getHeight() * 0.2);
  58. context.font = "30px Arial";
  59. context.fillText(this.winner, this.getWidth() / 2, this.getHeight() * 0.4);
  60. context.font = "48px Arial";
  61. context.fillText(this.finalScore, this.getWidth() / 2, this.getHeight() * 0.7);
  62. return;
  63. }
  64. context.fillStyle = "#000000";
  65. context.textAlign = "center";
  66. context.font = "14px Arial";
  67. for(let index in this.paddles) {
  68. context.fillText(this.paddles[index].getScore(), this.paddles[index].getPosition().x, 10);
  69. this.paddles[index].draw(context);
  70. }
  71. this.ball.draw(context);
  72. }
  73. collideSide(collisionBox) {
  74. let collisionSide = "";
  75. for(let side in collisionBox) {
  76. let collider = collisionBox[side];
  77. if(side == "top" || side == "left") {
  78. if(collider < 0) {
  79. return side;
  80. }
  81. }
  82. if(side == "bottom" && collider > this.getHeight()) {
  83. return side;
  84. }
  85. if(side == "right" && collider > this.getWidth()) {
  86. return side;
  87. }
  88. }
  89. }
  90. getWidth() {
  91. return this.engine.viewport.w;
  92. }
  93. getHeight() {
  94. return this.engine.viewport.h;
  95. }
  96. getBallPosition() {
  97. return {x: this.ball.position.x, y: this.ball.position.y};
  98. }
  99. getPaddle(side) {
  100. return this.paddles[side];
  101. }
  102. }