import AbstractView from './AbstractView.js' export default class extends AbstractView { constructor(params) { super(params, 'Game'); this.game = null; } async getHtml() { return `

Game

`; } async postInit() { document.getElementById('startGameButton').onclick = this.startGame.bind(this); document.getElementById('stopGameButton').onclick = this.stopGame.bind(this); } startGame() { if (this.game == null) { document.getElementById('startGameButton').innerHTML = 'Reset Game'; this.game = new Game; } else { document.getElementById('app').removeChild(this.game.canvas); this.game.cleanup(); this.game = new Game; } } stopGame() { if (!this.game) return; document.getElementById('app').removeChild(this.game.canvas); this.game.cleanup(); this.game = null; document.getElementById('startGameButton').innerHTML = 'Start Game'; } } class Game { constructor() { //Global variables this.globVars = { CANVASHEIGHT: 270, CANVASWIDTH: 480, PADDLEHEIGHT: 70, PADDLEWIDTH: 10, PADDLEMARGIN: 5, PADDLESPEED: 3, BALLRADIUS: 5, BALLSPEED: 2, MAXBOUNCEANGLE: 10 * (Math.PI / 12) }; this.canvas = document.createElement('canvas'); this.canvas.id = 'gameCanvas'; this.canvas.width = this.globVars.CANVASWIDTH; this.canvas.height = this.globVars.CANVASHEIGHT; this.canvas.style.border = '1px solid #d3d3d3'; this.canvas.style.backgroundColor = '#f1f1f1'; this.context = this.canvas.getContext('2d'); document.getElementById('app').appendChild(this.canvas); this.paddles = [ new Paddle(this.context, this.globVars.PADDLEMARGIN, this.globVars ), new Paddle(this.context, this.globVars.CANVASWIDTH - this.globVars.PADDLEMARGIN - this.globVars.PADDLEWIDTH, this.globVars ) ]; this.ball = new Ball(this.context, this.globVars); this.interval = setInterval(this.updateGame.bind(this), 10); this.keys = []; this.keyUpHandler = this.keyUpHandler.bind(this); this.keyDownHandler = this.keyDownHandler.bind(this); document.addEventListener('keydown', this.keyDownHandler); document.addEventListener('keyup', this.keyUpHandler); } cleanup() { clearInterval(this.interval); document.removeEventListener('keydown', this.keyDownHandler); document.removeEventListener('keyup', this.keyUpHandler); } clear() { this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); } updateGame() { if (this.keys.includes('Escape')) this.cleanup(); //Paddle movement if (this.keys.includes('s') && this.paddles[0].y + this.globVars.PADDLEHEIGHT < this.globVars.CANVASHEIGHT - this.globVars.PADDLEMARGIN) this.paddles[0].y += this.globVars.PADDLESPEED; if (this.keys.includes('w') && this.paddles[0].y > 0 + this.globVars.PADDLEMARGIN) this.paddles[0].y -= this.globVars.PADDLESPEED; if (this.keys.includes('ArrowDown') && this.paddles[1].y + this.globVars.PADDLEHEIGHT < this.globVars.CANVASHEIGHT - this.globVars.PADDLEMARGIN) this.paddles[1].y += this.globVars.PADDLESPEED; if (this.keys.includes('ArrowUp') && this.paddles[1].y > 0 + this.globVars.PADDLEMARGIN) this.paddles[1].y -= this.globVars.PADDLESPEED; //Ball collisions if (this.detectCollision(this.paddles[0], this.ball)) this.calculateBallVelocity(this.paddles[0].getCenter().y, this.ball); else if (this.detectCollision(this.paddles[1], this.ball)) this.calculateBallVelocity(this.paddles[1].getCenter().y, this.ball, -1); if (this.ball.y - this.ball.radius <= 0) this.ball.vy *= -1; else if (this.ball.y + this.ball.radius >= this.canvas.height) this.ball.vy *= -1; this.ball.x += this.ball.vx; this.ball.y += this.ball.vy; this.clear(); this.paddles[0].update(); this.paddles[1].update(); this.ball.update(); } detectCollision(paddle, ball) { let paddleCenter = paddle.getCenter(); let dx = Math.abs(ball.x - paddleCenter.x); let dy = Math.abs(ball.y - paddleCenter.y); if (dx <= ball.radius + paddle.width / 2 && dy <= ball.radius + paddle.height / 2) return true; return false; } calculateBallVelocity(paddleCenterY, ball, side = 1) { let relativeIntersectY = paddleCenterY - ball.y; let normRelIntersectY = relativeIntersectY / this.globVars.PADDLEHEIGHT / 2; let bounceAngle = normRelIntersectY * this.globVars.MAXBOUNCEANGLE; ball.vx = this.globVars.BALLSPEED * side * Math.cos(bounceAngle); ball.vy = this.globVars.BALLSPEED * -Math.sin(bounceAngle); } keyUpHandler(ev) { const idx = this.keys.indexOf(ev.key); if (idx != -1) this.keys.splice(idx, 1); } keyDownHandler(ev) { if (!this.keys.includes(ev.key)) this.keys.push(ev.key); } } class Paddle { constructor(context, paddleSide, globVars) { this.width = globVars.PADDLEWIDTH; this.height = globVars.PADDLEHEIGHT; this.x = paddleSide; this.y = globVars.CANVASHEIGHT / 2 - this.height / 2; this.ctx = context; this.update(); } update() { this.ctx.fillStyle = 'black'; this.ctx.fillRect(this.x, this.y, this.width, this.height); } getCenter() { return { x: this.x + this.width / 2, y: this.y + this.height / 2 }; } } class Ball { constructor(context, globVars) { this.radius = globVars.BALLRADIUS; this.x = globVars.CANVASWIDTH / 2; this.y = globVars.CANVASHEIGHT / 2; this.vx = -globVars.BALLSPEED; this.vy = 0; this.ctx = context; this.update(); } update() { this.ctx.fillStyle = 'black'; this.ctx.beginPath(); this.ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI); this.ctx.fill(); } }