-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.vue
272 lines (247 loc) · 5.94 KB
/
App.vue
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<template>
<div id="app">
<Pointer/>
<AppHeader
:score="score"
:is-paused="isPaused"
:is-started="isStarted"
:level="level"
@quit="handleQuit"
/>
<TitleScreen
:score="score"
:is-paused="isPaused"
:is-started="isStarted"
:is-game-over="isGameOver"
@start="startGame"
@startOver="resetGame"
/>
<!-- Main Gamepad -->
<main
class="gamepad"
:class="gamepadClass"
>
<ul
class="gamepad__surface"
:style="gamepadTiltStyle"
>
<Slab
v-for="(slab, i) in slabs"
:data="slab"
:level="level"
:key="i"
:ref="`slab-${slab.i}`"
@hit="handleMoleClick"
@fail="handleGameOver"
/>
</ul>
</main>
<AppFooter v-if="!isPaused"/>
</div>
</template>
<script>
import Pointer from '@/components/Pointer'
import AppHeader from '@/components/AppHeader'
import AppFooter from '@/components/AppFooter'
import TitleScreen from '@/components/TitleScreen'
import Slab from '@/components/Slab'
export default {
name: 'App',
components: {
Pointer,
AppHeader,
AppFooter,
TitleScreen,
Slab,
},
data() {
return {
// State
isPaused: true,
isStarted: false,
isGameOver: false,
// Timers
timer: null,
moleTimer: null,
slabs: [],
// Score
score: {
current: 0,
high: 0,
timeLapsed: 1,
},
// Visual styling
pointer: {
x: 0,
y: 0,
},
viewport: {
width: 0,
height: 0,
},
}
},
mounted() {
this.createSlabs()
this.initListeners()
this.getViewportSize()
// Set high score from localstorage if there is one
if (localStorage.getItem('highscore')) {
this.score.high = localStorage.getItem('highscore')
}
},
beforeDestroy() {
this.destroyListeners()
},
watch: {
level() {
// Emit level up message when it increases
this.$root.$emit('footer', ['Level up!'])
},
},
methods: {
// Game timeline
startGame() {
this.isStarted = true
this.isPaused = false
this.startTimer()
this.startMoleTimer()
},
startTimer() {
this.timer = setInterval(() => {
this.score.timeLapsed += 1
}, 1000)
},
startMoleTimer() {
// Initiate first mole slab
const slab = this.getRandomSlab()
slab.isMoled = true
const setNextTimeout = () => {
// Time depends on difficulty level
const interval = Math.round(1000 * (3 / this.level))
this.moleTimer = setTimeout(() => {
const slab = this.getRandomSlab()
slab.isMoled = true
this.$refs[`slab-${slab.i}`][0].startTimer()
setNextTimeout()
}, interval)
}
setNextTimeout()
},
resetGame() {
// Reset game if previously played
this.createSlabs()
this.score.current = 0
this.isGameOver = false
this.isPaused = false
this.isStarted = true
this.startTimer()
this.startMoleTimer()
},
stopTimers() {
// Stop/clear timers
clearInterval(this.timer)
clearTimeout(this.moleTimer)
this.timer = null
this.moleTimer = null
this.score.timeLapsed = 1
this.$root.$emit('stop-slab-timers')
},
setHighScore() {
if (this.score.current > this.score.high) {
this.score.high = this.score.current
localStorage.setItem('highscore', this.score.current)
}
},
// Slab helpers
getRandomSlab() {
const [min, max] = [0, 20]
const i = Math.floor(Math.random() * (max - min)) + min
return this.slabs[i]
},
createSlabs() {
let slabs = []
for (let i = 0; i < 20; i++) {
slabs.push({ i, isMoled: false })
}
this.slabs = slabs
},
// Listeners and handlers
initListeners() {
window.addEventListener('mousemove', this.handleMousemove)
window.addEventListener('resize', this.getViewportSize)
},
destroyListeners() {
window.removeEventListener('mousemove', this.handleMousemove)
window.removeEventListener('resize', this.getViewportSize)
},
handleQuit() {
this.isStarted = false
this.isPaused = true
this.isGameOver = false
this.score.current = 0
this.createSlabs()
this.stopTimers()
},
handleGameOver() {
this.isGameOver = true
this.isPaused = true
this.isStarted = false
// Set high score
this.setHighScore()
// Stop/clear timers
this.stopTimers()
},
handleMoleClick(i) {
this.score.current += 1
this.slabs[i].isMoled = false
this.$root.$emit('footer', ['+1', this.score.current])
},
handleMousemove(ev) {
try {
this.pointer = {
x: ev.pageX,
y: ev.pageY,
}
} catch(e) {
console.error(e)
}
},
// Visual styling
getViewportSize() {
try {
this.viewport = {
width: window.innerWidth,
height: window.innerHeight,
}
} catch(e) {
console.error(e)
}
},
},
computed: {
gamepadTiltStyle() {
let tiltX, tiltY
const { width, height } = this.viewport // Viewport size
const { y, x } = this.pointer // Pointer location
const [ tiltMinDegX, tiltMaxDegX ] = [5, -5]
const [ tiltMinDegY, tiltMaxDegY ] = [5, 15]
tiltX = tiltMinDegX + ((x / width) * (tiltMaxDegX-tiltMinDegX))
tiltY = tiltMinDegY + ((y / height) * (tiltMaxDegY-tiltMinDegY))
return {
transform: `rotateY(${tiltX}deg) rotateX(${tiltY}deg)`,
}
},
gamepadClass() {
return {
'gamepad--paused': this.isPaused,
[`gamepad--level-${this.level}`]: true,
}
},
level() {
// Increase the level every 20 seconds
return Math.ceil(this.score.timeLapsed / 15)
},
},
}
</script>