Skip to content

Commit

Permalink
added omni directional keyboard movement per: ticket #81
Browse files Browse the repository at this point in the history
  • Loading branch information
E-A-N committed Nov 25, 2018
1 parent 4ca7029 commit 4c883d2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
2 changes: 1 addition & 1 deletion client/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const config = {
};

config.default.player = {
speed: 20,
speed: 4,
imageKey: "player",
spriteSrc: "../spriteLocation.png"
};
Expand Down
14 changes: 3 additions & 11 deletions client/js/gameLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},
Expand All @@ -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);
Expand All @@ -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;
Expand Down
16 changes: 9 additions & 7 deletions client/js/gameLoopTools/playerUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

//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;
};

0 comments on commit 4c883d2

Please sign in to comment.