-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
203 lines (186 loc) · 6.1 KB
/
Copy pathscript.js
File metadata and controls
203 lines (186 loc) · 6.1 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
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
// Get the timer box to reference many times
const $timerBox = $(".timer");
// Get tail box for reference
const $tail = $(".tail");
// Game Timer ID
let gameTimer;
// Boolean to check if game is already running
let isRunning = false;
// Get START/STOP button
const $startGame = $("#startGame");
// Get game canvas ".donkey"
const $gameCanvas = $(".game-canvas");
// Get hitbox
const $hitbox = $(".hitbox");
// Get tail width and heigh to prevent tail from crossing border
const tailWidth = parseInt($tail.width());
const tailHeight = parseInt($tail.height());
// Size of Donkey Div
const MAX_WIDTH = $gameCanvas.width() - tailWidth;
const MAX_HEIGHT = $gameCanvas.height() - tailHeight;
// Gets tail position, removes last two letters "px", and parses as an int to later modify
// Axis values are a string of either "top" or "left"
function getTailPosition(axis) {
return parseInt($tail.css(axis).slice(0, -2));
}
// Sets new tail position, returning position value to string and adding "px" to conform to CSS requirements
// Axis values are a string of either "top" or "left"
function setTailPosition(axis, value) {
stringValue = String(value) + "px";
$tail.css(axis, stringValue);
}
// Arrow button listeners
const upArrowListener = $(".up-arrow").on("click", function () {
const xPos = getTailPosition("left");
const yPos = getTailPosition("top");
const moveValue = 10;
if (yPos - moveValue >= 0) {
setTailPosition("top", yPos - moveValue);
}
});
const downArrowListener = $(".down-arrow").on("click", function () {
const xPos = getTailPosition("left");
const yPos = getTailPosition("top");
const moveValue = 10;
if (yPos + moveValue <= MAX_HEIGHT) {
setTailPosition("top", yPos + moveValue);
}
});
const leftArrowListener = $(".left-arrow").on("click", function () {
const xPos = getTailPosition("left");
const yPos = getTailPosition("top");
const moveValue = 10;
if (xPos - moveValue >= 0) {
setTailPosition("left", xPos - moveValue);
}
});
const rightArrowListener = $(".right-arrow").on("click", function () {
const xPos = getTailPosition("left");
const yPos = getTailPosition("top");
const moveValue = 10;
if (xPos + moveValue <= MAX_WIDTH) {
setTailPosition("left", xPos + moveValue);
}
});
// Key press listener for up, right, down, left and space bar key presses
// Each if statement prevents tail from going beyond the border of the Donkey Div
const keyPressListener = $(document).keydown(function (e) {
const xPos = getTailPosition("left");
const yPos = getTailPosition("top");
const moveValue = 5;
if ([32, 37, 38, 39, 40].includes(e.which)) {
// Prevent default keystroke behaviour
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
// Up
if (e.which == 38 && yPos - moveValue >= 0) {
setTailPosition("top", yPos - moveValue);
// Right
} else if (e.which == 39 && xPos + moveValue <= MAX_WIDTH) {
setTailPosition("left", xPos + moveValue);
// Down
} else if (e.which == 40 && yPos + moveValue <= MAX_HEIGHT) {
setTailPosition("top", yPos + moveValue);
// Left
} else if (e.which == 37 && xPos - moveValue >= 0) {
setTailPosition("left", xPos - moveValue);
// Space Bar
} else if (e.which == 32) {
// if modal is displayed, space bar will close it
if (!$(".modal").hasClass("modal-hide")) {
$(".modal").addClass("modal-hide");
} else {
startStopSwitch();
}
}
});
// On START/STOP click, run game start/stop switch
$startGame.on("click", startStopSwitch);
function startStopSwitch() {
if (!isRunning) {
isRunning = true;
$startGame.text("STOP");
$timerBox.toggleClass("display");
$startGame.toggleClass("start-button-animation");
startGame();
} else {
// ("READY SET GO" phase) No need to evaluate score, simply reset game visuals and timer
resetGame();
if ($tail.hasClass("display")) {
// if tail is displayed on screen... remove tail and Evaluate score
$tail.removeClass("display");
endGame();
}
}
}
function startTimer() {
// Game Timer definition
const timer = () => {
if (i < readySetGoArray.length) {
$timerBox.text(readySetGoArray[i]);
i++;
} else if (seconds >= 0) {
// Display the tail on the screen
$tail.addClass("display");
$timerBox.text(`Timer: ${seconds}s`);
seconds--;
} else {
startStopSwitch();
}
};
// First, display Ready, Set Go, Then a 10 second countdown
const readySetGoArray = ["READY...", "SET...", "GO!"];
let i = 0;
let seconds = 10;
// call timer first to circumvent delay of first iteration caused by setInterval
timer();
gameTimer = setInterval(timer, 1000);
}
function startGame() {
// Set tail to random x,y position in bordered area
const randomX = Math.floor(Math.random() * MAX_WIDTH);
const randomY = Math.floor(Math.random() * MAX_HEIGHT);
setTailPosition("left", randomX);
setTailPosition("top", randomY);
startTimer();
}
function resetGame() {
// Reset timer and other displayed items
// (timerBox text, Tail, isRunning Boolean and Button text)
clearInterval(gameTimer);
$timerBox.toggleClass("display");
$startGame.text("START");
$startGame.toggleClass("start-button-animation");
isRunning = false;
}
function endGame() {
// Check tail position and compare to hitbox
const xPos = getTailPosition("left");
const yPos = getTailPosition("top");
// check hitbox position
const hitboxLeft = parseInt($hitbox.css("left"));
const hitboxRight = parseInt($hitbox.css("left")) + $hitbox.width();
const hitboxTop = parseInt($hitbox.css("top"));
const hitboxBottom = parseInt($hitbox.css("top")) + $hitbox.height();
let modalMessage = "";
// set modal message
if (
xPos >= hitboxLeft &&
xPos <= hitboxRight &&
yPos >= hitboxTop &&
yPos <= hitboxBottom
) {
modalMessage = "Congratulations! Great Success!";
} else {
modalMessage = "Try Again WOMP WOMP";
}
// show modal by removing the .modal-hide class
$(".modal-message").text(modalMessage);
$(".modal").removeClass("modal-hide");
$(".modal-close").on("click", function () {
$(".modal").addClass("modal-hide");
});
}