implemented level restart

This commit is contained in:
Jeena 2014-01-12 04:33:08 +01:00
parent 81611050d2
commit 3edd664412
12 changed files with 104 additions and 47 deletions

View file

@ -4,35 +4,50 @@ define([
"Game/" + GLOBALS.context + "/Player"
],
function (Engine, Level, Player) {
function (PhysicsEngine, Level, Player) {
function GameController (physicsEngine) {
function GameController () {
this.players = {};
this.gameObjects = {
animated: [],
fixed: []
};
if (! physicsEngine instanceof Engine) {
throw physicsEngine + " is not of type Engine";
}
this.levelPath = null;
this.physicsEngine = physicsEngine;
this.physicsEngine = new PhysicsEngine();
this.physicsEngine.setCollisionDetector();
this.update();
}
GameController.prototype.update = function() {
};
GameController.prototype.getPhysicsEngine = function () {
return this.physicsEngine;
}
GameController.prototype.loadLevel = function (path) {
this.levelPath = path;
if (this.level) {
this.level.unload();
this.level.destroy();
this.gameObjects = {
animated: [],
fixed: []
};
}
this.level = new Level(path, this.physicsEngine, this.gameObjects);
this.level.loadLevelInToEngine();
}
GameController.prototype.onResetLevel = function() {
this.loadLevel(this.levelPath);
};
GameController.prototype.destroy = function () {
for(var player in this.players) {
this.players[player].destroy();
@ -53,6 +68,5 @@ function (Engine, Level, Player) {
return new Player(user.id, this.physicsEngine);
};
return GameController;
});

View file

@ -20,6 +20,8 @@ function (Box2D, Exception) {
GameObject.prototype.destroy = function() {
if(this.body instanceof Box2D.Dynamics.b2Body) {
this.body.GetWorld().DestroyBody(this.body);
} else {
throw new Exception("can not destroy body");
}
};

View file

@ -7,19 +7,19 @@ define([
function (Parent, Box2D, Settings) {
function Skateboard(physicsEngine, uid, options) {
this.physicsEngine = physicsEngine;
Parent.call(this, physicsEngine, uid, options);
this.addWheel(
options.x + 8,
options.y + 1.5
);
this.addWheel(
options.x - 8,
options.y + 1.5
);
this.wheels = [
this.addWheel(
options.x + 8,
options.y + 1.5
),
this.addWheel(
options.x - 8,
options.y + 1.5
)
];
}
Skateboard.prototype = Object.create(Parent.prototype);
@ -60,22 +60,23 @@ function (Parent, Box2D, Settings) {
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
var offset = 4,
factor = 80;
var density = ((this.options.weight + offset) / this.options.width / this.options.height) * factor;
var density = ((0.1 + offset) / 3 / 3) * factor;
fixtureDef.density = density;
fixtureDef.shape = wheelShape;
fixtureDef.isSensor = false;
var wheelBody = this.physicsEngine.getWorld().CreateBody(bodyDef);
var wheelBody = this.body.GetWorld().CreateBody(bodyDef);
wheelBody.CreateFixture(fixtureDef);
var revoluteJointDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef();
revoluteJointDef.enableMotor = false;
revoluteJointDef.Initialize(this.body, wheelBody, wheelBody.GetWorldCenter());
this.physicsEngine.getWorld().CreateJoint(revoluteJointDef);
this.body.GetWorld().CreateJoint(revoluteJointDef);
// FIXME this means, that we will have bodies in the world, which must not be
// updated (wheels) because they are always connected to a body which will be updated.
return wheelBody;
};
Skateboard.prototype.flip = function(direction) {
@ -83,6 +84,14 @@ function (Parent, Box2D, Settings) {
// FIXME: implement body flip if necessary
};
Skateboard.prototype.destroy = function() {
for (var i = 0; i < this.wheels.length; i++) {
this.body.GetWorld().DestroyBody(this.wheels[i]);
}
Parent.prototype.destroy.call(this);
};
return Skateboard;

View file

@ -22,9 +22,13 @@ define([
this.createItems();
}
Level.prototype.unload = function () {
// TODO unload level from engine if necessary
// Perhaps just remove all bodies?
Level.prototype.destroy = function () {
for (var key in this.gameObjects) {
for (var i = 0; i < this.gameObjects[key].length; i++) {
this.gameObjects[key][i].destroy();
}
//this.gameObjects[key] = [];
}
}
// Private
@ -235,7 +239,7 @@ microwave: 3.744
image:'skateboard.gif',
type:'skateboard',
category:'outdoor',
weight: 5,
weight: 1.5,
width:26,
height:6,
x:200,