Skip to content

Commit 8457e72

Browse files
author
jamie nichols
committed
Implimented collision detection on all entities
1 parent cfb9ddc commit 8457e72

File tree

7 files changed

+170
-76
lines changed

7 files changed

+170
-76
lines changed

.idea/workspace.xml

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

js/app/classes/entities/Entity.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,23 @@ define(['Class','Rectangle'],function(Class,Rectangle){
4141
getHeight:function(){
4242
return this.height;
4343
},
44-
44+
getCollisionBounds:function(xOffset,yOffset){
45+
return new Rectangle(parseInt(this.x+this.bounds.x + xOffset),
46+
parseInt(this.y+this.bounds.y+ yOffset),
47+
this.bounds.width,this.bounds.height);
48+
},
49+
checkEntityCollisions:function(xOffset,yOffset){
50+
var candidates = this.handler.getWorld().getEntityManager().getEntities();
51+
for(var i=0;i<candidates.length;i++){
52+
var e = candidates[i];
53+
if(e!=this){
54+
if(e.getCollisionBounds(0,0).intersects(this.getCollisionBounds(xOffset,yOffset))){
55+
return true;
56+
}
57+
}
58+
}
59+
return false;
60+
},
4561
//Setters
4662
setX:function(_x){
4763
this.x = _x;

js/app/classes/entities/creatures/Creature.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ define(['Entity',"Tile"],function(Entity,Tile){
2525
this.yMove = 0;
2626
},
2727
move:function(){
28-
this.moveX();
29-
this.moveY();
28+
if(!this.checkEntityCollisions(this.xMove,0))
29+
this.moveX();
30+
if(!this.checkEntityCollisions(0,this.yMove))
31+
this.moveY();
3032
},
3133
moveX:function(){
3234
if(this.xMove>0){

js/app/classes/entities/statics/Tree.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ define(['StaticEntity','Assets','Tile'],function(StaticEntity,Assets,Tile){
33
var Tree = StaticEntity.extend({
44
init:function(_handler,_x,_y){
55
this._super(_handler,_x,_y,Tile.TILEWIDTH * 5,Tile.TILEHEIGHT*5);
6-
this.bounds.x = 65;
7-
this.bounds.y = 52;
8-
this.bounds.width = this.bounds.height = 30;
6+
this.bounds.x = 45;
7+
this.bounds.y = 135;
8+
this.bounds.width = 60;
9+
this.bounds.height = 5;
910
},
1011
tick:function(){},
1112
render:function(_g){

js/app/classes/gfx/shapes/Rectangle.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,18 @@ define(['Class'],function(Class){
1616
this.y = _y;
1717
this.width = _width;
1818
this.height = _height;
19+
},
20+
intersects:function(_rect){
21+
if( this.x < _rect.x + _rect.width &&
22+
this.x + this.width > _rect.x &&
23+
this.y < _rect.y + _rect.height &&
24+
this.y + this.height> _rect.y){
25+
return true;
26+
}
27+
return false;
1928
}
2029
});
2130

31+
2232
return Rectangle;
2333
});

js/app/classes/worlds/World.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ define(['Class','TileLoader','Utils','EntityManager','Player','Tree'],function(C
1919
_handler.setWorld(this);
2020
this.entityManager = new EntityManager(_handler,new Player(_handler,100,100));
2121
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));
22+
this.entityManager.addEntity(new Tree(_handler,300,400));
2523
this.loadWorld(_path);
2624
this.entityManager.getPlayer().setX(this.spawnX);
2725
this.entityManager.getPlayer().setY(this.spawnY);
@@ -70,6 +68,9 @@ define(['Class','TileLoader','Utils','EntityManager','Player','Tree'],function(C
7068
},
7169
getHeight:function(){
7270
return this.height;
71+
},
72+
getEntityManager:function(){
73+
return this.entityManager;
7374
}
7475
});
7576

res/worlds/world1.wrd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2
1212
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2
1313
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2
14-
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2
14+
2 0 0 0 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2
1515
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2
1616
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2
1717
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2

0 commit comments

Comments
 (0)