From dfa71bc8e5d75d67ef9fdb856d635a64dcfc5e47 Mon Sep 17 00:00:00 2001 From: Jeena Date: Sun, 15 Mar 2015 16:51:38 +0100 Subject: [PATCH] added ASSERT, fixed #103 --- app/Game/Channel/Control/PlayerController.js | 17 +++-- app/Game/Channel/GameObjects/Doll.js | 19 +++-- app/Game/Channel/GameObjects/Item.js | 11 ++- app/Game/Channel/Player.js | 7 +- .../Client/Control/Inputs/KeyboardAndMouse.js | 9 ++- app/Game/Client/GameController.js | 49 +++++++----- app/Game/Client/Me.js | 25 +++--- app/Game/Config/ItemSettings.js | 2 + app/Game/Core/GameObjects/Doll.js | 76 +++++++++++-------- app/Game/Core/GameObjects/Item.js | 57 ++++++++------ app/Game/Core/GameObjects/Items/RagDoll.js | 40 +++++++--- app/Game/Core/GameObjects/Items/Rube.js | 8 +- app/Game/Core/GameObjects/Items/Skateboard.js | 11 ++- app/Game/Core/GameObjects/Tile.js | 22 +++--- app/Game/Core/Loader/Level.js | 2 + app/Game/Core/Loader/TiledLevel.js | 26 +++---- app/Lib/Utilities/Assert.js | 21 +++++ 17 files changed, 249 insertions(+), 153 deletions(-) create mode 100644 app/Lib/Utilities/Assert.js diff --git a/app/Game/Channel/Control/PlayerController.js b/app/Game/Channel/Control/PlayerController.js index dd379db..807ebfd 100755 --- a/app/Game/Channel/Control/PlayerController.js +++ b/app/Game/Channel/Control/PlayerController.js @@ -6,6 +6,8 @@ define([ ], function(Parent, Nc, Parser, Settings) { + + "use strict"; function PlayerController(player) { @@ -33,6 +35,9 @@ function(Parent, Nc, Parser, Settings) { }; PlayerController.prototype.handActionRequest = function(options) { + options.x = parseFloat(options.x) || 0; + options.y = parseFloat(options.y) || 0; + options.av = parseFloat(options.av) || 0; if (options) this.player.handActionRequest(options); }; @@ -43,21 +48,21 @@ function(Parent, Nc, Parser, Settings) { PlayerController.prototype.mePositionStateUpdate = function(update) { if(!this.player.doll) { - console.warn('me state update, even though doll does not exist'); + console.warn("me state update, even though doll does not exist"); return; } var difference = { x: Math.abs(update.p.x - this.player.doll.body.GetPosition().x), y: Math.abs(update.p.y - this.player.doll.body.GetPosition().y) - } + }; - if(true || difference.x < Settings.PUNKBUSTER_DIFFERENCE_METERS - && difference.y < Settings.PUNKBUSTER_DIFFERENCE_METERS) { + if(true || difference.x < Settings.PUNKBUSTER_DIFFERENCE_METERS && + difference.y < Settings.PUNKBUSTER_DIFFERENCE_METERS) { this.player.doll.updatePositionState(update); } else { // HARD UPDATE FOR SELF - console.log(this.player.user.options.nickname + ' is cheating.') + console.log(this.player.user.options.nickname + " is cheating."); var body = this.player.doll.body; @@ -66,7 +71,7 @@ function(Parent, Nc, Parser, Settings) { lv: body.GetLinearVelocity() }; - Nc.trigger(Nc.ns.channel.to.client.user.gameCommand.send + this.player.id, 'positionStateReset', options); + Nc.trigger(Nc.ns.channel.to.client.user.gameCommand.send + this.player.id, "positionStateReset", options); } }; diff --git a/app/Game/Channel/GameObjects/Doll.js b/app/Game/Channel/GameObjects/Doll.js index 26461f6..390ab9c 100755 --- a/app/Game/Channel/GameObjects/Doll.js +++ b/app/Game/Channel/GameObjects/Doll.js @@ -2,10 +2,11 @@ define([ "Game/Core/GameObjects/Doll", "Game/Channel/GameObjects/Item", "Lib/Vendor/Box2D", - "Lib/Utilities/NotificationCenter" + "Lib/Utilities/NotificationCenter", + "Lib/Utilities/Assert" ], -function (Parent, Item, Box2D, Nc) { +function (Parent, Item, Box2D, Nc, Assert) { "use strict"; @@ -15,7 +16,7 @@ function (Parent, Item, Box2D, Nc) { Doll.prototype = Object.create(Parent.prototype); - Doll.prototype.findCloseItem = function(x, y) { + Doll.prototype.findCloseItem = function(x) { var self = this; @@ -33,7 +34,7 @@ function (Parent, Item, Box2D, Nc) { } else { return findItem(this.reachableItems.right); } - } + }; Doll.prototype.onImpact = function(isColliding, fixture) { var self = this; @@ -52,7 +53,7 @@ function (Parent, Item, Box2D, Nc) { var b2Math = Box2D.Common.Math.b2Math; - var absItemVelocity = b2Math.AbsV(itemVelocity) + var absItemVelocity = b2Math.AbsV(itemVelocity); var max = 1; if(absItemVelocity.x > max || absItemVelocity.y > max) { @@ -71,9 +72,9 @@ function (Parent, Item, Box2D, Nc) { var callback = function() { self.player.addDamage(damage, player, item); - } + }; - Nc.trigger(Nc.ns.channel.engine.worldQueue.add, callback) + Nc.trigger(Nc.ns.channel.engine.worldQueue.add, callback); } } @@ -81,10 +82,12 @@ function (Parent, Item, Box2D, Nc) { } } } - } + }; Doll.prototype.updatePositionState = function(update) { if(!this.isAnotherPlayerNearby()) { + Assert.number(update.p.x, update.p.y); + Assert.number(update.lv.x, update.lv.y); this.body.SetAwake(true); this.body.SetPosition(update.p); this.body.SetLinearVelocity(update.lv); diff --git a/app/Game/Channel/GameObjects/Item.js b/app/Game/Channel/GameObjects/Item.js index 0102d49..ce30779 100755 --- a/app/Game/Channel/GameObjects/Item.js +++ b/app/Game/Channel/GameObjects/Item.js @@ -21,14 +21,14 @@ function (Parent) { this.lastMoved = { player: player, timestamp: new Date() - } + }; } else { this.lastMoved = null; } }; - Item.prototype.isGrabbingAllowed = function(player) { - return this.heldByPlayers.length == 0; + Item.prototype.isGrabbingAllowed = function(player) { // jshint unused:false + return this.heldByPlayers.length === 0; }; Item.prototype.beingGrabbed = function(player) { @@ -40,7 +40,7 @@ function (Parent) { } }; - Item.prototype.isReleasingAllowed = function(player) { + Item.prototype.isReleasingAllowed = function(player) { // jshint unused:false return true; }; @@ -81,8 +81,7 @@ function (Parent) { } } } - } - + }; return Item; diff --git a/app/Game/Channel/Player.js b/app/Game/Channel/Player.js index d7d09cf..06c8b56 100755 --- a/app/Game/Channel/Player.js +++ b/app/Game/Channel/Player.js @@ -22,13 +22,13 @@ function (Parent, Nc) { if (isHolding) { item = this.holdingItem; } else { - item = this.doll.findCloseItem(options.x, options.y); + item = this.doll.findCloseItem(options.x); } if(item) { this.handAction(options, isHolding, item); } - } + }; Player.prototype.handAction = function(options, isHolding, item) { @@ -110,8 +110,7 @@ function (Parent, Nc) { stats: this.stats }); }; - - + return Player; diff --git a/app/Game/Client/Control/Inputs/KeyboardAndMouse.js b/app/Game/Client/Control/Inputs/KeyboardAndMouse.js index 515f288..998e94f 100644 --- a/app/Game/Client/Control/Inputs/KeyboardAndMouse.js +++ b/app/Game/Client/Control/Inputs/KeyboardAndMouse.js @@ -7,6 +7,8 @@ define([ ], function (Parent, KeyboardInput, DomController, Settings, Swiper) { + + "use strict"; function KeyboardAndMouse(playerController) { Parent.call(this); @@ -42,7 +44,7 @@ function (Parent, KeyboardInput, DomController, Settings, Swiper) { minus: 189, minusfx: 173, zero: 48 - } + }; this.playerController = playerController; this.keyboardInit(); @@ -87,7 +89,7 @@ function (Parent, KeyboardInput, DomController, Settings, Swiper) { this.lastLookDirection = -1; this.onXyChange(this.lastLookDirection * Settings.VIEWPORT_LOOK_AHEAD, 0); } - this.playerController.moveLeft() + this.playerController.moveLeft(); }; KeyboardAndMouse.prototype.moveRight = function() { @@ -129,7 +131,8 @@ function (Parent, KeyboardInput, DomController, Settings, Swiper) { if(!self.playerController.player.isHoldingSomething()) { var options = { x: self.x, - y: self.y + y: self.y, + av: 0 }; self.playerController.handActionRequest(options); } else { diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index c239aa0..c8feefe 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -13,10 +13,11 @@ define([ "Lib/Utilities/Protocol/Helper", "Game/Client/Me", "Game/Client/AudioPlayer", - "Game/Client/PointerLockManager" + "Game/Client/PointerLockManager", + "Lib/Utilities/Assert" ], -function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, requestAnimFrame, Settings, GameObject, Doll, DomController, ProtocolHelper, Me, AudioPlayer, PointerLockManager) { +function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, requestAnimFrame, Settings, GameObject, Doll, DomController, ProtocolHelper, Me, AudioPlayer, PointerLockManager, Assert) { "use strict"; @@ -39,7 +40,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque GameController.prototype.getMe = function () { return this.me; - } + }; GameController.prototype.update = function () { @@ -59,7 +60,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque this.view.render(); DomController.fpsStep(); - } + }; GameController.prototype.mePositionStateUpdate = function() { if(this.me.isPositionStateUpdateNeeded()) { @@ -68,27 +69,28 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque }; GameController.prototype.onClientReadyResponse = function(options) { - + var i; + if (options.worldUpdate) { this.onWorldUpdate(options.worldUpdate); } if (options.runtimeItems) { - for (var i = 0; i < options.runtimeItems.length; i++) { + for (i = 0; i < options.runtimeItems.length; i++) { var itemDef = options.runtimeItems[i]; var alreadyExists = false; - for (var i = 0; i < this.gameObjects.animated.length; i++) { - if(this.gameObjects.animated[i].uid == itemDef.uid) { + for (var j = 0; j < this.gameObjects.animated.length; j++) { + if(this.gameObjects.animated[j].uid == itemDef.uid) { alreadyExists = true; break; } - }; + } if(!alreadyExists) { - var item = this.level.createItem(itemDef.uid, itemDef.options); + this.level.createItem(itemDef.uid, itemDef.options); } - }; + } } this.setMe(); @@ -96,7 +98,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque this.clientIsReady = true; // needs to stay before onSpawnPlayer if (options.spawnedPlayers) { - for(var i = 0; i < options.spawnedPlayers.length; i++) { + for(i = 0; i < options.spawnedPlayers.length; i++) { this.onSpawnPlayer(options.spawnedPlayers[i]); } } @@ -126,6 +128,11 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque gameObject.lookAt(update.laxy.x, update.laxy.y); } + Assert.number(update.p.x, update.p.y); + Assert.number(update.a); + Assert.number(update.lv.x, update.lv.y); + Assert.number(update.av); + body.SetAwake(true); body.SetPosition(update.p); body.SetAngle(update.a); @@ -133,10 +140,10 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque body.SetAngularVelocity(update.av); } } - + } while (body = body.GetNext()); - } + }; GameController.prototype.createMe = function(user) { this.me = new Me(user.id, this.physicsEngine, user); @@ -146,7 +153,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque GameController.prototype.setMe = function() { this.me.setPlayerController(new PlayerController(this.me)); this.view.setMe(this.me); - } + }; GameController.prototype.onGameCommand = function(message) { ProtocolHelper.applyCommand(message, this); @@ -172,7 +179,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque playerId: playerId }); } - } + }; GameController.prototype.onHandActionResponse = function(options) { var player = this.players[options.playerId]; @@ -184,7 +191,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque item = currentItem; break; } - }; + } if(item) { if(options.action == "throw") { @@ -193,7 +200,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque player.grab(item); } } else { - console.warn("Item for joint can not be found locally. " + options.itemUid) + console.warn("Item for joint can not be found locally. " + options.itemUid); } }; @@ -243,18 +250,18 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque GameController.prototype.loadLevel = function (path) { Parent.prototype.loadLevel.call(this, path); - } + }; GameController.prototype.onLevelLoaded = function () { PointerLockManager.update(null, {start:true}); - } + }; GameController.prototype.toggleGameStats = function(show) { var playersArray = []; for (var key in this.players) { playersArray.push(this.players[key]); - }; + } var sortedPlayers = playersArray.sort(function(a,b) { if(a.stats.score > b.stats.score) return -1; diff --git a/app/Game/Client/Me.js b/app/Game/Client/Me.js index 31436a9..047befd 100644 --- a/app/Game/Client/Me.js +++ b/app/Game/Client/Me.js @@ -1,10 +1,11 @@ define([ "Game/Client/Player", "Game/Config/Settings", - "Lib/Utilities/NotificationCenter" + "Lib/Utilities/NotificationCenter", + "Lib/Utilities/Assert" ], -function (Parent, Settings, Nc) { +function (Parent, Settings, Nc, Assert) { "use strict"; @@ -15,7 +16,7 @@ function (Parent, Settings, Nc) { this.lookAtXY = { x: Settings.VIEWPORT_LOOK_AHEAD, y: 0 - } + }; this.lastServerPositionState = { p: { @@ -34,7 +35,7 @@ function (Parent, Settings, Nc) { this.lookAtXY = { x: x, y: y - } + }; Parent.prototype.lookAt.call(this, x, y); }; @@ -63,10 +64,10 @@ function (Parent, Settings, Nc) { var difference = { x: Math.abs(this.lastServerPositionState.p.x - this.doll.body.GetPosition().x), y: Math.abs(this.lastServerPositionState.p.y - this.doll.body.GetPosition().y) - } + }; - if(difference.x > Settings.ME_STATE_MAX_DIFFERENCE_METERS - || difference.y > Settings.ME_STATE_MAX_DIFFERENCE_METERS) { + if(difference.x > Settings.ME_STATE_MAX_DIFFERENCE_METERS || + difference.y > Settings.ME_STATE_MAX_DIFFERENCE_METERS) { return true; } return false; @@ -76,7 +77,7 @@ function (Parent, Settings, Nc) { return { p: this.doll.body.GetPosition().Copy(), lv: this.doll.body.GetLinearVelocity().Copy() - } + }; }; Me.prototype.acceptPositionStateUpdateFromServer = function() { @@ -85,11 +86,13 @@ function (Parent, Settings, Nc) { }; Me.prototype.resetPositionState = function(options) { + Assert.number(options.p.x, options.p.y); + Assert.number(options.lv.x, options.lv.y); this.doll.body.SetPosition(options.p); this.doll.body.SetLinearVelocity(options.lv); }; - Me.prototype.createAndAddArrow = function(visible) { + Me.prototype.createAndAddArrow = function() { var self = this; var position = this.getPosition(); @@ -101,7 +104,7 @@ function (Parent, Settings, Nc) { var callback = function(arrowMesh) { self.arrowMesh = arrowMesh; - } + }; Nc.trigger(Nc.ns.client.view.playerArrow.createAndAdd, callback, options); }; @@ -113,7 +116,7 @@ function (Parent, Settings, Nc) { var options = { x: position.x * Settings.RATIO, y: position.y * Settings.RATIO, - } + }; Nc.trigger(Nc.ns.client.view.playerArrow.update, this.arrowMesh, options); }; diff --git a/app/Game/Config/ItemSettings.js b/app/Game/Config/ItemSettings.js index 5da7ff2..5706819 100644 --- a/app/Game/Config/ItemSettings.js +++ b/app/Game/Config/ItemSettings.js @@ -2,6 +2,8 @@ define([ ], function () { + + "use strict"; var ItemSettings = { diff --git a/app/Game/Core/GameObjects/Doll.js b/app/Game/Core/GameObjects/Doll.js index fdc356d..8090fb8 100755 --- a/app/Game/Core/GameObjects/Doll.js +++ b/app/Game/Core/GameObjects/Doll.js @@ -5,10 +5,11 @@ define([ "Game/Config/Settings", "Game/" + GLOBALS.context + "/Collision/Detector", "Game/" + GLOBALS.context + "/GameObjects/Item", - "Lib/Utilities/NotificationCenter" + "Lib/Utilities/NotificationCenter", + "Lib/Utilities/Assert" ], -function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { +function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc, Assert) { "use strict"; @@ -27,8 +28,8 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { this.standing = false; this.moveDirection = 0; this.lookDirection = 0; - this.legs; - this.footSensor; + this.legs = null; + this.footSensor = null; this.actionState = null; this.lookAtXY = { x:0, y:0 }; this.reachableItems = { @@ -59,6 +60,10 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { }; Doll.prototype.createFixtures = function () { + Assert.number(this.width, this.height); + Assert.number(this.reachDistance); + Assert.number(this.areaSize); + var self = this; var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); @@ -67,13 +72,15 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { fixtureDef.restitution = Settings.PLAYER_RESTITUTION; var headShape = new Box2D.Collision.Shapes.b2CircleShape(); - headShape.SetRadius(this.width / 2 / Settings.RATIO); + var radius = this.width / 2 / Settings.RATIO; + headShape.SetRadius(radius); + headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0, -(this.height - (this.width / 2)) / Settings.RATIO)); fixtureDef.shape = headShape; fixtureDef.isSensor = false; fixtureDef.userData = { onCollisionChange: this.onImpact.bind(this) - } + }; this.body.CreateFixture(fixtureDef); @@ -106,7 +113,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { fixtureDef.userData = { onCollisionChange: this.onFootSensorDetection.bind(this) - } + }; this.footSensor = this.body.CreateFixture(fixtureDef); @@ -125,7 +132,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { onCollisionChange: function(isColliding, fixture) { self.onFixtureWithinReach(isColliding, "left", fixture); } - } + }; this.body.CreateFixture(fixtureDef); var grabSensorRightShape = new Box2D.Collision.Shapes.b2PolygonShape(); @@ -144,7 +151,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { onCollisionChange: function(isColliding, fixture) { self.onFixtureWithinReach(isColliding, "right", fixture); } - } + }; this.body.CreateFixture(fixtureDef); @@ -163,7 +170,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { fixtureDef.userData = { onCollisionChange: function(isColliding, fixture) { - var userData = fixture.GetBody().GetUserData() + var userData = fixture.GetBody().GetUserData(); if(userData instanceof Doll) { var doll = userData; var i = self.nearbyDolls.indexOf(doll); @@ -178,28 +185,29 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { } } } - } + }; this.body.CreateFixture(fixtureDef); - } + }; Doll.prototype.setActionState = function(state) { this.actionState = state; - } + }; Doll.prototype.getActionState = function() { return this.actionState; - } + }; Doll.prototype.isWalking = function() { return ["walk", "walkback", "run"].indexOf(this.actionState) >= 0; - } + }; Doll.prototype.spawn = function (x, y) { + Assert.number(x, y); this.body.SetPosition(new Box2D.Common.Math.b2Vec2(x / Settings.RATIO, y / Settings.RATIO)); this.body.SetActive(true); this.setActionState("fall"); - } + }; Doll.prototype.getHeadPosition = function() { var pos = this.body.GetPosition(); @@ -212,10 +220,12 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { Doll.prototype.setFriction = function (friction) { if(!friction) friction = -1; + Assert.number(friction); + if (this.legs.GetFriction() != friction) { this.legs.SetFriction(friction); } - } + }; Doll.prototype.move = function (direction) { @@ -245,6 +255,8 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { this.setFriction(Settings.PLAYER_MOTION_FRICTION); this.body.SetAwake(true); + + Assert.number(speed, direction); var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this.body.GetLinearVelocity().y); this.body.SetLinearVelocity(vector); @@ -261,7 +273,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { this.setActionState("walkback"); } } - } + }; Doll.prototype.stop = function () { this.moveDirection = 0; @@ -273,23 +285,20 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { vector.x *= Settings.JUMP_STOP_DAMPING_FACTOR; this.body.SetLinearVelocity(vector); } - } + }; Doll.prototype.jump = function () { if (this.isStanding()) { this.body.SetAwake(true); - var jumpSpeed = Settings.JUMP_SPEED; - - var vector = new Box2D.Common.Math.b2Vec2(0, -jumpSpeed); + var vector = new Box2D.Common.Math.b2Vec2(0, -Settings.JUMP_SPEED); this.body.SetLinearVelocity(vector); this.setStanding(false); - this.setActionState("jump"); } - } + }; Doll.prototype.jumpStop = function () { if (!this.isStanding() ) { @@ -300,17 +309,17 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { this.body.SetLinearVelocity(vector); } } - } + }; Doll.prototype.setStanding = function (isStanding) { if (this.standing == isStanding) return; this.standing = isStanding; if(isStanding) this.setActionState("stand"); - } + }; Doll.prototype.isStanding = function () { return this.standing; - } + }; Doll.prototype.lookAt = function(x, y) { var oldLookDirection = this.lookDirection; @@ -344,6 +353,9 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { } var bodyPosition = this.body.GetPosition(); + + Assert.number(this.width, this.height); + Assert.number(this.lookDirection); var handPosition = new Box2D.Common.Math.b2Vec2( bodyPosition.x + ((this.width / 2 / Settings.RATIO) * this.lookDirection), bodyPosition.y - this.height / 4 * 2 / Settings.RATIO // 2/3 of the body height @@ -382,7 +394,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { return this.nearbyDolls.length > 0; }; - Doll.prototype.onFootSensorDetection = function(isColliding, fixture) { + Doll.prototype.onFootSensorDetection = function(isColliding, fixture) { // jshint unused:false var self = this; @@ -419,9 +431,9 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { self.setStanding(false); } } - } + }; - Doll.prototype.onImpact = function(isColliding, fixture) { + Doll.prototype.onImpact = function(isColliding, fixture) { // jshint unused:false // overwrite if necessary }; @@ -439,7 +451,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { this.reachableItems[side].splice(i, 1); } } - } + }; Doll.prototype.getVelocities = function() { return { @@ -450,7 +462,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) { Doll.prototype.update = function() { - if (this.body.GetLinearVelocity().x == 0 && this.isWalking()) { + if (this.body.GetLinearVelocity().x === 0 && this.isWalking()) { this.stop(); } diff --git a/app/Game/Core/GameObjects/Item.js b/app/Game/Core/GameObjects/Item.js index 6109010..d848fa3 100644 --- a/app/Game/Core/GameObjects/Item.js +++ b/app/Game/Core/GameObjects/Item.js @@ -4,10 +4,11 @@ define([ "Lib/Utilities/Options", "Game/Config/Settings", "Lib/Utilities/Exception", - "Lib/Utilities/NotificationCenter" + "Lib/Utilities/NotificationCenter", + "Lib/Utilities/Assert" ], -function (Parent, Box2D, Options, Settings, Exception, Nc) { +function (Parent, Box2D, Options, Settings, Exception, Nc, Assert) { "use strict"; @@ -37,13 +38,13 @@ function (Parent, Box2D, Options, Settings, Exception, Nc) { this.body.ResetMassData(); this.flipDirection = 1; - Nc.trigger(Nc.ns.core.game.gameObject.add, 'animated', this); + Nc.trigger(Nc.ns.core.game.gameObject.add, "animated", this); } Item.prototype = Object.create(Parent.prototype); Item.prototype.getBodyDef = function() { - + Assert.number(this.options.x, this.options.y); var bodyDef = new Box2D.Dynamics.b2BodyDef(); bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; bodyDef.position.x = this.options.x / Settings.RATIO; @@ -51,14 +52,18 @@ function (Parent, Box2D, Options, Settings, Exception, Nc) { bodyDef.angle = 0; return bodyDef; - } + }; Item.prototype.getFixtureDef = function() { + Assert.number(this.options.width, this.options.height); + Assert.number(this.options.weight); + Assert.number(this.options.bounce); + var itemShape; var w = this.options.width / Settings.RATIO; var h = this.options.height / Settings.RATIO; - if(this.options.type == 'circle'){ + if(this.options.type == "circle") { var r = (w + h) / 4 ; itemShape = new Box2D.Collision.Shapes.b2CircleShape(); itemShape.SetRadius(r); @@ -68,22 +73,19 @@ function (Parent, Box2D, Options, Settings, Exception, Nc) { itemShape.SetAsOrientedBox(w / 2, h / 2, new Box2D.Common.Math.b2Vec2(0, -(h/2))); } - var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); fixtureDef.shape = itemShape; fixtureDef.density = this.options.weight; fixtureDef.friction = Settings.ITEM_FRICTION; - fixtureDef.restitution = this.options.bounce - ? this.options.bounce / 10 - : Settings.ITEM_RESTITUTION; + fixtureDef.restitution = this.options.bounce ? this.options.bounce / 10 : Settings.ITEM_RESTITUTION; fixtureDef.isSensor = false; fixtureDef.userData = { onCollisionChange: this.onCollisionChange.bind(this) - } + }; return fixtureDef; }; @@ -91,7 +93,7 @@ function (Parent, Box2D, Options, Settings, Exception, Nc) { Item.prototype.createFixture = function () { var fixtureDef = this.getFixtureDef(); this.body.CreateFixture(fixtureDef); - } + }; Item.prototype.flip = function(direction) { this.flipDirection = direction; @@ -99,24 +101,29 @@ function (Parent, Box2D, Options, Settings, Exception, Nc) { // FIXME: implement body flip if necessary }; - Item.prototype.beingGrabbed = function(player) { + Item.prototype.beingGrabbed = function(player) { // jshint unused:false // overwrite if necessary }; - Item.prototype.beingReleased = function(player) { + Item.prototype.beingReleased = function(player) { // jshint unused:false // overwrite if necessary }; - Item.prototype.onCollisionChange = function(isColliding, fixture, info) { + Item.prototype.onCollisionChange = function(isColliding, fixture, info) { // jshint unused:false // overwrite if necessary }; Item.prototype.reposition = function(handPosition, direction) { + Assert.number(handPosition.x, handPosition.y); + Assert.number(direction); + Assert.number(this.options.width); + Assert.number(this.options.grabAngle); + this.body.SetAwake(true); var position = new Box2D.Common.Math.b2Vec2( handPosition.x + ((this.options.width / Settings.RATIO / 2) * direction), handPosition.y - ) + ); this.body.SetPosition(position); this.flip(direction); this.body.SetAngle((this.options.grabAngle || 0) * direction); @@ -131,20 +138,24 @@ function (Parent, Box2D, Options, Settings, Exception, Nc) { }; Item.prototype.accelerateBody = function(body, options, carrierVelocity) { + Assert.number(this.options.weight); + Assert.number(carrierVelocity.x, carrierVelocity.y); + Assert.number(options.x, options.y); + Assert.number(options.av); body.SetAwake(true); - var vector = new Box2D.Common.Math.b2Vec2( - options.x * Settings.MAX_THROW_FORCE / this.options.weight + carrierVelocity.x, - -options.y * Settings.MAX_THROW_FORCE / this.options.weight + carrierVelocity.y - ); - + var x = options.x * Settings.MAX_THROW_FORCE / this.options.weight + carrierVelocity.x; + var y = -options.y * Settings.MAX_THROW_FORCE / this.options.weight + carrierVelocity.y; + var vector = new Box2D.Common.Math.b2Vec2(x, y); body.SetLinearVelocity(vector); - body.SetAngularVelocity(-options.av * Settings.MAX_THROW_ANGULAR_VELOCITY); + + var av = -options.av * Settings.MAX_THROW_ANGULAR_VELOCITY; + body.SetAngularVelocity(av); }; Item.prototype.destroy = function() { - Nc.trigger(Nc.ns.core.game.gameObject.remove, 'animated', this); + Nc.trigger(Nc.ns.core.game.gameObject.remove, "animated", this); Parent.prototype.destroy.call(this); }; diff --git a/app/Game/Core/GameObjects/Items/RagDoll.js b/app/Game/Core/GameObjects/Items/RagDoll.js index fa2bdd0..ef753e6 100644 --- a/app/Game/Core/GameObjects/Items/RagDoll.js +++ b/app/Game/Core/GameObjects/Items/RagDoll.js @@ -2,10 +2,13 @@ define([ "Game/" + GLOBALS.context + "/GameObjects/Item", "Lib/Vendor/Box2D", "Game/Config/Settings", - "Lib/Utilities/NotificationCenter" + "Lib/Utilities/NotificationCenter", + "Lib/Utilities/Assert", + "Lib/Utilities/Options", + "Game/Config/ItemSettings", ], -function (Parent, Box2D, Settings, Nc) { +function (Parent, Box2D, Settings, Nc, Assert, Options, ItemSettings) { "use strict"; @@ -92,10 +95,9 @@ function (Parent, Box2D, Settings, Nc) { - - - - + // FIXME + var ragdollOptions = Options.merge(ItemSettings.RagDoll, ItemSettings.Default); + options = Options.merge(options, ragdollOptions); Parent.call(this, physicsEngine, uid, options); //this.createSensor(); @@ -187,6 +189,8 @@ function (Parent, Box2D, Settings, Nc) { }; RagDoll.prototype.getFixtureDef = function() { + Assert.number(this.options.limbs.chest.width, this.options.limbs.chest.height); + var fixtureDef = Parent.prototype.getFixtureDef.call(this); fixtureDef.density = Settings.PLAYER_DENSITY; fixtureDef.friction = Settings.PLAYER_FRICTION; @@ -206,6 +210,8 @@ function (Parent, Box2D, Settings, Nc) { }; RagDoll.prototype.createSensor = function() { + Assert.number(this.options.width, this.options.height); + var w = this.options.width / Settings.RATIO; var h = this.options.height / Settings.RATIO; @@ -218,12 +224,16 @@ function (Parent, Box2D, Settings, Nc) { fixtureDef.userData = { onCollisionChange: this.onCollisionChange.bind(this) - } + }; this.body.CreateFixture(fixtureDef); }; RagDoll.prototype.addHead = function() { + Assert.number(this.options.x, this.options.y); + Assert.number(this.options.limbs.head.x, this.options.limbs.head.y); + Assert.number(this.options.limbs.head.width); + var x = this.options.x + this.options.limbs.head.x, y = this.options.y + this.options.limbs.head.y; @@ -270,6 +280,11 @@ function (Parent, Box2D, Settings, Nc) { }; RagDoll.prototype.addLimb = function(name, connectTo, xOffset, yOffset) { + Assert.number(xOffset, yOffset); + Assert.number(this.options.x, this.options.y); + Assert.number(this.options.limbs[name].x, this.options.limbs[name].y); + Assert.number(this.options.limbs[name].width, this.options.limbs[name].height); + var x = this.options.x + this.options.limbs[name].x, y = this.options.y + this.options.limbs[name].y; @@ -322,6 +337,10 @@ function (Parent, Box2D, Settings, Nc) { }; RagDoll.prototype.reposition = function(handPosition, direction) { + Assert.number(this.options.limbs.head.x, this.options.limbs.head.y); + Assert.number(this.options.grabAngle); + Assert.number(direction); + Parent.prototype.reposition.call(this, handPosition, direction); var chestPosition = this.body.GetPosition(); @@ -329,7 +348,7 @@ function (Parent, Box2D, Settings, Nc) { var position = new Box2D.Common.Math.b2Vec2( chestPosition.x + this.options.limbs.head.x / Settings.RATIO, chestPosition.y + this.options.limbs.head.y / Settings.RATIO - ) + ); this.limbs.head.SetPosition(position); this.limbs.head.SetAngle((this.options.grabAngle || 0) * direction); }; @@ -345,6 +364,9 @@ function (Parent, Box2D, Settings, Nc) { }; RagDoll.prototype.setVelocities = function(options) { + Assert.number(options.linearVelocity.x, options.linearVelocity.y); + Assert.number(options.angularVelocity); + this.body.SetLinearVelocity(options.linearVelocity); this.body.SetAngularVelocity(options.angularVelocity); for(var name in this.limbs) { @@ -354,7 +376,7 @@ function (Parent, Box2D, Settings, Nc) { RagDoll.prototype.destroy = function() { - Nc.trigger(Nc.ns.core.game.gameObject.remove, 'animated', this); + Nc.trigger(Nc.ns.core.game.gameObject.remove, "animated", this); var world = this.body.GetWorld(); for (var name in this.limbs) { diff --git a/app/Game/Core/GameObjects/Items/Rube.js b/app/Game/Core/GameObjects/Items/Rube.js index 2a5c179..ccc06a6 100644 --- a/app/Game/Core/GameObjects/Items/Rube.js +++ b/app/Game/Core/GameObjects/Items/Rube.js @@ -2,10 +2,11 @@ define([ "Game/" + GLOBALS.context + "/GameObjects/Item", "Lib/Vendor/RubeLoader", "Lib/Vendor/Box2D", - "Game/Config/Settings" + "Game/Config/Settings", + "Lib/Utilities/Assert" ], -function (Parent, RubeLoader, Box2D, Settings ) { +function (Parent, RubeLoader, Box2D, Settings, Assert) { "use strict"; @@ -13,6 +14,7 @@ function (Parent, RubeLoader, Box2D, Settings ) { var __ragdollJson; function Rube(physicsEngine, uid, options) { + Assert.number(options.x, options.y); this.rubeLoader = null; this.body = null; @@ -1398,7 +1400,7 @@ function (Parent, RubeLoader, Box2D, Settings ) { "subStepping" : false, "velocityIterations" : 8, "warmStarting" : true -} +}; diff --git a/app/Game/Core/GameObjects/Items/Skateboard.js b/app/Game/Core/GameObjects/Items/Skateboard.js index 0be6e76..311f01e 100755 --- a/app/Game/Core/GameObjects/Items/Skateboard.js +++ b/app/Game/Core/GameObjects/Items/Skateboard.js @@ -1,10 +1,11 @@ define([ "Game/" + GLOBALS.context + "/GameObjects/Item", "Lib/Vendor/Box2D", - "Game/Config/Settings" + "Game/Config/Settings", + "Lib/Utilities/Assert" ], -function (Parent, Box2D, Settings) { +function (Parent, Box2D, Settings, Assert) { "use strict"; @@ -27,6 +28,8 @@ function (Parent, Box2D, Settings) { Skateboard.prototype = Object.create(Parent.prototype); Skateboard.prototype.createFixture = function () { + Assert.number(this.options.width, this.options.height); + Assert.number(this.options.weight); var deckShape = new Box2D.Collision.Shapes.b2PolygonShape(); var w = this.options.width / Settings.RATIO; @@ -45,10 +48,12 @@ function (Parent, Box2D, Settings) { fixtureDef.isSensor = false; this.body.CreateFixture(fixtureDef); - } + }; Skateboard.prototype.addWheel = function(x, y) { + Assert.number(x, y); + var bodyDef = new Box2D.Dynamics.b2BodyDef(); bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; bodyDef.position.x = x / Settings.RATIO; diff --git a/app/Game/Core/GameObjects/Tile.js b/app/Game/Core/GameObjects/Tile.js index 45d67eb..8a46fce 100755 --- a/app/Game/Core/GameObjects/Tile.js +++ b/app/Game/Core/GameObjects/Tile.js @@ -3,10 +3,11 @@ define([ "Lib/Vendor/Box2D", "Game/Config/Settings", "Lib/Utilities/Exception", - "Lib/Utilities/NotificationCenter" + "Lib/Utilities/NotificationCenter", + "Lib/Utilities/Assert" ], -function (Parent, Box2D, Settings, Exception, Nc) { +function (Parent, Box2D, Settings, Exception, Nc, Assert) { "use strict"; @@ -15,12 +16,14 @@ function (Parent, Box2D, Settings, Exception, Nc) { Parent.call(this, physicsEngine, uid); this.createPhysicTile(this.options); - Nc.trigger(Nc.ns.core.game.gameObject.add, 'fixed', this); + Nc.trigger(Nc.ns.core.game.gameObject.add, "fixed", this); } Tile.prototype = Object.create(Parent.prototype); Tile.prototype.getBodyDef = function() { + Assert.number(this.options.x, this.options.y); + Assert.number(this.options.r); var bodyDef = new Box2D.Dynamics.b2BodyDef(); bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody; @@ -29,7 +32,7 @@ function (Parent, Box2D, Settings, Exception, Nc) { bodyDef.angle = (this.options.r || 0) * 90 * Math.PI / 180; return bodyDef; - } + }; Tile.prototype.createPhysicTile = function (tile) { var vertices = this.createVertices(tile); @@ -43,7 +46,7 @@ function (Parent, Box2D, Settings, Exception, Nc) { fixtureDef.restitution = Settings.TILE_RESTITUTION; fixtureDef.isSensor = false; this.body.CreateFixture(fixtureDef); - } + }; Tile.prototype.createVertices = function (tile) { var vs = []; @@ -104,22 +107,21 @@ function (Parent, Box2D, Settings, Exception, Nc) { default: throw new Exception("Tile Creation - no shape given"); - break; } return vs; - } + }; Tile.prototype.mkArg = function (multiplier) { return Settings.TILE_SIZE / 2 / Settings.RATIO * multiplier; - } + }; Tile.prototype.addVec = function (vs, m1, m2) { return vs.push(new Box2D.Common.Math.b2Vec2(this.mkArg(m1), this.mkArg(m2))); - } + }; Tile.prototype.destroy = function() { - Nc.trigger(Nc.ns.core.game.gameObject.remove, 'fixed', this); + Nc.trigger(Nc.ns.core.game.gameObject.remove, "fixed", this); }; return Tile; diff --git a/app/Game/Core/Loader/Level.js b/app/Game/Core/Loader/Level.js index ee029d5..a599bb3 100755 --- a/app/Game/Core/Loader/Level.js +++ b/app/Game/Core/Loader/Level.js @@ -12,6 +12,8 @@ define([ ], function (Settings, Box2D, Nc, Abstract, CollisionDetector, Tile, Item, Skateboard, RagDoll, Rube) { + "use strict"; + function Level (uid, engine) { this.uid = uid; this.engine = engine; diff --git a/app/Game/Core/Loader/TiledLevel.js b/app/Game/Core/Loader/TiledLevel.js index 50cc5d0..85ff92d 100755 --- a/app/Game/Core/Loader/TiledLevel.js +++ b/app/Game/Core/Loader/TiledLevel.js @@ -14,6 +14,8 @@ define([ ], function (Parent, Settings, ItemSettings, Box2D, Options, Exception, Nc, AbstractLayer, CollisionDetector, Tile, Item, Skateboard) { + "use strict"; + function TiledLevel (path, engine) { this.layerMapping = { @@ -109,7 +111,7 @@ define([ } lastLayerId = layerOptions.layerId; - }; + } Parent.prototype.setup.call(this, levelData); @@ -137,21 +139,21 @@ define([ t: imagePath, x: i % options.width, y: parseInt(i / options.width , 10) - } + }; tilesOptions.push(tileOptions); } Parent.prototype.createTiles.call(this, tilesOptions); - } + }; TiledLevel.prototype.createItems = function(options) { var objects = options.objects; - var itemsOptions = [] + var itemsOptions = []; for (var i = 0; i < objects.length; i++) { - var options = this.gatherOptions(objects[i]); + options = this.gatherOptions(objects[i]); itemsOptions.push(options); - }; + } Parent.prototype.createItems.call(this, itemsOptions); }; @@ -188,18 +190,14 @@ define([ TiledLevel.prototype.getDefaultItemSettingsByName = function(name) { if(!name) { - throw new Exception('Item name cannot be be empty'); + throw new Exception("Item name cannot be be empty"); } if(ItemSettings[name] === undefined) { - throw new Exception('Item name (' + name + ') cannot be found in item list'); + throw new Exception("Item name (" + name + ") cannot be found in item list"); } - var options = ItemSettings.Default; - - options = Options.merge(ItemSettings[name], options); - - return options; + return Options.merge(ItemSettings[name], ItemSettings.Default); }; TiledLevel.prototype.getTileImagePath = function(gid) { @@ -210,7 +208,7 @@ define([ return tileset.tiles["" + (gid - offset)].image; } } - } + }; return TiledLevel; }); \ No newline at end of file diff --git a/app/Lib/Utilities/Assert.js b/app/Lib/Utilities/Assert.js new file mode 100644 index 0000000..0a89ed7 --- /dev/null +++ b/app/Lib/Utilities/Assert.js @@ -0,0 +1,21 @@ +define([ + "Lib/Utilities/Exception" +], + +function (Exception) { + + "use strict"; + + var Assert = {}; + + Assert.number = function() { + for (var i = 0; i < arguments.length; i++) { + if(isNaN(parseFloat(arguments[i]))) { + throw new Exception("Assert: not a number ", JSON.stringify(arguments)); + } + } + }; + + return Assert; + +}); \ No newline at end of file