-
Notifications
You must be signed in to change notification settings - Fork 1
/
entityManager.js
114 lines (88 loc) · 2.63 KB
/
entityManager.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"use strict";
var entityManager = {
// "PRIVATE" DATA
_entities : [],
// "PRIVATE" METHODS
// PUBLIC METHODS
// A special return value, used by other objects,
// to request the blessed release of death!
//
KILL_ME_NOW : -1,
spawnBallInterval : 200,
spawnBallCountDown : 100,
// Some things must be deferred until after initial construction
// i.e. thing which need `this` to be defined.
//
init: function() {
this.generateTebert({"loc":[0,0.5,0,1]});
this.disableEnemies = false;
},
generateBall : function(descr) {
if(this.disableEnemies) return;
var bd = {"loc":[0,0.5,0,1]};
var b = new Ball(bd);
this._entities.push(b);
},
generateSam : function(descr) {
if(this.disableEnemies) return;
var s = new Sam({"loc":[0,0.5,0,1], "color": [0.0,1.0,0.0,0.0]});
s.move([-1,1],true);
this._entities.push(s);
},
generateSnake : function(descr) {
if(this.disableEnemies) return;
var s = new Snake({"loc":[0,0.0,0,1]});
s.move([2,1],true);
this._entities.push(s);
},
generateTebert : function(descr) {
var t = new Tebert(descr);
this._entities.push(t);
},
getTebert : function(){
var t = this._entities[0];
return t;
},
checkCollisions : function() {
var tebert = this.getTebert();
for (var i = 0; i < this._entities.length; i++) {
var entity = this._entities[i];
if (entity.isDeadly
&& Math.round(entity.loc[0]) === Math.round(tebert.loc[0])
&& Math.round(entity.loc[2]) === Math.round(tebert.loc[2]))
tebert.kill();
}
},
killOutOfBounds : function(x,y) {
for (var i = 0; i < this._entities.length; i++) {
var entity = this._entities[i];
if (!entity.loc)
console.log(entity);
if (pyramid.isOutOfBounds(entity.loc[0], entity.loc[2]))
entity.kill();
}
},
update: function(du) {
var i = 0;
while (i < this._entities.length) {
var status = this._entities[i].update(du);
if (status === this.KILL_ME_NOW) {
// remove the dead guy, and shuffle the others down to
// prevent a confusing gap from appearing in the array
this._entities.splice(i,1);
}
else {
++i;
}
}
this.spawnBallCountDown -= du;
if (this.spawnBallCountDown < 0) {
this.spawnBallCountDown = this.spawnBallInterval;
this.generateBall();
}
},
render: function(gl,transformMatrix) {
for (var i = 0; i < this._entities.length; i++)
this._entities[i].render(gl,transformMatrix);
}
};