diff --git a/README.md b/README.md index ac2156b..a1bd510 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 [port] [log level]``` 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 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/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 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); } } 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() { 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) { 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; } 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 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