diff --git a/app/Game/Client/Control/PlayerController.js b/app/Game/Client/Control/PlayerController.js index 1537e2f..f7e6c6f 100755 --- a/app/Game/Client/Control/PlayerController.js +++ b/app/Game/Client/Control/PlayerController.js @@ -66,8 +66,9 @@ function (Parent, KeyboardInput, MouseInput, NotificationCenter) { } PlayerController.prototype.setXY = function(x, y) { - Parent.prototype.lookAt.call(this, x, y); - NotificationCenter.trigger('sendGameCommand', 'lookAt', x, y); // implement that x and y are received + var options = {x:x, y:y}; + Parent.prototype.lookAt.call(this, options); + NotificationCenter.trigger('sendGameCommand', 'lookAt', options); }; PlayerController.prototype.update = function () { diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index 510742a..877e5bd 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -7,10 +7,12 @@ define([ "Game/Core/NotificationCenter", "Lib/Utilities/RequestAnimFrame", "Game/Config/Settings", - "Lib/Vendor/Stats" + "Lib/Vendor/Stats", + "Game/Client/GameObjects/GameObject", + "Game/Client/Physics/Doll" ], -function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, NotificationCenter, requestAnimFrame, Settings, Stats) { +function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, NotificationCenter, requestAnimFrame, Settings, Stats, GameObject, Doll) { function GameController () { this.view = ViewManager.createView(); @@ -85,41 +87,21 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat var body = this.physicsEngine.world.GetBodyList(); do { - var bodyName = body.GetUserData(); - if(bodyName && updateData[bodyName]) { - var update = updateData[bodyName]; - body.SetAwake(true); - - if(false && this.me && this.me.getBody() == body) { - - var p = body.GetPosition(); - var x = update.p.x - p.x; - var y = update.p.y - p.y; - - var max = 0.5; - var factor = 0.2; - - //if(x > max || x < -max || y > max || y < -max) { - if(!this.mouse_joint) this.makeMouseJoint(update.p); - else this.mouse_joint.SetTarget(update.p); - var self = this; - /*setTimeout(function() { - self.physicsEngine.world.DestroyJoint(self.mouse_joint); - self.mouse_joint = null; - }, Settings.WORLD_UPDATE_BROADCAST_INTERVAL / 2)*/ - //} else { - //this.physicsEngine.world.DestroyJoint(this.mouse_joint); - //this.mouse_joint = null; - //} - - // NEXT TIME, try to create a simple body, that gets position and velocities from server doll - // and connect the joint to that. - - } else { + var userData = body.GetUserData(); + if (userData instanceof GameObject) { + var gameObject = userData; + if(updateData[gameObject.uid]) { + var update = updateData[gameObject.uid]; + body.SetAwake(true); body.SetPosition(update.p); body.SetAngle(update.a); body.SetLinearVelocity(update.lv); - body.SetAngularVelocity(update.av); + body.SetAngularVelocity(update.av); + + if (gameObject instanceof Doll) { + gameObject.setActionState(update.as); + gameObject.lookAt(update.laxy.x, update.laxy.y); + } } } } while (body = body.GetNext()); diff --git a/app/Game/Client/GameObjects/GameObject.js b/app/Game/Client/GameObjects/GameObject.js index b8645a0..8a9be9d 100644 --- a/app/Game/Client/GameObjects/GameObject.js +++ b/app/Game/Client/GameObjects/GameObject.js @@ -5,8 +5,8 @@ define([ function (Parent, Exception) { - function GameObject(physicsEngine) { - Parent.call(this, physicsEngine); + function GameObject(physicsEngine, uid) { + Parent.call(this, physicsEngine, uid); this.createMesh(); this.render(); } diff --git a/app/Game/Client/GameObjects/Tile.js b/app/Game/Client/GameObjects/Tile.js index 7af1f02..4df9e52 100644 --- a/app/Game/Client/GameObjects/Tile.js +++ b/app/Game/Client/GameObjects/Tile.js @@ -6,8 +6,8 @@ define([ function (Parent, Settings, NotificationCenter) { - function Tile(physicsEngine, options) { - Parent.call(this, physicsEngine, options); + function Tile(physicsEngine, uid, options) { + Parent.call(this, physicsEngine, uid, options); } Tile.prototype = Object.create(Parent.prototype); diff --git a/app/Game/Client/Physics/Doll.js b/app/Game/Client/Physics/Doll.js index 54a2e12..6ba2466 100644 --- a/app/Game/Client/Physics/Doll.js +++ b/app/Game/Client/Physics/Doll.js @@ -1,10 +1,11 @@ define([ "Game/Core/Physics/Doll", "Game/Config/Settings", - "Game/Core/NotificationCenter" + "Game/Core/NotificationCenter", + "Lib/Utilities/Exception" ], -function (Parent, Settings, NotificationCenter) { +function (Parent, Settings, NotificationCenter, Exception) { function Doll(physicsEngine, playerId) { this.animationDef = { @@ -20,6 +21,7 @@ function (Parent, Settings, NotificationCenter) { } this.animatedMeshes = {}; + this.headMesh = null; Parent.call(this, physicsEngine, playerId); } @@ -30,6 +32,8 @@ function (Parent, Settings, NotificationCenter) { if(this.actionState == state) return; + if(!state) throw new Exception("action state is undefined"); + if(this.animatedMeshes[this.actionState]) { NotificationCenter.trigger("view/updateMesh", this.animatedMeshes[this.actionState], { visible: false }); } @@ -41,6 +45,8 @@ function (Parent, Settings, NotificationCenter) { Doll.prototype.createMesh = function() { + // Body + var padF = function(n) { if(n<10) return "00" + n; if(n<100) return "0" + n; @@ -65,6 +71,16 @@ function (Parent, Settings, NotificationCenter) { NotificationCenter.trigger("view/createAnimatedMesh", texturePaths, callback, { visible: false, pivot: "mb" }); } + + // Head + + var texturePath = Settings.GRAPHICS_PATH + "Characters/Chuck/head.png"; + var callback = function (mesh) { + self.headMesh = mesh; + NotificationCenter.trigger("view/addMesh", mesh); + } + NotificationCenter.trigger("view/createMesh", texturePath, callback, { pivot: "mb" }); + } Doll.prototype.render = function() { @@ -77,11 +93,19 @@ function (Parent, Settings, NotificationCenter) { } ); + 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; + Parent.prototype.lookAt.call(this, x, y); if(oldLookDirection != this.lookDirection) { @@ -94,9 +118,28 @@ function (Parent, Settings, NotificationCenter) { ); } } - }; - // TODO: implement destroy + var angle = Math.atan2(this.lookAtXY.x, this.lookAtXY.y) / 2 - 0.7855 * this.lookDirection; // 0.7855 = 45° + + NotificationCenter.trigger("view/updateMesh", + this.headMesh, + { + xScale: this.lookDirection, + rotation: angle + } + ); + } + + + Doll.prototype.destroy = function () { + for (var key in this.animatedMeshes) { + NotificationCenter.trigger("view/removeMesh", this.animatedMeshes[key]); + } + + NotificationCenter.trigger("view/removeMesh", this.headMesh); + + Parent.prototype.destroy.call(this); + } return Doll; diff --git a/app/Game/Client/View/Views/AbstractView.js b/app/Game/Client/View/Views/AbstractView.js index c247a7d..641d5f7 100644 --- a/app/Game/Client/View/Views/AbstractView.js +++ b/app/Game/Client/View/Views/AbstractView.js @@ -12,9 +12,10 @@ function (DomController, Settings, Exception, NotificationCenter) { this.canvas = null; NotificationCenter.on("view/createMesh", this.createMesh, this); - NotificationCenter.on("view/addMesh", this.addMesh, this); - NotificationCenter.on("view/updateMesh", this.updateMesh, this); NotificationCenter.on("view/createAnimatedMesh", this.createAnimatedMesh, this); + NotificationCenter.on("view/addMesh", this.addMesh, this); + NotificationCenter.on("view/removeMesh", this.removeMesh, this); + NotificationCenter.on("view/updateMesh", this.updateMesh, this); } AbstractView.prototype.isWebGlEnabled = function () { @@ -51,18 +52,22 @@ function (DomController, Settings, Exception, NotificationCenter) { throw new Exception('Abstract Function createMesh not overwritten '); } + AbstractView.prototype.createAnimatedMesh = function (texturePaths, callback, options) { + throw new Exception('Abstract Function createAnimatedMesh not overwritten '); + } + AbstractView.prototype.addMesh = function(mesh) { throw new Exception('Abstract Function addMesh not overwritten '); }; + AbstractView.prototype.removeMesh = function(mesh) { + throw new Exception('Abstract Function removeMesh not overwritten '); + }; + AbstractView.prototype.updateMesh = function(mesh, options) { throw new Exception('Abstract Function updateMesh not overwritten '); }; - AbstractView.prototype.createAnimatedMesh = function (texturePaths, callback, options) { - throw new Exception('Abstract Function createAnimatedMesh not overwritten '); - } - AbstractView.prototype.setMe = function(player) { this.me = player; }; diff --git a/app/Game/Client/View/Views/PixiView.js b/app/Game/Client/View/Views/PixiView.js index 3516c10..1b060a1 100755 --- a/app/Game/Client/View/Views/PixiView.js +++ b/app/Game/Client/View/Views/PixiView.js @@ -48,6 +48,10 @@ function (Parent, DomController, PIXI, Settings) { this.container.addChild(mesh); }; + PixiView.prototype.removeMesh = function(mesh) { + this.container.removeChild(mesh); + }; + PixiView.prototype.createMesh = function (texturePath, callback, options) { var texture = PIXI.Texture.fromImage(texturePath); @@ -84,15 +88,10 @@ function (Parent, DomController, PIXI, Settings) { if (options.height) mesh.height = options.height; if (options.visible === true || options.visible === false) mesh.visible = options.visible; if (options.pivot) { - switch(options.pivot) { - case "mb": - mesh.pivot.x = mesh.width / 2; - mesh.pivot.y = mesh.height; - break; - case "mm": + switch(options.pivot.length) { default: mesh.pivot.x = mesh.width / 2; - mesh.pivot.y = mesh.height / 2; + mesh.pivot.y = mesh.height; break; } diff --git a/app/Game/Core/Control/PlayerController.js b/app/Game/Core/Control/PlayerController.js index d81ab4a..f446154 100755 --- a/app/Game/Core/Control/PlayerController.js +++ b/app/Game/Core/Control/PlayerController.js @@ -29,8 +29,8 @@ define(function () { this.player.jump(); } - PlayerController.prototype.lookAt = function (x, y) { - this.player.lookAt(x, y); + PlayerController.prototype.lookAt = function (options) { + if(options) this.player.lookAt(options.x, options.y); } PlayerController.prototype.update = function () { diff --git a/app/Game/Core/GameObjects/GameObject.js b/app/Game/Core/GameObjects/GameObject.js index 9fd8f49..60cb6b6 100644 --- a/app/Game/Core/GameObjects/GameObject.js +++ b/app/Game/Core/GameObjects/GameObject.js @@ -5,8 +5,11 @@ define([ function (Box2D, Exception) { - function GameObject(physicsEngine) { + function GameObject(physicsEngine, uid) { + this.uid = uid; + var def = this.getBodyDef(); + def.userData = this; this.body = physicsEngine.getWorld().CreateBody(def); } diff --git a/app/Game/Core/GameObjects/Tile.js b/app/Game/Core/GameObjects/Tile.js index fb7ad10..33178fc 100644 --- a/app/Game/Core/GameObjects/Tile.js +++ b/app/Game/Core/GameObjects/Tile.js @@ -7,9 +7,9 @@ define([ function (Parent, Box2D, Settings, CollisionDetector) { - function Tile(physicsEngine, options) { + function Tile(physicsEngine, uid, options) { this.options = options; - Parent.call(this, physicsEngine); + Parent.call(this, physicsEngine, uid); this.createPhysicTile(this.options); } diff --git a/app/Game/Core/Loader/Level.js b/app/Game/Core/Loader/Level.js index 9363cb8..cd96a80 100755 --- a/app/Game/Core/Loader/Level.js +++ b/app/Game/Core/Loader/Level.js @@ -34,7 +34,7 @@ define([ var tiles = this.levelObject.tiles; for (var i = 0; i < tiles.length; i++) { - this.gameObjects.fixed.push(new Tile(this.engine, tiles[i])); + this.gameObjects.fixed.push(new Tile(this.engine, "tile-" + i, tiles[i])); } } diff --git a/app/Game/Core/Physics/Doll.js b/app/Game/Core/Physics/Doll.js index 92c29ba..796cd2b 100755 --- a/app/Game/Core/Physics/Doll.js +++ b/app/Game/Core/Physics/Doll.js @@ -7,17 +7,16 @@ define([ function (Parent, Box2D, Settings, CollisionDetector) { - function Doll (physicsEngine, playerId) { + function Doll (physicsEngine, uid) { - this.playerId = playerId; - - Parent.call(this, physicsEngine); + Parent.call(this, physicsEngine, uid); this.standing = false; this.moveDirection = 0; this.lookDirection = 0; this.legs; this.actionState = null; + this.lookAtXY = {x:0, y:0}; this.createFixtures(); this.body.SetActive(false); @@ -32,8 +31,6 @@ function (Parent, Box2D, Settings, CollisionDetector) { bodyDef.fixedRotation = true; bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING; bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; - bodyDef.userData = CollisionDetector.IDENTIFIER.PLAYER + '-' + this.playerId; - return bodyDef; }; @@ -83,6 +80,10 @@ function (Parent, Box2D, Settings, CollisionDetector) { this.actionState = state; } + Doll.prototype.getActionState = function() { + return this.actionState; + } + Doll.prototype.isWalking = function() { return ["walk", "walkback", "run"].indexOf(this.actionState) >= 0; } @@ -141,6 +142,7 @@ function (Parent, Box2D, Settings, CollisionDetector) { Doll.prototype.stop = function () { this.moveDirection = 0; this.setFriction(Settings.PLAYER_FRICTION); + if(this.isStanding()) this.setActionState("stand"); } Doll.prototype.jump = function () { @@ -157,12 +159,13 @@ function (Parent, Box2D, Settings, CollisionDetector) { } Doll.prototype.destroy = function () { - this.body.GetWorld().DestroyBody(this.body); + Parent.prototype.destroy.call(this); } Doll.prototype.setStanding = function (isStanding) { + if (this.standing == isStanding) return; this.standing = isStanding; - this.setActionState("stand"); + if(isStanding) this.setActionState("stand"); } Doll.prototype.isStanding = function () { @@ -170,33 +173,30 @@ function (Parent, Box2D, Settings, CollisionDetector) { } Doll.prototype.lookAt = function(x, y) { - var lastLookDirection = this.lookDirection; - /* - var degree = Math.atan2(Settings.STAGE_WIDTH / 2 - x, Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180); - if (x < Settings.STAGE_WIDTH / 2) { - this.mc.scaleX = -1; - this.lookDirection = -1; - degree = (-45 + degree / 2); - this.mc.head.rotation = degree; - } else if (x >= Settings.STAGE_WIDTH / 2) { - this.mc.scaleX = 1; - this.lookDirection = 1; - degree = (45 + -degree / 2) - 90; - this.mc.head.rotation = degree; - } - */ - + this.body.SetAwake(true); if(x < 0) { this.lookDirection = -1; } else { this.lookDirection = 1; } + + this.lookAtXY.x = x; + this.lookAtXY.y = y; }; Doll.prototype.onFootSensorDetection = function(isColliding) { - if(isColliding && !(this.body.GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding())) { + //if(isColliding && !(this.body.GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding())) { + // this.setStanding(true); + //} + + + var hasJumpStartVelocity = this.body.GetLinearVelocity().y < -Settings.JUMP_SPEED; + + if(isColliding && !hasJumpStartVelocity) { this.setStanding(true); } + + }; Doll.prototype.update = function() { diff --git a/app/Game/Core/Player.js b/app/Game/Core/Player.js index 6183180..9fc4a3d 100755 --- a/app/Game/Core/Player.js +++ b/app/Game/Core/Player.js @@ -19,7 +19,7 @@ function (Doll, Settings) { }; Player.prototype.spawn = function (x, y) { - this.doll = new Doll(this.physicsEngine, this.id); + this.doll = new Doll(this.physicsEngine, "doll-" + this.id); this.doll.spawn(x, y); this.isSpawned = true; } diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index e77f3ac..4fbe5c6 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -5,10 +5,12 @@ define([ "Game/Server/Control/PlayerController", "Lib/Utilities/RequestAnimFrame", "Game/Core/NotificationCenter", - "Game/Server/Player" + "Game/Server/Player", + "Game/Server/GameObjects/GameObject", + "Game/Server/Physics/Doll" ], -function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, NotificationCenter, Player) { +function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, NotificationCenter, Player, GameObject, Doll) { function GameController (channel) { Parent.call(this, new PhysicsEngine()); @@ -74,17 +76,28 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N var body = this.physicsEngine.world.GetBodyList(); do { - var userData = body.GetUserData(); + if(body.IsAwake()) { + var userData = body.GetUserData(); - if(userData && body.IsAwake()) { - update[userData] = { - p: body.GetPosition(), - a: body.GetAngle(), - lv: body.GetLinearVelocity(), - av: body.GetAngularVelocity() - }; - isUpdateNeeded = true; + if (userData instanceof GameObject) { + var gameObject = userData; + + update[gameObject.uid] = { + p: body.GetPosition(), + a: body.GetAngle(), + lv: body.GetLinearVelocity(), + av: body.GetAngularVelocity() + }; + + if(gameObject instanceof Doll) { + update[gameObject.uid].as = gameObject.getActionState(); + update[gameObject.uid].laxy = gameObject.lookAtXY; + } + + isUpdateNeeded = true; + } } + } while (body = body.GetNext()); if(isUpdateNeeded) { diff --git a/app/Game/Server/GameObject.js b/app/Game/Server/GameObject.js deleted file mode 100644 index 78655df..0000000 --- a/app/Game/Server/GameObject.js +++ /dev/null @@ -1,9 +0,0 @@ -define([ - "Game/Core/GameObject" -], - -function(Parent) { - - return Parent; - -}); \ No newline at end of file diff --git a/static/html/index.html b/static/html/index.html index d06ed64..264e15f 100755 --- a/static/html/index.html +++ b/static/html/index.html @@ -1,6 +1,7 @@
+