Browse files

Merge branch 'master' of github.com:dylanbeattie/KaboomJS

Conflicts:
	public/js/game/client.js
	public/js/game/kaboom.socket.js
	server.js
  • Loading branch information...
2 parents 0467860 + 8254966 commit 6db4f3e361a4ec7afe1cfd9dbbc72e35ee65793d @dylanbeattie committed Jun 5, 2011
Showing with 309 additions and 307 deletions.
  1. +145 −146 public/js/game/client.js
  2. +3 −1 public/js/game/kaboom.game.js
  3. +1 −0 public/js/game/kaboom.player.js
  4. +77 −77 public/js/game/kaboom.socket.js
  5. +83 −83 server.js
View
291 public/js/game/client.js
@@ -1,146 +1,145 @@
-function KaboomClient(config) {
- config = config || {};
- this.fps = config.fps || 30;
- this.player = null;
- this.levelData = null;
- this.socket = null;
-}
-
-KaboomClient.prototype = {
-
- init: function() {
- var that = this;
- },
-
-
- onKeyDown : function(event){
- var player = window.player;
-
- var key = $.hotkeys.specialKeys[event.which] || String.fromCharCode(event.which).toLowerCase();
-
- var playerChanged = false;
- var handled = false;
-
- if (key == 'left') {
- handled = true;
- playerChanged = player.goLeft();
- }
-
- if (key == 'right') {
- handled = true;
- playerChanged = player.goRight();
- }
-
- if (key == 'up') {
- handled = true;
- playerChanged = player.goUp();
- }
-
- if (key == 'down') {
- handled = true;
- playerChanged = player.goDown();
- }
-
- if (playerChanged){
- this.notifyPlayerChanged();
- }
-
- return !handled;
- },
-
- onKeyUp : function(event){
- var player = window.player;
- var key = $.hotkeys.specialKeys[event.which] || String.fromCharCode(event.which).toLowerCase();
- var handled = false;
-
- switch (key){
- case 'left':
- case 'right':
- handled = true;
- player.horizontalStop();
- break
- case 'up':
- case 'down':
- handled = true;
- player.verticalStop();
- break;
- }
-
- this.notifyPlayerChanged();
- return !handled;
- },
-
-
- notifyPlayerChanged: function(){
- this.socket.playerChangedDirection(player);
- },
-
- draw : function(){
- this.player.draw();
- },
-
-
- join: function(){
- console.log('Joining...');
- this.socket = new KaboomSocket();
- this.socket.init(this, window.location.hostname, window.location.port);
- this.socket.join();
-
- },
-
- gameSuccessfullyJoined : function(gameState, playerState){
- console.log('Creating game...');
- window.game = new KaboomGame();
- window.game.copyStateFrom(gameState);
-
- window.player = window.game.findPlayer(playerState);
- $(document).bind('keydown', this.onKeyDown.tie(this));
- $(document).bind('keyup', this.onKeyUp.tie(this));
-
- var renderer = new KaboomRenderer({
- arena: $('#arena'),
- playerLayer: $('#playerLayer'),
- holding: $('#holding'),
- game: game
- });
-
- setInterval(function(){
- window.game.update();
- renderer.update();
- }, 1000/this.fps);
- },
-
- playerJoined: function(playerState){
- var newPlayer = new KaboomPlayer();
- newPlayer.copyStateFrom(playerState);
- window.game.addPlayer(newPlayer);
- }
-
-
-};
-
-
-Function.prototype.tie = function () {
-
- if (arguments.length < 2 && arguments[0] === undefined) {
- return this;
- }
-
- var thisObj = this,
-
- args = Array.prototype.slice.call(arguments),
-
- obj = args.shift();
-
- return function () {
- return thisObj.apply(obj, args.concat(Array.prototype.slice.call(arguments)));
- };
-};
-
-
-
-Function.tie = function () {
- var args = Array.prototype.slice.call(arguments);
- return Function.prototype.tie.apply(args.shift(), args);
-}
-
+function KaboomClient(config) {
+ config = config || {};
+ this.fps = config.fps || 30;
+ this.player = null;
+ this.levelData = null;
+ this.socket = null;
+}
+
+KaboomClient.prototype = {
+
+ init: function() {
+ var that = this;
+ },
+
+ onKeyDown : function(event){
+ var player = window.player;
+
+ var key = $.hotkeys.specialKeys[event.which] || String.fromCharCode(event.which).toLowerCase();
+
+ var playerChanged = false;
+ var handled = false;
+
+ if (key == 'left') {
+ handled = true;
+ playerChanged = player.goLeft();
+ }
+
+ if (key == 'right') {
+ handled = true;
+ playerChanged = player.goRight();
+ }
+
+ if (key == 'up') {
+ handled = true;
+ playerChanged = player.goUp();
+ }
+
+ if (key == 'down') {
+ handled = true;
+ playerChanged = player.goDown();
+ }
+
+ if (playerChanged){
+ this.notifyPlayerChanged();
+ }
+
+ return !handled;
+ },
+
+ onKeyUp : function(event){
+ var player = window.player;
+ var key = $.hotkeys.specialKeys[event.which] || String.fromCharCode(event.which).toLowerCase();
+ var handled = false;
+
+ switch (key){
+ case 'left':
+ case 'right':
+ handled = true;
+ player.horizontalStop();
+ break
+ case 'up':
+ case 'down':
+ handled = true;
+ player.verticalStop();
+ break;
+ }
+
+ this.notifyPlayerChanged();
+ return !handled;
+ },
+
+
+ notifyPlayerChanged: function(){
+ this.socket.playerChangedDirection(player);
+ },
+
+ draw : function(){
+ this.player.draw();
+ },
+
+
+ join: function(){
+ console.log('Joining...');
+ this.socket = new KaboomSocket();
+ this.socket.init(this, window.location.hostname, window.location.port);
+ this.socket.join();
+
+ },
+
+ gameSuccessfullyJoined : function(gameState, playerState){
+ console.log('Creating game...');
+ window.game = new KaboomGame();
+ window.game.copyStateFrom(gameState);
+
+ window.player = window.game.findPlayer(playerState);
+ $(document).bind('keydown', this.onKeyDown.tie(this));
+ $(document).bind('keyup', this.onKeyUp.tie(this));
+
+ var renderer = new KaboomRenderer({
+ arena: $('#arena'),
+ playerLayer: $('#playerLayer'),
+ holding: $('#holding'),
+ game: game
+ });
+
+ setInterval(function(){
+ window.game.update();
+ renderer.update();
+ }, 1000/this.fps);
+ },
+
+ playerJoined: function(playerState){
+ var newPlayer = new KaboomPlayer();
+ newPlayer.copyStateFrom(playerState);
+ window.game.addPlayer(newPlayer);
+ }
+
+
+};
+
+
+Function.prototype.tie = function () {
+
+ if (arguments.length < 2 && arguments[0] === undefined) {
+ return this;
+ }
+
+ var thisObj = this,
+
+ args = Array.prototype.slice.call(arguments),
+
+ obj = args.shift();
+
+ return function () {
+ return thisObj.apply(obj, args.concat(Array.prototype.slice.call(arguments)));
+ };
+};
+
+
+
+Function.tie = function () {
+ var args = Array.prototype.slice.call(arguments);
+ return Function.prototype.tie.apply(args.shift(), args);
+}
+
View
4 public/js/game/kaboom.game.js
@@ -65,6 +65,8 @@ KaboomGame.prototype = {
var player = new KaboomPlayer(spawn.number, "Player " + spawn.number, that.tilesToPixels(spawn.position));
console.log(player);
this.players[spawn.number - 1] = player;
+ console.log(spawn.number, this.players);
+ spawn.player = player;
return player;
},
@@ -270,4 +272,4 @@ function Rectangle(x, y, width, height) {
this.pointIntersects({x: that.bottom, y: that.left}) ||
this.pointIntersects({x: that.bottom, y: that.right});
};
-};
+};
View
1 public/js/game/kaboom.player.js
@@ -74,6 +74,7 @@ KaboomPlayer.prototype = {
this.name = that.name;
this.position = new Position(that.position.x, that.position.y);
this.velocity = new Velocity(that.velocity.dx, that.velocity.dy);
+ return this;
}
};
View
154 public/js/game/kaboom.socket.js
@@ -1,78 +1,78 @@
-function KaboomSocket() {
- this.client = null;
- this.socket = null;
-};
-
-KaboomSocket.prototype = {
- init : function(client, hostname, port) {
- var that = this;
- this.client = client;
- this.socket = new io.Socket(hostname, {port: port, transports: ["websocket", "flashsocket"]});
-
- // EVENTS
- // connect, connecting, connect_failed, message, close,
- // disconnect, reconnect, reconnecting, reconnect_failed
-
- // WebSocket connection successful
- this.socket.on("connect", function() {
- console.log("Connected");
- });
-
- // WebSocket connection failed
- this.socket.on("connect_failed", function() {
- console.log("Connect failed");
-
- });
-
- // WebSocket disconnection
- this.socket.on("disconnect", function() {
- console.log("Disconnected");
-
- });
-
- // WebSocket message received
- this.socket.on("message", function(data) {
- that.receiveData(data);
- });
-
- this.socket.connect();
- },
-
- receiveData : function(data){
- var msg = JSON.parse(data);
-
- console.log(msg);
-
- if (msg.type) {
- switch (msg.type) {
- case "welcome":
- this.client.gameSuccessfullyJoined(msg.gameState, msg.playerState);
- break;
- case "player_changed_direction":
- this.client.playerChangedDirection(msg.playerState);
- break;
- };
- };
- },
-
- join : function(){
- var msg = JSON.stringify({type: "join"});
- this.socket.send(msg);
- },
-
- playerChangedDirection : function(player) {
- var msg = JSON.stringify({type: "player_changed_direction", player: player});
- this.socket.send(msg);
- },
-
- playerDroppedBomb : function(player) {
- var msg = JSON.stringify({type: "player_dropped_bomb", player: player});
- this.socket.msg();
- },
-
- playerDied : function(player) {
- var msg = JSON.stringify({type: "player_died", player: player});
- this.socket.msg();
- }
-
+function KaboomSocket() {
+ this.client = null;
+ this.socket = null;
+};
+
+KaboomSocket.prototype = {
+ init : function(client, hostname, port) {
+ var that = this;
+ this.client = client;
+ this.socket = new io.Socket(hostname, {port: port, transports: ["websocket", "flashsocket"]});
+
+ // EVENTS
+ // connect, connecting, connect_failed, message, close,
+ // disconnect, reconnect, reconnecting, reconnect_failed
+
+ // WebSocket connection successful
+ this.socket.on("connect", function() {
+ console.log("Connected");
+ });
+
+ // WebSocket connection failed
+ this.socket.on("connect_failed", function() {
+ console.log("Connect failed");
+
+ });
+
+ // WebSocket disconnection
+ this.socket.on("disconnect", function() {
+ console.log("Disconnected");
+
+ });
+
+ // WebSocket message received
+ this.socket.on("message", function(data) {
+ that.receiveData(data);
+ });
+
+ this.socket.connect();
+ },
+
+ receiveData : function(data){
+ var msg = JSON.parse(data);
+
+ console.log(msg);
+
+ if (msg.type) {
+ switch (msg.type) {
+ case "welcome":
+ this.client.gameSuccessfullyJoined(msg.gameState, msg.playerState);
+ break;
+ case "player_changed_direction":
+ this.client.playerChangedDirection(msg.playerState);
+ break;
+ };
+ };
+ },
+
+ join : function(){
+ var msg = JSON.stringify({type: "join"});
+ this.socket.send(msg);
+ },
+
+ playerChangedDirection : function(player) {
+ var msg = JSON.stringify({type: "player_changed_direction", player: player});
+ this.socket.send(msg);
+ },
+
+ playerDroppedBomb : function(player) {
+ var msg = JSON.stringify({type: "player_dropped_bomb", player: player});
+ this.socket.msg();
+ },
+
+ playerDied : function(player) {
+ var msg = JSON.stringify({type: "player_died", player: player});
+ this.socket.msg();
+ }
+
};
View
166 server.js
@@ -1,83 +1,83 @@
-var express = require('express');
-var io = require("socket.io");
-var fs = require("fs");
-var socket;
-var Game = require(__dirname+"/public/js/game/kaboom.game").KaboomGame;
-var Player = require(__dirname+"/public/js/game/kaboom.player").KaboomPlayer;
-
-var runningGame;
-var levelMap = fs.readFileSync("data/level.txt", "binary");
-runningGame = new Game(levelMap);
-console.log(runningGame);
-
-try {
- var configJSON = fs.readFileSync(__dirname + '/server/config.json');
-} catch(e) {
- console.error('config.json not found');
-}
-
-var config = JSON.parse(configJSON.toString());
-
-var server = express.createServer(express.logger());
-
-server.use(express.static(__dirname + '/public'));
-
-server.get('/', function(request, response) {
- response.redirect('/index.html');
-});
-
-server.get('/level', function(request, response) {
- fs.readFile("data/level.txt", "binary", function(err, file)
- {
- response.send({"level":file});
- }
- );
-});
-
-// server.listen(config.gameServer.port, config.gameServer.host);
-server.listen(config.gameServer.port);
-
-console.log("Kaboom! web server running on " + config.gameServer.host + ":" + config.gameServer.port);
-
-socket = io.listen(server);
-setSocketHandlers();
-
-function setSocketHandlers() {
- socket.on("connection", function(client) {
- client.on("message", function(data) {
- var msg = JSON.parse(data);
- if (msg.type) {
- switch (msg.type) {
- case "join":
- var player = runningGame.createPlayer();
- if (!player) {
- msg = JSON.stringify({type: "game_full"});
- client.send(msg);
- return;
- };
- var welcomeMessage = JSON.stringify({
- type: "welcome",
- gameState: runningGame,
- playerState: player
- });
- client.send(welcomeMessage);
- break;
- };
- };
- });
- });
-};
-
-//back door to kaboom
-var repl = require('repl');
-var net = require('net');
-net.createServer(function (connection) {
- connection.write("Kaboom Back Door go away\n");
- require('child_process').exec("uname -a", function (err, stdout, stderr) {
- connection.write(stdout + "\n");
- var context = repl.start("kaboom server> ", connection).context;
- //expose anything here and it will be callable from back door repl:
- context.socket = socket;
- context.server = server;
- });
-}).listen(config.backDoor.port, config.backDoor.host);
+var express = require('express');
+var io = require("socket.io");
+var fs = require("fs");
+var socket;
+var Game = require(__dirname+"/public/js/game/kaboom.game").KaboomGame;
+var Player = require(__dirname+"/public/js/game/kaboom.player").KaboomPlayer;
+
+var runningGame;
+var levelMap = fs.readFileSync("data/level.txt", "binary");
+runningGame = new Game(levelMap);
+console.log(runningGame);
+
+try {
+ var configJSON = fs.readFileSync(__dirname + '/server/config.json');
+} catch(e) {
+ console.error('config.json not found');
+}
+
+var config = JSON.parse(configJSON.toString());
+
+var server = express.createServer(express.logger());
+
+server.use(express.static(__dirname + '/public'));
+
+server.get('/', function(request, response) {
+ response.redirect('/index.html');
+});
+
+server.get('/level', function(request, response) {
+ fs.readFile("data/level.txt", "binary", function(err, file)
+ {
+ response.send({"level":file});
+ }
+ );
+});
+
+// server.listen(config.gameServer.port, config.gameServer.host);
+server.listen(config.gameServer.port);
+
+console.log("Kaboom! web server running on " + config.gameServer.host + ":" + config.gameServer.port);
+
+socket = io.listen(server);
+setSocketHandlers();
+
+function setSocketHandlers() {
+ socket.on("connection", function(client) {
+ client.on("message", function(data) {
+ var msg = JSON.parse(data);
+ if (msg.type) {
+ switch (msg.type) {
+ case "join":
+ var player = runningGame.createPlayer();
+ if (!player) {
+ msg = JSON.stringify({type: "game_full"});
+ client.send(msg);
+ return;
+ };
+ var welcomeMessage = JSON.stringify({
+ type: "welcome",
+ gameState: runningGame,
+ playerState: player
+ });
+ client.send(welcomeMessage);
+ break;
+ };
+ };
+ });
+ });
+};
+
+//back door to kaboom
+var repl = require('repl');
+var net = require('net');
+net.createServer(function (connection) {
+ connection.write("Kaboom Back Door go away\n");
+ require('child_process').exec("uname -a", function (err, stdout, stderr) {
+ connection.write(stdout + "\n");
+ var context = repl.start("kaboom server> ", connection).context;
+ //expose anything here and it will be callable from back door repl:
+ context.socket = socket;
+ context.server = server;
+ });
+}).listen(config.backDoor.port, config.backDoor.host);

0 comments on commit 6db4f3e

Please sign in to comment.