-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredator.js
76 lines (76 loc) · 2.81 KB
/
predator.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
var Main = require("./class.main.js");
module.exports = class predator extends Main{
findSlots(identifier) {
this.directions = [
[this.x - 1, this.y - 1],
[this.x, this.y - 1],
[this.x + 1, this.y - 1],
[this.x - 1, this.y],
[this.x + 1, this.y],
[this.x - 1, this.y + 1],
[this.x, this.y + 1],
[this.x + 1, this.y + 1]
];
var found = [];
for (var i in this.directions) {
var x = this.directions[i][0];
var y = this.directions[i][1];
if (x >= 0 && x < matrix[0].length && y >= 0 && y < matrix.length) {
if (matrix[y][x] == identifier) {
found.push(this.directions[i]);
}
}
}
return found;
}
moveAndEat() {
if(global.weather != "winter"){ // in winter they sleep untill the spring comes
var aviableSlots = this.findSlots(2).concat(this.findSlots(2.5)); //eat function part --> grassEater detected
if (aviableSlots[0]) {
var slot = random(aviableSlots);
matrix[this.y][this.x] = 0;
this.x = slot[0];
this.y = slot[1];
this.energy = 5;
matrix[slot[1]][slot[0]] = 3;
for (var i = 0; i < global.grassEaterArray.length; i++) {
if (slot[0] == global.grassEaterArray[i].x && slot[1] == global.grassEaterArray[i].y) {
global.grassEaterArray.splice(i, 1);
break;
}
}
}
else { //move function part --> no grassEater detected
var slot = random(this.findSlots(0).concat(this.findSlots(1)));
if (slot) {
if(matrix[slot[1]][slot[0]] == 1){
for (var i = 0; i < global.grassArray.length; i++) {
if (slot[0] == global.grassArray[i].x && slot[1] == global.grassArray[i].y) {
global.grassArray.splice(i, 1);
break;
}
}
}
matrix[this.y][this.x] = 0;
this.x = slot[0];
this.y = slot[1];
matrix[slot[1]][slot[0]] = 3;
}
this.death();
}
}
}
death() {
if (this.energy <= -10) {
for (var i = 0; i < global.predatorArray.length; i++) {
if (global.predatorArray[i].x == this.x && global.predatorArray[i].y == this.y) {
global.predatorArray.splice(i, 1);
break;
}
}
matrix[this.y][this.x] = 0;
}
this.energy--;
}
}
var random = require("./random.js");