diff options
Major Game Mechanics implementation
3 files changed, 247 insertions, 32 deletions
| -rw-r--r-- | scripts/core/config.js | 10 | ||||
| -rw-r--r-- | scripts/core/game.js | 261 | ||||
| -rw-r--r-- | scripts/core/home.js | 8 |
diff --git a/scripts/core/config.js b/scripts/core/config.js new file mode 100644 index 0000000..4f06f7b --- /dev/null +++ b/scripts/core/config.js @@ -0,0 +1,10 @@ +var app = app || {}; + +const config = (function () { + return { + SERVICE_URL: 'https://localhost:44385', + USER: {}, + }; +}()); + +app.config = config; diff --git a/scripts/core/game.js b/scripts/core/game.js index 2d846e9..6d0646c 100644 --- a/scripts/core/game.js +++ b/scripts/core/game.js @@ -2,12 +2,17 @@ var app = app || {}; class Game { constructor() { + this.isLoaded = false; + this.playerColor = null; this.appStage = null; this.pixiApplication = null; - this.gameObjects = []; + this.isRunning = false; + this.gameObjects = []; // TODO: Make into player container + this.shells = new ShellContainer(); this.config = { sprites: {}, sounds: {}, + state: {}, }; } @@ -37,40 +42,211 @@ class Game { this.config.sprites["giga-tank-red"] = new PIXI.Sprite(PIXI.loader.resources["images/giga-tank-red.png"].texture); this.config.sprites["laser-tank-red"] = new PIXI.Sprite(PIXI.loader.resources["images/laser-tank-red.png"].texture); this.config.sprites["basic-tank-red"] = new PIXI.Sprite(PIXI.loader.resources["images/basic-tank-red.png"].texture); - this.config.sprites["shell"] = new PIXI.Sprite(PIXI.loader.resources["images/shell.png"].texture); + this.config.sprites["shell"] = () => new PIXI.Sprite(PIXI.loader.resources["images/shell.png"].texture); - // Giga Tank - this.config.sprites["giga-tank-blue"].width = 100; - this.config.sprites["giga-tank-blue"].height = 80; - this.config.sprites["giga-tank-red"].width = 100; - this.config.sprites["giga-tank-red"].height = 80; + callback(); + }); + } - // Laser Tank - this.config.sprites["laser-tank-blue"].width = 75; - this.config.sprites["laser-tank-blue"].height = 60; - this.config.sprites["laser-tank-red"].width = 75; - this.config.sprites["laser-tank-red"].height = 60; + getCurrentPlayerTank() { + return this.gameObjects + .filter(go => go instanceof Tank) + .find(go => go.color === this.playerColor); + } - // Basic Tank - this.config.sprites["basic-tank-blue"].width = 50; - this.config.sprites["basic-tank-blue"].height = 40; - this.config.sprites["basic-tank-red"].width = 50; - this.config.sprites["basic-tank-red"].height = 40; + getOtherPlayerTank() { + return this.gameObjects + .filter(go => go instanceof Tank) + .find(go => go.color !== this.playerColor); + } - // Shells - this.config.sprites["shell"].width = 25; - this.config.sprites["shell"].height = 25; + dispatch(command) { + const currentPlayerTank = this.getCurrentPlayerTank(); + let result = false; - callback(); - }); + switch (command) { + case "left": { + result = true; + break; + } + case "up": { + result = true; + break; + } + case "right": { + result = true; + break; + } + case "down": { + result = true; + break; + } + case "rotate-left": { + currentPlayerTank.rotateLeft(); + result = true; + break; + } + case "rotate-right": { + currentPlayerTank.rotateRight(); + result = true; + break; + } + case "pew": { + this.config.sounds.pew.play(); + result = true; + break; + } + default: + alert('Invalid command!') + } + + if (result) { + app.socketService.send('game', 'Update', command); + } + + return result; + } + + updateShells() { + this.shells.each(shell => { + if (!this.config.state.shells?.some(stateShell => stateShell.id === shell.id)) { + shell.isDestroyed = true; + } + }); + + this.config.state.shells?.forEach(stateShell => { + if (!this.shells.contains(stateShell.id)) { + const shell = new TankShell( + stateShell.id, + this.config.sprites["shell"].call(), + {width: 25, height: 25}, + {x: stateShell.x, y: stateShell.y}, + stateShell.direction, + ); + + this.shells.add(shell); + } else { + const currentShell = this.shells.get(stateShell.id); + currentShell.x = stateShell.x; + currentShell.y = stateShell.y; + } + }); + + this.shells.update(); + } + + updatePlayers() { + const currentTank = this.getCurrentPlayerTank(); + const otherTank = this.getOtherPlayerTank(); + if(currentTank && otherTank && this.playerColor === 'blue') { + currentTank.x = this.config.state.blue.x; + currentTank.y = this.config.state.blue.y; + otherTank.x = this.config.state.red.x; + otherTank.y = this.config.state.red.y; + } else if(currentTank && otherTank && this.playerColor === 'red') { + currentTank.x = this.config.state.red.x; + currentTank.y = this.config.state.red.y; + otherTank.x = this.config.state.blue.x; + otherTank.y = this.config.state.blue.y; + } } update() { - // this.gameObjects.forEach(gameObject => gameObject.update()); + app.socketService.send('game', 'GetState'); + + this.updateShells(); + this.updatePlayers(); + + this.gameObjects.forEach(gameObject => gameObject.update()); } draw() { - // this.gameObjects.forEach(gameObject => gameObject.draw()); + this.gameObjects.forEach(gameObject => this.pixiApplication.stage.addChild(gameObject.draw())); + this.shells.each(shell => this.pixiApplication.stage.addChild(shell.draw())); + } + + initGameObjects() { + const bluePlayer = new Tank( + 'blue', + 'basic', + this.config.sprites['basic-tank-blue'], + { + width: 50, + height: 40 + }, + { + x: 50, + y: 384 + }, + 'right'); + + const redPlayer = new Tank( + 'red', + 'basic', + this.config.sprites['basic-tank-red'], + { + width: 50, + height: 40 + }, + { + x: 1266, + y: 384 + }, + 'left'); + + this.gameObjects.push(bluePlayer); + this.gameObjects.push(redPlayer); + }; + + initEvents() { + app.eventService.addEventHandler('OtherUp', () => { + this.getOtherPlayerTank().moveUp(); + }); + + app.eventService.addEventHandler('OtherRight', () => { + this.getOtherPlayerTank().moveRight(); + }); + + app.eventService.addEventHandler('OtherDown', () => { + this.getOtherPlayerTank().moveDown(); + }); + + app.eventService.addEventHandler('OtherLeft', () => { + this.getOtherPlayerTank().moveLeft(); + }); + + app.eventService.addEventHandler('OtherRotateLeft', () => { + this.getOtherPlayerTank().rotateLeft(); + }); + + app.eventService.addEventHandler('OtherRotateRight', () => { + this.getOtherPlayerTank().rotateRight(); + }); + + app.eventService.addEventHandler('GameState', (state) => { + this.config.state = state; + }); + } + + gameLoop() { + if (this.isRunning) { + setTimeout(() => { + }, 17); + requestAnimationFrame(() => this.update()); + requestAnimationFrame(() => this.draw()); + requestAnimationFrame(() => this.gameLoop()); + } + } + + loadResources(callback) { + if (!this.isLoaded) { + this.isLoaded = true; + this.loadSounds(() => { + }); + this.loadSprites(callback); + } else { + callback(); + } } init() { @@ -91,17 +267,42 @@ class Game { this.pixiApplication = new PIXI.Application(appOptions); this.pixiApplication.renderer.autoResize = true; - this.loadSounds(() => {}); - this.loadSprites(this.draw); - - this.pixiApplication.ticker.add((delta) => { - this.update(); + this.loadResources(() => { + this.initGameObjects(); + this.draw(); }); + + this.initEvents(); + this.isRunning = true; + this.gameLoop(); } - load() { + reset() { + this.appStage.innerHTML = ''; + this.playerColor = null; + this.appStage = null; + this.pixiApplication = null; + this.isRunning = false; + this.gameObjects = []; // TODO: Make into player container + this.shells = new ShellContainer(); + this.config.state = {}; + } + + // -> move to init + load(playerColor) { + this.playerColor = playerColor; this.appStage = app.htmlService.getElement('.app-stage'); this.appStage.appendChild(this.pixiApplication.view); + + app.htmlService.addEventHandler('#chat', 'keyup', (e) => { + if (e.key === 'Enter' || e.keyCode === 13) { + const command = e.target.value; + + if (this.dispatch(command)) { + e.target.value = ""; + } + } + }); } } diff --git a/scripts/core/home.js b/scripts/core/home.js index 625d3ad..b9c4ea8 100644 --- a/scripts/core/home.js +++ b/scripts/core/home.js @@ -2,7 +2,11 @@ var app = app || {}; const supportedCommands = { '-play': { - invoke: () => {}, + invoke: () => { + app.socketService.initGame().finally(() => { + app.socketService.send('game', 'Login', app.userService.user); + }); + }, help: 'Initiate matchmaking to find an opponent and play the game.', }, '-help': { @@ -55,7 +59,7 @@ class Home { if(this.isCommand(message)) { this.dispatch(message); } else { - app.socketService.send('SendMessage', app.userService.user, message); + app.socketService.send('home', 'SendMessage', app.userService.user, message); } } } |