added ASSERT, fixed #103

This commit is contained in:
Jeena 2015-03-15 16:51:38 +01:00
parent 55eff36f34
commit dfa71bc8e5
17 changed files with 249 additions and 153 deletions

View file

@ -7,6 +7,8 @@ define([
function(Parent, Nc, Parser, Settings) { function(Parent, Nc, Parser, Settings) {
"use strict";
function PlayerController(player) { function PlayerController(player) {
Parent.call(this, player); Parent.call(this, player);
@ -33,6 +35,9 @@ function(Parent, Nc, Parser, Settings) {
}; };
PlayerController.prototype.handActionRequest = function(options) { 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); if (options) this.player.handActionRequest(options);
}; };
@ -43,21 +48,21 @@ function(Parent, Nc, Parser, Settings) {
PlayerController.prototype.mePositionStateUpdate = function(update) { PlayerController.prototype.mePositionStateUpdate = function(update) {
if(!this.player.doll) { 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; return;
} }
var difference = { var difference = {
x: Math.abs(update.p.x - this.player.doll.body.GetPosition().x), x: Math.abs(update.p.x - this.player.doll.body.GetPosition().x),
y: Math.abs(update.p.y - this.player.doll.body.GetPosition().y) y: Math.abs(update.p.y - this.player.doll.body.GetPosition().y)
} };
if(true || difference.x < Settings.PUNKBUSTER_DIFFERENCE_METERS if(true || difference.x < Settings.PUNKBUSTER_DIFFERENCE_METERS &&
&& difference.y < Settings.PUNKBUSTER_DIFFERENCE_METERS) { difference.y < Settings.PUNKBUSTER_DIFFERENCE_METERS) {
this.player.doll.updatePositionState(update); this.player.doll.updatePositionState(update);
} else { } else {
// HARD UPDATE FOR SELF // 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; var body = this.player.doll.body;
@ -66,7 +71,7 @@ function(Parent, Nc, Parser, Settings) {
lv: body.GetLinearVelocity() 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);
} }
}; };

View file

@ -2,10 +2,11 @@ define([
"Game/Core/GameObjects/Doll", "Game/Core/GameObjects/Doll",
"Game/Channel/GameObjects/Item", "Game/Channel/GameObjects/Item",
"Lib/Vendor/Box2D", "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"; "use strict";
@ -15,7 +16,7 @@ function (Parent, Item, Box2D, Nc) {
Doll.prototype = Object.create(Parent.prototype); Doll.prototype = Object.create(Parent.prototype);
Doll.prototype.findCloseItem = function(x, y) { Doll.prototype.findCloseItem = function(x) {
var self = this; var self = this;
@ -33,7 +34,7 @@ function (Parent, Item, Box2D, Nc) {
} else { } else {
return findItem(this.reachableItems.right); return findItem(this.reachableItems.right);
} }
} };
Doll.prototype.onImpact = function(isColliding, fixture) { Doll.prototype.onImpact = function(isColliding, fixture) {
var self = this; var self = this;
@ -52,7 +53,7 @@ function (Parent, Item, Box2D, Nc) {
var b2Math = Box2D.Common.Math.b2Math; var b2Math = Box2D.Common.Math.b2Math;
var absItemVelocity = b2Math.AbsV(itemVelocity) var absItemVelocity = b2Math.AbsV(itemVelocity);
var max = 1; var max = 1;
if(absItemVelocity.x > max || absItemVelocity.y > max) { if(absItemVelocity.x > max || absItemVelocity.y > max) {
@ -71,9 +72,9 @@ function (Parent, Item, Box2D, Nc) {
var callback = function() { var callback = function() {
self.player.addDamage(damage, player, item); 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) { Doll.prototype.updatePositionState = function(update) {
if(!this.isAnotherPlayerNearby()) { 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.SetAwake(true);
this.body.SetPosition(update.p); this.body.SetPosition(update.p);
this.body.SetLinearVelocity(update.lv); this.body.SetLinearVelocity(update.lv);

View file

@ -21,14 +21,14 @@ function (Parent) {
this.lastMoved = { this.lastMoved = {
player: player, player: player,
timestamp: new Date() timestamp: new Date()
} };
} else { } else {
this.lastMoved = null; this.lastMoved = null;
} }
}; };
Item.prototype.isGrabbingAllowed = function(player) { Item.prototype.isGrabbingAllowed = function(player) { // jshint unused:false
return this.heldByPlayers.length == 0; return this.heldByPlayers.length === 0;
}; };
Item.prototype.beingGrabbed = function(player) { 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; return true;
}; };
@ -81,8 +81,7 @@ function (Parent) {
} }
} }
} }
} };
return Item; return Item;

View file

@ -22,13 +22,13 @@ function (Parent, Nc) {
if (isHolding) { if (isHolding) {
item = this.holdingItem; item = this.holdingItem;
} else { } else {
item = this.doll.findCloseItem(options.x, options.y); item = this.doll.findCloseItem(options.x);
} }
if(item) { if(item) {
this.handAction(options, isHolding, item); this.handAction(options, isHolding, item);
} }
} };
Player.prototype.handAction = function(options, isHolding, item) { Player.prototype.handAction = function(options, isHolding, item) {
@ -112,7 +112,6 @@ function (Parent, Nc) {
}; };
return Player; return Player;
}); });

View file

@ -8,6 +8,8 @@ define([
function (Parent, KeyboardInput, DomController, Settings, Swiper) { function (Parent, KeyboardInput, DomController, Settings, Swiper) {
"use strict";
function KeyboardAndMouse(playerController) { function KeyboardAndMouse(playerController) {
Parent.call(this); Parent.call(this);
@ -42,7 +44,7 @@ function (Parent, KeyboardInput, DomController, Settings, Swiper) {
minus: 189, minus: 189,
minusfx: 173, minusfx: 173,
zero: 48 zero: 48
} };
this.playerController = playerController; this.playerController = playerController;
this.keyboardInit(); this.keyboardInit();
@ -87,7 +89,7 @@ function (Parent, KeyboardInput, DomController, Settings, Swiper) {
this.lastLookDirection = -1; this.lastLookDirection = -1;
this.onXyChange(this.lastLookDirection * Settings.VIEWPORT_LOOK_AHEAD, 0); this.onXyChange(this.lastLookDirection * Settings.VIEWPORT_LOOK_AHEAD, 0);
} }
this.playerController.moveLeft() this.playerController.moveLeft();
}; };
KeyboardAndMouse.prototype.moveRight = function() { KeyboardAndMouse.prototype.moveRight = function() {
@ -129,7 +131,8 @@ function (Parent, KeyboardInput, DomController, Settings, Swiper) {
if(!self.playerController.player.isHoldingSomething()) { if(!self.playerController.player.isHoldingSomething()) {
var options = { var options = {
x: self.x, x: self.x,
y: self.y y: self.y,
av: 0
}; };
self.playerController.handActionRequest(options); self.playerController.handActionRequest(options);
} else { } else {

View file

@ -13,10 +13,11 @@ define([
"Lib/Utilities/Protocol/Helper", "Lib/Utilities/Protocol/Helper",
"Game/Client/Me", "Game/Client/Me",
"Game/Client/AudioPlayer", "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"; "use strict";
@ -39,7 +40,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
GameController.prototype.getMe = function () { GameController.prototype.getMe = function () {
return this.me; return this.me;
} };
GameController.prototype.update = function () { GameController.prototype.update = function () {
@ -59,7 +60,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
this.view.render(); this.view.render();
DomController.fpsStep(); DomController.fpsStep();
} };
GameController.prototype.mePositionStateUpdate = function() { GameController.prototype.mePositionStateUpdate = function() {
if(this.me.isPositionStateUpdateNeeded()) { if(this.me.isPositionStateUpdateNeeded()) {
@ -68,27 +69,28 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
}; };
GameController.prototype.onClientReadyResponse = function(options) { GameController.prototype.onClientReadyResponse = function(options) {
var i;
if (options.worldUpdate) { if (options.worldUpdate) {
this.onWorldUpdate(options.worldUpdate); this.onWorldUpdate(options.worldUpdate);
} }
if (options.runtimeItems) { 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 itemDef = options.runtimeItems[i];
var alreadyExists = false; var alreadyExists = false;
for (var i = 0; i < this.gameObjects.animated.length; i++) { for (var j = 0; j < this.gameObjects.animated.length; j++) {
if(this.gameObjects.animated[i].uid == itemDef.uid) { if(this.gameObjects.animated[j].uid == itemDef.uid) {
alreadyExists = true; alreadyExists = true;
break; break;
} }
}; }
if(!alreadyExists) { if(!alreadyExists) {
var item = this.level.createItem(itemDef.uid, itemDef.options); this.level.createItem(itemDef.uid, itemDef.options);
} }
}; }
} }
this.setMe(); this.setMe();
@ -96,7 +98,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
this.clientIsReady = true; // needs to stay before onSpawnPlayer this.clientIsReady = true; // needs to stay before onSpawnPlayer
if (options.spawnedPlayers) { 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]); 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); 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.SetAwake(true);
body.SetPosition(update.p); body.SetPosition(update.p);
body.SetAngle(update.a); body.SetAngle(update.a);
@ -136,7 +143,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
} while (body = body.GetNext()); } while (body = body.GetNext());
} };
GameController.prototype.createMe = function(user) { GameController.prototype.createMe = function(user) {
this.me = new Me(user.id, this.physicsEngine, 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() { GameController.prototype.setMe = function() {
this.me.setPlayerController(new PlayerController(this.me)); this.me.setPlayerController(new PlayerController(this.me));
this.view.setMe(this.me); this.view.setMe(this.me);
} };
GameController.prototype.onGameCommand = function(message) { GameController.prototype.onGameCommand = function(message) {
ProtocolHelper.applyCommand(message, this); ProtocolHelper.applyCommand(message, this);
@ -172,7 +179,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
playerId: playerId playerId: playerId
}); });
} }
} };
GameController.prototype.onHandActionResponse = function(options) { GameController.prototype.onHandActionResponse = function(options) {
var player = this.players[options.playerId]; var player = this.players[options.playerId];
@ -184,7 +191,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
item = currentItem; item = currentItem;
break; break;
} }
}; }
if(item) { if(item) {
if(options.action == "throw") { if(options.action == "throw") {
@ -193,7 +200,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
player.grab(item); player.grab(item);
} }
} else { } 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) { GameController.prototype.loadLevel = function (path) {
Parent.prototype.loadLevel.call(this, path); Parent.prototype.loadLevel.call(this, path);
} };
GameController.prototype.onLevelLoaded = function () { GameController.prototype.onLevelLoaded = function () {
PointerLockManager.update(null, {start:true}); PointerLockManager.update(null, {start:true});
} };
GameController.prototype.toggleGameStats = function(show) { GameController.prototype.toggleGameStats = function(show) {
var playersArray = []; var playersArray = [];
for (var key in this.players) { for (var key in this.players) {
playersArray.push(this.players[key]); playersArray.push(this.players[key]);
}; }
var sortedPlayers = playersArray.sort(function(a,b) { var sortedPlayers = playersArray.sort(function(a,b) {
if(a.stats.score > b.stats.score) return -1; if(a.stats.score > b.stats.score) return -1;

View file

@ -1,10 +1,11 @@
define([ define([
"Game/Client/Player", "Game/Client/Player",
"Game/Config/Settings", "Game/Config/Settings",
"Lib/Utilities/NotificationCenter" "Lib/Utilities/NotificationCenter",
"Lib/Utilities/Assert"
], ],
function (Parent, Settings, Nc) { function (Parent, Settings, Nc, Assert) {
"use strict"; "use strict";
@ -15,7 +16,7 @@ function (Parent, Settings, Nc) {
this.lookAtXY = { this.lookAtXY = {
x: Settings.VIEWPORT_LOOK_AHEAD, x: Settings.VIEWPORT_LOOK_AHEAD,
y: 0 y: 0
} };
this.lastServerPositionState = { this.lastServerPositionState = {
p: { p: {
@ -34,7 +35,7 @@ function (Parent, Settings, Nc) {
this.lookAtXY = { this.lookAtXY = {
x: x, x: x,
y: y y: y
} };
Parent.prototype.lookAt.call(this, x, y); Parent.prototype.lookAt.call(this, x, y);
}; };
@ -63,10 +64,10 @@ function (Parent, Settings, Nc) {
var difference = { var difference = {
x: Math.abs(this.lastServerPositionState.p.x - this.doll.body.GetPosition().x), x: Math.abs(this.lastServerPositionState.p.x - this.doll.body.GetPosition().x),
y: Math.abs(this.lastServerPositionState.p.y - this.doll.body.GetPosition().y) y: Math.abs(this.lastServerPositionState.p.y - this.doll.body.GetPosition().y)
} };
if(difference.x > Settings.ME_STATE_MAX_DIFFERENCE_METERS if(difference.x > Settings.ME_STATE_MAX_DIFFERENCE_METERS ||
|| difference.y > Settings.ME_STATE_MAX_DIFFERENCE_METERS) { difference.y > Settings.ME_STATE_MAX_DIFFERENCE_METERS) {
return true; return true;
} }
return false; return false;
@ -76,7 +77,7 @@ function (Parent, Settings, Nc) {
return { return {
p: this.doll.body.GetPosition().Copy(), p: this.doll.body.GetPosition().Copy(),
lv: this.doll.body.GetLinearVelocity().Copy() lv: this.doll.body.GetLinearVelocity().Copy()
} };
}; };
Me.prototype.acceptPositionStateUpdateFromServer = function() { Me.prototype.acceptPositionStateUpdateFromServer = function() {
@ -85,11 +86,13 @@ function (Parent, Settings, Nc) {
}; };
Me.prototype.resetPositionState = function(options) { 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.SetPosition(options.p);
this.doll.body.SetLinearVelocity(options.lv); this.doll.body.SetLinearVelocity(options.lv);
}; };
Me.prototype.createAndAddArrow = function(visible) { Me.prototype.createAndAddArrow = function() {
var self = this; var self = this;
var position = this.getPosition(); var position = this.getPosition();
@ -101,7 +104,7 @@ function (Parent, Settings, Nc) {
var callback = function(arrowMesh) { var callback = function(arrowMesh) {
self.arrowMesh = arrowMesh; self.arrowMesh = arrowMesh;
} };
Nc.trigger(Nc.ns.client.view.playerArrow.createAndAdd, callback, options); Nc.trigger(Nc.ns.client.view.playerArrow.createAndAdd, callback, options);
}; };
@ -113,7 +116,7 @@ function (Parent, Settings, Nc) {
var options = { var options = {
x: position.x * Settings.RATIO, x: position.x * Settings.RATIO,
y: position.y * Settings.RATIO, y: position.y * Settings.RATIO,
} };
Nc.trigger(Nc.ns.client.view.playerArrow.update, this.arrowMesh, options); Nc.trigger(Nc.ns.client.view.playerArrow.update, this.arrowMesh, options);
}; };

View file

@ -3,6 +3,8 @@ define([
function () { function () {
"use strict";
var ItemSettings = { var ItemSettings = {
// weight is a number between 0.1 for very light and 10 for very heavy // weight is a number between 0.1 for very light and 10 for very heavy

View file

@ -5,10 +5,11 @@ define([
"Game/Config/Settings", "Game/Config/Settings",
"Game/" + GLOBALS.context + "/Collision/Detector", "Game/" + GLOBALS.context + "/Collision/Detector",
"Game/" + GLOBALS.context + "/GameObjects/Item", "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"; "use strict";
@ -27,8 +28,8 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
this.standing = false; this.standing = false;
this.moveDirection = 0; this.moveDirection = 0;
this.lookDirection = 0; this.lookDirection = 0;
this.legs; this.legs = null;
this.footSensor; this.footSensor = null;
this.actionState = null; this.actionState = null;
this.lookAtXY = { x:0, y:0 }; this.lookAtXY = { x:0, y:0 };
this.reachableItems = { this.reachableItems = {
@ -59,6 +60,10 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
}; };
Doll.prototype.createFixtures = function () { Doll.prototype.createFixtures = function () {
Assert.number(this.width, this.height);
Assert.number(this.reachDistance);
Assert.number(this.areaSize);
var self = this; var self = this;
var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
@ -67,13 +72,15 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
fixtureDef.restitution = Settings.PLAYER_RESTITUTION; fixtureDef.restitution = Settings.PLAYER_RESTITUTION;
var headShape = new Box2D.Collision.Shapes.b2CircleShape(); 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)); headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0, -(this.height - (this.width / 2)) / Settings.RATIO));
fixtureDef.shape = headShape; fixtureDef.shape = headShape;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = { fixtureDef.userData = {
onCollisionChange: this.onImpact.bind(this) onCollisionChange: this.onImpact.bind(this)
} };
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
@ -106,7 +113,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
fixtureDef.userData = { fixtureDef.userData = {
onCollisionChange: this.onFootSensorDetection.bind(this) onCollisionChange: this.onFootSensorDetection.bind(this)
} };
this.footSensor = this.body.CreateFixture(fixtureDef); this.footSensor = this.body.CreateFixture(fixtureDef);
@ -125,7 +132,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
onCollisionChange: function(isColliding, fixture) { onCollisionChange: function(isColliding, fixture) {
self.onFixtureWithinReach(isColliding, "left", fixture); self.onFixtureWithinReach(isColliding, "left", fixture);
} }
} };
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
var grabSensorRightShape = new Box2D.Collision.Shapes.b2PolygonShape(); var grabSensorRightShape = new Box2D.Collision.Shapes.b2PolygonShape();
@ -144,7 +151,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
onCollisionChange: function(isColliding, fixture) { onCollisionChange: function(isColliding, fixture) {
self.onFixtureWithinReach(isColliding, "right", fixture); self.onFixtureWithinReach(isColliding, "right", fixture);
} }
} };
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
@ -163,7 +170,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
fixtureDef.userData = { fixtureDef.userData = {
onCollisionChange: function(isColliding, fixture) { onCollisionChange: function(isColliding, fixture) {
var userData = fixture.GetBody().GetUserData() var userData = fixture.GetBody().GetUserData();
if(userData instanceof Doll) { if(userData instanceof Doll) {
var doll = userData; var doll = userData;
var i = self.nearbyDolls.indexOf(doll); var i = self.nearbyDolls.indexOf(doll);
@ -178,28 +185,29 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
} }
} }
} }
} };
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
} };
Doll.prototype.setActionState = function(state) { Doll.prototype.setActionState = function(state) {
this.actionState = state; this.actionState = state;
} };
Doll.prototype.getActionState = function() { Doll.prototype.getActionState = function() {
return this.actionState; return this.actionState;
} };
Doll.prototype.isWalking = function() { Doll.prototype.isWalking = function() {
return ["walk", "walkback", "run"].indexOf(this.actionState) >= 0; return ["walk", "walkback", "run"].indexOf(this.actionState) >= 0;
} };
Doll.prototype.spawn = function (x, y) { 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.SetPosition(new Box2D.Common.Math.b2Vec2(x / Settings.RATIO, y / Settings.RATIO));
this.body.SetActive(true); this.body.SetActive(true);
this.setActionState("fall"); this.setActionState("fall");
} };
Doll.prototype.getHeadPosition = function() { Doll.prototype.getHeadPosition = function() {
var pos = this.body.GetPosition(); var pos = this.body.GetPosition();
@ -212,10 +220,12 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
Doll.prototype.setFriction = function (friction) { Doll.prototype.setFriction = function (friction) {
if(!friction) friction = -1; if(!friction) friction = -1;
Assert.number(friction);
if (this.legs.GetFriction() != friction) { if (this.legs.GetFriction() != friction) {
this.legs.SetFriction(friction); this.legs.SetFriction(friction);
} }
} };
Doll.prototype.move = function (direction) { Doll.prototype.move = function (direction) {
@ -245,6 +255,8 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
this.setFriction(Settings.PLAYER_MOTION_FRICTION); this.setFriction(Settings.PLAYER_MOTION_FRICTION);
this.body.SetAwake(true); this.body.SetAwake(true);
Assert.number(speed, direction);
var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this.body.GetLinearVelocity().y); var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this.body.GetLinearVelocity().y);
this.body.SetLinearVelocity(vector); this.body.SetLinearVelocity(vector);
@ -261,7 +273,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
this.setActionState("walkback"); this.setActionState("walkback");
} }
} }
} };
Doll.prototype.stop = function () { Doll.prototype.stop = function () {
this.moveDirection = 0; this.moveDirection = 0;
@ -273,23 +285,20 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
vector.x *= Settings.JUMP_STOP_DAMPING_FACTOR; vector.x *= Settings.JUMP_STOP_DAMPING_FACTOR;
this.body.SetLinearVelocity(vector); this.body.SetLinearVelocity(vector);
} }
} };
Doll.prototype.jump = function () { Doll.prototype.jump = function () {
if (this.isStanding()) { if (this.isStanding()) {
this.body.SetAwake(true); this.body.SetAwake(true);
var jumpSpeed = Settings.JUMP_SPEED; var vector = new Box2D.Common.Math.b2Vec2(0, -Settings.JUMP_SPEED);
var vector = new Box2D.Common.Math.b2Vec2(0, -jumpSpeed);
this.body.SetLinearVelocity(vector); this.body.SetLinearVelocity(vector);
this.setStanding(false); this.setStanding(false);
this.setActionState("jump"); this.setActionState("jump");
} }
} };
Doll.prototype.jumpStop = function () { Doll.prototype.jumpStop = function () {
if (!this.isStanding() ) { if (!this.isStanding() ) {
@ -300,17 +309,17 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
this.body.SetLinearVelocity(vector); this.body.SetLinearVelocity(vector);
} }
} }
} };
Doll.prototype.setStanding = function (isStanding) { Doll.prototype.setStanding = function (isStanding) {
if (this.standing == isStanding) return; if (this.standing == isStanding) return;
this.standing = isStanding; this.standing = isStanding;
if(isStanding) this.setActionState("stand"); if(isStanding) this.setActionState("stand");
} };
Doll.prototype.isStanding = function () { Doll.prototype.isStanding = function () {
return this.standing; return this.standing;
} };
Doll.prototype.lookAt = function(x, y) { Doll.prototype.lookAt = function(x, y) {
var oldLookDirection = this.lookDirection; var oldLookDirection = this.lookDirection;
@ -344,6 +353,9 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
} }
var bodyPosition = this.body.GetPosition(); var bodyPosition = this.body.GetPosition();
Assert.number(this.width, this.height);
Assert.number(this.lookDirection);
var handPosition = new Box2D.Common.Math.b2Vec2( var handPosition = new Box2D.Common.Math.b2Vec2(
bodyPosition.x + ((this.width / 2 / Settings.RATIO) * this.lookDirection), bodyPosition.x + ((this.width / 2 / Settings.RATIO) * this.lookDirection),
bodyPosition.y - this.height / 4 * 2 / Settings.RATIO // 2/3 of the body height 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; return this.nearbyDolls.length > 0;
}; };
Doll.prototype.onFootSensorDetection = function(isColliding, fixture) { Doll.prototype.onFootSensorDetection = function(isColliding, fixture) { // jshint unused:false
var self = this; var self = this;
@ -419,9 +431,9 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
self.setStanding(false); self.setStanding(false);
} }
} }
} };
Doll.prototype.onImpact = function(isColliding, fixture) { Doll.prototype.onImpact = function(isColliding, fixture) { // jshint unused:false
// overwrite if necessary // overwrite if necessary
}; };
@ -439,7 +451,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
this.reachableItems[side].splice(i, 1); this.reachableItems[side].splice(i, 1);
} }
} }
} };
Doll.prototype.getVelocities = function() { Doll.prototype.getVelocities = function() {
return { return {
@ -450,7 +462,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc) {
Doll.prototype.update = function() { Doll.prototype.update = function() {
if (this.body.GetLinearVelocity().x == 0 && this.isWalking()) { if (this.body.GetLinearVelocity().x === 0 && this.isWalking()) {
this.stop(); this.stop();
} }

View file

@ -4,10 +4,11 @@ define([
"Lib/Utilities/Options", "Lib/Utilities/Options",
"Game/Config/Settings", "Game/Config/Settings",
"Lib/Utilities/Exception", "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"; "use strict";
@ -37,13 +38,13 @@ function (Parent, Box2D, Options, Settings, Exception, Nc) {
this.body.ResetMassData(); this.body.ResetMassData();
this.flipDirection = 1; 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 = Object.create(Parent.prototype);
Item.prototype.getBodyDef = function() { Item.prototype.getBodyDef = function() {
Assert.number(this.options.x, this.options.y);
var bodyDef = new Box2D.Dynamics.b2BodyDef(); var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
bodyDef.position.x = this.options.x / Settings.RATIO; bodyDef.position.x = this.options.x / Settings.RATIO;
@ -51,14 +52,18 @@ function (Parent, Box2D, Options, Settings, Exception, Nc) {
bodyDef.angle = 0; bodyDef.angle = 0;
return bodyDef; return bodyDef;
} };
Item.prototype.getFixtureDef = function() { 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 itemShape;
var w = this.options.width / Settings.RATIO; var w = this.options.width / Settings.RATIO;
var h = this.options.height / Settings.RATIO; var h = this.options.height / Settings.RATIO;
if(this.options.type == 'circle'){ if(this.options.type == "circle") {
var r = (w + h) / 4 ; var r = (w + h) / 4 ;
itemShape = new Box2D.Collision.Shapes.b2CircleShape(); itemShape = new Box2D.Collision.Shapes.b2CircleShape();
itemShape.SetRadius(r); 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))); itemShape.SetAsOrientedBox(w / 2, h / 2, new Box2D.Common.Math.b2Vec2(0, -(h/2)));
} }
var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
fixtureDef.shape = itemShape; fixtureDef.shape = itemShape;
fixtureDef.density = this.options.weight; fixtureDef.density = this.options.weight;
fixtureDef.friction = Settings.ITEM_FRICTION; fixtureDef.friction = Settings.ITEM_FRICTION;
fixtureDef.restitution = this.options.bounce fixtureDef.restitution = this.options.bounce ? this.options.bounce / 10 : Settings.ITEM_RESTITUTION;
? this.options.bounce / 10
: Settings.ITEM_RESTITUTION;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = { fixtureDef.userData = {
onCollisionChange: this.onCollisionChange.bind(this) onCollisionChange: this.onCollisionChange.bind(this)
} };
return fixtureDef; return fixtureDef;
}; };
@ -91,7 +93,7 @@ function (Parent, Box2D, Options, Settings, Exception, Nc) {
Item.prototype.createFixture = function () { Item.prototype.createFixture = function () {
var fixtureDef = this.getFixtureDef(); var fixtureDef = this.getFixtureDef();
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
} };
Item.prototype.flip = function(direction) { Item.prototype.flip = function(direction) {
this.flipDirection = direction; this.flipDirection = direction;
@ -99,24 +101,29 @@ function (Parent, Box2D, Options, Settings, Exception, Nc) {
// FIXME: implement body flip if necessary // FIXME: implement body flip if necessary
}; };
Item.prototype.beingGrabbed = function(player) { Item.prototype.beingGrabbed = function(player) { // jshint unused:false
// overwrite if necessary // overwrite if necessary
}; };
Item.prototype.beingReleased = function(player) { Item.prototype.beingReleased = function(player) { // jshint unused:false
// overwrite if necessary // overwrite if necessary
}; };
Item.prototype.onCollisionChange = function(isColliding, fixture, info) { Item.prototype.onCollisionChange = function(isColliding, fixture, info) { // jshint unused:false
// overwrite if necessary // overwrite if necessary
}; };
Item.prototype.reposition = function(handPosition, direction) { 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); this.body.SetAwake(true);
var position = new Box2D.Common.Math.b2Vec2( var position = new Box2D.Common.Math.b2Vec2(
handPosition.x + ((this.options.width / Settings.RATIO / 2) * direction), handPosition.x + ((this.options.width / Settings.RATIO / 2) * direction),
handPosition.y handPosition.y
) );
this.body.SetPosition(position); this.body.SetPosition(position);
this.flip(direction); this.flip(direction);
this.body.SetAngle((this.options.grabAngle || 0) * 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) { 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); body.SetAwake(true);
var vector = new Box2D.Common.Math.b2Vec2( var x = options.x * Settings.MAX_THROW_FORCE / this.options.weight + carrierVelocity.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;
-options.y * Settings.MAX_THROW_FORCE / this.options.weight + carrierVelocity.y var vector = new Box2D.Common.Math.b2Vec2(x, y);
);
body.SetLinearVelocity(vector); 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() { 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); Parent.prototype.destroy.call(this);
}; };

View file

@ -2,10 +2,13 @@ define([
"Game/" + GLOBALS.context + "/GameObjects/Item", "Game/" + GLOBALS.context + "/GameObjects/Item",
"Lib/Vendor/Box2D", "Lib/Vendor/Box2D",
"Game/Config/Settings", "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"; "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); Parent.call(this, physicsEngine, uid, options);
//this.createSensor(); //this.createSensor();
@ -187,6 +189,8 @@ function (Parent, Box2D, Settings, Nc) {
}; };
RagDoll.prototype.getFixtureDef = function() { RagDoll.prototype.getFixtureDef = function() {
Assert.number(this.options.limbs.chest.width, this.options.limbs.chest.height);
var fixtureDef = Parent.prototype.getFixtureDef.call(this); var fixtureDef = Parent.prototype.getFixtureDef.call(this);
fixtureDef.density = Settings.PLAYER_DENSITY; fixtureDef.density = Settings.PLAYER_DENSITY;
fixtureDef.friction = Settings.PLAYER_FRICTION; fixtureDef.friction = Settings.PLAYER_FRICTION;
@ -206,6 +210,8 @@ function (Parent, Box2D, Settings, Nc) {
}; };
RagDoll.prototype.createSensor = function() { RagDoll.prototype.createSensor = function() {
Assert.number(this.options.width, this.options.height);
var w = this.options.width / Settings.RATIO; var w = this.options.width / Settings.RATIO;
var h = this.options.height / Settings.RATIO; var h = this.options.height / Settings.RATIO;
@ -218,12 +224,16 @@ function (Parent, Box2D, Settings, Nc) {
fixtureDef.userData = { fixtureDef.userData = {
onCollisionChange: this.onCollisionChange.bind(this) onCollisionChange: this.onCollisionChange.bind(this)
} };
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
}; };
RagDoll.prototype.addHead = function() { 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, var x = this.options.x + this.options.limbs.head.x,
y = this.options.y + this.options.limbs.head.y; 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) { 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, var x = this.options.x + this.options.limbs[name].x,
y = this.options.y + this.options.limbs[name].y; y = this.options.y + this.options.limbs[name].y;
@ -322,6 +337,10 @@ function (Parent, Box2D, Settings, Nc) {
}; };
RagDoll.prototype.reposition = function(handPosition, direction) { 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); Parent.prototype.reposition.call(this, handPosition, direction);
var chestPosition = this.body.GetPosition(); var chestPosition = this.body.GetPosition();
@ -329,7 +348,7 @@ function (Parent, Box2D, Settings, Nc) {
var position = new Box2D.Common.Math.b2Vec2( var position = new Box2D.Common.Math.b2Vec2(
chestPosition.x + this.options.limbs.head.x / Settings.RATIO, chestPosition.x + this.options.limbs.head.x / Settings.RATIO,
chestPosition.y + this.options.limbs.head.y / Settings.RATIO chestPosition.y + this.options.limbs.head.y / Settings.RATIO
) );
this.limbs.head.SetPosition(position); this.limbs.head.SetPosition(position);
this.limbs.head.SetAngle((this.options.grabAngle || 0) * direction); this.limbs.head.SetAngle((this.options.grabAngle || 0) * direction);
}; };
@ -345,6 +364,9 @@ function (Parent, Box2D, Settings, Nc) {
}; };
RagDoll.prototype.setVelocities = function(options) { RagDoll.prototype.setVelocities = function(options) {
Assert.number(options.linearVelocity.x, options.linearVelocity.y);
Assert.number(options.angularVelocity);
this.body.SetLinearVelocity(options.linearVelocity); this.body.SetLinearVelocity(options.linearVelocity);
this.body.SetAngularVelocity(options.angularVelocity); this.body.SetAngularVelocity(options.angularVelocity);
for(var name in this.limbs) { for(var name in this.limbs) {
@ -354,7 +376,7 @@ function (Parent, Box2D, Settings, Nc) {
RagDoll.prototype.destroy = function() { 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(); var world = this.body.GetWorld();
for (var name in this.limbs) { for (var name in this.limbs) {

View file

@ -2,10 +2,11 @@ define([
"Game/" + GLOBALS.context + "/GameObjects/Item", "Game/" + GLOBALS.context + "/GameObjects/Item",
"Lib/Vendor/RubeLoader", "Lib/Vendor/RubeLoader",
"Lib/Vendor/Box2D", "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"; "use strict";
@ -13,6 +14,7 @@ function (Parent, RubeLoader, Box2D, Settings ) {
var __ragdollJson; var __ragdollJson;
function Rube(physicsEngine, uid, options) { function Rube(physicsEngine, uid, options) {
Assert.number(options.x, options.y);
this.rubeLoader = null; this.rubeLoader = null;
this.body = null; this.body = null;
@ -1398,7 +1400,7 @@ function (Parent, RubeLoader, Box2D, Settings ) {
"subStepping" : false, "subStepping" : false,
"velocityIterations" : 8, "velocityIterations" : 8,
"warmStarting" : true "warmStarting" : true
} };

View file

@ -1,10 +1,11 @@
define([ define([
"Game/" + GLOBALS.context + "/GameObjects/Item", "Game/" + GLOBALS.context + "/GameObjects/Item",
"Lib/Vendor/Box2D", "Lib/Vendor/Box2D",
"Game/Config/Settings" "Game/Config/Settings",
"Lib/Utilities/Assert"
], ],
function (Parent, Box2D, Settings) { function (Parent, Box2D, Settings, Assert) {
"use strict"; "use strict";
@ -27,6 +28,8 @@ function (Parent, Box2D, Settings) {
Skateboard.prototype = Object.create(Parent.prototype); Skateboard.prototype = Object.create(Parent.prototype);
Skateboard.prototype.createFixture = function () { 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 deckShape = new Box2D.Collision.Shapes.b2PolygonShape();
var w = this.options.width / Settings.RATIO; var w = this.options.width / Settings.RATIO;
@ -45,10 +48,12 @@ function (Parent, Box2D, Settings) {
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
} };
Skateboard.prototype.addWheel = function(x, y) { Skateboard.prototype.addWheel = function(x, y) {
Assert.number(x, y);
var bodyDef = new Box2D.Dynamics.b2BodyDef(); var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
bodyDef.position.x = x / Settings.RATIO; bodyDef.position.x = x / Settings.RATIO;

View file

@ -3,10 +3,11 @@ define([
"Lib/Vendor/Box2D", "Lib/Vendor/Box2D",
"Game/Config/Settings", "Game/Config/Settings",
"Lib/Utilities/Exception", "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"; "use strict";
@ -15,12 +16,14 @@ function (Parent, Box2D, Settings, Exception, Nc) {
Parent.call(this, physicsEngine, uid); Parent.call(this, physicsEngine, uid);
this.createPhysicTile(this.options); 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 = Object.create(Parent.prototype);
Tile.prototype.getBodyDef = function() { Tile.prototype.getBodyDef = function() {
Assert.number(this.options.x, this.options.y);
Assert.number(this.options.r);
var bodyDef = new Box2D.Dynamics.b2BodyDef(); var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody; 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; bodyDef.angle = (this.options.r || 0) * 90 * Math.PI / 180;
return bodyDef; return bodyDef;
} };
Tile.prototype.createPhysicTile = function (tile) { Tile.prototype.createPhysicTile = function (tile) {
var vertices = this.createVertices(tile); var vertices = this.createVertices(tile);
@ -43,7 +46,7 @@ function (Parent, Box2D, Settings, Exception, Nc) {
fixtureDef.restitution = Settings.TILE_RESTITUTION; fixtureDef.restitution = Settings.TILE_RESTITUTION;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
} };
Tile.prototype.createVertices = function (tile) { Tile.prototype.createVertices = function (tile) {
var vs = []; var vs = [];
@ -104,22 +107,21 @@ function (Parent, Box2D, Settings, Exception, Nc) {
default: default:
throw new Exception("Tile Creation - no shape given"); throw new Exception("Tile Creation - no shape given");
break;
} }
return vs; return vs;
} };
Tile.prototype.mkArg = function (multiplier) { Tile.prototype.mkArg = function (multiplier) {
return Settings.TILE_SIZE / 2 / Settings.RATIO * multiplier; return Settings.TILE_SIZE / 2 / Settings.RATIO * multiplier;
} };
Tile.prototype.addVec = function (vs, m1, m2) { Tile.prototype.addVec = function (vs, m1, m2) {
return vs.push(new Box2D.Common.Math.b2Vec2(this.mkArg(m1), this.mkArg(m2))); return vs.push(new Box2D.Common.Math.b2Vec2(this.mkArg(m1), this.mkArg(m2)));
} };
Tile.prototype.destroy = function() { 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; return Tile;

View file

@ -12,6 +12,8 @@ define([
], function (Settings, Box2D, Nc, Abstract, CollisionDetector, Tile, Item, Skateboard, RagDoll, Rube) { ], function (Settings, Box2D, Nc, Abstract, CollisionDetector, Tile, Item, Skateboard, RagDoll, Rube) {
"use strict";
function Level (uid, engine) { function Level (uid, engine) {
this.uid = uid; this.uid = uid;
this.engine = engine; this.engine = engine;

View file

@ -14,6 +14,8 @@ define([
], function (Parent, Settings, ItemSettings, Box2D, Options, Exception, Nc, AbstractLayer, CollisionDetector, Tile, Item, Skateboard) { ], function (Parent, Settings, ItemSettings, Box2D, Options, Exception, Nc, AbstractLayer, CollisionDetector, Tile, Item, Skateboard) {
"use strict";
function TiledLevel (path, engine) { function TiledLevel (path, engine) {
this.layerMapping = { this.layerMapping = {
@ -109,7 +111,7 @@ define([
} }
lastLayerId = layerOptions.layerId; lastLayerId = layerOptions.layerId;
}; }
Parent.prototype.setup.call(this, levelData); Parent.prototype.setup.call(this, levelData);
@ -137,21 +139,21 @@ define([
t: imagePath, t: imagePath,
x: i % options.width, x: i % options.width,
y: parseInt(i / options.width , 10) y: parseInt(i / options.width , 10)
} };
tilesOptions.push(tileOptions); tilesOptions.push(tileOptions);
} }
Parent.prototype.createTiles.call(this, tilesOptions); Parent.prototype.createTiles.call(this, tilesOptions);
} };
TiledLevel.prototype.createItems = function(options) { TiledLevel.prototype.createItems = function(options) {
var objects = options.objects; var objects = options.objects;
var itemsOptions = [] var itemsOptions = [];
for (var i = 0; i < objects.length; i++) { for (var i = 0; i < objects.length; i++) {
var options = this.gatherOptions(objects[i]); options = this.gatherOptions(objects[i]);
itemsOptions.push(options); itemsOptions.push(options);
}; }
Parent.prototype.createItems.call(this, itemsOptions); Parent.prototype.createItems.call(this, itemsOptions);
}; };
@ -188,18 +190,14 @@ define([
TiledLevel.prototype.getDefaultItemSettingsByName = function(name) { TiledLevel.prototype.getDefaultItemSettingsByName = function(name) {
if(!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) { 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; return Options.merge(ItemSettings[name], ItemSettings.Default);
options = Options.merge(ItemSettings[name], options);
return options;
}; };
TiledLevel.prototype.getTileImagePath = function(gid) { TiledLevel.prototype.getTileImagePath = function(gid) {
@ -210,7 +208,7 @@ define([
return tileset.tiles["" + (gid - offset)].image; return tileset.tiles["" + (gid - offset)].image;
} }
} }
} };
return TiledLevel; return TiledLevel;
}); });

View file

@ -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;
});