-
Notifications
You must be signed in to change notification settings - Fork 0
/
mario.html
361 lines (330 loc) · 10.2 KB
/
mario.html
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>mario</title>
<style>
body {
position: relative;
width: 100%;
height: 100vh;
margin: 0;
background-color: #333;
}
#playground {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
width: 800px;
height: 600px;
background-color: #81d9ff;
}
</style>
</head>
<body>
<canvas id="playground"></canvas>
<script>
/*
想法是,实现基本操作,并且能调整手感。已经有一个问题了,那些物理效果是放在全局好,还是放在操作实体上好?全都要|具体问题具体分析?——全局变量要不要再搞一个objects={},像碰撞检测这种东西,会需要...嗯...
*/
// #region Constants
const GRAVITY = 0.5 // 重力加速度
const FRICTION = 0.8 // 摩擦系数
const RUN_MAX_SPEED = 10 // 最大跑动速度
const RUN_ACC = 1 // 跑动加速度
const RUN_DEC = 2 // 跑动减速度
const JUMP = 40 // 跳跃初速度
const FALL_MAX_SPEED = 20 // 最大下落速度
const STATES = ["idle", "run", "jump", "fall"]
// #endregion
// #region helpers
/*
class Vector2 {
constructor(x, y) {
this.x = x
this.y = y
}
add(v) {
return new Vector2(this.x + v.x, this.y + v.y)
}
sub(v) {
return new Vector2(this.x - v.x, this.y - v.y)
}
mul(v) {
return new Vector2(this.x * v.x, this.y * v.y)
}
length() {
return Math.sqrt(this.x * this.x + this.y * this.y)
}
normalize() {
let len = this.length()
return new Vector2(this.x / len, this.y / len)
}
}
*/
function Vector2(x, y) {
this.x = x
this.y = y
}
Vector2.prototype.add = function (v) {
return new Vector2(this.x + v.x, this.y + v.y)
}
Vector2.prototype.sub = function (v) {
return new Vector2(this.x - v.x, this.y - v.y)
}
Vector2.prototype.mul = function (v) {
return new Vector2(this.x * v.x, this.y * v.y)
}
// #endregion
// #region Variables
let position = new Vector2(0, 0)
let speed = new Vector2(0, 0)
let cameraAnchor = new Vector2(0, 0)
let objects = []
// #endregion
// #region Rendering
const playground = document.getElementById("playground")
const ctx = playground.getContext("2d")
const devicePixelRatio = 1
playground.width = playground.clientWidth * devicePixelRatio
playground.height = playground.clientHeight * devicePixelRatio
ctx.scale(devicePixelRatio, devicePixelRatio)
function render() {
ctx.clearRect(0, 0, playground.width, playground.height)
ctx.fillStyle = "#81d9ff"
ctx.fillRect(0, 0, playground.width, playground.height)
ctx.fillStyle = "#000000"
ctx.lineWidth = 1
ctx.strokeStyle = "#000000"
ctx.strokeRect(0 - cameraAnchor.x, 0 - cameraAnchor.y, 800, 600)
objects.forEach((obj) => {
ctx.fillRect(obj.position.x - cameraAnchor.x, obj.position.y - cameraAnchor.y, obj.size.x, obj.size.y)
})
}
// #endregion
// #region Updating
function update() {
applyMovement()
applyFriction()
applyCollision()
applyGravity()
applyCamera()
render()
// window.requestAnimationFrame(update)
}
// window.requestAnimationFrame(update)
let timer = setInterval(update, 1000 / 60)
// #endregion
// #region Controllers
/* 我在想这个 key 是不是应该放在 variables 那边... */
const key = {
left: false,
right: false,
up: false,
down: false,
/* 这两个的处理..也有点不一样,它只看按下的那一瞬,can jump can dash,然后在实行成功或被忽略后直接复位,不用管 keyup,另一个是,按键时间和组合问题... */
jump: false,
dash: false,
}
document.addEventListener("keydown", (e) => {
switch (e.key) {
case "ArrowLeft":
key.left = true
break
case "ArrowRight":
key.right = true
break
case "ArrowUp":
key.up = true
break
case "ArrowDown":
key.down = true
break
case "c":
if (e.repeat) break
key.jump = true
break
case "x":
if (e.repeat) break
key.dash = true
break
}
})
document.addEventListener("keyup", (e) => {
switch (e.key) {
case "ArrowLeft":
key.left = false
break
case "ArrowRight":
key.right = false
break
case "ArrowUp":
key.up = false
break
case "ArrowDown":
key.down = false
break
case "c":
// End the jump early if button released
key.jump = false
objects[0].speed.y = 0
break
}
})
if (window.DeviceOrientationEvent) {
window.addEventListener(
"deviceorientation",
function (event) {
const gamma = event.gamma
const beta = event.beta
if (gamma > 15) key.right = true
else if (gamma < -15) key.left = true
else {
key.right = false
key.left = false
}
},
false
)
window.addEventListener(
"touchstart",
function (event) {
key.jump = true
},
false
)
window.addEventListener(
"touchend",
function (event) {
key.jump = false
objects[0].speed.y = 0
},
false
)
}
// #endregion
// #region constructor
class RigidObject {
constructor(position, size) {
this.position = position
this.size = size
}
}
class Mario extends RigidObject {
constructor(position, size) {
super(position, size)
this.speed = new Vector2(0, 0)
this.state = STATES[0]
}
}
class Block extends RigidObject {
constructor(position, size) {
super(position, size)
}
}
// #endregion
// #region SetWorld
let mario = new Mario(new Vector2(0, 0), new Vector2(10, 10))
objects.push(mario)
for (let i = 0; i < 20; i++) {
objects.push(new Block(new Vector2((i - 1) * 20, i * 30), new Vector2(30, 10)))
}
// #endregion
// #region Physics & Movement
function applyGravity() {
if (mario.position.y + mario.size.y < playground.height) {
if (mario.speed.y < FALL_MAX_SPEED) {
mario.speed.y += GRAVITY * 0.1
} else {
mario.speed.y = FALL_MAX_SPEED
}
}
}
function applyMovement() {
if (key.left) {
mario.speed.x -= RUN_ACC * 0.05
}
if (key.right) {
mario.speed.x += RUN_ACC * 0.05
}
if (key.jump) {
mario.speed.y = -JUMP * 0.05
key.jump = false
}
if (key.dash) {
mario.speed.x *= 2
key.dash = false
}
mario.position = mario.position.add(mario.speed)
}
function applyFriction() {
if (mario.speed.x > 0) {
mario.speed.x -= FRICTION * 0.03
} else if (mario.speed.x < 0) {
mario.speed.x += FRICTION * 0.03
}
}
function applyCollision() {
// collision detection
objects.forEach((obj) => {
if (obj === mario) return
if (mario.position.x < obj.position.x + obj.size.x && mario.position.x + mario.size.x > obj.position.x && mario.position.y < obj.position.y + obj.size.y && mario.position.y + mario.size.y > obj.position.y) {
// collision detected!
let overlap = new Vector2(Math.min(mario.position.x + mario.size.x, obj.position.x + obj.size.x) - Math.max(mario.position.x, obj.position.x), Math.min(mario.position.y + mario.size.y, obj.position.y + obj.size.y) - Math.max(mario.position.y, obj.position.y))
if (overlap.x > overlap.y) {
// collision on y axis
if (mario.position.y < obj.position.y) {
// collision on top
mario.position.y -= overlap.y
mario.speed.y = 0
} else {
// collision on bottom
mario.position.y += overlap.y
mario.speed.y = 0
}
} else {
// collision on x axis
if (mario.position.x < obj.position.x) {
// collision on left
mario.position.x -= overlap.x
mario.speed.x = 0
} else {
// collision on right
mario.position.x += overlap.x
mario.speed.x = 0
}
}
}
})
// edge detection
if (mario.position.x < 0) {
mario.position.x = 0
mario.speed.x = 0
}
if (mario.position.x + mario.size.x > playground.width) {
mario.position.x = playground.width - mario.size.x
mario.speed.x = 0
}
if (mario.position.y < 0) {
mario.position.y = 0
mario.speed.y = 0
}
if (mario.position.y + mario.size.y > playground.height) {
mario.position.y = playground.height - mario.size.y
mario.speed.y = 0
}
}
function applyCamera() {
cameraAnchor = mario.position.add(new Vector2(-playground.width / 2, -playground.height / 2))
}
// #endregion
// #region States
// #endregion
</script>
</body>
</html>