Skip to content

Commit

Permalink
Continued rrefactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenanduong committed Mar 19, 2013
1 parent 3826b01 commit ea2ff05
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 15 deletions.
45 changes: 41 additions & 4 deletions BattleCityOnline/src/client/BattleCityGame.js
Expand Up @@ -5,6 +5,9 @@ define([
"frozen/box2d/BoxGame",
"frozen/box2d/RectangleEntity",

"bco-client/entity/BrickTile",
"bco-client/entity/ConcreteTile",

"frozen/plugins/loadImage!script/bco-client/image/bullet_0_up.png",
"frozen/plugins/loadImage!script/bco-client/image/bullet_0_down.png",
"frozen/plugins/loadImage!script/bco-client/image/bullet_0_left.png",
Expand Down Expand Up @@ -32,6 +35,9 @@ define([
BoxGame,
Rectangle,

BrickTile,
ConcreteTile,

bullet_0_up,
bullet_0_down,
bullet_0_left,
Expand Down Expand Up @@ -225,13 +231,21 @@ define([
this.box.resolveCollisions = true;
this.box.addContactListener({
beginContact: function (obj1, obj2) {
/*console.log(obj1, obj2)
if (obj1.indexOf("bullet_") === 0) {
console.log("Fired", arguments);
self.entities[obj1].state = "exploded";
if (obj2.indexOf("tile_") === 0) {
self.entities[obj2].state = "dead";
}
}*/

if (self.entities[obj1].beginContact) {
self.entities[obj1].beginContact(self.entities[obj2]);
}
if (self.entities[obj2].beginContact) {
self.entities[obj2].beginContact(self.entities[obj1]);
}
},
endContact: function () {
Expand All @@ -248,17 +262,31 @@ define([
row.forEach(function(cell, cellIndex) {
switch (cell) {
case 1: // brick
this._createTile(rowIndex, cellIndex, "brick", "l_t");
/*this._createTile(rowIndex, cellIndex, "brick", "l_t");
this._createTile(rowIndex, cellIndex, "brick", "r_t");
this._createTile(rowIndex, cellIndex, "brick", "r_b");
this._createTile(rowIndex, cellIndex, "brick", "l_b");
this._createTile(rowIndex, cellIndex, "brick", "l_b");*/

//TODO: TileFactory
this._addEntity(new BrickTile({
id: "tile_" + rowIndex + "_" + cellIndex,
x: cellIndex * 16 + 8,
y: rowIndex * 16 + 8
}));

break;
case 2: // concrete
this._createTile(rowIndex, cellIndex, "concrete", "l_t");
/*this._createTile(rowIndex, cellIndex, "concrete", "l_t");
this._createTile(rowIndex, cellIndex, "concrete", "r_t");
this._createTile(rowIndex, cellIndex, "concrete", "r_b");
this._createTile(rowIndex, cellIndex, "concrete", "l_b");
this._createTile(rowIndex, cellIndex, "concrete", "l_b");*/

//TODO: TileFactory
this._addEntity(new ConcreteTile({
id: "tile_" + rowIndex + "_" + cellIndex,
x: cellIndex * 16 + 8,
y: rowIndex * 16 + 8
}));

break;
case 3: // forrest
Expand All @@ -274,6 +302,15 @@ define([
this._createWalls();
},

_addEntity: function (entity) {
this.box.addBody(entity);

//shims
this.box.bodiesMap[entity.id].GetFixtureList().SetSensor(entity.isSensor);

this.entities[entity.id] = entity;
},

_createWalls: function () {
var topWall = new Rectangle({
id: "wall_top",
Expand Down
Empty file.
10 changes: 7 additions & 3 deletions BattleCityOnline/src/client/entity/BrickTile.js
@@ -1,13 +1,17 @@
define([
"dojo/_base/declare",

"bco-client/entity/Tile"
"bco-client/entity/Tile",

"frozen/plugins/loadImage!script/bco-client/image/brick.png"
], function (
declare,

Tile) {
Tile,

return declare([Tile], {
brickTileImage) {

return declare([Tile], {
tileImage: brickTileImage
});
});
10 changes: 7 additions & 3 deletions BattleCityOnline/src/client/entity/ConcreteTile.js
@@ -1,13 +1,17 @@
define([
"dojo/_base/declare",

"bco-client/entity/Tile"
"bco-client/entity/Tile",

"frozen/plugins/loadImage!script/bco-client/image/concrete.png"
], function (
declare,

Tile) {
Tile,

return declare([Tile], {
concreteTileImage) {

return declare([Tile], {
tileImage: concreteTileImage
});
});
29 changes: 24 additions & 5 deletions BattleCityOnline/src/client/entity/Tile.js
@@ -1,21 +1,40 @@
define([
"dojo/_base/declare",

"frozen/box2d/RectangleEntity"
"frozen/box2d/RectangleEntity",

"bco-client/entity/_ContactAwareMixin",
"bco-client/entity/_NonBulletMixin",
], function (
declare,

RectangleEntity) {
RectangleEntity,

_ContactAwareMixin,
_NonBulletMixin) {

return declare([RectangleEntity], {
halfWidth: 4,
halfHeight: 4,
return declare([RectangleEntity, _ContactAwareMixin, _NonBulletMixin], {
halfWidth: 8,
halfHeight: 8,

restitution: 0,
friction: 0,
isSensor: true,

staticBody: true,

tileImage: null,

draw: function (ctx) {
ctx.save();

ctx.drawImage(
this.tileImage,
(this.x-this.halfWidth) * this.scale,
(this.y-this.halfHeight) * this.scale
);

ctx.restore();
}
});
});
19 changes: 19 additions & 0 deletions BattleCityOnline/src/client/entity/_ContactAwareMixin.js
@@ -0,0 +1,19 @@
define([
"dojo/_base/declare"
], function (
declare) {

return declare([], {
beginContact: function (aa, bb) {

},

endContact: function (aa, bb) {

},

postSolve: function (aa, bb) {

}
});
});
15 changes: 15 additions & 0 deletions BattleCityOnline/src/client/entity/_NonBulletMixin.js
@@ -0,0 +1,15 @@
define([
"dojo/_base/declare"
], function (
declare) {

return declare([], {
beginContact: function (other) {
console.log(other);
},

onHit: function () {

}
});
});

0 comments on commit ea2ff05

Please sign in to comment.