-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameState.js
More file actions
139 lines (127 loc) · 4.33 KB
/
GameState.js
File metadata and controls
139 lines (127 loc) · 4.33 KB
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
"use strict"
class GameState {
constructor() {}
update() {
throw new Error("abstract")
}
}
class TitleScreenGameState extends GameState {
constructor() {
super()
this.fadeDirection = -1
this.alpha = 1
}
update() {
uiContext.clearRect(0, 0, playerCanvas.width, playerCanvas.height)
if (keys[ENTER]) {
titlebgm.pause()
menuchimesfx.play()
enemy.health = 0
currentGameState = bossInitializationState
return
}
graphicsContext.fillStyle = bgPattern
graphicsContext.fillRect(0, 0, graphicsCanvas.width, graphicsCanvas.height)
uiContext.drawImage(titleText, 52, 100)
this.alpha = this.alpha + this.fadeDirection * 0.00075 * deltaTime
if (!(0 <= this.alpha && this.alpha <= 1)) {
this.fadeDirection *= -1
this.alpha = Math.min(1, Math.max(0, this.alpha))
}
uiContext.save()
uiContext.globalAlpha = this.alpha
uiContext.drawImage(enterText, 145, 700)
uiContext.restore()
}
}
class BossInitializationState extends GameState {
constructor() {
super()
}
update() {
uiContext.clearRect(0, 0, enemyBulletCanvas.width, enemyBulletCanvas.height)
enemy.health += deltaTime * 0.500
if (enemy.health >= enemy.maxHealth) {
enemy.health = enemy.maxHealth
currentGameState = normalGameState
starttime = Date.now()
}
player.redraw(playerContext)
enemy.redraw(enemyContext)
}
}
class NormalGameState extends GameState {
constructor() {
super()
this.star1 = true
this.star2 = true
}
update() {
var hit = false
graphicsContext.clearRect(0, 0, playerCanvas.width, playerCanvas.height)
playerContext.clearRect(0, 0, playerCanvas.width, playerCanvas.height)
enemyContext.clearRect(0, 0, enemyCanvas.width, enemyCanvas.height)
playerBulletContext.clearRect(0, 0, playerBulletCanvas.width, playerBulletCanvas.height)
enemyBulletContext.clearRect(0, 0, enemyBulletCanvas.width, enemyBulletCanvas.height)
uiContext.clearRect(0, 0, enemyBulletCanvas.width, enemyBulletCanvas.height)
bgOffset += 1
bgOffset %= 2560
graphicsContext.fillStyle = bgPattern
graphicsContext.translate(0, bgOffset)
graphicsContext.fillRect(0, -bgOffset, WIDTH, HEIGHT)
graphicsContext.translate(0, -bgOffset)
star1Offset += 1.2
star1Offset %= 600
star2Offset += 1.4
star2Offset %= 446
this.star1 = !this.star1
this.star2 = !this.star2
if (this.star1) {
graphicsContext.fillStyle = star1Pattern
graphicsContext.translate(0, star1Offset)
graphicsContext.fillRect(0, -star1Offset, WIDTH, HEIGHT)
graphicsContext.translate(0, -star1Offset)
}
if (this.star2) {
graphicsContext.fillStyle = star2Pattern
graphicsContext.translate(0, star2Offset)
graphicsContext.fillRect(200, -star2Offset, WIDTH, HEIGHT)
graphicsContext.translate(0, -star2Offset)
}
player.update()
player.redraw(playerContext)
enemy.update()
enemy.redraw(enemyContext)
for (let i = 0; i < enemyBullets.length; i++) {
enemyBullets[i].update()
enemyBullets[i].redraw()
if (!enemyBullets[i].checkBounds()) {
enemyBullets.splice(i--, 1)
continue
}
if (enemyBullets[i].checkCollision()) {
deathsfx.play()
uiContext.drawImage(diededText, 144, 200)
clearInterval(mainTimer)
keys = []
}
}
for (let i = playerBullets.length - 1; i >= 0; i--) {
playerBullets[i].update()
playerBullets[i].redraw()
if (!playerBullets[i].checkBounds()) {
playerBullets.splice(i, 1)
continue
}
if (playerBullets[i].checkCollision()) {
enemy.health--
hit = true
playerBullets.splice(i, 1)
}
}
if (hit) {
var sfx = corehitPool.request()
sfx.play()
}
}
}