Skip to content
Browse files

refactored movement code

  • Loading branch information...
1 parent 8aca882 commit 6911b4b69cdb6a51e09c74884670df5ea73f05c3 @seanhess committed Nov 10, 2012
Showing with 109 additions and 81 deletions.
  1. +53 −39 public/controllers/GameCtrl.ts
  2. +1 −1 public/directives/keys.ts
  3. +43 −28 public/services/Board.ts
  4. +8 −9 public/services/Missiles.ts
  5. +4 −4 public/services/Players.ts
View
92 public/controllers/GameCtrl.ts
@@ -75,7 +75,7 @@ angular.module('controllers')
function getSprite(newDirection) {
var slide,
- previousDirection = players.current.facing,
+ previousDirection = players.current.direction,
previous = players.current.sprite;
if(previousDirection === newDirection) {
@@ -87,50 +87,64 @@ angular.module('controllers')
return slide;
}
+ // ignore ALL key presses if they are dead
$scope.keypress = function (e) {
- if (e.keyCode === 32) {
- Missiles.fireMissile(missiles, players.current)
- }
-
- else {
- var boardDirection = keyCodeToDirection(e.keyCode)
- if (!boardDirection) return
- var position = Board.getPosition(boardDirection)
- var location = Board.move(players.current, position)
- if (location && players.current.state != "dead") {
- players.current.walking = true;
-
- setTimeout(function(){
- $scope.$apply(function() {
- players.current.walking = false;
- Players.move(players, players.current);
- });
- }, 500);
+ // you can do ANYTHING if you are dead
+ if (!Players.isAlive(players.current)) return
+
+ if (e.keyCode === 32)
+ return Missiles.fireMissile(missiles, players.current)
+
+ var direction = keyCodeToDirection(e.keyCode)
+ if (!direction) return
+
+ var position = Board.move(players.current, direction)
+ if (!position) return
+
+ players.current.x = position.x
+ players.current.y = position.y
+ players.current.direction = position.direction
+ console.log(players.current.direction)
+
+ Players.move(players, players.current);
+
+ // you need:
+ // a list of updates: { x: 1, y: 2, direction: 'down' }
+
+ //players.current.walking = true;
+
+ //setTimeout(function(){
+ //$scope.$apply(function() {
+ //players.current.walking = false;
+ //Players.move(players, players.current);
+ //});
+ //}, 500);
/*players.current[location.axis] = location.location;
players.current.facing = location.facing;
players.move(players.current);*/
- var collision = false;
- Players.alivePlayers(players.all).forEach(function(val,key){
- if (val.name != players.current.name && val.state != "dead") {
- if (location.axis == "x") {
- if (val.x == location.location && val.y == players.current.y) collision = true;
- }
- if (location.axis == "y") {
- if (val.y == location.location && val.x == players.current.x) collision = true;
- }
- }
- });
- if (!collision) {
- players.current[location.axis] = location.location;
- players.current.facing = location.facing;
- Players.move(players, players.current);
- } else {
- // we can play a collision sound here!
- }
- }
- }
+ // WILL I HIT ANY OTHER PLAYERS?
+
+ //var collision = false;
+ //Players.alivePlayers(players.all).forEach(function(p:IPlayer){
+ //if (p.name != players.current.name && p.state != "dead") {
+ //if (location.axis == "x") {
+ //if (p.x == location.location && p.y == players.current.y) collision = true;
+ //}
+ //if (location.axis == "y") {
+ //if (p.y == location.location && p.x == players.current.x) collision = true;
+ //}
+ //}
+ //});
+ //if (!collision) {
+
+
+ //} else {
+ //// we can play a collision sound here!
+ //}
+ //}
+ // }
}
})
View
2 public/directives/keys.ts
@@ -1,8 +1,8 @@
///<reference path="../def/angular.d.ts"/>
///<reference path="../def/jquery.d.ts"/>
+console.log("KEYPRESS")
angular.module('directives')
-
.directive('keypress', function($parse) {
return function(scope:ng.IScope, element:JQuery, attrs) {
// element is a jquery element
View
71 public/services/Board.ts
@@ -1,6 +1,7 @@
///<reference path="../def/angular.d.ts"/>
+///<reference path="../def/underscore.d.ts"/>
-interface IPosition {
+interface IPositionChange {
axis:string;
distance:number;
}
@@ -10,12 +11,18 @@ interface IPoint {
y:number;
}
+interface IDirectional extends IPoint {
+ x:number;
+ y:number;
+ direction:string;
+}
+
interface IBoard {
- getPosition(direction:string):IPosition;
- move(object:IPoint, position:IPosition);
+ move(object:IPoint, direction:string):IDirectional;
isHit(one:IPoint, two:IPoint):bool;
randomX():number;
randomY():number;
+ findHit(objects:IPoint[], object:IPoint):IPoint;
// constants
LEFT: string;
@@ -32,7 +39,7 @@ interface IBoard {
interface IMove {
axis: string;
location: number;
- facing: string;
+ direction: string;
}
angular.module('services')
@@ -43,16 +50,16 @@ angular.module('services')
var HEIGHT = 600
var UNIT = 50
var GRID = {
- x: WIDTH / UNIT,
- y: HEIGHT / UNIT,
+ maxX: WIDTH / UNIT,
+ maxY: HEIGHT / UNIT,
}
var Board = {
move: move,
- getPosition: getPosition,
isHit: isHit,
- randomX: makeRandomN(GRID.x),
- randomY: makeRandomN(GRID.y),
+ findHit: findHit,
+ randomX: makeRandomN(GRID.maxX),
+ randomY: makeRandomN(GRID.maxY),
// Direction Constants
LEFT: "left",
@@ -67,7 +74,7 @@ angular.module('services')
return Board
- function getPosition(direction:string):IPosition {
+ function getPositionChange(direction:string):IPositionChange {
if(direction === Board.UP) {
return {axis: 'y', distance: -1}
@@ -91,26 +98,34 @@ angular.module('services')
}
}
- function move(object:IPoint, position:IPosition) {
- var axis = position.axis
- var distance = position.distance
- var potential = object[axis] + distance;
- var direction;
- if (axis == 'x' && distance > 0) direction = Board.RIGHT
- else if (axis == 'x' && distance < 0) direction = Board.LEFT
- else if (axis == 'y' && distance > 0) direction = Board.DOWN
- else if (axis == 'y' && distance < 0) direction = Board.UP
- else console.log("BAD MOVE", axis, distance)
-
- if (GRID[axis] <= potential || potential < 0) {
- return null
- }
+ // returns a new directional position for you
+ function move(object:IPoint, direction:string):IDirectional {
+
+ var dp = getPositionChange(direction)
+ if (!dp) return
- return {
- axis: axis,
- location: potential,
- facing: direction
+ // start with the original
+ var moved:IDirectional = {
+ x: object.x,
+ y: object.y,
+ direction: direction,
}
+
+ moved[dp.axis] += dp.distance
+
+ if (!inBounds(moved)) return
+
+ return moved
+ }
+
+ function inBounds(p:IPoint) {
+ return (0 <= p.x && p.x < GRID.maxX && 0 <= p.y && p.y < GRID.maxY)
+ }
+
+ function findHit(objects:IPoint[], object:IPoint):IPoint {
+ return objects.filter((obj:IPoint) {
+ return Board.isHit(obj, object)
+ })[0]
}
function isHit(one:IPoint, two:IPoint):bool {
View
17 public/services/Missiles.ts
@@ -66,7 +66,7 @@ angular.module('services')
var missile = {
x: player.x,
y: player.y,
- direction: player.facing,
+ direction: player.direction,
sourcePlayer: player.name
}
@@ -92,16 +92,15 @@ angular.module('services')
function moveMissile() {
// move the missile
- var position = Board.getPosition(missile.direction)
- var location = Board.move(missile, position);
- if (!location) return explodeMissile(missile)
- missile[position.axis] = location.location
+ var position = Board.move(missile, missile.direction)
+ if (!position) return explodeMissile(missile)
- // Check to see if the missile hits anyone
- var hitPlayer = players.all.filter((player:IPlayer) {
- Board.isHit(player, missile)
- })[0]
+ missile.x = position.x
+ missile.y = position.y
+ missile.direction = position.direction
+ // Check to see if the missile hits anyone
+ var hitPlayer = <IPlayer> Board.findHit(players.all, missile)
if (hitPlayer) {
explodeMissile(missile)
if (hitPlayer == players.current)
View
8 public/services/Players.ts
@@ -17,7 +17,7 @@ interface IPlayer {
name:string;
avatar:string;
- facing?:string;
+ direction?:string;
message?:string;
killer?:string;
@@ -116,7 +116,7 @@ angular.module('services')
player.x = Board.randomX()
player.y = Board.randomY()
player.sprite = 1
- player.facing = "down"
+ player.direction = Board.DOWN
player.state = STATE.ALIVE
player.wins = player.wins || 0
player.losses = player.losses || 0
@@ -150,7 +150,7 @@ angular.module('services')
// copy as many properites as you care about
player.x = remotePlayer.x
player.y = remotePlayer.y
- player.facing = remotePlayer.facing
+ player.direction = remotePlayer.direction
player.state = remotePlayer.state;
player.wins = remotePlayer.wins;
player.losses = remotePlayer.losses;
@@ -231,7 +231,7 @@ angular.module('services')
player.x = Board.randomX()
player.y = Board.randomY()
player.sprite = 1
- player.facing = "down"
+ player.direction = Board.DOWN
player.state = STATE.ALIVE
FB.update(state.playersRef.child(player.name), player)
})

0 comments on commit 6911b4b

Please sign in to comment.
Something went wrong with that request. Please try again.