-
Notifications
You must be signed in to change notification settings - Fork 0
/
wall.js
58 lines (50 loc) · 1.62 KB
/
wall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
export class Wall {
constructor (type, x, y, length, width, colour, id, room, entityToDependOn){
this.type = type;
this.x = x;
this.y = y;
this.xSpeed = 0;
this.ySpeed = 0;
this.xAccel = 2;
this.yAccel = 2;
this.length = length;
this.width = width;
this.colour = colour;
this.id = id;
this.room = room;
this.entityToDependOn = entityToDependOn;
}
draw(){
fill(this.colour);
rect(this.x - xRange + shake.x, this.y - yRange + shake.y, this.length, this.width);
}
checkExist(entities, walls){
if (this.room != null && this.entityToDependOn != null){
var exist = false;
var wall = this
Object.keys(entities).forEach(function(id) {
if (entities[id].name == wall.entityToDependOn && wall.rectRectDetect(entities[id], wall.room)){
exist = true;
}
});
if (!exist){
walls.splice(walls.indexOf(this), 1);
}
}
}
update(){
this.x += this.xSpeed;
this.y += this.ySpeed;
}
rectRectDetect(rect, rect2){
var leftSide = rect.x;
var rightSide = rect.x + rect.length;
var topSide = rect.y;
var botSide = rect.y + rect.width;
if (rect2.x + rect2.length > leftSide && rect2.x < rightSide && rect2.y + rect2.width> topSide && rect2.y < botSide){
return true;
} else {
return false;
}
}
}