-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrassEater.js
More file actions
109 lines (101 loc) · 3.06 KB
/
GrassEater.js
File metadata and controls
109 lines (101 loc) · 3.06 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var LivingCreuture = require("./LivingCreuture.js")
module.exports = class GrassEater 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] = 2;
let grassEater = new GrassEater(x, y);
grassEaterArr.push(grassEater);
grassEaterHashiv++
this.energy = 0;
}
}
die() {
matrix[this.y][this.x] = 0;
for (let index = 0; index < grassEaterArr.length; index++) {
if (grassEaterArr[index].x == this.x && grassEaterArr[index].y == this.y) {
grassEaterArr.splice(index, 1)
grassEaterHashiv--
}
}
}
eat() {
this.getNewDirections();
let chooseCells = this.chooseCell(1);
let newCell = chooseCells[Math.floor(Math.random() * chooseCells.length)]
if (newCell) {
this.energy += 5;
let x = newCell[0];
let y = newCell[1];
matrix[y][x] = 2;
matrix[this.y][this.x] = 0;
this.y = y;
this.x = x;
for (let index = 0; index < grassArr.length; index++) {
if (grassArr[index].x == x && grassArr[index].y == y) {
grassArr.splice(index, 1)
grassHashiv--
}
}
if (weather == "Spring") {
if (this.energy > 40) {
this.mul()
}
}
else if (weather == "Summer") {
if (this.energy > 20) {
this.mul()
}
}
else if (weather == "Winter") {
if (this.energy > 100) {
this.mul()
}
}
else if (weather == "Autumn") {
if (this.energy > 50) {
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] = 2;
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();
}
}
}