Skip to content

Commit cfb9ddc

Browse files
author
jamie nichols
committed
Created the Entity Manager class and implimented it
1 parent 6a76fa6 commit cfb9ddc

File tree

6 files changed

+164
-89
lines changed

6 files changed

+164
-89
lines changed

.idea/workspace.xml

Lines changed: 105 additions & 78 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ requirejs.config({
2929
"DirtTile":"app/classes/tiles/DirtTile",
3030
"Display":"app/classes/display/Display",
3131
"Entity":"app/classes/entities/Entity",
32+
"EntityManager":"app/classes/entities/EntityManager",
3233
"Game":"app/classes/Game",
3334
"GameCamera":"app/classes/gfx/GameCamera",
3435
"GameState":"app/classes/states/GameState",

js/app/classes/States/GameState.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,18 @@
99
* jamie337nichols
1010
1111
*/
12-
define(['State','Player','World','Tile'],function(State,Player,World,Tile){
13-
var ex = 28*Tile.TILEWIDTH;
14-
var ey = 28*Tile.TILEHEIGHT;
12+
define(['State','World'],function(State,World){
1513

1614
var GameState = State.extend({
1715
init:function(_handler){
1816
this._super(_handler);
19-
this.player = new Player(_handler,43,43);
2017
this.world = new World("res/worlds/world1.wrd",_handler);
21-
this.player.setX(this.world.spawnX);
22-
this.player.setY(this.world.spawnY);
2318
},
2419
tick:function(_dt){
2520
this.world.tick(_dt);
26-
this.player.tick(_dt);
2721
},
2822
render:function(_g){
2923
this.world.render(_g);
30-
this.player.render(_g);
3124
}
3225
});
3326

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
define(['Class'],function(Class){
2+
3+
var handler,player,entities;
4+
5+
var EntityManager = Class.extend({
6+
init:function(_handler,_player){
7+
handler = _handler;
8+
player = _player;
9+
entities = new Array(player);
10+
},
11+
tick:function(_dt){
12+
entities.sort(compare);
13+
for(var i = 0; i<entities.length;i++){
14+
var e = entities[i];
15+
e.tick(_dt);
16+
}
17+
},
18+
render:function(_g){
19+
entities.forEach(function(e){
20+
e.render(_g);
21+
});
22+
},
23+
//Getters
24+
getPlayer:function(){
25+
return player;
26+
},
27+
getHandler:function(){
28+
return handler;
29+
},
30+
getEntities:function(){
31+
return entities;
32+
},
33+
//Setters
34+
addEntity:function(e){
35+
entities.push(e);
36+
}
37+
});
38+
39+
40+
function compare(a,b){
41+
if(a.getY()+ a.getHeight()< b.getY()+ b.getHeight())return -1;
42+
return 1;
43+
}
44+
45+
return EntityManager;
46+
});

js/app/classes/worlds/World.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,22 @@
99
* jamie337nichols
1010
1111
*/
12-
define(['Class','TileLoader','Utils',],function(Class,Tile,Utils){
12+
define(['Class','TileLoader','Utils','EntityManager','Player','Tree'],function(Class,Tile,Utils,EntityManager,Player,Tree){
1313

1414

1515
var World = Class.extend({
1616
init:function(_path,_handler){
1717
this.tiles = [];
1818
this.handler = _handler;
1919
_handler.setWorld(this);
20+
this.entityManager = new EntityManager(_handler,new Player(_handler,100,100));
21+
this.entityManager.addEntity(new Tree(_handler,100,400));
22+
this.entityManager.addEntity(new Tree(_handler,200,500));
23+
this.entityManager.addEntity(new Tree(_handler,200,450));
24+
this.entityManager.addEntity(new Tree(_handler,300,700));
2025
this.loadWorld(_path);
26+
this.entityManager.getPlayer().setX(this.spawnX);
27+
this.entityManager.getPlayer().setY(this.spawnY);
2128
},
2229
loadWorld:function(_path){
2330
var file = Utils.loadFileAsString(_path);
@@ -35,6 +42,7 @@ define(['Class','TileLoader','Utils',],function(Class,Tile,Utils){
3542
}
3643
},
3744
tick:function(_dt) {
45+
this.entityManager.tick(_dt);
3846
},
3947
render:function(_g){
4048
var xStart = parseInt(Math.max(0,
@@ -52,7 +60,7 @@ define(['Class','TileLoader','Utils',],function(Class,Tile,Utils){
5260
this.getTile(x,y).render(_g,x * Tile.TILEWIDTH - this.handler.getGameCamera().getxOffset(),y * Tile.TILEHEIGHT - this.handler.getGameCamera().getyOffset());
5361
}
5462
}
55-
63+
this.entityManager.render(_g);
5664
},
5765
getTile:function(_x,_y){
5866
return Tile.tiles[this.tiles[_x][_y]];

res/worlds/world1.wrd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
30 30
2-
2 0
2+
3 5
33
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
44
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
55
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2

0 commit comments

Comments
 (0)