-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.js
More file actions
261 lines (247 loc) · 9.53 KB
/
GameObject.js
File metadata and controls
261 lines (247 loc) · 9.53 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
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
"use strict"
class GameObject {
constructor(position = new Vector(), hitboxType = HitBoxType.RED22, rotation = 0) {
this.position = position
this.hitboxType = hitboxType
this.rotation = rotation
}
update() {
throw new Error("GameObject::update not overridden")
}
checkCollision() {
throw new Error("GameObject::checkCollision not overridden")
}
getVertices() {
var vertices = []
for (let i = 0; i < hitBoxes[this.hitboxType].vertices.length; i++) {
/*
* broken
if (this.rotation == 0) {
vertices[i] = this.position.add(hitBoxes[this.hitboxType].vertices[i])
} else {
var tmp = this.position.add(hitBoxes[this.hitboxType].vertices[i])
vertices[i] = tmp.rotateAbout(this.getCenter(), this.rotation)
}
*/
vertices[i] = this.position.add(hitBoxes[this.hitboxType].vertices[i])
}
return vertices
}
getCenter() {
return new Vector(this.position.x + (hitBoxes[this.hitboxType].width - 1) / 2, this.position.y - (hitBoxes[this.hitboxType].height - 1) / 2)
}
redraw(context = enemyBulletContext) {
context.save()
if (this.rotation) {
context.translate(this.position.x + hitBoxes[this.hitboxType].width / 2,
this.position.y + hitBoxes[this.hitboxType].height / 2)
context.rotate(this.rotation)
context.drawImage(
hitBoxes[this.hitboxType].src,
hitBoxes[this.hitboxType].x,
hitBoxes[this.hitboxType].y,
hitBoxes[this.hitboxType].width,
hitBoxes[this.hitboxType].height,
-hitBoxes[this.hitboxType].width / 2,
-hitBoxes[this.hitboxType].height / 2,
hitBoxes[this.hitboxType].width,
hitBoxes[this.hitboxType].height)
context.restore()
} else {
context.drawImage(
hitBoxes[this.hitboxType].src,
hitBoxes[this.hitboxType].x,
hitBoxes[this.hitboxType].y,
hitBoxes[this.hitboxType].width,
hitBoxes[this.hitboxType].height,
Math.floor(this.position.x),
Math.floor(this.position.y),
hitBoxes[this.hitboxType].width,
hitBoxes[this.hitboxType].height)
}
}
checkBounds() {
if (this.position.x < -hitBoxes[this.hitboxType].width || this.position.x > WIDTH
|| this.position.y < -hitBoxes[this.hitboxType].height || this.position.y > HEIGHT) {
return false
}
return true
}
}
class Bullet extends GameObject {
constructor(position = new Vector(), velocity = new Vector(), hitboxType = HitBoxType.RED22, rotation = 0) {
super(position, hitboxType, rotation)
this.velocity = velocity
}
update() {
this.position.x += this.velocity.x * deltaSeconds
this.position.y += this.velocity.y * deltaSeconds
this.redraw()
}
}
class PlayerBullet extends Bullet {
constructor(position = new Vector(), velocity = new Vector(0, -1500), hitboxType = HitBoxType.REDKUNAI, rotation = Math.PI) {
super(position, velocity, hitboxType, rotation)
}
checkCollision() {
return (collisionTest(this, enemy))
}
redraw() {
super.redraw(playerBulletContext)
}
}
class EnemyBullet extends Bullet {
constructor(position = new Vector(), velocity = new Vector(), hitboxType = HitBoxType.RED22, rotation = 0) {
super(position, velocity, hitboxType, rotation)
}
checkCollision() {
return (collisionTest(this, player))
}
}
class Player extends GameObject {
constructor(position = new Vector(), hitboxType = HitBoxType.BLUE12) {
super(position, hitboxType)
this.bulletHitBoxType = HitBoxType.REDKUNAI
this.normalSpeed = 425
this.slowSpeed = 125
this.gunCooldown = 50
this.bombCooldown = 1000
this.currentGunCooldown = 0
this.currentBombCooldown = 0
this.bombs = 3
this.currentSprite = 0
}
redraw(context = playerContext) {
graphicsContext.drawImage(shipSheet, this.currentSprite++ * 64, 0, 64, 68, this.position.x - 32 + 6, this.position.y - 34 + 6, 64, 68)
this.currentSprite %= shipCount
super.redraw(context)
}
update() {
this.currentGunCooldown -= deltaTime
this.currentBombCooldown -= deltaTime
var moveVector = new Vector()
var speed = keys[SHIFT] ? this.slowSpeed : this.normalSpeed // if shift is held down, then slow speed
if (keys[w] || keys[UP]) {
moveVector.y -= 1
// console.log("you're holding down 'w'")
}
if (keys[a] || keys[LEFT]) {
moveVector.x -= 1
// console.log("you're holding down 'a'")
}
if (keys[s] || keys[DOWN]) {
moveVector.y += 1
// console.log("you're holding down 's'")
}
if (keys[d] || keys[RIGHT]) {
moveVector.x += 1
// console.log("you're holding down 'd'")
}
if (keys[c] || autofire) {
if (this.currentGunCooldown <= 0) {
this.fireBullet()
this.currentGunCooldown = this.gunCooldown
}
}
/*
if (keys[x]) {
if (this.currentBombCooldown <= 0 && this.bombs > 0) {
var sfx = new Audio(SPELLCARD)
sfx.play()
enemyBullets = []
this.currentBombCooldown = this.bombCooldown
this.bombs--
}
}
*/
moveVector = moveVector.normalize()
this.position.x += moveVector.x * speed * deltaSeconds
this.position.y += moveVector.y * speed * deltaSeconds
this.position.x = Math.max(0, Math.min(WIDTH - hitBoxes[this.hitboxType].width, this.position.x))
this.position.y = Math.max(0, Math.min(HEIGHT - hitBoxes[this.hitboxType].height, this.position.y))
}
fireBullet() {
var sfx = laserPool.request()
sfx.play()
var innerleftv = this.position.clone()
innerleftv.x -= hitBoxes[this.bulletHitBoxType].width
innerleftv.y -= 12
var innerleft = new PlayerBullet(innerleftv, new Vector(0, -1500), this.bulletHitBoxType)
var innerrightv = this.position.clone()
innerrightv.x += hitBoxes[this.hitboxType].width
innerrightv.y -= 12
var innerright = new PlayerBullet(innerrightv, new Vector(0, -1500), this.bulletHitBoxType)
var outerleftv = innerleft.position.clone()
outerleftv.x -= hitBoxes[this.bulletHitBoxType].width
outerleftv.y += 14
var outerleft = new PlayerBullet(outerleftv, new Vector(0, -1500), this.bulletHitBoxType)
var outerrightv = innerright.position.clone()
outerrightv.x += hitBoxes[this.bulletHitBoxType].width
outerrightv.y += 14
var outerright = new PlayerBullet(outerrightv, new Vector(0, -1500), this.bulletHitBoxType)
playerBullets.push(innerleft)
playerBullets.push(innerright)
playerBullets.push(outerleft)
playerBullets.push(outerright)
}
}
class Enemy extends GameObject {
constructor(position = new Vector(), hitboxType = HitBoxType.RED22, health = 1) {
super(position, hitboxType)
this.health = health
this.maxHealth = health
this.states = []
this.currentState
}
}
class Boss extends Enemy {
constructor(position = new Vector(), hitboxType = HitBoxType.GREENBOSS76X82, health = 1000) {
super(position, hitboxType, health)
this.states.push(new BossMultiFlowerState(this))
this.states.push(new TransitionState(this, 2, new Vector(400 - 31, 30), 300))
this.states.push(new BossSpiralState(this))
this.states.push(new TransitionState(this, 4, new Vector(400 - 31, 30), 300))
this.states.push(new BossCurtainState(this))
this.currentState = this.states[0]
this.statesRemaining = 1
}
moveTowards(destination, speed) {
if (calculateDistance(destination, this.position) <= speed * deltaSeconds) {
this.position.x = destination.x
this.position.y = destination.y
return true
}
var direction = new Vector(destination.x - this.position.x, destination.y - this.position.y)
var normal = direction.normalize()
this.position.x += normal.x * speed * deltaSeconds
this.position.y += normal.y * speed * deltaSeconds
return false
}
update() {
this.currentState.update()
}
redraw(context = enemyContext) {
super.redraw(context)
uiContext.drawImage(enemyText, 0, 0)
if (this.health > 0) {
uiContext.fillStyle = "#00FF00"
uiContext.fillRect(110, 10, (WIDTH - 2 * 110) * this.health / this.maxHealth, 10)
var stateimg = zeroText
if (this.statesRemaining == 1) {
stateimg = oneText
}
uiContext.drawImage(stateimg, 90, 10)
}
}
}
class HitBox {
constructor(imagesrc = BULLETPNG, objectPosition = new Vector(), imageDimensions = new Vector(), radius = 0, vertices = null) {
this.src = imagesrc
this.x = objectPosition.x
this.y = objectPosition.y
this.width = imageDimensions.x
this.height = imageDimensions.y
this.radius = radius
this.vertices = vertices
}
}