diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index 877e5bd..74c9bbc 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -36,25 +36,6 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat document.body.appendChild(this.stats.domElement); }; - GameController.prototype.makeMouseJoint = function(p) { - var ground = this.physicsEngine.getGround(); - var body = this.me.getBody(); - - var def = new Box2D.Dynamics.Joints.b2MouseJointDef(); - - def.bodyA = ground; - def.bodyB = body; - def.target = p; - - def.collideConnected = false; - def.maxForce = 100; - def.dampingRatio = 0.99; - - this.mouse_joint = this.physicsEngine.world.CreateJoint(def); - - body.SetAwake(true); - } - GameController.prototype.destruct = function() { //destroy box2d world etc. }; @@ -128,5 +109,13 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat Parent.prototype.loadLevel.call(this, path); } + GameController.prototype.userLeft = function(user) { + var doll = this.players[user.id].doll; + var i = this.gameObjects.animated.indexOf(doll); + if(i>=0) this.gameObjects.animated.splice(i, 1); + + Parent.prototype.userLeft.call(this, user); + } + return GameController; }); diff --git a/app/Game/Client/GameObjects/Item.js b/app/Game/Client/GameObjects/Item.js new file mode 100644 index 0000000..5051c01 --- /dev/null +++ b/app/Game/Client/GameObjects/Item.js @@ -0,0 +1,53 @@ +define([ + "Game/Core/GameObjects/Item", + "Game/Config/Settings", + "Game/Core/NotificationCenter" +], + +function (Parent, Settings, NotificationCenter) { + + function Item(physicsEngine, uid, options) { + Parent.call(this, physicsEngine, uid, options); + } + + Item.prototype = Object.create(Parent.prototype); + + Item.prototype.createMesh = function() { + var self = this; + + var texturePath = Settings.GRAPHICS_PATH + + Settings.GRAPHICS_SUBPATH_ITEMS + + this.options.category + '/' + + this.options.image; + + var callback = function(mesh) { + self.mesh = mesh; + NotificationCenter.trigger("view/addMesh", mesh); + } + + NotificationCenter.trigger("view/createMesh", + texturePath, + callback, + { + width: this.options.width, + height: this.options.height, + pivot: "mb" + } + ); + }; + + Item.prototype.render = function() { + + NotificationCenter.trigger("view/updateMesh", + this.mesh, + { + x: this.body.GetPosition().x * Settings.RATIO, + y: this.body.GetPosition().y * Settings.RATIO, + rotation: this.body.GetAngle() + } + ); + } + + return Item; + +}); \ No newline at end of file diff --git a/app/Game/Client/GameObjects/Tile.js b/app/Game/Client/GameObjects/Tile.js index 4df9e52..946e623 100644 --- a/app/Game/Client/GameObjects/Tile.js +++ b/app/Game/Client/GameObjects/Tile.js @@ -15,10 +15,9 @@ function (Parent, Settings, NotificationCenter) { Tile.prototype.createMesh = function() { var self = this; - var material = "Stones"; var texturePath = Settings.GRAPHICS_PATH + Settings.GRAPHICS_SUBPATH_TILES - + material + '/' + + this.options.m + '/' + this.options.s + '' + (this.options.r || 0) + '.gif'; diff --git a/app/Game/Client/Networker.js b/app/Game/Client/Networker.js index 55e5380..8b4edb7 100755 --- a/app/Game/Client/Networker.js +++ b/app/Game/Client/Networker.js @@ -63,11 +63,13 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings) { if (options.spawnedPlayers) { for(var i = 0; i < options.spawnedPlayers.length; i++) { this.gameController.onSpawnPlayer(options.spawnedPlayers[i]); - - console.log("already spawned player, options: ", options.spawnedPlayers[i]) } } + if (options.worldUpdate) { + this.gameController.onWorldUpdate(options.worldUpdate); + } + this.initPing(); } diff --git a/app/Game/Client/Physics/Doll.js b/app/Game/Client/Physics/Doll.js index 6ba2466..a88972f 100644 --- a/app/Game/Client/Physics/Doll.js +++ b/app/Game/Client/Physics/Doll.js @@ -83,26 +83,6 @@ function (Parent, Settings, NotificationCenter, Exception) { } - Doll.prototype.render = function() { - if(this.actionState) { - NotificationCenter.trigger("view/updateMesh", - this.animatedMeshes[this.actionState], - { - x: this.body.GetPosition().x * Settings.RATIO, - y: this.body.GetPosition().y * Settings.RATIO - } - ); - - NotificationCenter.trigger("view/updateMesh", - this.headMesh, - { - x: this.body.GetPosition().x * Settings.RATIO, - y: this.body.GetPosition().y * Settings.RATIO - 31 - } - ) - } - } - Doll.prototype.lookAt = function(x, y) { var oldLookDirection = this.lookDirection; @@ -140,6 +120,26 @@ function (Parent, Settings, NotificationCenter, Exception) { Parent.prototype.destroy.call(this); } + + Doll.prototype.render = function() { + if(this.actionState) { + NotificationCenter.trigger("view/updateMesh", + this.animatedMeshes[this.actionState], + { + x: this.body.GetPosition().x * Settings.RATIO, + y: this.body.GetPosition().y * Settings.RATIO + } + ); + + NotificationCenter.trigger("view/updateMesh", + this.headMesh, + { + x: this.body.GetPosition().x * Settings.RATIO, + y: this.body.GetPosition().y * Settings.RATIO - 31 + } + ) + } + } return Doll; diff --git a/app/Game/Client/View/Views/AbstractView.js b/app/Game/Client/View/Views/AbstractView.js index 641d5f7..48154c8 100644 --- a/app/Game/Client/View/Views/AbstractView.js +++ b/app/Game/Client/View/Views/AbstractView.js @@ -104,16 +104,5 @@ function (DomController, Settings, Exception, NotificationCenter) { throw new Exception('Abstract Function setCameraZoom not overwritten '); } - // TODO Move to Level - AbstractView.prototype.tileAtPositionExists = function(objects, x, y) { - - for (var i = 0; i < objects.length; i++) { - var o = objects[i]; - if(o.x == x && o.y == y) return true; - } - return false; - }; - - return AbstractView; }); \ No newline at end of file diff --git a/app/Game/Core/GameObjects/Item.js b/app/Game/Core/GameObjects/Item.js new file mode 100644 index 0000000..eaf1f31 --- /dev/null +++ b/app/Game/Core/GameObjects/Item.js @@ -0,0 +1,46 @@ +define([ + "Game/" + GLOBALS.context + "/GameObjects/GameObject", + "Lib/Vendor/Box2D", + "Game/Config/Settings" +], + +function (Parent, Box2D, Settings) { + + function Item(physicsEngine, uid, options) { + this.options = options; + Parent.call(this, physicsEngine, uid); + this.createFixture(); + } + + Item.prototype = Object.create(Parent.prototype); + + Item.prototype.getBodyDef = function() { + + var bodyDef = new Box2D.Dynamics.b2BodyDef(); + bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; + bodyDef.position.x = this.options.x / Settings.RATIO; + bodyDef.position.y = this.options.y / Settings.RATIO; + bodyDef.angle = 0; + + return bodyDef; + } + + Item.prototype.createFixture = function () { + + var itemShape = new Box2D.Collision.Shapes.b2PolygonShape(); + var w = this.options.width / 2 / Settings.RATIO; + var h = this.options.height / 2 / Settings.RATIO; + itemShape.SetAsOrientedBox(w, h, new Box2D.Common.Math.b2Vec2(0, -h)); + + var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); + fixtureDef.shape = itemShape; + fixtureDef.density = Settings.ITEM_DENSITY; + fixtureDef.friction = Settings.ITEM_FRICTION; + fixtureDef.restitution = Settings.ITEM_RESTITUTION; + fixtureDef.isSensor = false; + this.body.CreateFixture(fixtureDef); + } + + return Item; + +}); \ No newline at end of file diff --git a/app/Game/Core/GameObjects/Tile.js b/app/Game/Core/GameObjects/Tile.js index 33178fc..52cb61b 100644 --- a/app/Game/Core/GameObjects/Tile.js +++ b/app/Game/Core/GameObjects/Tile.js @@ -1,11 +1,10 @@ define([ "Game/" + GLOBALS.context + "/GameObjects/GameObject", "Lib/Vendor/Box2D", - "Game/Config/Settings", - "Game/" + GLOBALS.context + "/Collision/Detector" + "Game/Config/Settings" ], -function (Parent, Box2D, Settings, CollisionDetector) { +function (Parent, Box2D, Settings) { function Tile(physicsEngine, uid, options) { this.options = options; @@ -37,7 +36,6 @@ function (Parent, Box2D, Settings, CollisionDetector) { fixtureDef.friction = Settings.TILE_FRICTION; fixtureDef.restitution = Settings.TILE_RESTITUTION; fixtureDef.isSensor = false; - fixtureDef.userData = CollisionDetector.IDENTIFIER.TILE; this.body.CreateFixture(fixtureDef); } diff --git a/app/Game/Core/Loader/Level.js b/app/Game/Core/Loader/Level.js index cd96a80..cc11cc1 100755 --- a/app/Game/Core/Loader/Level.js +++ b/app/Game/Core/Loader/Level.js @@ -2,9 +2,10 @@ define([ "Game/Config/Settings", "Lib/Vendor/Box2D", "Game/" + GLOBALS.context + "/Collision/Detector", - "Game/" + GLOBALS.context + "/GameObjects/Tile" + "Game/" + GLOBALS.context + "/GameObjects/Tile", + "Game/" + GLOBALS.context + "/GameObjects/Item" -], function (Settings, Box2D, CollisionDetector, Tile) { +], function (Settings, Box2D, CollisionDetector, Tile, Item) { // Public function Level (path, engine, gameObjects) { @@ -17,6 +18,7 @@ define([ Level.prototype.loadLevelInToEngine = function () { this.loadLevelObjectFromPath(this.path); this.createTiles(); + this.createItems(); } Level.prototype.unload = function () { @@ -34,10 +36,23 @@ define([ var tiles = this.levelObject.tiles; for (var i = 0; i < tiles.length; i++) { - this.gameObjects.fixed.push(new Tile(this.engine, "tile-" + i, tiles[i])); + var options = tiles[i]; + options.m = this.tileAtPositionExists(options.x, options.y - 1) ? "Soil" : "GrassSoil"; + + this.gameObjects.fixed.push(new Tile(this.engine, "tile-" + i, options)); } } + Level.prototype.createItems = function() { + var items = this.levelObject.items; + + for (var i = 0; i < items.length; i++) { + var options = items[i]; + + this.gameObjects.animated.push(new Item(this.engine, "item-" + i, options)); + }; + }; + Level.prototype.loadLevelObjectFromPath = function (path) { // TODO: load JSON levelObject from path @@ -89,87 +104,182 @@ define([ // o o o this.levelObject = { + items: [ + { + name:'Banana', + image:'banana.gif', + shape:'rectangle', + category:'kitchen', + weight:0.2, + width:5, + height:9, + x:20, + y:0, + rotation: 0 + }, + { + name:'Large Cleaver', + image:'cleaver_large.gif', + shape:'rectangle', + category:'kitchen', + weight:1.1, + width:8, + height:22, + x:40, + y:0, + rotation: 0 + }, + { + name:'Small Cleaver', + image:'cleaver_small.gif', + shape:'rectangle', + category:'kitchen', + weight:0.8, + width:6, + height:17, + x:60, + y:0, + rotation: 0 + }, + { + name:'Coffeemachine', + image:'coffeemachine.gif', + shape:'rectangle', + category:'kitchen', + weight:3, + width:11, + height:14, + x:80, + y:0, + rotation: 0 + }, + { + name:'Microwave', + image:'microwave.gif', + shape:'rectangle', + category:'kitchen', + weight:7, + width:19, + height:12, + x:100, + y:0, + rotation: 0 + }, + { + name:'Refridgerator', + image:'fridge.gif', + shape:'rectangle', + category:'kitchen', + weight:5, + width:31, + height:53, + x:120, + y:200, + rotation: 0 + }, + { + name:'Knife', + image:'knife.gif', + shape:'rectangle', + category:'kitchen', + weight:0.3, + width:4, + height:15, + x:140, + y:0, + rotation: 0 + } + ], tiles: [ -{s:1, x:1, y:1, r:0}, -{s:1, x:3, y:18}, -{s:1, x:37, y:27}, -{s:1, x:20, y:24}, -{s:1, x:24, y:27}, -{s:1, x:37, y:26}, -{s:1, x:9, y:18}, -{s:2, x:32, y:25, r:1}, -{s:1, x:23, y:27}, -{s:3, x:34, y:24, r:1}, -{s:1, x:35, y:28}, -{s:4, x:17, y:21}, -{s:2, x:21, y:24}, -{s:2, x:42, y:23, r:3}, -{s:3, x:30, y:24, r:3}, -{s:2, x:22, y:25}, -{s:1, x:40, y:25}, -{s:1, x:38, y:26}, -{s:1, x:8, y:18}, -{s:1, x:38, y:25}, -{s:1, x:28, y:28}, -{s:1, x:36, y:27}, -{s:1, x:7, y:18}, -{s:2, x:20, y:23}, -{s:2, x:43, y:23, r:1}, -{s:6, x:31, y:24}, -{s:1, x:16, y:21}, -{s:1, x:1, y:18}, -{s:1, x:31, y:29}, -{s:2, x:30, y:25, r:2}, -{s:4, x:11, y:18}, -{s:1, x:28, y:27}, -{s:1, x:28, y:26}, -{s:1, x:28, y:29}, -{s:1, x:19, y:23}, -{s:5, x:12, y:18, r:1}, -{s:1, x:42, y:24}, -{s:6, x:33, y:24, r:2}, -{s:1, x:39, y:25}, -{s:1, x:33, y:29}, -{s:1, x:29, y:29}, -{s:1, x:21, y:25}, -{s:1, x:27, y:27}, -{s:5, x:16, y:20, r:1}, -{s:1, x:5, y:18}, -{s:5, x:18, y:21, r:1}, -{s:4, x:13, y:19}, -{s:1, x:14, y:20}, -{s:1, x:30, y:29}, -{s:1, x:4, y:18}, -{s:1, x:6, y:18}, -{s:1, x:2, y:18}, -{s:1, x:32, y:24}, -{s:1, x:34, y:29}, -{s:1, x:32, y:29}, -{s:2, x:1, y:16}, -{s:1, x:10, y:18}, -{s:1, x:42, y:25}, -{s:2, x:28, y:25, r:3}, -{s:2, x:0, y:16, r:2}, -{s:1, x:22, y:27}, -{s:1, x:25, y:27}, -{s:1, x:31, y:25}, -{s:5, x:14, y:19, r:1}, -{s:1, x:41, y:25}, -{s:1, x:36, y:28}, -{s:4, x:15, y:20}, -{s:2, x:19, y:22}, -{s:3, x:26, y:26, r:3}, -{s:1, x:26, y:27}, -{s:1, x:18, y:22}, -{s:6, x:27, y:26}, -{s:1, x:22, y:26}, -{s:1, x:1, y:17}, -{s:1, x:35, y:29}, -{s:1, x:12, y:19} - - ] + {s:1, x:1, y:1, r:0}, + {s:1, x:3, y:18}, + {s:1, x:37, y:27}, + {s:1, x:20, y:24}, + {s:1, x:24, y:27}, + {s:1, x:37, y:26}, + {s:1, x:9, y:18}, + {s:2, x:32, y:25, r:1}, + {s:1, x:23, y:27}, + {s:3, x:34, y:24, r:1}, + {s:1, x:35, y:28}, + {s:4, x:17, y:21}, + {s:2, x:21, y:24}, + {s:2, x:42, y:23, r:3}, + {s:3, x:30, y:24, r:3}, + {s:2, x:22, y:25}, + {s:1, x:40, y:25}, + {s:1, x:38, y:26}, + {s:1, x:8, y:18}, + {s:1, x:38, y:25}, + {s:1, x:28, y:28}, + {s:1, x:36, y:27}, + {s:1, x:7, y:18}, + {s:2, x:20, y:23}, + {s:2, x:43, y:23, r:1}, + {s:6, x:31, y:24}, + {s:1, x:16, y:21}, + {s:1, x:1, y:18}, + {s:1, x:31, y:29}, + {s:2, x:30, y:25, r:2}, + {s:4, x:11, y:18}, + {s:1, x:28, y:27}, + {s:1, x:28, y:26}, + {s:1, x:28, y:29}, + {s:1, x:19, y:23}, + {s:5, x:12, y:18, r:1}, + {s:1, x:42, y:24}, + {s:6, x:33, y:24, r:2}, + {s:1, x:39, y:25}, + {s:1, x:33, y:29}, + {s:1, x:29, y:29}, + {s:1, x:21, y:25}, + {s:1, x:27, y:27}, + {s:5, x:16, y:20, r:1}, + {s:1, x:5, y:18}, + {s:5, x:18, y:21, r:1}, + {s:4, x:13, y:19}, + {s:1, x:14, y:20}, + {s:1, x:30, y:29}, + {s:1, x:4, y:18}, + {s:1, x:6, y:18}, + {s:1, x:2, y:18}, + {s:1, x:32, y:24}, + {s:1, x:34, y:29}, + {s:1, x:32, y:29}, + {s:2, x:1, y:16}, + {s:1, x:10, y:18}, + {s:1, x:42, y:25}, + {s:2, x:28, y:25, r:3}, + {s:2, x:0, y:16, r:2}, + {s:1, x:22, y:27}, + {s:1, x:25, y:27}, + {s:1, x:31, y:25}, + {s:5, x:14, y:19, r:1}, + {s:1, x:41, y:25}, + {s:1, x:36, y:28}, + {s:4, x:15, y:20}, + {s:2, x:19, y:22}, + {s:3, x:26, y:26, r:3}, + {s:1, x:26, y:27}, + {s:1, x:18, y:22}, + {s:6, x:27, y:26}, + {s:1, x:22, y:26}, + {s:1, x:1, y:17}, + {s:1, x:35, y:29}, + {s:1, x:12, y:19} + ] } } + // TODO remove hack + Level.prototype.tileAtPositionExists = function(x, y) { + + for (var i = 0; i < this.levelObject.tiles.length; i++) { + var o = this.levelObject.tiles[i]; + if(o.x == x && o.y == y) return true; + } + return false; + }; + return Level; }) \ No newline at end of file diff --git a/app/Game/Core/Physics/Doll.js b/app/Game/Core/Physics/Doll.js index 796cd2b..9b504bb 100755 --- a/app/Game/Core/Physics/Doll.js +++ b/app/Game/Core/Physics/Doll.js @@ -158,10 +158,6 @@ function (Parent, Box2D, Settings, CollisionDetector) { } } - Doll.prototype.destroy = function () { - Parent.prototype.destroy.call(this); - } - Doll.prototype.setStanding = function (isStanding) { if (this.standing == isStanding) return; this.standing = isStanding; diff --git a/app/Game/Server/Channel.js b/app/Game/Server/Channel.js index de6b9ed..58187c8 100755 --- a/app/Game/Server/Channel.js +++ b/app/Game/Server/Channel.js @@ -40,6 +40,7 @@ var user = new User(userId, this); var joinedUsers = Object.keys(this.users); var spawnedPlayers = this.gameController.getSpawnedPlayersAndTheirPositions(); + var worldUpdate = this.gameController.getWorldUpdateObject(true); this.users[user.id] = user; @@ -47,7 +48,9 @@ userId: user.id, channelName: this.name, joinedUsers: joinedUsers, - spawnedPlayers: spawnedPlayers + spawnedPlayers: spawnedPlayers, + worldUpdate: worldUpdate + }; NotificationCenter.trigger('user/' + user.id + "/joinSuccess", options); diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index 4fbe5c6..91f8204 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -71,12 +71,23 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N GameController.prototype.updateWorld = function () { + var update = this.getWorldUpdateObject(false); + + if(Object.getOwnPropertyNames(update).length > 0) { + NotificationCenter.trigger("sendControlCommandToAllUsers", 'gameCommand', {worldUpdate:update}); + } + + setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); + } + + GameController.prototype.getWorldUpdateObject = function(getSleeping) { + getSleeping = getSleeping || false; + var update = {}; - var isUpdateNeeded = false; var body = this.physicsEngine.world.GetBodyList(); do { - if(body.IsAwake()) { + if(getSleeping || body.IsAwake()) { var userData = body.GetUserData(); if (userData instanceof GameObject) { @@ -93,28 +104,12 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N update[gameObject.uid].as = gameObject.getActionState(); update[gameObject.uid].laxy = gameObject.lookAtXY; } - - isUpdateNeeded = true; } } } while (body = body.GetNext()); - - if(isUpdateNeeded) { - NotificationCenter.trigger("sendControlCommandToAllUsers", 'gameCommand', {worldUpdate:update}); - } - setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); - } - - GameController.prototype.getSpawnedPlayers = function() { - var spawnedPlayers = {}; - for(player in this.players) { - if(player.isSpawned) { - spawnedPlayers[player.id] = player; - } - } - return spawnedPlayers; + return update; }; GameController.prototype.getSpawnedPlayersAndTheirPositions = function() { @@ -130,11 +125,9 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N } } - // FIXME: OR: use get Spawned Players and fetch them into a sort of transfer objects - // that contains only necessary data - return spawnedPlayers; }; + return GameController; }); diff --git a/app/Game/Server/GameObjects/Item.js b/app/Game/Server/GameObjects/Item.js new file mode 100644 index 0000000..4884a06 --- /dev/null +++ b/app/Game/Server/GameObjects/Item.js @@ -0,0 +1,9 @@ +define([ + "Game/Core/GameObjects/Item" +], + +function(Parent) { + + return Parent; + +}); \ No newline at end of file