Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: benfoxall/Twon
base: 11d1af3721
...
head fork: benfoxall/Twon
compare: d8270f525f
Checking mergeability… Don't worry, you can still create the pull request.
  • 8 commits
  • 4 files changed
  • 0 commit comments
  • 2 contributors
View
2  public/arena.html
@@ -60,4 +60,4 @@
});
</script>
</body>
-</html>
+</html>
View
2  public/client.js
@@ -36,7 +36,7 @@
ctx.fillStyle="red"
this.on('updatePlayer', function(id,x,y){
// *5 for debug
- ctx.fillRect(x*5,y*5,2,2);
+ ctx.fillRect(x,y,1,1);
});
// set up the click handlers for the elements
View
172 public/controller.js
@@ -13,7 +13,18 @@ var controller = function(io){
var rest = function(arr){
return Array.prototype.slice.call(arr,1);
};
-
+ var equalCoordinates = function (coord1, coord2) {
+ return coord1[0] === coord2[0] && coord1[1] === coord2[1];
+ };
+ var checkCoordinateInArray = function (coord, arr) {
+ var isInArray = false;
+ each(arr, function (item) {
+ if (equalCoordinates(coord, item)) {
+ isInArray = true;
+ }
+ });
+ return isInArray;
+ };
var ArenaCounter = 0;
var PlayerCounter = 0;
@@ -23,8 +34,6 @@ var controller = function(io){
var players = [];
-
-
/*
Arena & Controller logic
*/
@@ -39,25 +48,38 @@ var controller = function(io){
var g = this;
(function l(){
g.loop();
- setTimeout(l,500);
+ setTimeout(l,50);
})()
};
// Add entities to the game
Game.prototype.addPlayer = function(player){
this.players.push(player);
+ this.start();
};
Game.prototype.addArena = function(arena){
this.arenas.push(arena);
+ this.start();
};
/* start the game, position the players, etc */
Game.prototype.start = function(){
+ if(this.arenas.length == 0){
+ console.log("Couldn't start game, no arenas."); return;
+ }
+
+ var startArena = this.arenas[0];
var y = 0;
+
+ var startArena = this.arenas[0];
+
each(this.players, function(player){
y += 10;
- player.posArray = [[0,y]];
+ player.x = 0;
+ player.y = y;
+ player.posArray = [0,y];
player.nextDirection = 'right';
+ player.arena = startArena;// XXX temporary
});
}
@@ -70,8 +92,8 @@ var controller = function(io){
each(this.players, function(player){
player.advance();
//update the arena with the new player position
- if(arenas[0]){
- arenas[0].draw(player);
+ if(player.arena){
+ player.arena.draw(player);
}
});
@@ -110,19 +132,88 @@ var controller = function(io){
}
});
+
+ this.usedpoints = [];
};
- Arena.prototype.color = function(color){
- this.emit('backgroundtop',color);
+ // this checks to see if a player is still on this
+ // arena and moves them to another one (or null)
+ // as appropriate
+ Arena.prototype.updatePlayer = function(player){
+ if(player.x < 0){
+ // left
+ if(player.arena = this.links.left)
+ player.arena.entry(this, player, player.y/this.height);
+
+ } else if(player.x > this.width){
+ // right
+ if(player.arena = this.links.right)
+ player.arena.entry(this, player, player.y/this.height);
+
+ } else if(player.y < 0){
+ //top
+ if(player.arena = this.links.top)
+ player.arena.entry(this, player, player.x/this.width);
+ } else if(player.y > this.height){
+ //bottom
+ if(player.arena = this.links.bottom)
+ player.arena.entry(this, player, player.x/this.width);
+ }
+ };
+
+ // this repositions the player as they have entered from another
+ // arena
+ Arena.prototype.entry = function(fromarena, player, position){
+ for(link in this.links){
+ if(this.links[link] === fromarena){
+ // update the direction and position
+ if(link == 'left'){
+ player.x = 0;
+ player.y = position * this.height;
+ player.nextDirection = 'right';
+ };
+ if(link == 'right'){
+ player.x = this.width;
+ player.y = position * this.height;
+ player.nextDirection = 'left';
+ };
+ if(link == 'top'){
+ player.x = position * this.width;
+ player.y = 0;
+ player.nextDirection = 'down';
+ };
+ if(link == 'bottom'){
+ player.x = position * this.width;
+ player.y = this.height;
+ player.nextDirection = 'up';
+ };
+
+ console.log("Switched player" + player.id + ' from arena' + fromarena.id + ' --> arena' + this.id);
+ }
+ }
}
// update the view of the arena
Arena.prototype.draw = function(player){
- // console.log(player.id, player.x, player.y);
this.emit('updatePlayer', player.id, player.x, player.y)
}
-
+ Arena.prototype.addUsedPoint = function(points){
+ // [x,y,playerid]
+ // console.log(points)
+
+ this.usedpoints.push(points);
+ }
+
+ Arena.prototype.checkCollision = function(head){
+ // console.log('head:' + head)
+ // console.log('used: '+ this.usedpoints);
+ // [[x,y,playerid],[x,y,playerid]]
+ playerCollision = checkCoordinateInArray(head, this.usedpoints);
+ if(playerCollision){
+ console.log('BOOM!');
+ }
+ }
// the controller representation of a player
@@ -131,7 +222,7 @@ var controller = function(io){
this.transport = transport;
this.on('left', function(){
- console.log("<<<Player"+this.id+" should go LEFT<<<");
+ console.log("<<<Player"+this.id+" LEFT");
// increment the direction
this.nextDirection = {
'right':'up',
@@ -142,7 +233,7 @@ var controller = function(io){
});
this.on('right', function(){
- console.log(">>>Player"+id+" should go RIGHT>>>");
+ console.log(">>>Player"+id+" RIGHT");
// increment the direction
this.nextDirection = {
'right':'down',
@@ -153,39 +244,40 @@ var controller = function(io){
});
};
-
+
Player.prototype.advance = function(){
- var nextPosition = this.posArray[0].slice();
- direction = this.nextDirection;
- switch (direction) {
- case 'left':
- nextPosition[0] -= 1;
- break;
- case 'up':
- nextPosition[1] -= 1;
- break;
- case 'right':
- nextPosition[0] += 1;
- break;
- case 'down':
- nextPosition[1] += 1;
- break;
- default:
- throw('Invalid direction');
+ // increment x y appropriately
+ this.x += {left:-1, right:1}[this.nextDirection] || 0;
+ this.y += {down:1, up:-1 }[this.nextDirection] || 0;
+
+ // update/transistion the arena based on the position
+ if(this.arena){
+ this.arena.updatePlayer(this);
+
+ if(!this.arena){
+ // we arent on an arena anymore
+ console.log("KABOOM");
+
+ } else {
+ this.checkCollision();
+ // store the point for collisions
+ var nextPosition = [this.x,this.y,this.id];
+ this.arena.addUsedPoint(nextPosition);
+ }
+
}
- this.x = nextPosition[0];
- this.y = nextPosition[1];
-
- previousPosArray = this.posArray.slice();
-
- //add the new position to the beginning of the array
- this.posArray.unshift(nextPosition);
- // console.log(this.posArray);
+
+
}
-
+ Player.prototype.checkCollision = function(){
+
+ // actually, we just ask the arena for now
+ this.arena.checkCollision([this.x,this.y,this.id]);
+
+ }
/*
View
3  public/dev.html
@@ -19,6 +19,7 @@
controller(io);
</script>
+
<iframe src="arena.html" width="400" height="300"></iframe>
<iframe src="arena.html" width="400" height="300"></iframe>
<iframe src="player.html" width="400" height="300"></iframe>
@@ -28,4 +29,4 @@
<iframe src="player.html" width="400" height="300"></iframe>
-->
</body>
-</html>
+</html>

No commit comments for this range

Something went wrong with that request. Please try again.