first attempt to implement GameObject

This commit is contained in:
Jeena 2013-12-19 15:16:15 +01:00
parent aa30a23ca1
commit fe0d4a66e2
14 changed files with 328 additions and 129 deletions

View file

@ -8,7 +8,11 @@ function (Engine, Level, Player) {
function GameController (physicsEngine) {
this.players = {};
this.gameObjects = {
animated: [],
fixed: []
};
if (! physicsEngine instanceof Engine) {
throw physicsEngine + " is not of type Engine";
}
@ -25,7 +29,7 @@ function (Engine, Level, Player) {
this.level.unload();
}
this.level = new Level(path, this.physicsEngine);
this.level = new Level(path, this.physicsEngine, this.gameObjects);
this.level.loadLevelInToEngine();
}

View file

@ -1,16 +1,25 @@
define([
"Lib/Vendor/Box2D",
"Lib/Utilities/Exception"
],
function() {
function (Box2D, Exception) {
function GameObject() {
this.body = null;
}
GameObject.prototype.render = function() {
return null;
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;
};

View file

@ -0,0 +1,29 @@
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;
});

View file

@ -0,0 +1,122 @@
define([
"Game/" + GLOBALS.context + "/GameObjects/GameObject",
"Lib/Vendor/Box2D",
"Game/Config/Settings",
"Game/" + GLOBALS.context + "/Collision/Detector"
],
function (Parent, Box2D, Settings, CollisionDetector) {
function Tile(physicsEngine, options) {
this.options = options;
Parent.call(this, physicsEngine);
this.createPhysicTile(this.options);
}
Tile.prototype = Object.create(Parent.prototype);
Tile.prototype.getBodyDef = function() {
var bodyDef = new Box2D.Dynamics.b2BodyDef();
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;
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();
fixtureDef.shape = tileShape;
fixtureDef.density = 0;
fixtureDef.friction = Settings.TILE_FRICTION;
fixtureDef.restitution = Settings.TILE_RESTITUTION;
fixtureDef.isSensor = false;
fixtureDef.userData = CollisionDetector.IDENTIFIER.TILE;
this.body.CreateFixture(fixtureDef);
}
Tile.prototype.createVertices = function (tile) {
var vs = [];
switch(tile.s) {
case 1:
this.addVec(vs, -1, -1); // o o o
this.addVec(vs, 1, -1); // o o o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1);
break;
case 2:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 1, 1); // o o
this.addVec(vs, -1, 1); // o o o
break;
case 3:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 0, 1); // o
this.addVec(vs, -1, 1); // o o
break;
case 4:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 1, 0); // o o o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1);
break;
case 5:
this.addVec(vs, 1, -1); // o
this.addVec(vs, 1, 1); // o
this.addVec(vs, 0, 1); // o o
break;
case 6:
this.addVec(vs, 1, -1); // o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1); // o o o
this.addVec(vs, -1, 0);
break;
case 7:
this.addVec(vs, -1, 0); //
this.addVec(vs, 0, 1); // o
this.addVec(vs, -1, 1); // o o
break;
case 8:
this.addVec(vs, -1, -1); // o o
this.addVec(vs, 0, -1); // o o o
this.addVec(vs, 1, 0); // o o o
this.addVec(vs, 1, 1);
this.addVec(vs, -1, 1);
break;
default:
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)));
}
return Tile;
});

View file

@ -1,20 +1,23 @@
define([
"Game/Config/Settings",
"Lib/Vendor/Box2D",
"Game/" + GLOBALS.context + "/Collision/Detector"
"Game/" + GLOBALS.context + "/Collision/Detector",
"Game/" + GLOBALS.context + "/GameObjects/Tile"
], function (Settings, Box2D, CollisionDetector) {
], function (Settings, Box2D, CollisionDetector, Tile) {
// Public
function Level (path, engine) {
function Level (path, engine, gameObjects, view) {
this.path = path;
this.engine = engine;
this.levelObject = null;
this.gameObjects = gameObjects;
this.view = view;
}
Level.prototype.loadLevelInToEngine = function () {
this.loadLevelObjectFromPath(this.path);
this.createPhysicTiles();
this.createTiles();
}
Level.prototype.unload = function () {
@ -24,114 +27,17 @@ define([
// Private
Level.prototype.createPhysicTiles = function () {
Level.prototype.createTiles = function () {
if (!this.levelObject || !this.levelObject.tiles || this.levelObject.tiles.length < 1) {
throw "Level: Can't create physic tiles, no tiles found";
}
var tiles = this.levelObject.tiles;
for (var i = tiles.length - 1; i >= 0; i--) {
this.createPhysicTile(tiles[i]);
this.gameObjects.fixed.push(new Tile(this.engine, tiles[i]));
}
}
Level.prototype.createPhysicTile = function (tile) {
tile.r = tile.r || 0;
var vertices = this.createVertices(tile);
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
bodyDef.position.x = tile.x * Settings.TILE_SIZE / Settings.RATIO;
bodyDef.position.y = tile.y * Settings.TILE_SIZE / Settings.RATIO;
bodyDef.angle = tile.r * 90 * Math.PI / 180;
var tileShape = new Box2D.Collision.Shapes.b2PolygonShape();
tileShape.SetAsArray(vertices, vertices.length);
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
fixtureDef.shape = tileShape;
fixtureDef.density = 0;
fixtureDef.friction = Settings.TILE_FRICTION;
fixtureDef.restitution = Settings.TILE_RESTITUTION;
fixtureDef.isSensor = false;
fixtureDef.userData = CollisionDetector.IDENTIFIER.TILE;
this.engine.createBody(bodyDef).CreateFixture(fixtureDef);
}
Level.prototype.createVertices = function (tile) {
var vs = [];
switch(tile.s) {
case 1:
this.addVec(vs, -1, -1); // o o o
this.addVec(vs, 1, -1); // o o o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1);
break;
case 2:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 1, 1); // o o
this.addVec(vs, -1, 1); // o o o
break;
case 3:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 0, 1); // o
this.addVec(vs, -1, 1); // o o
break;
case 4:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 1, 0); // o o o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1);
break;
case 5:
this.addVec(vs, 1, -1); // o
this.addVec(vs, 1, 1); // o
this.addVec(vs, 0, 1); // o o
break;
case 6:
this.addVec(vs, 1, -1); // o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1); // o o o
this.addVec(vs, -1, 0);
break;
case 7:
this.addVec(vs, -1, 0); //
this.addVec(vs, 0, 1); // o
this.addVec(vs, -1, 1); // o o
break;
case 8:
this.addVec(vs, -1, -1); // o o
this.addVec(vs, 0, -1); // o o o
this.addVec(vs, 1, 0); // o o o
this.addVec(vs, 1, 1);
this.addVec(vs, -1, 1);
break;
default:
break;
}
return vs;
}
Level.prototype.mkArg = function (multiplier) {
return Settings.TILE_SIZE / 2 / Settings.RATIO * multiplier;
}
Level.prototype.addVec = function (vs, m1, m2) {
return vs.push(new Box2D.Common.Math.b2Vec2(this.mkArg(m1), this.mkArg(m2)));
}
Level.prototype.loadLevelObjectFromPath = function (path) {
// TODO: load JSON levelObject from path