From 4c883d2ef63e0e77ccc235d98deea3ed9cf5af8e Mon Sep 17 00:00:00 2001 From: e-a-n Date: Sun, 25 Nov 2018 07:53:59 -0800 Subject: [PATCH] added omni directional keyboard movement per: ticket #81 --- client/config/config.js | 2 +- client/js/gameLoop.js | 14 +++----------- client/js/gameLoopTools/playerUtilities.js | 16 +++++++++------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/client/config/config.js b/client/config/config.js index 56499bf..a0c287c 100644 --- a/client/config/config.js +++ b/client/config/config.js @@ -113,7 +113,7 @@ const config = { }; config.default.player = { - speed: 20, + speed: 4, imageKey: "player", spriteSrc: "../spriteLocation.png" }; diff --git a/client/js/gameLoop.js b/client/js/gameLoop.js index d0dddf1..1d95864 100644 --- a/client/js/gameLoop.js +++ b/client/js/gameLoop.js @@ -32,25 +32,15 @@ gameLoop = { gameLoop.debugMode = false; } }, - keyboardMovement: (player, playerSpeed) => { - if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { - player.x -= playerSpeed; - } - if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { - player.x += playerSpeed; - } - }, movePlayer : (player, speed, type) => { var mouse = 0; var keyboard = 1; if (type === keyboard) { - //gameLoop.keyboardMovement(player, speed); playerUtilities.keyboardMovement(player, speed); } else { - //gameLoop.mouseMovement(player,speed); playerUtilities.mouseMovement(player, speed); } }, @@ -67,6 +57,8 @@ gameLoop = { ]; // setup player gameLoop.player.sprite = game.add.sprite(...playerStartData); + game.physics.enable(gameLoop.player.sprite, Phaser.Physics.ARCADE); + gameLoop.player.sprite.body.collideWorldBounds = true; //1st is x, 2nd is Y! const spriteCenter = [0.5, 0.5]; gameLoop.player.sprite.anchor.setTo(...spriteCenter); @@ -88,7 +80,7 @@ gameLoop = { let movementData = [ gameLoop.player.sprite, gameLoop.player.speed, - gameLoop.controlType + 1 //gameLoop.controlType ]; gameLoop.movePlayer(...movementData); //gameLoop.score.amount += gameLoop.score.bonus1; diff --git a/client/js/gameLoopTools/playerUtilities.js b/client/js/gameLoopTools/playerUtilities.js index ec8e6bb..5562664 100644 --- a/client/js/gameLoopTools/playerUtilities.js +++ b/client/js/gameLoopTools/playerUtilities.js @@ -15,13 +15,15 @@ playerUtilities.mouseMovement = (player, playerSpeed) => { }; player.x += playerMovementXDelta; player.y += playerMovementYDelta; + }; playerUtilities.keyboardMovement = (player, playerSpeed) => { - if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { - player.x -= playerSpeed; - } - if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { - player.x += playerSpeed; - } -} \ No newline at end of file + + //Taking advantage of coercion to implement keyboard control algorithm + let xVelocityInput = (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT) - game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) * playerSpeed; + let yVelocityInput = (game.input.keyboard.isDown(Phaser.Keyboard.DOWN) - game.input.keyboard.isDown(Phaser.Keyboard.UP)) * playerSpeed; + + player.x += xVelocityInput; + player.y += yVelocityInput; +}; \ No newline at end of file