-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredator.js
More file actions
91 lines (84 loc) · 2.56 KB
/
Predator.js
File metadata and controls
91 lines (84 loc) · 2.56 KB
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
var LivingCreuture = require("./LivingCreuture.js")
module.exports = class Predator extends LivingCreuture{
constructor(x, y) {
super(x,y)
this.energy = 30;
}
getNewDirections() {
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]
];
}
mul() {
let chooseCells = this.chooseCell(0);
let newCell = chooseCells[Math.floor(Math.random() * chooseCells.length)]
if (newCell) {
let x = newCell[0];
let y = newCell[1];
matrix[y][x] = 3;
let predator = new Predator(x, y);
predatorArr.push(predator);
this.energy = 0;
predatorHashiv++
}
}
die() {
matrix[this.y][this.x] = 0;
for (let index = 0; index < predatorArr.length; index++) {
if (predatorArr[index].x == this.x && predatorArr[index].y == this.y) {
predatorArr.splice(index, 1)
predatorHashiv--
}
}
}
eat() {
this.getNewDirections();
let chooseCells = this.chooseCell(2);
let newCell = chooseCells[Math.floor(Math.random() * chooseCells.length)]
if (newCell) {
this.energy += 20;
let x = newCell[0];
let y = newCell[1];
matrix[y][x] = 3;
matrix[this.y][this.x] = 0;
this.y = y;
this.x = x;
for (let index = 0; index < grassEaterArr.length; index++) {
if (grassEaterArr[index].x == x && grassEaterArr[index].y == y) {
grassEaterArr.splice(index, 1)
grassEaterHashiv--
}
}
if (this.energy > 60) {
this.mul()
}
}
else { this.move() }
}
move() {
this.energy--;
let chooseCells = this.chooseCell(0);
let newCell = chooseCells[Math.floor(Math.random() * chooseCells.length)]
if (newCell) {
let x = newCell[0];
let y = newCell[1];
matrix[y][x] = 3;
matrix[this.y][this.x] = 0;
this.y = y;
this.x = x;
}
if (newCell && this.energy < 0) {
this.die();
}
if (this.energy < 0) {
this.die();
}
}
}