-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
199 lines (147 loc) · 4.09 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
import { Pipe, Bird } from './classes.js'
// ---------------------------------------------------------------
// --- DOM ---
// ---------------------------------------------------------------
const doc = window.document
/** @type {HTMLCanvasElement} */
export const canvas = doc.getElementById('canvas')
export const ctx = canvas.getContext('2d')
canvas.style.backgroundColor = 'blue'
canvas.width = 280
canvas.height = 480
const dieElement = doc.getElementById('die-message')
const timerElement = doc.getElementById('timer')
// ---------------------------------------------------------------
// --- GAME OBJECT ---
// ---------------------------------------------------------------
const player = new Bird({ name: 'galih', x: 12, y: 24, speed: 10 })
const pipes = [
new Pipe({ x: canvas.width }),
new Pipe({ x: canvas.width * 2 - 20 }),
]
// --- Game states
// TODO : create an object
let treshold = 0
let onJump = false
let die = false
let score = 0
// --- Game function mechanics
// --- player control
function control(e) {
if (e.keyCode === 32) {
onJump = true
treshold += 0.25
}
}
// ---------------------------------------------------------------
// --- EVENT LISTENER ---
// ---------------------------------------------------------------
// --- Browser event listener for game controller
// window.addEventListener('mousemove', (e) => {
// const rect = canvas.getBoundingClientRect()
// console.log(e.clientX - rect.x)
// pipes.forEach((p, i) => {
// console.log(`
// pipes property:
// x : ${p.x}
// bottom pipes (coords) :
// ${p.y.bot}
// upper pipes (coords) :
// ${p.y.top}
// `)
// })
// })
window.addEventListener('keydown', (e) => control(e))
window.addEventListener('keyup', (e) => {
// reset treshold
treshold = 0
onJump = false
})
// ---------------------------------------------------------------
// --- MAIN FUNCTION ---
// ---------------------------------------------------------------
function collision(pipes) {
const p = pipes
if (player.x + player.width >= p.x && player.y <= p.y.top + p.topHeight || player.y + player.height >= p.y.bot) {
dieElement.hidden = false
die = true
}
}
// --- Animate function
// TODO : REFACTOR WHOLE FUNCTION
function animation() {
ctx.clearRect(0, 0, canvas.width, canvas.height)
// TODO : move to single function
if (onJump === false) {
player.updates()
} else {
player.jump(treshold)
}
// TODO : move to single function
pipes.forEach((p, i) => {
p.updates()
// collision detection
const distances = (p.x) - (player.x + player.width)
if (distances < 10) {
console.log('approaching...')
collision(p)
}
if (p.x + p.width <= 0) {
p.regenerate(i)
}
})
const frames = requestAnimationFrame(animation)
fpsTime(frames)
if (die) {
cancelAnimationFrame(frames)
timer(false)
}
}
// --- Timer function
function timer(on) {
if (!on) return
let miliSecond = 0
let second = 0
let min = 0
let hour = 0
setInterval(() => {
// miliSecond % 1000 == 0 ? 0 : miliSecond
miliSecond++
if (miliSecond == 1000) {
miliSecond = 0
second++
}
if (second == 60) {
second = 0
min++
}
if (min == 60) {
min = 60
hour++
}
timerElement.innerText = `${hour}:${min}:${second}:${miliSecond}`
}, 1)
}
function fpsTime(frames) {
const fpsTimer = doc.getElementById('fps')
let fm = frames % 60 == 0 ? 0 : frames
let second = Math.floor(fm / 60)
let min = 0
let hour = 0
if (second == 60) {
second = 0
min++
}
if (min == 60) {
min = 60
hour++
}
fpsTimer.innerText = `${hour}:${min}:${second}:${frames}`
}
// --- Main function
function init() {
dieElement.hidden = true
animation()
timer(true)
}
init()