From 84afcdfdd06f078aa5bf76ef6e8d17eebe56de3a Mon Sep 17 00:00:00 2001 From: logsol Date: Sun, 15 Jul 2012 21:57:09 +0200 Subject: [PATCH 1/9] deleted obsolete Game Module --- lib/Chuck/Game.js | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 lib/Chuck/Game.js diff --git a/lib/Chuck/Game.js b/lib/Chuck/Game.js deleted file mode 100644 index a2eb3a9..0000000 --- a/lib/Chuck/Game.js +++ /dev/null @@ -1,30 +0,0 @@ -define(["Chuck/Settings", "Chuck/Processors/ServerProcessor", "Chuck/Processors/ClientProcessor"], function(Settings, ServerProcessor, ClientProcessor){ - - function Game(networker){ - this.networker = networker; - this.processor = this.createProcessor(); - - this.processor.loadLevel("default.json"); - } - - Game.prototype.createProcessor = function(){ - var processor; - - if(Settings.IS_BROWSER_ENVIRONMENT){ - processor = new ClientProcessor(); - } else { - processor = new ServerProcessor(); - } - return processor; - } - - Game.prototype.processGameCommand = function(command, options){ - console.log('(not implemented) processGameCommand:', command, options); - } - - Game.prototype.destruct = function(){ - this.processor.destruct(); - } - - return Game; -}); \ No newline at end of file From d523b88e66ba692257109c86e1c10495ca409c0b Mon Sep 17 00:00:00 2001 From: logsol Date: Sun, 15 Jul 2012 21:58:24 +0200 Subject: [PATCH 2/9] using assemble. fixes #7 --- lib/Client/Networker.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/Client/Networker.js b/lib/Client/Networker.js index efb3270..c09c49b 100644 --- a/lib/Client/Networker.js +++ b/lib/Client/Networker.js @@ -67,9 +67,7 @@ define(["Protocol/Helper", "Chuck/ClientGame"], function(ProtocolHelper, ClientG } Networker.prototype.sendGameCommand = function(command, options) { - var message = {}; - message[command] = options || true; - this.sendCommand('gameCommand', message); + this.sendCommand('gameCommand', ProtocolHelper.assemble(command, options)); } Networker.prototype.onUserLeft = function(userId) { From 3899d9b018237b35e1ce4c13a1e7e1947260ed1f Mon Sep 17 00:00:00 2001 From: logsol Date: Sun, 15 Jul 2012 21:59:36 +0200 Subject: [PATCH 3/9] setting NULL for command option default value. fixes #8 --- lib/Protocol/Helper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Protocol/Helper.js b/lib/Protocol/Helper.js index a383a0e..f25aa91 100644 --- a/lib/Protocol/Helper.js +++ b/lib/Protocol/Helper.js @@ -8,7 +8,7 @@ define(["Protocol/Parser"], function(Parser) { Helper.assemble = function(command, options){ var commands = {}; - commands[command] = options; + commands[command] = options || null; return commands; } From f3e186afb197f891914df9767c6f4b79c4578ab7 Mon Sep 17 00:00:00 2001 From: logsol Date: Sun, 15 Jul 2012 22:05:34 +0200 Subject: [PATCH 4/9] added Server Factory module and empty notification center. #6 --- lib/Server/Factory.js | 24 ++++++++++++++++++++++++ lib/Server/NotificationCenter.js | 7 +++++++ 2 files changed, 31 insertions(+) create mode 100644 lib/Server/Factory.js create mode 100644 lib/Server/NotificationCenter.js diff --git a/lib/Server/Factory.js b/lib/Server/Factory.js new file mode 100644 index 0000000..e83252b --- /dev/null +++ b/lib/Server/Factory.js @@ -0,0 +1,24 @@ +define(['Server/NotificationCenter'], function(NotificationCenter) { + + function Factory() { + this.notificationCenter = new NotificationCenter(); + } + + Factory.prototype.new = function () { + + if (arguments.length < 1) + throw 'Too fiew arguments'; + if (typeof arguments[0] != 'function') + throw arguments[0] + ' is not a function'; + + var type = arguments[0]; + var object = new (type.bind.apply(type,arguments))(); + object.notificationCenter = this.notificationCenter; + object.factory = this; + return object; + + } + + return Factory; + +}); \ No newline at end of file diff --git a/lib/Server/NotificationCenter.js b/lib/Server/NotificationCenter.js new file mode 100644 index 0000000..26848fc --- /dev/null +++ b/lib/Server/NotificationCenter.js @@ -0,0 +1,7 @@ +define(function() { + + function NotificationCenter() { + } + + return NotificationCenter; +}); \ No newline at end of file From 3c1e7008e5235dd85481253cbfbe6da67061a41f Mon Sep 17 00:00:00 2001 From: logsol Date: Sun, 15 Jul 2012 22:32:44 +0200 Subject: [PATCH 5/9] added Constants module. fixes #4 --- lib/Chuck/Collision/Detector.js | 6 ++++-- lib/Chuck/Constants.js | 16 +++++++++++++++- lib/Chuck/Loader/Level.js | 4 ++-- lib/Chuck/Physics/Doll.js | 16 ++++++++-------- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/lib/Chuck/Collision/Detector.js b/lib/Chuck/Collision/Detector.js index f9a18fb..42378ce 100644 --- a/lib/Chuck/Collision/Detector.js +++ b/lib/Chuck/Collision/Detector.js @@ -1,4 +1,4 @@ -define(["Vendor/Box2D"], function(Box2D) { +define(["Vendor/Box2D", "Chuck/Constants"], function(Box2D, Constants) { function Detector(me) { this.me = me; @@ -15,7 +15,9 @@ define(["Vendor/Box2D"], function(Box2D) { } Detector.prototype.handleStand = function(point, isColliding) { - if (point.GetFixtureA().GetUserData() == 'myFeet' || point.GetFixtureB().GetUserData() == 'myFeet') { + if (point.GetFixtureA().GetUserData() == Constants.COLLISION_IDENTIFIER_FOOTSENSOR + || point.GetFixtureB().GetUserData() == Constants.COLLISION_IDENTIFIER_FOOTSENSOR) { + this.me.onFootSensorDetection(isColliding); } } diff --git a/lib/Chuck/Constants.js b/lib/Chuck/Constants.js index 435d77f..4e41b00 100644 --- a/lib/Chuck/Constants.js +++ b/lib/Chuck/Constants.js @@ -1 +1,15 @@ -Constants.js \ No newline at end of file +define(function() { + + var Constants = { + COLLISION_IDENTIFIER_PLAYER: 'player', + COLLISION_IDENTIFIER_PLAYER_HEAD: 'head', + COLLISION_IDENTIFIER_PLAYER_CHEST: 'chest', + COLLISION_IDENTIFIER_PLAYER_LEGS: 'legs', + COLLISION_IDENTIFIER_PLAYER_FOOT_SENSOR: 'footsensor', + + COLLISION_IDENTIFIER_TILE: 'tile' + } + + return Constants; + +}); \ No newline at end of file diff --git a/lib/Chuck/Loader/Level.js b/lib/Chuck/Loader/Level.js index acc7f0e..a45137d 100644 --- a/lib/Chuck/Loader/Level.js +++ b/lib/Chuck/Loader/Level.js @@ -1,4 +1,4 @@ -define(["Chuck/Settings", "Vendor/Box2D"], function(Settings, Box2D) { +define(["Chuck/Settings", "Chuck/Constants", "Vendor/Box2D"], function(Settings, Constants, Box2D) { // Public function Level(path, engine) { @@ -50,7 +50,7 @@ define(["Chuck/Settings", "Vendor/Box2D"], function(Settings, Box2D) { fixtureDef.friction = Settings.TILE_FRICTION; fixtureDef.restitution = Settings.TILE_RESTITUTION; fixtureDef.isSensor = false; - fixtureDef.userData = 'tile'; + fixtureDef.userData = Constants.COLLISION_IDENTIFIER_TILE; this.engine.createBody(bodyDef).CreateFixture(fixtureDef); } diff --git a/lib/Chuck/Physics/Doll.js b/lib/Chuck/Physics/Doll.js index a9455ff..26f2277 100644 --- a/lib/Chuck/Physics/Doll.js +++ b/lib/Chuck/Physics/Doll.js @@ -1,4 +1,4 @@ -define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ +define(["Vendor/Box2D", "Chuck/Constants", "Chuck/Settings"], function(Box2D, Constants, Settings){ function Doll (physicsEngine, id){ this.id = id; @@ -18,7 +18,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ bodyDef.fixedRotation = true; bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING; bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; - bodyDef.userData = 'player-' + this.id; + bodyDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER + '-' + this.id; this.body = world.CreateBody(bodyDef); @@ -32,14 +32,14 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO)); fixtureDef.shape = headShape; fixtureDef.isSensor = false; - fixtureDef.userData = 'myHead-' + this.id; + fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_HEAD; this.body.CreateFixture(fixtureDef); var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape(); bodyShape.SetAsOrientedBox(5 / Settings.RATIO, 16 / Settings.RATIO, new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -21 / Settings.RATIO)); fixtureDef.shape = bodyShape; fixtureDef.isSensor = false; - fixtureDef.userData = 'myBody-' + this.id; + fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_CHEST; this.body.CreateFixture(fixtureDef); var legsShape = new Box2D.Collision.Shapes.b2CircleShape(); @@ -48,7 +48,8 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ fixtureDef.shape = legsShape; fixtureDef.friction = Settings.PLAYER_FRICTION; fixtureDef.isSensor = false; - fixtureDef.userData = 'myLegs-' + this.id; + fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_LEGS; + this.legs = this.body.CreateFixture(fixtureDef); var feetShape = new Box2D.Collision.Shapes.b2CircleShape(); @@ -56,7 +57,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ feetShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, 0 / Settings.RATIO)); fixtureDef.shape = feetShape; fixtureDef.isSensor = true; - fixtureDef.userData = 'myFeet-' + this.id; + fixtureDef.userData = Constants.COLLISION_IDENTIFIER_FOOTSENSOR; this.body.CreateFixture(fixtureDef); this.body.SetActive(false); @@ -74,8 +75,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ Doll.prototype.setFriction = function (friction) { if(!friction) friction = -1; - if (this.legs.GetFriction() != friction) - { + if (this.legs.GetFriction() != friction) { this.legs.SetFriction(friction); } } From 48039054bb812936b68e62113d000d70fd461894 Mon Sep 17 00:00:00 2001 From: logsol Date: Mon, 16 Jul 2012 00:04:06 +0200 Subject: [PATCH 6/9] optimized server starting, compatible for cloud9, added port as 2nd argument and socket.io log level as 3rd argv --- lib/Server/Socket.js | 15 +++++++++------ server.js | 20 ++++++++------------ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/Server/Socket.js b/lib/Server/Socket.js index c903dc8..36e117b 100644 --- a/lib/Server/Socket.js +++ b/lib/Server/Socket.js @@ -1,18 +1,21 @@ define(['socket.io'], function(io) { - function Socket(server, coordinator) { + function Socket(server, options, coordinator) { + options.logLevel = typeof options.logLevel != 'undefined' + ? options.logLevel + : 0; + this.coordinator = coordinator; this.socket = io.listen(server); - this.init(server); + this.init(options); } - Socket.prototype.init = function(){ + Socket.prototype.init = function(options){ var self = this; - this.socket.configure('development', function(){ - this.set('log level', 0); + this.set('log level', options.logLevel); }); this.socket.on('connection', function(user){ @@ -26,4 +29,4 @@ define(['socket.io'], function(io) { return Socket; -}); \ No newline at end of file +}); diff --git a/server.js b/server.js index 3be08d8..df57cf0 100644 --- a/server.js +++ b/server.js @@ -13,28 +13,24 @@ var requirements = [ "Server/Coordinator" ]; +var port = process.argv[2] + || process.env.PORT + || process.env.npm_package_config_port; + requirejs(requirements, function(HttpServer, Socket, Coordinator) { var options = { - port: process.env.npm_package_config_port, + port: port, rootDirectory: './', - caching: false + caching: false, + logLevel: process.argv[3] }; var coordinator = new Coordinator(); var httpServer = new HttpServer(options); - var socket = new Socket(httpServer.getServer(), coordinator); + var socket = new Socket(httpServer.getServer(), options, coordinator); inspector.coordinator = coordinator; }); exports = module.exports = inspector; - -/* -belongs to channel.js -var chuck; -requirejs(["Chuck/Chuck"], function(Chuck) { - Chuck.init(); - chuck = Chuck; -}); -*/ \ No newline at end of file From 0a80a701a2aa7d80b636290b5f541beea0d01132 Mon Sep 17 00:00:00 2001 From: Karl Date: Mon, 16 Jul 2012 01:38:38 +0300 Subject: [PATCH 7/9] added port and log level info --- README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ac2156b..9e48b14 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,11 @@ Follow the development at http://chuck-game.tumblr.com/ How to run: -* git clone https://github.com/logsol/chuck.js.git -* cd chuck.js -* npm install -* node server.js +* ```git clone https://github.com/logsol/chuck.js.git``` +* ```cd chuck.js``` +* ```npm install``` +* ```node server.js``` Open in browser http://localhost:1234 - - This game is licensed under the GPLv3 licence http://www.gnu.org/licenses/gpl-3.0.html \ No newline at end of file From a54dfc05f82e794c7bf98f9679e352e2b2a425e8 Mon Sep 17 00:00:00 2001 From: Karl Date: Mon, 16 Jul 2012 01:40:08 +0300 Subject: [PATCH 8/9] added port and log level info --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e48b14..a1bd510 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ How to run: * ```git clone https://github.com/logsol/chuck.js.git``` * ```cd chuck.js``` * ```npm install``` -* ```node server.js``` +* ```node server.js [port] [log level]``` Open in browser http://localhost:1234 From 16d2c4e5033de59a22109bfa6554c5474b943af6 Mon Sep 17 00:00:00 2001 From: logsol Date: Mon, 16 Jul 2012 00:57:48 +0200 Subject: [PATCH 9/9] missing delete from players array --- lib/Chuck/Processors/ServerProcessor.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Chuck/Processors/ServerProcessor.js b/lib/Chuck/Processors/ServerProcessor.js index 03e0d7e..86cb47b 100755 --- a/lib/Chuck/Processors/ServerProcessor.js +++ b/lib/Chuck/Processors/ServerProcessor.js @@ -70,6 +70,7 @@ define(requires, function(PhysicsEngine, Player, Box2D, Level, InputController, ServerProcessor.prototype.userIdLeft = function(id) { var player = this.players[id].player; player.destroy(); + delete this.players[id]; } ServerProcessor.prototype.updateWorld = function() {