From d51c705c1cb8777a077a7f54750a6d6abdfc0e92 Mon Sep 17 00:00:00 2001 From: jeena Date: Mon, 23 Dec 2013 02:39:05 +0100 Subject: [PATCH] implemented gameobject for tiles and doll --- .gitignore | 1 + app/Game/Client/GameController.js | 7 +- app/Game/Client/GameObjects/GameObject.js | 10 +- app/Game/Client/GameObjects/Tile.js | 43 +++--- app/Game/Client/Physics/Doll.js | 49 +++++- app/Game/Client/View/Views/AbstractView.js | 21 ++- app/Game/Client/View/Views/PixiView.js | 49 ------ app/Game/Config/Settings.js | 2 +- app/Game/Core/Collision/Detector.js | 23 ++- app/Game/Core/GameObject.js | 29 ---- app/Game/Core/GameObjects/Tile.js | 6 +- app/Game/Core/Loader/Level.js | 6 +- app/Game/Core/Physics/Doll.js | 136 ++++++++++++----- app/Game/Core/Physics/Engine.js | 1 - app/Game/Core/Player.js | 164 ++------------------- 15 files changed, 221 insertions(+), 326 deletions(-) delete mode 100644 app/Game/Core/GameObject.js diff --git a/.gitignore b/.gitignore index 3c45938..f98b544 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ *.log .DS_Store +pixi.js diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index 42feeb7..510742a 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -127,7 +127,6 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat } GameController.prototype.onJoinMe = function (playerId) { - //this.onSpawnPlayer(options); this.me = this.players[playerId]; this.me.setPlayerController(new PlayerController(this.me)); this.view.setMe(this.me); @@ -140,15 +139,11 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat var player = this.players[playerId]; player.spawn(x, y); - - // add to view controller - this.view.addPlayer(player); + this.gameObjects.animated.push(player.getDoll()); } GameController.prototype.loadLevel = function (path) { Parent.prototype.loadLevel.call(this, path); - var tiles = this.level.levelObject.tiles; - this.view.loadMeshes(tiles); } return GameController; diff --git a/app/Game/Client/GameObjects/GameObject.js b/app/Game/Client/GameObjects/GameObject.js index 116139c..b8645a0 100644 --- a/app/Game/Client/GameObjects/GameObject.js +++ b/app/Game/Client/GameObjects/GameObject.js @@ -1,11 +1,11 @@ define([ - "Game/Core/GameObjects/GameObject" + "Game/Core/GameObjects/GameObject", + "Lib/Utilities/Exception" ], -function(Parent) { +function (Parent, Exception) { - function GameObject(physicsEngine, view) { - this.view = view; + function GameObject(physicsEngine) { Parent.call(this, physicsEngine); this.createMesh(); this.render(); @@ -23,7 +23,7 @@ function(Parent) { } GameObject.prototype.createMesh = function() { - throw new Exception('Abstract method GameObject.getMesh not overwritten'); + throw new Exception('Abstract method GameObject.createMesh not overwritten'); }; return GameObject; diff --git a/app/Game/Client/GameObjects/Tile.js b/app/Game/Client/GameObjects/Tile.js index c85f3c8..21cdb4e 100644 --- a/app/Game/Client/GameObjects/Tile.js +++ b/app/Game/Client/GameObjects/Tile.js @@ -1,12 +1,13 @@ define([ "Game/Core/GameObjects/Tile", - "Game/Config/Settings" + "Game/Config/Settings", + "Game/Core/NotificationCenter" ], -function (Parent, Settings) { +function (Parent, Settings, NotificationCenter) { - function Tile(physicsEngine, view, options) { - Parent.call(this, physicsEngine, view, options); + function Tile(physicsEngine, options) { + Parent.call(this, physicsEngine, options); } Tile.prototype = Object.create(Parent.prototype); @@ -19,28 +20,32 @@ function (Parent, Settings) { + Settings.GRAPHICS_SUBPATH_TILES + material + '/' + this.options.s + '' - + this.options.r + '.gif'; + + (this.options.r || 0) + '.gif'; var callback = function(mesh) { self.mesh = mesh; - self.view.addMesh(mesh); + NotificationCenter.trigger("view/addMesh", mesh); } - - this.view.createMesh( - Settings.TILE_SIZE, - Settings.TILE_SIZE, - 0, - 0, - imgPath, - callback - ); + + NotificationCenter.trigger("view/createMesh", + Settings.TILE_SIZE, + Settings.TILE_SIZE, + 0, + 0, + imgPath, + callback + ); }; Tile.prototype.render = function() { - this.view.updateMesh(this.mesh, { - x: this.options.x * Settings.TILE_SIZE, - y: this.options.y * Settings.TILE_SIZE, - }) + + NotificationCenter.trigger("view/updateMesh", + this.mesh, + { + x: this.options.x * Settings.TILE_SIZE, + y: this.options.y * Settings.TILE_SIZE, + } + ); } return Tile; diff --git a/app/Game/Client/Physics/Doll.js b/app/Game/Client/Physics/Doll.js index 56624e6..305ba84 100644 --- a/app/Game/Client/Physics/Doll.js +++ b/app/Game/Client/Physics/Doll.js @@ -1,9 +1,52 @@ define([ - "Game/Core/Physics/Doll" + "Game/Core/Physics/Doll", + "Game/Config/Settings", + "Game/Core/NotificationCenter" ], -function(Parent) { +function (Parent, Settings, NotificationCenter) { - return Parent; + function Doll(physicsEngine, playerId) { + Parent.call(this, physicsEngine, playerId); + this.height = 36; + } + + Doll.prototype = Object.create(Parent.prototype); + + Doll.prototype.createMesh = function() { + var self = this; + + var imgPath = Settings.GRAPHICS_PATH + + Settings.GRAPHICS_SUBPATH_CHARACTERS + + 'Chuck' + '/' + + 'chuck.png'; + + var callback = function(mesh) { + self.mesh = mesh; + NotificationCenter.trigger("view/addMesh", mesh); + } + + NotificationCenter.trigger("view/createMesh", + 10, + 36, + 0, + 0, + imgPath, + callback + ); + }; + + Doll.prototype.render = function() { + + NotificationCenter.trigger("view/updateMesh", + this.mesh, + { + x: this.body.GetPosition().x * Settings.RATIO + 4, + y: (this.body.GetPosition().y * Settings.RATIO) - 29 + } + ); + } + + return Doll; }); \ No newline at end of file diff --git a/app/Game/Client/View/Views/AbstractView.js b/app/Game/Client/View/Views/AbstractView.js index 2287659..9feed2e 100644 --- a/app/Game/Client/View/Views/AbstractView.js +++ b/app/Game/Client/View/Views/AbstractView.js @@ -1,14 +1,19 @@ define([ "Game/Client/View/DomController", "Game/Config/Settings", - "Lib/Utilities/Exception" + "Lib/Utilities/Exception", + "Game/Core/NotificationCenter" ], -function (DomController, Settings, Exception) { +function (DomController, Settings, Exception, NotificationCenter) { function AbstractView () { this.me = null; this.canvas = null; + + NotificationCenter.on("view/createMesh", this.createMesh, this); + NotificationCenter.on("view/addMesh", this.addMesh, this); + NotificationCenter.on("view/updateMesh", this.updateMesh, this); } AbstractView.prototype.isWebGlEnabled = function () { @@ -29,10 +34,6 @@ function (DomController, Settings, Exception) { } } - AbstractView.prototype.addMesh = function(mesh) { - throw new Exception('Abstract Function addMesh not overwritten '); - }; - AbstractView.prototype.loadPlayerMesh = function(player) { throw new Exception('Abstract Function loadPlayerMesh not overwritten '); }; @@ -49,6 +50,14 @@ function (DomController, Settings, Exception) { throw new Exception('Abstract Function createMesh not overwritten '); } + AbstractView.prototype.addMesh = function(mesh) { + throw new Exception('Abstract Function addMesh not overwritten '); + }; + + AbstractView.prototype.updateMesh = function(mesh, options) { + throw new Exception('Abstract Function updateMesh 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 60e398a..df80760 100755 --- a/app/Game/Client/View/Views/PixiView.js +++ b/app/Game/Client/View/Views/PixiView.js @@ -35,42 +35,12 @@ function (Parent, DomController, PIXI, Settings) { this.setCanvas(this.renderer.view); } - PixiView.prototype.loadMeshes = function(objects) { - var self = this; - for (var i = 0; i < objects.length; i++) { - (function() { - var o = objects[i]; - var x = o.x * Settings.TILE_SIZE; - var y = o.y * Settings.TILE_SIZE; - var r = o.r ? o.r : 0; - var rad = 0.5 * Math.PI * -r; - - var material = self.tileAtPositionExists(objects, o.x, o.y -1) ? "Soil" : "GrassSoil"; - var callback = function(mesh) { - self.addMesh(mesh); - //console.log("img height:", mesh.material.map.image.height); - //mesh.rotation.z = rad; - }; - self.createMesh(Settings.TILE_SIZE, Settings.TILE_SIZE, x, y, 'static/img/Tiles/' + material + '/' + o.s + '' + o.r + '.gif', callback, true); - })(); - }; - }; - PixiView.prototype.render = function () { if(this.me) { var pos = this.calculateCameraPosition(); this.setCameraPosition(pos.x, pos.y); } - /* - for (var i = 0; i < this.movableObjects.length; i++) { - var obj = this.movableObjects[i]; - var pos = obj.player.getPosition(); - obj.mesh.position.x = pos.x * Settings.RATIO + 4 ; - obj.mesh.position.y = pos.y * Settings.RATIO - 34; // weirdly a different magic number as for three - } - */ - this.renderer.render(this.stage); } @@ -101,25 +71,6 @@ function (Parent, DomController, PIXI, Settings) { if (options.height) mesh.height = options.height; }; - PixiView.prototype.addPlayer = function(player) { - var self = this; - var mesh = null; - var pos = player.getPosition(); - var size = {w:10, h:42}; - var callback = function(mesh) { - self.addMesh(mesh); - self.movableObjects.push({ - player: player, - mesh: mesh - }); - } - this.createMesh(size.w, size.h, pos.x, pos.y, "static/img/Characters/Chuck/chuck.png", callback, false); - }; - - PixiView.prototype.removPlayer = function(player) { - // nothing - }; - PixiView.prototype.initCamera = function () { this.container = new PIXI.DisplayObjectContainer(); this.stage.addChild(this.container); diff --git a/app/Game/Config/Settings.js b/app/Game/Config/Settings.js index ac087a7..f8c89bc 100755 --- a/app/Game/Config/Settings.js +++ b/app/Game/Config/Settings.js @@ -47,7 +47,7 @@ define({ IS_BROWSER_ENVIRONMENT: typeof window !== 'undefined', USE_WEGBL: true, - DEBUG_MODE: 0, + DEBUG_MODE: 1, // NETWORKING WORLD_UPDATE_BROADCAST_INTERVAL: 70, diff --git a/app/Game/Core/Collision/Detector.js b/app/Game/Core/Collision/Detector.js index d867da4..baa4377 100755 --- a/app/Game/Core/Collision/Detector.js +++ b/app/Game/Core/Collision/Detector.js @@ -14,38 +14,35 @@ function (Box2D) { Detector.IDENTIFIER = { TILE: "tile", - PLAYER: "player", - PLAYER_HEAD: 'head', - PLAYER_CHEST: 'chest', - PLAYER_LEGS: 'legs', - PLAYER_FOOT_SENSOR: 'footsensor' + PLAYER: "player" } Detector.prototype.getListener = function () { return this.listener; } - Detector.prototype.handleStand = function (point, isColliding) { + Detector.prototype.onCollisionChange = function (point, isColliding) { + var userDataA = point.GetFixtureA().GetUserData(); + var userDataB = point.GetFixtureB().GetUserData(); - if (point.GetFixtureA().GetUserData().identifier == Detector.IDENTIFIER.PLAYER_FOOT_SENSOR) { - point.GetFixtureA().GetUserData().player.onFootSensorDetection(isColliding); - } else if (point.GetFixtureB().GetUserData().identifier == Detector.IDENTIFIER.PLAYER_FOOT_SENSOR) { - point.GetFixtureB().GetUserData().player.onFootSensorDetection(isColliding); + if (userDataA && userDataA.onCollisionChange) { + userDataA.onCollisionChange(isColliding); + } else if (userDataB && userDataB.onCollisionChange) { + userDataB.onCollisionChange(isColliding); } } /** Extension **/ Detector.prototype.BeginContact = function (point) { - this.chuckDetector.handleStand(point, true); + this.chuckDetector.onCollisionChange(point, true); } Detector.prototype.PostSolve = function (point, impulse) { - //this.chuckDetector.handleStand(point, true); } Detector.prototype.EndContact = function (point) { - this.chuckDetector.handleStand(point, false); + this.chuckDetector.onCollisionChange(point, false); } return Detector; diff --git a/app/Game/Core/GameObject.js b/app/Game/Core/GameObject.js deleted file mode 100644 index 9fd8f49..0000000 --- a/app/Game/Core/GameObject.js +++ /dev/null @@ -1,29 +0,0 @@ -define([ - "Lib/Vendor/Box2D", - "Lib/Utilities/Exception" -], - -function (Box2D, Exception) { - - function GameObject(physicsEngine) { - var def = this.getBodyDef(); - this.body = physicsEngine.getWorld().CreateBody(def); - } - - GameObject.prototype.getBodyDef = function() { - throw new Exception('Abstract method GameObject.getBodyDef not overwritten'); - }; - - GameObject.prototype.destroy = function() { - if(this.body instanceof Box2D.Dynamics.b2Body) { - this.body.GetWorld().DestroyBody(this.body); - } - }; - - GameObject.prototype.getBody = function() { - return this.body; - }; - - return GameObject; - -}); \ No newline at end of file diff --git a/app/Game/Core/GameObjects/Tile.js b/app/Game/Core/GameObjects/Tile.js index 9b259cd..fb7ad10 100644 --- a/app/Game/Core/GameObjects/Tile.js +++ b/app/Game/Core/GameObjects/Tile.js @@ -21,17 +21,14 @@ function (Parent, Box2D, Settings, CollisionDetector) { bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody; bodyDef.position.x = this.options.x * Settings.TILE_SIZE / Settings.RATIO; bodyDef.position.y = this.options.y * Settings.TILE_SIZE / Settings.RATIO; - bodyDef.angle = this.options.r * 90 * Math.PI / 180; + bodyDef.angle = (this.options.r || 0) * 90 * Math.PI / 180; return bodyDef; } Tile.prototype.createPhysicTile = function (tile) { - tile.r = tile.r || 0; var vertices = this.createVertices(tile); - var tileShape = new Box2D.Collision.Shapes.b2PolygonShape(); - tileShape.SetAsArray(vertices, vertices.length); var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); @@ -41,7 +38,6 @@ function (Parent, Box2D, Settings, CollisionDetector) { 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 aa46f5b..9363cb8 100755 --- a/app/Game/Core/Loader/Level.js +++ b/app/Game/Core/Loader/Level.js @@ -7,12 +7,11 @@ define([ ], function (Settings, Box2D, CollisionDetector, Tile) { // Public - function Level (path, engine, gameObjects, view) { + function Level (path, engine, gameObjects) { this.path = path; this.engine = engine; this.levelObject = null; this.gameObjects = gameObjects; - this.view = view; } Level.prototype.loadLevelInToEngine = function () { @@ -33,7 +32,8 @@ define([ } var tiles = this.levelObject.tiles; - for (var i = tiles.length - 1; i >= 0; i--) { + + for (var i = 0; i < tiles.length; i++) { this.gameObjects.fixed.push(new Tile(this.engine, tiles[i])); } } diff --git a/app/Game/Core/Physics/Doll.js b/app/Game/Core/Physics/Doll.js index d9b0114..b5512b4 100755 --- a/app/Game/Core/Physics/Doll.js +++ b/app/Game/Core/Physics/Doll.js @@ -1,32 +1,42 @@ define([ + "Game/" + GLOBALS.context + "/GameObjects/GameObject", "Lib/Vendor/Box2D", "Game/Config/Settings", "Game/" + GLOBALS.context + "/Collision/Detector" ], -function (Box2D, Settings, CollisionDetector) { +function (Parent, Box2D, Settings, CollisionDetector) { - function Doll (physicsEngine, player) { - this.player = player; - this.physicsEngine = physicsEngine; - this.body; + function Doll (physicsEngine, playerId) { + + Parent.call(this, physicsEngine); + + this.playerId = playerId; + + this.standing = false; + this.moveDirection = 0; + this.lookDirection = 0; this.legs; - this.contactPoint; - this.init(this.physicsEngine.getWorld()); + this.createFixtures(); + this.body.SetActive(false); } - Doll.prototype.init = function (world) { + Doll.prototype = Object.create(Parent.prototype); + Doll.prototype.getBodyDef = function() { var bodyDef = new Box2D.Dynamics.b2BodyDef(); bodyDef.position.x = 220 / Settings.RATIO; bodyDef.position.y = 0 / Settings.RATIO; bodyDef.fixedRotation = true; bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING; bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; - bodyDef.userData = CollisionDetector.IDENTIFIER.PLAYER + '-' + this.player.id; + bodyDef.userData = CollisionDetector.IDENTIFIER.PLAYER + '-' + this.playerId; - this.body = world.CreateBody(bodyDef); + return bodyDef; + }; + + Doll.prototype.createFixtures = function () { var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); fixtureDef.density = Settings.PLAYER_DENSITY; @@ -38,14 +48,12 @@ function (Box2D, Settings, CollisionDetector) { headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO)); fixtureDef.shape = headShape; fixtureDef.isSensor = false; - fixtureDef.userData = CollisionDetector.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 = CollisionDetector.IDENTIFIER.PLAYER_CHEST; this.body.CreateFixture(fixtureDef); var legsShape = new Box2D.Collision.Shapes.b2CircleShape(); @@ -54,7 +62,6 @@ function (Box2D, Settings, CollisionDetector) { fixtureDef.shape = legsShape; fixtureDef.friction = Settings.PLAYER_FRICTION; fixtureDef.isSensor = false; - fixtureDef.userData = CollisionDetector.IDENTIFIER.PLAYER_LEGS; this.legs = this.body.CreateFixture(fixtureDef); @@ -65,13 +72,10 @@ function (Box2D, Settings, CollisionDetector) { fixtureDef.isSensor = true; fixtureDef.userData = { - identifier: CollisionDetector.IDENTIFIER.PLAYER_FOOT_SENSOR, - player: this.player + onCollisionChange: this.onFootSensorDetection.bind(this) } this.body.CreateFixture(fixtureDef); - - this.body.SetActive(false); } Doll.prototype.spawn = function (x, y) { @@ -79,9 +83,9 @@ function (Box2D, Settings, CollisionDetector) { this.body.SetActive(true); } - Doll.prototype.getBody = function () { - return this.body; - } + Doll.prototype.getPosition = function() { + return this.body.GetPosition(); + }; Doll.prototype.setFriction = function (friction) { if(!friction) friction = -1; @@ -91,7 +95,25 @@ function (Box2D, Settings, CollisionDetector) { } } - Doll.prototype.move = function (direction, speed) { + Doll.prototype.move = function (direction) { + + this.moveDirection = direction; + var speed; + + switch(true) { + case direction == this.lookDirection && this.isStanding(): + speed = Settings.RUN_SPEED; + break; + + case !this.isStanding(): + speed = Settings.FLY_SPEED; + break; + + default: + speed = Settings.WALK_SPEED; + break; + } + this.setFriction(Settings.PLAYER_MOTION_FRICTION); this.body.SetAwake(true); var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this.body.GetLinearVelocity().y); @@ -99,29 +121,73 @@ function (Box2D, Settings, CollisionDetector) { } Doll.prototype.stop = function () { + this.moveDirection = 0; this.setFriction(Settings.PLAYER_FRICTION); } Doll.prototype.jump = function () { - this.body.SetAwake(true); - - var vector = new Box2D.Common.Math.b2Vec2(0, -Settings.JUMP_SPEED); - this.body.ApplyImpulse(vector, this.body.GetPosition()); - - // maybe change to a constant force instead of applying of force? - // to prevent higher jumping running uphill, etc. + if (this.isStanding()) { + + this.body.SetAwake(true); + var vector = new Box2D.Common.Math.b2Vec2(0, -Settings.JUMP_SPEED); + this.body.ApplyImpulse(vector, this.body.GetPosition()); + + this.setStanding(false); + } } - /* - Doll.prototype.jumping = function () { - var vector = new Box2D.Common.Math.b2Vec2(0, -0.05); - this.body.ApplyImpulse(vector, this.body.GetPosition()); - } - */ - Doll.prototype.destroy = function () { this.body.GetWorld().DestroyBody(this.body); } + Doll.prototype.setStanding = function (isStanding) { + this.standing = isStanding; + } + + Doll.prototype.isStanding = function () { + return this.standing; + } + + 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; + } + */ + + if(x < 0) { + this.lookDirection = -1; + } else { + this.lookDirection = 1; + } + }; + + Doll.prototype.onFootSensorDetection = function(isColliding) { + if(isColliding && !(this.body.GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding())) { + this.setStanding(true); + } + }; + + Doll.prototype.update = function() { + + if (this.body.GetLinearVelocity().x == 0) { + this.stop(); + } + + if (!this.body.IsAwake() && !this.isStanding()) { + this.setStanding(true); + } + }; + return Doll; }); \ No newline at end of file diff --git a/app/Game/Core/Physics/Engine.js b/app/Game/Core/Physics/Engine.js index 3cf5344..d9a920f 100755 --- a/app/Game/Core/Physics/Engine.js +++ b/app/Game/Core/Physics/Engine.js @@ -13,7 +13,6 @@ function (Settings, Box2D, CollisionDetector) { ); this.ground = null; this.lastStep = Date.now(); - console.log(Settings.BOX2D_TIME_STEP) } Engine.prototype.getWorld = function () { diff --git a/app/Game/Core/Player.js b/app/Game/Core/Player.js index e16851e..c4eb723 100755 --- a/app/Game/Core/Player.js +++ b/app/Game/Core/Player.js @@ -8,185 +8,47 @@ function (Doll, Settings) { function Player (id, physicsEngine) { this.physicsEngine = physicsEngine; this.playerController = null; + this.doll; this.id = id; - this.standing = false; - this.doll; - this.currentAnimationState = 'stand'; - this.moveDirection = 0; - this.lookDirection = 0; this.isSpawned = false; - } + Player.prototype.getDoll = function() { + return this.doll; + }; + Player.prototype.spawn = function (x, y) { - this.doll = new Doll(this.physicsEngine, this); + this.doll = new Doll(this.physicsEngine, this.id); this.doll.spawn(x, y); this.isSpawned = true; } - Player.prototype.getDoll = function () { - return this.doll; - } - - Player.prototype.getBody = function () { - return this.doll.getBody(); - } - Player.prototype.getPosition = function () { - return this.getBody().GetPosition(); - } - - Player.prototype.setStanding = function (isStanding) { - var resetStates = ['jump', 'jumploop']; - if (resetStates.indexOf(this.currentAnimationState)>=0 && !this.standing && isStanding) { - this.animate('stand'); - } - this.standing = isStanding; - } - - Player.prototype.isStanding = function () { - return this.standing; + if(!this.doll) return false; + return this.doll.getPosition(); } Player.prototype.move = function (direction) { - this.moveDirection = direction; - - switch(true) { - case direction == this.lookDirection && this.isStanding(): - this.doll.move(direction, Settings.RUN_SPEED); - break; - - case !this.isStanding(): - this.doll.move(direction, Settings.FLY_SPEED); - break; - - default: - this.doll.move(direction, Settings.WALK_SPEED); - break; - } - - if (this.isStanding()) { - this.animate(this.calculateWalkAnimation()); - } + this.doll.move(direction); } Player.prototype.stop = function () { - this.moveDirection = 0; this.doll.stop(); - if (this.isWalking() || this.standing) { - this.animate('stand'); - } } Player.prototype.jump = function () { - if (this.isStanding()) { - this.doll.jump(); - this.animate('jump'); - this.setStanding(false); - } - } - - Player.prototype.jumping = function () { - if (!this.isStanding()) { - this.doll.jumping(); - } - } - - Player.prototype.duck = function () { - if (this.standing && !this.isWalking()) { - this.animate('duck'); - } - } - - Player.prototype.standUp = function () { - if (this.standing) { - this.animate('standup'); - } - } - - Player.prototype.animate = function (type) { - if (type == this.currentAnimationState) { - return; - } - - this.currentAnimationState = type; - } - - Player.prototype.calculateWalkAnimation = function () { - // FIXME dont know if this is right - if (this.moveDirection == (this.lookDirection > 0)) { - return 'run'; - } - return 'walkback'; + this.doll.jump(); } Player.prototype.lookAt = function (x, y) { - //console.log("player looking at ", 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; - } - */ - - if(x < 0) { - this.lookDirection = -1; - } else { - this.lookDirection = 1; - } - - if (this.lookDirection != lastLookDirection && this.isWalking()) { - this.animate(this.calculateWalkAnimation()); - } - } - - Player.prototype.isWalking = function () { - var states = ['walk', 'walkback', 'run']; - - if (states.indexOf(this.currentAnimationState) >= 0) { - return true; - } - return false; - } - - // called by CollisionDetection - Player.prototype.onFootSensorDetection = function (isColliding) { - if(isColliding) { - if(this.doll.getBody().GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding()) { - return; - } - this.setStanding(true); - } else { - // TODO This needs some more thought to it. - // maybe take a look at collision groups for collision detection, - // to group all tiles together - - //this.setStanding(false); - //this.animate('jumploop'); - } + if(this.doll) this.doll.lookAt(x, y); } Player.prototype.update = function () { - //this.mc.head.y = this.mc.head_posmask.y; - if (this.doll.getBody().GetLinearVelocity().x == 0 && this.isWalking()) { - this.stop(); - } - - if (!this.doll.getBody().IsAwake() && !this.isStanding()) { - this.setStanding(true); + if(this.doll) { + this.doll.update(); } if(this.playerController) {