Skip to content

Commit 943834f

Browse files
committed
Fin preparations
1 parent 3ae3af2 commit 943834f

File tree

2 files changed

+226
-0
lines changed

2 files changed

+226
-0
lines changed

public/class.human.js

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
class human{
2+
constructor(x, y){
3+
this.x = x;
4+
this.y = y;
5+
this.directions = [];
6+
this.count = 180;
7+
}
8+
findSlots(identifier){
9+
this.directions = [
10+
[this.x - 1, this.y - 1],
11+
[this.x, this.y - 1],
12+
[this.x + 1, this.y - 1],
13+
[this.x - 1, this.y],
14+
[this.x + 1, this.y],
15+
[this.x - 1, this.y + 1],
16+
[this.x, this.y + 1],
17+
[this.x + 1, this.y + 1]
18+
];
19+
var found = [];
20+
for(var i in this.directions) {
21+
var x = this.directions[i][0];
22+
var y = this.directions[i][1];
23+
if (x >= 0 && x < matrix[0].length && y >= 0 && y < matrix.length) {
24+
if (matrix[y][x] != identifier && matrix[y][x] != 4) { // everything except --> human(himself) and fire
25+
found.push(this.directions[i]);
26+
}
27+
}
28+
}
29+
return found;
30+
}
31+
move(){
32+
this.count++;
33+
var slot = random(this.findSlots(7));
34+
if(slot){
35+
if(matrix[slot[1]][slot[0]] == 1){ // is grass -- remove
36+
for(var i = 0; i < grassArray.length; i++) {
37+
if(slot[0] == grassArray[i].x && slot[1] == grassArray[i].y) {
38+
grassArray.splice(i, 1);
39+
break;
40+
}
41+
}
42+
}
43+
else if(matrix[slot[1]][slot[0]] == 2){ // is grassEater -- remove
44+
for(var i = 0; i < grassEaterArray.length; i++) {
45+
if(slot[0] == grassEaterArray[i].x && slot[1] == grassEaterArray[i].y) {
46+
grassEaterArray.splice(i, 1);
47+
break;
48+
}
49+
}
50+
}
51+
else if(matrix[slot[1]][slot[0]] == 3){ // is predator -- remove
52+
for(var i = 0; i < predatorArray.length; i++) {
53+
if(slot[0] == predatorArray[i].x && slot[1] == predatorArray[i].y) {
54+
predatorArray.splice(i, 1);
55+
break;
56+
}
57+
}
58+
}
59+
matrix[this.y][this.x] = 0;
60+
matrix[slot[1]][slot[0]] = 7;
61+
this.x = slot[0];
62+
this.y = slot[1];
63+
}
64+
this.shoot();
65+
this.balance();
66+
}
67+
findDiagonals1(){
68+
var diagonals = [];
69+
for(var x=this.x,y=this.y;x>=0,y>=0;x--,y--){ // depi nerqev -- arajin ankyunagic
70+
var Dslot = [x, y];
71+
diagonals.push(Dslot);
72+
}
73+
for(x=this.x,y=this.y;x<matrix[0].length,y<matrix.length;x++,y++){ // depi verev -- arajin ankyunagic
74+
var Dslot = [x, y];
75+
diagonals.push(Dslot);
76+
}
77+
return diagonals;
78+
}
79+
findDiagonals2(){
80+
var diagonals = [];
81+
for(var x=this.x,y=this.y;x<matrix[0].length,y>=0;y--,x++){ // depi verev -- erkrord ankyunagic
82+
var Dslot = [x, y];
83+
diagonals.push(Dslot);
84+
}
85+
for(x=this.x,y=this.y;x>=0,y<matrix.length;y++,x--){ // depi nerqev -- arajin ankyunagic
86+
var Dslot = [x, y];
87+
diagonals.push(Dslot);
88+
}
89+
return diagonals;
90+
}
91+
shoot(){
92+
var slots = this.findDiagonals1().concat(this.findDiagonals2()); // merge diagonal slots arrays
93+
for(var e in slots){
94+
if(matrix[slots[e][1]][slots[e][0]] == 3){
95+
for(var i = 0; i < predatorArray.length; i++) {
96+
if(slots[e][0] == predatorArray[i].x && slots[e][1] == predatorArray[i].y) {
97+
predatorArray.splice(i, 1);
98+
break;
99+
}
100+
}
101+
matrix[this.y][this.x] = 0;
102+
matrix[slots[e][1]][slots[e][0]] = 7;
103+
this.x = slots[e][0];
104+
this.y = slots[e][1];
105+
}
106+
}
107+
}
108+
balance(){
109+
if(grassArray.length >= (matrix[0].length*matrix.length)*50/100 && this.count >= 10){
110+
var x = parseInt(random(0, matrix[0].length - 1)); // get random coordinates to place the fire
111+
var y = parseInt(random(0, matrix.length - 1));
112+
var slot = [x, y];
113+
var pajar = new fire(slot[0], slot[1]);
114+
if(matrix[slot[1]][slot[0]] == 1){ // is grass -- remove
115+
for(var i = 0; i < grassArray.length; i++) {
116+
if(slot[0] == grassArray[i].x && slot[1] == grassArray[i].y) {
117+
grassArray.splice(i, 1);
118+
break;
119+
}
120+
}
121+
}
122+
else if(matrix[slot[1]][slot[0]] == 2){ // is grassEater -- remove
123+
for(var i = 0; i < grassEaterArray.length; i++) {
124+
if(slot[0] == grassEaterArray[i].x && slot[1] == grassEaterArray[i].y) {
125+
grassEaterArray.splice(i, 1);
126+
break;
127+
}
128+
}
129+
}
130+
else if(matrix[slot[1]][slot[0]] == 3){ // is predator -- remove
131+
for(var i = 0; i < predatorArray.length; i++) {
132+
if(slot[0] == predatorArray[i].x && slot[1] == predatorArray[i].y) {
133+
predatorArray.splice(i, 1);
134+
break;
135+
}
136+
}
137+
}
138+
matrix[slot[1]][slot[0]] = 4;
139+
fireArray.push(pajar);
140+
this.count = 0;
141+
}
142+
}
143+
}

public/class.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
class Creature{
2+
constructor(x, y, index){
3+
this.x = x;
4+
this.y = y;
5+
this.count = 0;
6+
this.energy = 5;
7+
this.directions = [];
8+
}
9+
findSlots(identifier) {
10+
this.directions = [
11+
[this.x - 1, this.y - 1],
12+
[this.x, this.y - 1],
13+
[this.x + 1, this.y - 1],
14+
[this.x - 1, this.y],
15+
[this.x + 1, this.y],
16+
[this.x - 1, this.y + 1],
17+
[this.x, this.y + 1],
18+
[this.x + 1, this.y + 1]
19+
];
20+
var found = [];
21+
for (var i in this.directions) {
22+
var x = this.directions[i][0];
23+
var y = this.directions[i][1];
24+
if (x >= 0 && x < matrix[0].length && y >= 0 && y < matrix.length) {
25+
if (matrix[y][x] == identifier) {
26+
found.push(this.directions[i]);
27+
}
28+
}
29+
}
30+
return found;
31+
}
32+
moveAndEat() {
33+
var aviableSlots = this.findSlots(1); //eat function part --> grass detected
34+
if (aviableSlots[0]) {
35+
var slot = random(aviableSlots);
36+
matrix[this.y][this.x] = 0;
37+
this.x = slot[0];
38+
this.y = slot[1];
39+
this.energy = 5;
40+
matrix[slot[1]][slot[0]] = 2;
41+
this.multiplyF();
42+
for (var i = 0; i < grassArray.length; i++) {
43+
if (this.x == grassArray[i].x && this.y == grassArray[i].y) {
44+
grassArray.splice(i, 1);
45+
break;
46+
}
47+
}
48+
}
49+
else { //move function part --> no grass detected
50+
var slot = random(this.findSlots(0));
51+
if (slot) {
52+
matrix[this.y][this.x] = 0;
53+
this.x = slot[0];
54+
this.y = slot[1];
55+
matrix[slot[1]][slot[0]] = 2;
56+
}
57+
this.death();
58+
}
59+
}
60+
death() {
61+
if (this.energy <= 0) {
62+
if(this.index == 2){
63+
for (var i = 0; i < grassEaterArray.length; i++) {
64+
if (grassEaterArray[i].x == this.x && grassEaterArray[i].y == this.y) {
65+
grassEaterArray.splice(i, 1);
66+
break;
67+
}
68+
}
69+
matrix[this.y][this.x] = 0;
70+
}
71+
else if(this.index == 2){
72+
for (var i = 0; i < predatorArray.length; i++) {
73+
if (predatorArray[i].x == this.x && predatorArray[i].y == this.y) {
74+
predatorArray.splice(i, 1);
75+
break;
76+
}
77+
}
78+
matrix[this.y][this.x] = 0;
79+
}
80+
}
81+
this.energy--;
82+
}
83+
}

0 commit comments

Comments
 (0)