-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
219 lines (192 loc) · 5.61 KB
/
script.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
let canvas = document.querySelector('canvas');
let wrapper = document.querySelector('.wrapper');
let resetGameBtn = document.querySelector('#reset');
let diceDisplay = document.querySelector('#diceThrow');
let playerDisplay = document.querySelector('.playerName');
let message = document.querySelector('.message');
let ctx = canvas.getContext('2d');
let height = 500;
let width = 500;
let gridSize = 50;
let gridMid = 25;
let walking, walkSpeed = 450;
let locked = false;
let slideSpeed = .5;
let rolled = '', rolling, rollCount, rollMax, rollSpeed=85;
let player1 = {current:0, target:0, x:0, y:0, colour:'#f36d', id:'You'};
let player2 = {current:0, target:0, x:0, y:0, colour:'#8a2d', id:'AutoBot'};
let activePlayer = player1;
const obstacles = [
{type:'snake', start:97, end:78},
{type:'snake', start:95, end:56},
{type:'snake', start:88, end:24},
{type:'snake', start:62, end:18},
{type:'snake', start:48, end:26},
{type:'snake', start:36, end:6},
{type:'snake', start:32, end:10},
{type:'ladder', start:1, end:38},
{type:'ladder', start:4, end:14},
{type:'ladder', start:8, end:30},
{type:'ladder', start:21, end:42},
{type:'ladder', start:28, end:76},
{type:'ladder', start:50, end:67},
{type:'ladder', start:71, end:92},
{type:'ladder', start:80, end:99}
];
canvas.width = width;
canvas.height = height;
wrapper.style.width = `${width}px`;
ctx.strokeStyle = '#555';
ctx.lineWidth = 2;
const setLocked = (tf) => {
locked = tf;
}
const boustrophedonWalk = (cols, rows) => {
let temp = [];
for(let row=0; row<rows; row++){
let t = Array.apply(null, Array(cols)).map((x, col) => {
return {id:col+row*cols, y:height - gridSize - row*gridSize, x:col*gridSize};
});
t = row % 2 ? t.reverse() : t;
temp = [...temp, ...t];
}
return temp;
}
const drawPlayers = () => {
ctx.clearRect(0, 0, width, height);
if(player1.current > 0) {
ctx.fillStyle = player1.colour;
ctx.beginPath();
ctx.arc(player1.x+gridMid, player1.y+gridMid, 16, 0, 2 * Math.PI);
ctx.fill();
ctx.stroke();
}
if(player2.current > 0) {
ctx.fillStyle = player2.colour;
ctx.beginPath();
if(player2.current === player1.current){
ctx.arc(player2.x+gridMid, player2.y+gridMid, 16, 45, Math.PI + 45);
}
else {
ctx.arc(player2.x+gridMid, player2.y+gridMid, 16, 0, 2 * Math.PI);
}
ctx.fill();
ctx.stroke();
}
}
const walk = () => {
let activeCounter = activePlayer.current++;
let sliding = false;
activePlayer.x = walkSequence[activeCounter].x;
activePlayer.y = walkSequence[activeCounter].y;
drawPlayers();
if(activeCounter === 99){
clearInterval(walking);
showWinner();
return;
}
if(activePlayer.current >= activePlayer.target){
clearInterval(walking);
// check obstacles
for(let i=0; i < obstacles.length; i++){
if(obstacles[i].start === activePlayer.target){
let endSquare = obstacles[i].end;
activePlayer.target = obstacles[i].end;
sliding = true;
slide(activePlayer, walkSequence[endSquare-1].x, walkSequence[endSquare-1].y, slideSpeed);
break;
}
}
if(!sliding){
resetTurn();
togglePlayer();
}
}
}
const showWinner = () => {
setPlayerID('is the winner!');
resetGameBtn.classList.remove('hidden');
}
const setPlayerID = (msg='') => {
playerDisplay.innerHTML = `${activePlayer.id} ${msg}`;
message.innerHTML = "Click dice to play";
document.body.classList = `player${activePlayer.id}`;
}
const resetTurn = () => {
setLocked(false);
}
const slide = (element, dX, dY, dur=1) => {
gsap.to(element, {x:dX, y:dY, duration:dur, delay: 0.25, onUpdate:doOnUpdate, onComplete:doOnComplete});
}
const doOnUpdate = () => {
drawPlayers();
}
const doOnComplete = () => {
activePlayer.current = activePlayer.target;
drawPlayers();
resetTurn();
togglePlayer();
}
const togglePlayer = () => {
activePlayer = activePlayer.id === player1.id ? player2 : player1;
setPlayerID();
if(activePlayer === player2){
rollDice();
}
}
const rollDice = (evt) => {
if(evt) evt.preventDefault();
if (locked) return;
setLocked(true);
message.innerHTML = activePlayer === player1 ? "Rolling..." : 'Auto rolling...';
rollCount = 0;
rollMax = Math.random()*10 + 15;
rolling = setInterval(doRoll, rollSpeed);
}
const doRoll = () => {
rolled = Math.floor(Math.random() * 6 + 1);
diceRollDisplay(rolled);
if(rollCount++ >= rollMax){
clearInterval(rolling);
message.innerHTML = "Moving...";
activePlayer.target += rolled;
walking = setInterval(walk, walkSpeed);
}
}
const diceRollDisplay = (spots) => {
diceDisplay.classList = `s${spots}`
}
const resetGame = () => {
player1.current = 0;
player1.target = 0;
player1.x = 0;
player1.y = 0;
player2.current = 0;
player2.target = 0;
player2.x = 0;
player2.y = 0;
activePlayer = player1;
locked = false;
diceRollDisplay('');
setPlayerID();
drawPlayers();
resetGameBtn.classList.add('hidden');
}
diceDisplay.addEventListener('click', rollDice);
resetGameBtn.addEventListener('click', resetGame);
let walkSequence = boustrophedonWalk(10, 10);
setPlayerID();
// Test method to show obstacles
const drawObstacles = () => {
ctx.clearRect(0, 0, width, height);
for(let i=0; i < obstacles.length; i++){
let ob = obstacles[i];
ctx.strokeStyle = ob.type === 'snake' ? '#d00' : '#0d0';
ctx.beginPath();
ctx.moveTo(walkSequence[ob.start-1].x+gridSize*.5, walkSequence[ob.start-1].y+gridSize*.5);
ctx.lineTo(walkSequence[ob.end-1].x+gridSize*.5, walkSequence[ob.end-1].y+gridSize*.5);
ctx.stroke();
ctx.closePath();
}
}
// drawObstacles();