-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
103 lines (87 loc) · 2.28 KB
/
game.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
import Phaser from "phaser"
let player = undefined
const playerWidth = 32
const playerHeight = 48
let playerFacing = "left"
let jumpTimer = 0
let movementKeys = undefined
let animsInitialized = false
class InitialScene extends Phaser.Scene {
constructor() {
super()
}
preload() {
const { load } = this
load.spritesheet("player", "assets/player.png", {
frameWidth: playerWidth,
frameHeight: playerHeight,
})
}
create() {
const { anims, physics, input } = this
if (!player) {
player = physics.add.sprite(playerWidth, playerHeight, "player")
player.body.velocity.y = 2 ** 20
player.body.collideWorldBounds = true
player.body.setSize(playerWidth, playerHeight)
}
if (!animsInitialized) {
anims.create({
key: "playerGoesLeft",
frames: anims.generateFrameNames("player", { start: 0, end: 3 }),
frameRate: 12,
repeat: -1,
})
anims.create({
key: "playerGoesRight",
frames: anims.generateFrameNames("player", { start: 5, end: 8 }),
frameRate: 12,
repeat: -1,
})
animsInitialized = true
}
if (!movementKeys) {
movementKeys = input.keyboard.createCursorKeys()
}
}
update() {
const { time } = this
player.body.velocity.x = 0
if (movementKeys.left.isDown) {
player.body.velocity.x = -150
if (playerFacing != "left") {
player.play("playerGoesLeft")
playerFacing = "left"
}
} else if (movementKeys.right.isDown) {
player.body.velocity.x = 150
if (playerFacing != "right") {
player.play("playerGoesRight")
playerFacing = "right"
}
} else if (playerFacing != "idle") {
player.stop()
if (playerFacing == "left") {
player.setFrame(0)
} else {
player.setFrame(5)
}
playerFacing = "idle"
}
if (
movementKeys.up.isDown &&
player.body.onFloor() &&
time.now > jumpTimer
) {
player.body.velocity.y = -250
jumpTimer = time.now + 750
}
}
}
new Phaser.Game({
type: Phaser.AUTO,
parent: "game",
scale: { mode: Phaser.Scale.FIT, autoCenter: Phaser.Scale.CENTER_BOTH },
scene: [InitialScene],
physics: { default: "arcade", arcade: { gravity: { y: 300 } } },
})