-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
223 lines (202 loc) · 6.19 KB
/
functions.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//add event listeners to selection and ability buttons
function buttonSetUp() {
for (let i = 0; i < game.meeples.length; i++) {
const selectionButton = document.getElementById(game.meeples[i].name);
const abilityButton = document.getElementById(`${game.meeples[i].name}ab`);
selectionButton.addEventListener("click", function () {
activateMeeple(game.meeples[i]);
updateVisuals();
});
abilityButton.addEventListener("click", function () {
activateMeepleAbility(game.meeples[i]);
updateVisuals();
});
}
const resetButton = document.getElementById("resetButton");
resetButton.addEventListener("click", resetBoardState);
}
//activate meeple passed as argument, deactivate others, adjust html classes for visuals
function activateMeeple(m) {
if (getActiveMeeple() === m) return;
deactivateMeepleAbilities();
for (let i = 0; i < game.meeples.length; i++) {
game.meeples[i].isActive = m === game.meeples[i];
game.meeples[i].isActive
? document.getElementById(game.meeples[i].name).classList.add("active")
: document
.getElementById(game.meeples[i].name)
.classList.remove("active");
}
}
//activate meeple's ability passed as argument, deactivate others, call activate meeple
//If the passed meeple's ability is active, it becomes insactive and the function returns.
function activateMeepleAbility(m) {
activateMeeple(m);
if (m.abilityActive) {
m.abilityActive = !m.abilityActive;
return;
}
for (let i = 0; i < game.meeples.length; i++) {
game.meeples[i].abilityActive = m === game.meeples[i];
}
}
//deactivate all meeple's abilities
function deactivateMeepleAbilities() {
for (let i = 0; i < game.meeples.length; i++) {
game.meeples[i].abilityActive = false;
}
}
//returns active meeple
function getActiveMeeple() {
for (let i = 0; i < game.meeples.length; i++) {
if (game.meeples[i].isActive) {
return game.meeples[i];
}
}
}
//returns true if any meeple ability is active
function anAbilityIsActive() {
for (let i = 0; i < game.meeples.length; i++) {
if (game.meeples[i].abilityActive) {
return true;
}
}
return false;
}
//returns false if no active meeple or meeple had already hit target, otherwise checks if target was hit by active meeple
function targetHit() {
let m = getActiveMeeple();
if (m === undefined) return false;
if (m.reachedTarget) return false;
return m.xPos === game.targetX && m.yPos === game.targetY;
}
//changes opacity of target image when meeple reaches target
function applyReachedTargetStyle(m) {
const meeplehtml = document.getElementById(`${m.name}t`);
meeplehtml.style.opacity = 1;
}
//checks for winningMeeples array length being six
function checkForGameWon() {
console.log(game.winningMeeples);
return game.winningMeeples.length === 6;
}
//randomly positions target in valid square
function calculateTargetPosition() {
game.targetX = Math.floor(Math.random() * game.boardSize);
game.targetY = Math.floor(Math.random() * game.boardSize);
// if (!squareIsValid(game.targetX, game.targetY)) {
if (!squareIsValid(game.targetX, game.targetY)) {
calculateTargetPosition();
}
}
//function for checking square is legal
function squareIsValid(x, y) {
return squareIsEmpty(x, y) && squareIsInBounds(x, y);
}
//function for iterating over meeples coords to see if square empty
function squareIsEmpty(x, y) {
for (let i = 0; i < game.meeples.length; i++) {
if (game.meeples[i].xPos === x && game.meeples[i].yPos === y) {
return false;
}
}
return true;
}
//function for checking if in wall bounds
function squareIsInBounds(x, y) {
if (x < 0 || y < 0 || x > game.boardSize - 1 || y > game.boardSize - 1) {
return false;
}
if (x === 0 && y === 0) {
return false;
}
if (x === game.boardSize - 1 && y === game.boardSize - 1) {
return false;
}
return true;
}
function calculateLimits() {
let m = getActiveMeeple();
let r = m.yPos;
let c = m.xPos;
let limits = {
upper: 0,
lower: game.boardSize - 1,
left: 0,
right: game.boardSize - 1,
};
if (c == 0) limits.upper = 1;
if (c == game.boardSize - 1) limits.lower = game.boardSize - 2;
if (r == 0) limits.left = 1;
if (r == game.boardSize - 1) limits.right = game.boardSize - 2;
for (let i = 0; i < boardStructure.length; i++) {
let wall = boardStructure[i];
//columns
if (!wall.row && wall.existson == c) {
if (wall.after >= limits.upper && wall.after < r) {
limits.upper = wall.after + 1;
}
if (wall.after < limits.lower && wall.after >= r) {
limits.lower = wall.after;
}
}
//rows
if (wall.row && wall.existson == r) {
if (wall.after >= limits.left && wall.after < c) {
limits.left = wall.after + 1;
}
if (wall.after < limits.right && wall.after >= c) {
limits.right = wall.after;
}
}
}
for (let i = 0; i < game.meeples.length; i++) {
if (!game.meeples[i].isActive) {
//columns
if (game.meeples[i].xPos === c) {
if (game.meeples[i].yPos >= limits.upper && game.meeples[i].yPos < r) {
limits.upper = game.meeples[i].yPos + 1;
}
if (game.meeples[i].yPos <= limits.lower && game.meeples[i].yPos > r) {
limits.lower = game.meeples[i].yPos - 1;
}
}
//rows
if (game.meeples[i].yPos === r) {
if (game.meeples[i].xPos >= limits.left && game.meeples[i].xPos < c) {
limits.left = game.meeples[i].xPos + 1;
}
if (game.meeples[i].xPos <= limits.right && game.meeples[i].xPos > c) {
limits.right = game.meeples[i].xPos - 1;
}
}
}
}
return limits;
}
function move(e) {
let m = getActiveMeeple();
if (m === undefined) {
alert("select a meeple");
}
let boundaries = calculateLimits();
switch (e.code) {
case "ArrowUp":
m.yPos = boundaries.upper;
attempt.moveCounter++;
break;
case "ArrowDown":
m.yPos = boundaries.lower;
attempt.moveCounter++;
break;
case "ArrowLeft":
m.xPos = boundaries.left;
attempt.moveCounter++;
break;
case "ArrowRight":
m.xPos = boundaries.right;
attempt.moveCounter++;
break;
}
assessGameState();
}