From 3edd6644125d767a88e1a5a88aa374ebdb95ae9f Mon Sep 17 00:00:00 2001 From: Jeena Date: Sun, 12 Jan 2014 04:33:08 +0100 Subject: [PATCH] implemented level restart --- app/Game/Client/GameController.js | 13 +++---- app/Game/Client/GameObjects/GameObject.js | 6 ++-- app/Game/Client/GameObjects/Item.js | 5 +++ app/Game/Client/GameObjects/Tile.js | 5 +++ app/Game/Core/GameController.js | 32 ++++++++++++----- app/Game/Core/GameObjects/GameObject.js | 2 ++ app/Game/Core/GameObjects/Items/Skateboard.js | 35 ++++++++++++------- app/Game/Core/Loader/Level.js | 12 ++++--- app/Game/Server/Control/PlayerController.js | 2 ++ app/Game/Server/GameController.js | 22 ++++++++---- app/Game/Server/User.js | 16 +++++++-- client.js | 1 + 12 files changed, 104 insertions(+), 47 deletions(-) diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index f03c131..f1a6bc9 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -16,16 +16,10 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat function GameController () { this.view = ViewManager.createView(); - - Parent.call(this, new PhysicsEngine()); - - this.physicsEngine.setCollisionDetector(); - + this.initStats(); this.me = null; - this.initStats(); - - this.update(); + Parent.call(this); } GameController.prototype = Object.create(Parent.prototype); @@ -34,6 +28,9 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat this.stats = new Stats(); this.stats.setMode(0); document.body.appendChild(this.stats.domElement); + var button = document.createElement("button"); + button.onclick = inspector.resetLevel; + document.body.appendChild(button); }; GameController.prototype.destruct = function() { diff --git a/app/Game/Client/GameObjects/GameObject.js b/app/Game/Client/GameObjects/GameObject.js index 8a9be9d..be62ec2 100644 --- a/app/Game/Client/GameObjects/GameObject.js +++ b/app/Game/Client/GameObjects/GameObject.js @@ -1,9 +1,10 @@ define([ "Game/Core/GameObjects/GameObject", - "Lib/Utilities/Exception" + "Lib/Utilities/Exception", + "Lib/Utilities/NotificationCenter" ], -function (Parent, Exception) { +function (Parent, Exception, NotificationCenter) { function GameObject(physicsEngine, uid) { Parent.call(this, physicsEngine, uid); @@ -14,7 +15,6 @@ function (Parent, Exception) { GameObject.prototype = Object.create(Parent.prototype); GameObject.prototype.destroy = function() { - // view ... Parent.prototype.destroy.call(this); }; diff --git a/app/Game/Client/GameObjects/Item.js b/app/Game/Client/GameObjects/Item.js index 18be4ea..63824c5 100644 --- a/app/Game/Client/GameObjects/Item.js +++ b/app/Game/Client/GameObjects/Item.js @@ -36,6 +36,11 @@ function (Parent, Settings, NotificationCenter) { ); }; + Item.prototype.destroy = function() { + NotificationCenter.trigger("view/removeMesh", this.mesh); + Parent.prototype.destroy.call(this); + }; + Item.prototype.render = function() { NotificationCenter.trigger("view/updateMesh", diff --git a/app/Game/Client/GameObjects/Tile.js b/app/Game/Client/GameObjects/Tile.js index 432f120..116d097 100644 --- a/app/Game/Client/GameObjects/Tile.js +++ b/app/Game/Client/GameObjects/Tile.js @@ -37,6 +37,11 @@ function (Parent, Settings, NotificationCenter) { ); }; + Tile.prototype.destroy = function() { + NotificationCenter.trigger("view/removeMesh", this.mesh); + Parent.prototype.destroy.call(this); + }; + Tile.prototype.render = function() { NotificationCenter.trigger("view/updateMesh", diff --git a/app/Game/Core/GameController.js b/app/Game/Core/GameController.js index 4136f39..557a5c5 100755 --- a/app/Game/Core/GameController.js +++ b/app/Game/Core/GameController.js @@ -4,35 +4,50 @@ define([ "Game/" + GLOBALS.context + "/Player" ], -function (Engine, Level, Player) { +function (PhysicsEngine, Level, Player) { - function GameController (physicsEngine) { + function GameController () { this.players = {}; this.gameObjects = { animated: [], fixed: [] }; - - if (! physicsEngine instanceof Engine) { - throw physicsEngine + " is not of type Engine"; - } + this.levelPath = null; - this.physicsEngine = physicsEngine; + this.physicsEngine = new PhysicsEngine(); + this.physicsEngine.setCollisionDetector(); + + this.update(); } + GameController.prototype.update = function() { + + }; + GameController.prototype.getPhysicsEngine = function () { return this.physicsEngine; } GameController.prototype.loadLevel = function (path) { + this.levelPath = path; + if (this.level) { - this.level.unload(); + this.level.destroy(); + this.gameObjects = { + animated: [], + fixed: [] + }; } this.level = new Level(path, this.physicsEngine, this.gameObjects); this.level.loadLevelInToEngine(); } + GameController.prototype.onResetLevel = function() { + this.loadLevel(this.levelPath); + }; + + GameController.prototype.destroy = function () { for(var player in this.players) { this.players[player].destroy(); @@ -53,6 +68,5 @@ function (Engine, Level, Player) { return new Player(user.id, this.physicsEngine); }; - return GameController; }); diff --git a/app/Game/Core/GameObjects/GameObject.js b/app/Game/Core/GameObjects/GameObject.js index 60cb6b6..11703cf 100644 --- a/app/Game/Core/GameObjects/GameObject.js +++ b/app/Game/Core/GameObjects/GameObject.js @@ -20,6 +20,8 @@ function (Box2D, Exception) { GameObject.prototype.destroy = function() { if(this.body instanceof Box2D.Dynamics.b2Body) { this.body.GetWorld().DestroyBody(this.body); + } else { + throw new Exception("can not destroy body"); } }; diff --git a/app/Game/Core/GameObjects/Items/Skateboard.js b/app/Game/Core/GameObjects/Items/Skateboard.js index f3ec9fc..678f8c5 100644 --- a/app/Game/Core/GameObjects/Items/Skateboard.js +++ b/app/Game/Core/GameObjects/Items/Skateboard.js @@ -7,19 +7,19 @@ define([ function (Parent, Box2D, Settings) { function Skateboard(physicsEngine, uid, options) { - this.physicsEngine = physicsEngine; Parent.call(this, physicsEngine, uid, options); - this.addWheel( - options.x + 8, - options.y + 1.5 - ); - - this.addWheel( - options.x - 8, - options.y + 1.5 - ); + this.wheels = [ + this.addWheel( + options.x + 8, + options.y + 1.5 + ), + this.addWheel( + options.x - 8, + options.y + 1.5 + ) + ]; } Skateboard.prototype = Object.create(Parent.prototype); @@ -60,22 +60,23 @@ function (Parent, Box2D, Settings) { var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); var offset = 4, factor = 80; - var density = ((this.options.weight + offset) / this.options.width / this.options.height) * factor; + var density = ((0.1 + offset) / 3 / 3) * factor; fixtureDef.density = density; fixtureDef.shape = wheelShape; fixtureDef.isSensor = false; - var wheelBody = this.physicsEngine.getWorld().CreateBody(bodyDef); + var wheelBody = this.body.GetWorld().CreateBody(bodyDef); wheelBody.CreateFixture(fixtureDef); var revoluteJointDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef(); revoluteJointDef.enableMotor = false; revoluteJointDef.Initialize(this.body, wheelBody, wheelBody.GetWorldCenter()); - this.physicsEngine.getWorld().CreateJoint(revoluteJointDef); + this.body.GetWorld().CreateJoint(revoluteJointDef); // FIXME this means, that we will have bodies in the world, which must not be // updated (wheels) because they are always connected to a body which will be updated. + return wheelBody; }; Skateboard.prototype.flip = function(direction) { @@ -83,6 +84,14 @@ function (Parent, Box2D, Settings) { // FIXME: implement body flip if necessary }; + + Skateboard.prototype.destroy = function() { + for (var i = 0; i < this.wheels.length; i++) { + this.body.GetWorld().DestroyBody(this.wheels[i]); + } + + Parent.prototype.destroy.call(this); + }; return Skateboard; diff --git a/app/Game/Core/Loader/Level.js b/app/Game/Core/Loader/Level.js index 4b34eb4..671ead3 100755 --- a/app/Game/Core/Loader/Level.js +++ b/app/Game/Core/Loader/Level.js @@ -22,9 +22,13 @@ define([ this.createItems(); } - Level.prototype.unload = function () { - // TODO unload level from engine if necessary - // Perhaps just remove all bodies? + Level.prototype.destroy = function () { + for (var key in this.gameObjects) { + for (var i = 0; i < this.gameObjects[key].length; i++) { + this.gameObjects[key][i].destroy(); + } + //this.gameObjects[key] = []; + } } // Private @@ -235,7 +239,7 @@ microwave: 3.744 image:'skateboard.gif', type:'skateboard', category:'outdoor', - weight: 5, + weight: 1.5, width:26, height:6, x:200, diff --git a/app/Game/Server/Control/PlayerController.js b/app/Game/Server/Control/PlayerController.js index 3ba41d3..aebfefc 100644 --- a/app/Game/Server/Control/PlayerController.js +++ b/app/Game/Server/Control/PlayerController.js @@ -17,6 +17,8 @@ function(Parent, NotificationCenter, Parser) { * retrieves move (and other) commands from client and executes them at the server */ PlayerController.prototype.applyCommand = function(options) { + // FIXME: remove this function and use ProtocolHelper.applyCommand() instead + // Don't forget to change the function names to on... var message; if (typeof options == "string") { message = Parser.decode(options); diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index f424be5..114c190 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -13,17 +13,16 @@ define([ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, NotificationCenter, Player, GameObject, Doll) { function GameController (channel) { - Parent.call(this, new PhysicsEngine()); - - this.physicsEngine.setCollisionDetector(); - + this.channel = channel; - this.update(); + Parent.call(this); + this.updateWorld(); NotificationCenter.on('user/joined', this.userJoined, this); - NotificationCenter.on('user/left', this.userLeft, this); + NotificationCenter.on('user/left', this.userLeft, this); // FIXME: refactor this.userLeft -> this.onUserLeft, even in core and client + NotificationCenter.on('user/resetLevel', this.onResetLevel, this); console.checkpoint('starting game controller for channel ' + channel.name); } @@ -48,9 +47,10 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N } GameController.prototype.spawnPlayer = function(player) { - var x = 150, + var x = 150 + Math.random() * 300, y = 50; player.spawn(x, y); + this.gameObjects.animated.push(player.getDoll()); var message = { spawnPlayer: { @@ -128,6 +128,14 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N return spawnedPlayers; }; + GameController.prototype.onResetLevel = function(userId) { + Parent.prototype.onResetLevel.call(this); + NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", {resetLevel:true}); + for (var key in this.players) { + this.spawnPlayer(this.players[key]); + } + }; + return GameController; }); diff --git a/app/Game/Server/User.js b/app/Game/Server/User.js index cbaa026..622ae04 100644 --- a/app/Game/Server/User.js +++ b/app/Game/Server/User.js @@ -1,10 +1,11 @@ define([ "Game/Core/User", "Lib/Utilities/NotificationCenter", - "Lib/Utilities/Protocol/Helper" + "Lib/Utilities/Protocol/Helper", + "Lib/Utilities/Protocol/Parser", ], -function(Parent, NotificationCenter, ProtocolHelper) { +function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) { function User(id, channel) { Parent.call(this, id); @@ -38,7 +39,16 @@ function(Parent, NotificationCenter, ProtocolHelper) { // User command callbacks User.prototype.onGameCommand = function(command) { - this.player.playerController.applyCommand(command); + + if (typeof command == "string") { + command = ProtocolParser.decode(command); + } // FIXME: move this to Protocol helper as a function + + if(command.hasOwnProperty("resetLevel")) { + NotificationCenter.trigger("user/resetLevel", this.id); + } else { + this.player.playerController.applyCommand(command); + } }; diff --git a/client.js b/client.js index d78c613..8e3fd47 100755 --- a/client.js +++ b/client.js @@ -58,6 +58,7 @@ function (Networker, SocketIO, Settings, Exception, PIXI) { var networker = new Networker(socket); inspector.networker = networker; inspector.settings = Settings; + inspector.resetLevel = function() { networker.sendGameCommand("resetLevel"); } }); }); \ No newline at end of file