-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.js
112 lines (96 loc) · 2.9 KB
/
Engine.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
const Food = require('./Food')
class Engine{
constructor(){
// this.canvas = new Canvas(500, 500);
// this.canvas.init();
this.gameObjects = [];
this.cells = [];
this.iterations = 1;
this.removedIndexes = [];
this.rendering = true;
}
addObj(obj, tmp = false){
// if(tmp == false)return;
// if(this.gameObjects.length > 3000) return;
// this.gameObjects.push(obj);
// this.gameObjects[this.gameObjects.length - 1].indx = this.gameObjects.length - 1;
for(var i =0; i < this.gameObjects.length; i++){
if(this.gameObjects[i] == undefined){
this.gameObjects[i] =obj;
this.gameObjects[i].indx = i;
return i;
}
}
this.gameObjects.push(obj);
this.gameObjects[this.gameObjects.length - 1].indx = this.gameObjects.length - 1;
return this.gameObjects.length -1;
}
addCell(obj){
for(var i =0; i < this.cells.length; i++){
if(this.cells[i] == undefined){
this.cells[i] =obj;
this.cells[i].indx = i;
return i;
}
}
this.cells.push(obj);
this.cells[this.cells.length - 1].indx = this.cells.length - 1;
return this.cells.length -1;
}
removeCell(indx){
if(this.cells[indx]){
this.cells[indx].onRemove();
delete this.cells[indx];
}else{
// console.log('not removed');
}
}
removeObject(indx){
if(!this.gameObjects[indx]){
console.log('not removed',indx);
return;
}
delete this.gameObjects[indx];
// this.gameObjects[indx].onRemove();
// delete this.gameObjects[indx];
// this.removedIndexes.push(indx);
// this.gameObjects = this.gameObjects.filter(function (el) {
// return el != undefined || null;
// });
}
feed(indx, food_index){
if(this.cells[indx]){
if(this.gameObjects[food_index] && this.cells[indx].feed()){
this.removeObject(food_index);
}
}
}
update() {
for(var x in this.gameObjects){
if(this.gameObjects[x])
this.gameObjects[x].update();
}
for(var x in this.cells){
this.cells[x].update();
}
}
render() {
this.canvas.clr();
for(var x in this.gameObjects){
if(this.gameObjects[x])
this.gameObjects[x].render();
}
for(var x in this.cells){
this.cells[x].render();
}
}
addFood(x, y){
this.addObj(new Food(x, y));
}
main(){
for(var x = 0; x < this.iterations; x++){
this.update();
}
}
}
module.exports = Engine;