-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.js
187 lines (159 loc) · 6.92 KB
/
enemy.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { equalPositions } from './snake.js';
import { getGridSize } from './grid.js';
import { getDifficulty } from './game-ui.js';
// Array, because different difficulty level will have different number of enemies
const maxNumberOfEnemies = [2, 3, 4];
// Defining how many steps to take before newRandomDirection
const STEPS_BEFORE_NEW_DIRECTION = 5;
// Intializing empty arrays for enemies
let enemies;
let directionVectors;
let enemySteps;
function createEnemies(numberOfEnemies) {
// Function to create Enemies based on the number of Enemies provided, changes according the difficulty level
for(let i = 0; i < numberOfEnemies; i++) {
// Creating objects for Enemies
let enemy = {
x: Math.ceil(Math.random() * getGridSize()),
y: Math.ceil(Math.random() * getGridSize())
}
// Pushing enemy object to the enemies array
enemies.push(enemy);
// Create new directionVector object, with no direction.
let directionVector = {
x: 0, y: 0
}
// Pushing the new directionVector object to the relavant array
directionVectors.push(directionVector);
// Creating a new entry of enemyStep for the enemy.
enemySteps.push(Math.floor(Math.random() * STEPS_BEFORE_NEW_DIRECTION));
}
}
export function update() {
// Function to update Enemy position and direction, if neeeded.
enemies.forEach((enemy, i) => {
// Checking if the given enemy requires new direction, if so, give it one.
if ( enemySteps[i] % STEPS_BEFORE_NEW_DIRECTION == 0 ) {
directionVectors[i].x = randomDirection();
if ( directionVectors[i].x === 0 ) {
directionVectors[i].y = randomDirection({withoutZero : true});
} else directionVectors[i].y = 0;
// Re-initializing Enemy steps with new Direction
enemySteps[i] = 0;
}
// If enemy isn't outsideGrid, it takes a step
if(!enemyOutsideGrid(enemy)) {
enemy.x += directionVectors[i].x;
enemy.y += directionVectors[i].y;
}
// Checking if enemy is at the edge, then pull back.
if (enemy.x <= 1) enemy.x += 1;
if (enemy.y <= 1) enemy.y += 1;
if (enemy.x >= getGridSize()) enemy.x -= 1;
if (enemy.y >= getGridSize()) enemy.y -= 1;
// Adding a step, when enemy has taken a step.
enemySteps[i] += 1;
})
}
export function draw(gameBoard) {
// Draws updated enemy on gameBoard
enemies.forEach((enemy, i) => {
const enemyElement = document.createElement('div');
// Positioning Enemy on the grid
enemyElement.style.gridRowStart = enemy.y;
enemyElement.style.gridColumnStart = enemy.x;
// Adding class for styling
enemyElement.classList.add('enemy');
// Appending to the gameBoard
gameBoard.appendChild(enemyElement);
})
}
function randomDirection({withoutZero = false} = {}) {
// Function to return a random Direction vector for individual axis
// Taking a random number between 1 & 3 to use in switch case.
var randomNumber = Math.ceil(Math.random() * 3);
// Checking if withoutZero is true, and taking random b/w 1 & 2
if (randomNumber === 3 && withoutZero === true) randomNumber = Math.ceil(Math.random() * 2);
// Switch case to return diff values based on randomNumber
switch(randomNumber) {
case 1:
return -1;
case 2:
return 1;
case 3:
return 0;
}
}
function enemyOutsideGrid(position) {
// Function to check if the enemy is outside the grid
return position.x >= getGridSize() || position.x <= 1 || position.y >= getGridSize() || position.y <= 1
}
export function enemyKilledSnake(snakeBody) {
// Function to check if the Enemy killed Snake
let finalTest = [];
enemies.forEach((enemy) => {
// For every enemy, arrPerEnemy stores if it collides with any of the snakeBodyPart
let arrPerEnemy = [];
snakeBody.forEach((snakeBodyPart) => {
if (equalPositions(snakeBodyPart, enemy)) {
//Pushing 'true' if the snakeBodyPart collides with enemy, otherwise 'false'
arrPerEnemy.push(true)
} else arrPerEnemy.push(false)
})
// Now, if any of the snakeBodyPart collided with Enemy, adding 'true' to arrPerEnemy
if (arrPerEnemy.includes(true)) finalTest.push(true);
else finalTest.push(false);
})
// Checking if any of the enemy collided with snakeBody with 'true' check inside finalTest.
return finalTest.includes(true);
}
export function drawEnemyNextPath(gameBoard) {
// Drawing Enemy path for every enemy
enemies.forEach((enemy, index) => {
// Drawing path for only the steps left in the enemySteps in the same direction
for(let i = 0; i < 5 - enemySteps[index]; i++) {
// Create an HTML elem for every path
const enemyNextPathElem = document.createElement('div');
enemyNextPathElem.classList.add('enemy-path');
// Positioning the path with respect to directionVectors
if (directionVectors[index].x != 0) {
if (directionVectors[index].x > 0) {
if(enemy.x + directionVectors[index].x + i < getGridSize()) enemyNextPathElem.style.gridColumnStart = enemy.x + directionVectors[index].x + i;
else break;
} else {
if(enemy.x + directionVectors[index].x - i > 0) enemyNextPathElem.style.gridColumnStart = enemy.x + directionVectors[index].x - i;
else break;
}
enemyNextPathElem.style.gridRowStart = enemy.y + directionVectors[index].y;
} else if (directionVectors[index].y != 0) {
if (directionVectors[index].y > 0) {
if(enemy.y + directionVectors[index].y + i < getGridSize()) enemyNextPathElem.style.gridRowStart = enemy.y + directionVectors[index].y + i;
else break;
} else {
if(enemy.y + directionVectors[index].y - i > 0) enemyNextPathElem.style.gridRowStart = enemy.y + directionVectors[index].y - i;
else break;
}
enemyNextPathElem.style.gridColumnStart = enemy.x + directionVectors[index].x;
}
// Appending Enemy path elements to the gameBoard
gameBoard.appendChild(enemyNextPathElem);
}
})
}
export function reset() {
enemies = [];
directionVectors = [];
enemySteps = [];
let userDifficulty = getDifficulty();
switch (userDifficulty) {
case 'hard':
createEnemies(maxNumberOfEnemies[2]);
break;
case 'medium':
createEnemies(maxNumberOfEnemies[1]);
break;
case 'easy':
createEnemies(maxNumberOfEnemies[0]);
break;
}
}