Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: seanhess/uarbg2
base: b026c0b8ce
...
head fork: seanhess/uarbg2
compare: 6911b4b69c
  • 2 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
103 public/controllers/GameCtrl.ts
View
@@ -13,14 +13,14 @@
angular.module('controllers')
-.controller('GameCtrl', function ($scope, Players:IPlayerService, Missiles, $routeParams, CurrentPlayer, $location, Board, SoundEffects, AppVersion) {
+.controller('GameCtrl', function ($scope, Players:IPlayerService, Missiles:IMissileService, $routeParams, CurrentPlayer:ICurrentPlayerService, $location, Board:IBoard, SoundEffects:ISoundEffectsService, AppVersion:string) {
$scope.version = AppVersion
$scope.gameId = $routeParams.gameId
// DEBUG: you can set ?debugPlayerName and just hit refresh over and over to reconnect
if ($routeParams.debugPlayerName)
- CurrentPlayer.player = {name: $routeParams.debugPlayerName, avatar:"player" + Math.floor(Math.random()*6), state: "alive"}
+ CurrentPlayer.player = {name: $routeParams.debugPlayerName, avatar:"player" + Math.floor(Math.random()*6), state: "alive", x:0, y:0, wins:0, losses:0, version:AppVersion}
// only play if you are identified
if (!CurrentPlayer.player)
@@ -37,8 +37,7 @@ angular.module('controllers')
Players.join(players, CurrentPlayer.player)
$scope.players = players
- var missiles = new Missiles($scope.gameId,players)
- missiles.listen()
+ var missiles = Missiles.connect($scope.gameId, players)
$scope.missiles = missiles
$scope.latestAlert = "Welcome to Your Underwater Adventure"
@@ -66,17 +65,17 @@ angular.module('controllers')
DOWN = 40,
SPACE = 32
- function keyCodeToDirection(code) {
+ function keyCodeToDirection(code:number):string {
if (code == LEFT) return Board.LEFT
else if (code == RIGHT) return Board.RIGHT
else if (code == DOWN) return Board.DOWN
else if (code == UP) return Board.UP
- return false
+ return null
}
function getSprite(newDirection) {
var slide,
- previousDirection = players.current.facing,
+ previousDirection = players.current.direction,
previous = players.current.sprite;
if(previousDirection === newDirection) {
@@ -88,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(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.status != "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.status != "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!
+ //}
+ //}
+ // }
}
})
2  public/directives/keys.ts
View
@@ -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
2  public/partials/game.html
View
@@ -39,7 +39,7 @@ <h1 class="winner" ng-show="players.winner">{{players.winner.name}} Wins!</h1>
>
<div class="taunt triangle-border left" ng-show="players.taunt">{{players.taunt}}</div>
</div>
- <div ng-repeat="missile in missiles.allMissiles">
+ <div ng-repeat="missile in missiles.all">
<div class="missile {{missile.direction}}" ng-style="missile | position"></div>
</div>
71 public/services/Board.ts
View
@@ -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 {
20 public/services/FB.ts
View
@@ -4,7 +4,13 @@
// TODO switch to firebase.d.ts
declare var Firebase;
-module firebase {
+interface IFirebaseService {
+ game(gameId:string):fire.IRef;
+ apply(cb:fire.IValueCB):fire.IRefCB;
+ update(ref:fire.IRef, obj:any);
+}
+
+module fire {
export interface IRef {
child(name:string);
@@ -22,12 +28,6 @@ module firebase {
(val:any);
}
- export interface IFB {
- game(gameId:string):IRef;
- apply(cb:IValueCB):IRefCB;
- update(ref:IRef, obj:any);
- }
-
// Functional Style Class: it's the same as the module that you were looking for!
// wait, this sucks. To call other functions in the module, you have to use this
@@ -39,7 +39,7 @@ module firebase {
// minus: can lose this pointer
// minus: you should be using an interface type, so you have to define the interface twice anyway
- export class FB {
+ export class FB implements IFirebaseService{
constructor(
private $rootScope:ng.IRootScopeService
@@ -71,6 +71,6 @@ module firebase {
}
}
-angular.module('services').factory('FB', function($rootScope) {
- return new firebase.FB($rootScope)
+angular.module('services').factory('FB', function($rootScope):IFirebaseService {
+ return new fire.FB($rootScope)
})
124 public/services/Missiles.ts
View
@@ -1,35 +1,77 @@
///<reference path="../def/angular.d.ts"/>
// This service keeps track of the missiles
+interface IMissileState {
+ all: IMissile[];
+
+ // private stuff
+ missilesRef:fire.IRef;
+}
+
+interface IMissile extends IPoint {
+ x: number;
+ y: number;
+ direction: string;
+ sourcePlayer: string;
+}
+
+interface IMissileService {
+ connect(gameId:string, players:IPlayerState):IMissileState;
+ fireMissile(state:IMissileState, player:IPlayer);
+}
+
angular.module('services')
-.factory('Missiles', function($rootScope, FB, Board) {
- return function(gameId, Players) {
- var gameRef = FB.game(gameId)
- var missilesRef = gameRef.child('missiles')
- var gameStatusRef = gameRef.child('gameStatus')
+// TODO use signals / events instead of rootScope stuff
+.factory('Missiles', function($rootScope:ng.IRootScopeService, FB:IFirebaseService, Board:IBoard, Players:IPlayerService):IMissileService {
+
+ return {
+ connect: connect,
+ fireMissile: fireMissile,
+ }
+
+ function connect(gameId:string, players:IPlayerState):IMissileState {
+ var missilesRef = FB.game(gameId).child('missiles')
+
+ var all = []
+
+ var state = {
+ missilesRef:missilesRef,
+ all:all,
+ }
+
+ missilesRef.on('child_added', FB.apply((m) => onNewMissile(state, players, m)))
+ missilesRef.on('child_removed', FB.apply((m) => onRemovedMissile(state)))
+
+ return state
+ }
- // {x: 3, y: 3, direction:'right', sourcePlayer:"asdfsa"}
- var allMissiles = []
+ function missileByPlayerName(missiles:IMissile[], name:string) {
+ return missiles.filter(function(m:IMissile) {
+ return (m.sourcePlayer == name)
+ })[0]
+ }
- function listen() {
- missilesRef.on('child_added', FB.apply(onNewMissile))
- missilesRef.on('child_removed', FB.apply(onRemovedMissile))
+ function playerHasMissile(missiles:IMissile[], player:IPlayer) {
+ return missileByPlayerName(missiles, player.name)
}
- function fireMissile(player) {
- if (playerHasMissile(player) && Players.current.state != "dead") return
+ // this does NOT check for deadness. Do that somewhere else
+ function fireMissile(state:IMissileState, player:IPlayer) {
+
+ // can only fire one at a time
+ if (playerHasMissile(state.all, player)) return
var missile = {
x: player.x,
y: player.y,
- direction: player.facing,
+ direction: player.direction,
sourcePlayer: player.name
}
- missilesRef.child(player.name).removeOnDisconnect();
- missilesRef.child(player.name).set(missile)
+ state.missilesRef.child(player.name).removeOnDisconnect();
+ state.missilesRef.child(player.name).set(missile)
}
// everyone moves all missiles
@@ -38,11 +80,9 @@ angular.module('services')
// TODO use a SINGLE timer for ALL missiles (observer?)
// TODO do any missiles collide with each other?
- function onNewMissile(missile) {
- allMissiles.push(missile)
+ function onNewMissile(state:IMissileState, players:IPlayerState, missile:IMissile) {
+ state.all.push(missile)
$rootScope.$broadcast("missile", missile)
- console.log("onNewMissile()")
- console.log("allMissiles: "+allMissiles)
var missTimer = setInterval(function() {
$rootScope.$apply(function() {
@@ -52,52 +92,32 @@ 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(function(player) {
- return 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)
- Players.killPlayer(Players.current, missile.sourcePlayer)
+ if (hitPlayer == players.current)
+ Players.killPlayer(players, players.current, missile.sourcePlayer)
}
}
// how to do this? should I client-side predict?
- function explodeMissile(missile) {
- var idx = allMissiles.indexOf(missile)
- if (idx != -1) allMissiles.splice(idx,1)
+ function explodeMissile(missile:IMissile) {
+ var idx = state.all.indexOf(missile)
+ if (idx != -1) state.all.splice(idx,1)
clearInterval(missTimer)
- missilesRef.child(missile.sourcePlayer).remove()
+ state.missilesRef.child(missile.sourcePlayer).remove()
}
}
function onRemovedMissile(missile) {
console.log("onRemovedMissile()")
}
-
- function missileByPlayerName(name) {
- return allMissiles.filter(function(p) {
- return (p.sourcePlayer == name)
- })[0]
- }
-
- function playerHasMissile(player) {
- return missileByPlayerName(player.name)
- }
-
- var missiles = {
- allMissiles: allMissiles,
- listen: listen,
- fireMissile: fireMissile
- }
-
- return missiles
- }
})
31 public/services/Players.ts
View
@@ -10,20 +10,23 @@
interface IPlayer {
x:number;
y:number;
- facing:string;
- state:string;
+
wins:number;
losses:number;
- message:string;
version:string;
name:string;
- killer:string;
avatar:string;
- status:string;
+
+ direction?:string;
+ message?:string;
+ killer?:string;
+
+ // alive or dead
+ state:string;
// move this off of player
- walking:bool;
- sprite:number;
+ walking?:bool;
+ sprite?:number;
}
// only variables
@@ -36,8 +39,8 @@ interface IPlayerState {
// private stuff. not for binding
myname:string;
- gameRef:firebase.IRef;
- playersRef:firebase.IRef;
+ gameRef:fire.IRef;
+ playersRef:fire.IRef;
}
// only methods
@@ -56,7 +59,7 @@ interface IPlayerService {
angular.module('services')
-.factory('Players', function($rootScope:ng.IScope, FB:firebase.IFB, Board:IBoard, AppVersion:any):IPlayerService {
+.factory('Players', function($rootScope:ng.IScope, FB:IFirebaseService, Board:IBoard, AppVersion:any):IPlayerService {
// the big cheese. Does the deed
// you can make fancy bindings here, no?
@@ -113,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
@@ -147,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;
@@ -228,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)
})
@@ -251,7 +254,7 @@ angular.module('services')
// your function stuff is CRAP if you don't pass it in. no better than a class
// property of PLAYERS
function playerByName(players:IPlayer[], name:string):IPlayer {
- return players.filter((p) => (p.name == name))[0]
+ return players.filter((p:IPlayer) => (p.name == name))[0]
}
function latestVersion(players:IPlayer[]):string {

No commit comments for this range

Something went wrong with that request. Please try again.