-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
72 lines (59 loc) · 1.58 KB
/
player.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
import {
incrementCustomProperty,
setCustomProperty,
getCustomProperty,
} from "./updateCustomProperty.js"
const playerElem = document.querySelector("[data-player]")
const JUMP_SPEED = 0.45
const GRAVITY = 0.0015
const PLAYER_FRAME_COUNT = 2
const FRAME_TIME = 100
let isJumping
let playerFrame
let currentFrameTime
let yVelocity
export function setupPlayer() {
isJumping = false
playerFrame = 0
currentFrameTime = 0
yVelocity = 0
setCustomProperty(playerElem, "--bottom", 0)
document.removeEventListener("keydown", onJump)
document.addEventListener("keydown", onJump)
}
export function updatePlayer(delta, speedScale) {
handleRun(delta, speedScale)
handleJump(delta)
}
export function getPlayerRect() {
return playerElem.getBoundingClientRect()
}
export function setPlayerLose() {
playerElem.src = "imgs/playerJumps.png"
}
function handleRun(delta, speedScale) {
if (isJumping) {
playerElem.src = `imgs/playerJumps.png`
return
}
if (currentFrameTime >= FRAME_TIME) {
playerFrame = (playerFrame + 1) % PLAYER_FRAME_COUNT
playerElem.src = `imgs/playerRuns${playerFrame}.png`
currentFrameTime -= FRAME_TIME
}
currentFrameTime += delta * speedScale
}
function handleJump(delta) {
if (!isJumping) return
incrementCustomProperty(playerElem, "--bottom", yVelocity * delta)
if (getCustomProperty(playerElem, "--bottom") <= 0) {
setCustomProperty(playerElem, "--bottom", 0)
isJumping = false
}
yVelocity -= GRAVITY * delta
}
function onJump(e) {
if (e.code !== "Space" || isJumping) return
yVelocity = JUMP_SPEED
isJumping = true
}