-
Notifications
You must be signed in to change notification settings - Fork 1
/
level1.ts
304 lines (272 loc) · 13.7 KB
/
level1.ts
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
module Gamejam {
const PLAYER_DEFAULT_VELOCITY = 350
export class Level1 extends Phaser.State {
bottleSprites: Phaser.Group
platforms: Phaser.Group
crate: Phaser.Sprite
player: Phaser.Group
playerTorso: Phaser.Sprite
playerArm: Phaser.Sprite
playerBasket: Phaser.Sprite
cursors: Phaser.CursorKeys
blabberDirection = -1
boss: Phaser.Sprite
music: Phaser.Sound
bottle_sound: Phaser.Sound
pcb_sound: Phaser.Sound
throw_sound: Phaser.Sound
attachedBottles = new Set<Phaser.Sprite>()
bottlesToDestroy = new Set<Phaser.Sprite>()
lastThrowTime = 0
spacebar: Phaser.Key
score: number = 0
scoreBanner: Phaser.Text
redEmitter: Phaser.Particles.Arcade.Emitter
greenEmitter: Phaser.Particles.Arcade.Emitter
preload() {
this.game.load.image('bg', 'assets/Background1024.png')
this.game.load.image('ground', 'assets/platform.png')
this.game.load.image('bottle', 'assets/mateflasche_scaled.png')
this.game.load.image('pcb', 'assets/platine_scaled.png')
this.game.load.spritesheet('dude', 'assets/blabber624_scaled.png', 444 / 6, 220)
this.game.load.spritesheet('arm', 'assets/blabberarm1248_scaled.png', 148 / 2, 0)
this.game.load.image('boss', 'assets/endboss_scaled.png')
this.game.load.image('matekasten', 'assets/matekasten_laengs_scaled.png')
this.game.load.image('redtriangle', 'assets/redtriangle.png')
this.game.load.image('greentriangle', 'assets/greentriangle.png')
this.load.audio('level01', 'assets/audio/level01.ogg');
this.load.audio('mate_snd', 'assets/audio/mate1.ogg');
this.load.audio('pcb_snd', 'assets/audio/schrott1.ogg');
this.load.audio('throw_snd', 'assets/audio/throw.ogg');
}
create() {
this.game.physics.startSystem(Phaser.Physics.ARCADE)
this.music = this.add.audio('level01',)
this.music.loopFull()
this.bottle_sound = this.add.audio('mate_snd')
this.pcb_sound = this.add.audio('pcb_snd')
this.throw_sound = this.add.audio('throw_snd')
let bg = this.game.add.sprite(0, 0, 'bg')
this.platforms = this.game.add.group()
this.platforms.enableBody = true
let ground : Phaser.Sprite = this.platforms.create(0, this.game.world.height - 16, 'ground')
ground.body.immovable = true
ground.scale.setTo(3, 2)
this.crate = this.game.add.sprite(Phaser.Math.random(150, this.world.width - 150), 0, 'matekasten')
this.crate.position.y = ground.position.y - this.crate.height
this.game.physics.enable(this.crate)
this.crate.body.immovable = true
this.bottleSprites = this.game.add.group()
this.createPlayer()
this.createBoss()
this.createScoreBanner()
this.greenEmitter = this.game.add.emitter(this.scoreBanner.centerX, this.scoreBanner.centerY, 200);
this.greenEmitter.makeParticles(['greentriangle'], 0, 50, true, true)
this.redEmitter = this.game.add.emitter(this.scoreBanner.centerX, this.scoreBanner.centerY, 200);
this.redEmitter.makeParticles(['redtriangle'], 0, 50, true, true)
this.cursors = this.game.input.keyboard.createCursorKeys();
this.spacebar = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR)
}
createScoreBanner() {
this.scoreBanner = this.game.add.text(this.world.width - 260, 16, 'SCORE 0', { font: "40px Arial Black", fill: "#ec008c", align: "center" });
this.scoreBanner.setShadow(5, 5, 'rgba(0, 0, 0, 0.5)', 0);
}
createBottle(x: number, y: number): Phaser.Sprite {
let item = 'bottle'
if (this.game.rnd.integer() % 2 == 1) {
item = 'pcb'
}
let sprite = this.bottleSprites.create(x, y, item)
this.game.physics.arcade.enable(sprite);
let body: Phaser.Physics.Arcade.Body = sprite.body
body.bounce.y = 0.3;
body.bounce.x = 0.3;
body.gravity.y = 300;
body.collideWorldBounds = true;
return sprite
}
arcadeBodyOf(sprite: Phaser.Sprite) {
let body: Phaser.Physics.Arcade.Body = sprite.body
return body
}
createBoss() {
this.boss = this.game.add.sprite(100, 10, 'boss')
this.game.physics.enable(this.boss)
let body = this.arcadeBodyOf(this.boss)
body.bounce = new Phaser.Point(1, 0)
body.velocity.x = 400
body.collideWorldBounds = true
}
createPlayer() {
this.player = this.game.add.group()
this.playerArm = this.player.create(80, this.game.world.height - 120, 'arm')
this.game.physics.arcade.enable(this.playerArm)
let body = this.arcadeBodyOf(this.playerArm)
body.immovable = true
body.setSize(this.playerArm.body.width, this.playerArm.body.height * 0.8, 0, this.playerArm.body.height * 0.1)
this.playerTorso = this.player.create(132, this.game.world.height - 150, 'dude')
this.game.physics.arcade.enable(this.playerTorso)
body = this.arcadeBodyOf(this.playerTorso)
body.setSize(body.width * 0.7, body.height, body.width * 0.1, 0)
body.gravity.y = 300
body.collideWorldBounds = true
this.playerTorso.animations.add('left', [1, 2], 5, true)
this.playerTorso.animations.add('right', [3, 4], 5, true)
this.playerArm.animations.add('left', [0], 0, false)
this.playerArm.animations.add('right', [1], 0, false)
this.playerBasket = this.player.create(this.playerArm.position.x - 1, this.playerArm.position.y - 100, '')
this.game.physics.arcade.enable(this.playerBasket)
this.playerBasket.scale.multiply(0.1, 5)
this.playerBasket.body.immovable = true
}
update() {
let directionChanged = false
this.bottleSprites.forEach((sprite: Phaser.Sprite, platforms, playerTorso, playerArm) => {
let body: Phaser.Physics.Arcade.Body = sprite.body
this.game.physics.arcade.collide(sprite, playerTorso)
let collidedWithGround = this.game.physics.arcade.collide(sprite, platforms)
let collidedWithArm = this.game.physics.arcade.collide(sprite, playerArm)
let collidedWithCrate = this.game.physics.arcade.collide(sprite, this.crate)
// must have collided with the arm and be at roughly right height
if (collidedWithArm && sprite.position.y + sprite.height <= playerArm.position.y + 1) {
if (this.attachedBottles.size < 3) {
this.attachedBottles.add(sprite)
}
}
if (collidedWithGround) {
if (!this.bottlesToDestroy.has(sprite)) {
this.score -= 1
this.redEmitter.start(true, 1000, 50, 100)
this.initiateBottleDestruction(sprite)
}
}
if (collidedWithCrate) {
if (!this.bottlesToDestroy.has(sprite)) {
this.score += 1
this.greenEmitter.start(true, 1000, 50, 100)
this.initiateBottleDestruction(sprite)
}
}
}, this, true, this.platforms, this.playerTorso, this.playerArm)
this.game.physics.arcade.collide(this.player, this.platforms)
this.attachedBottles.forEach(bottle => {
this.game.physics.arcade.collide(bottle, this.playerBasket)
console.log(`collided ${bottle} with basket`)
}, this)
// drop bottles
if (this.cursors.down.isDown || this.spacebar.isDown) {
// give each bottle an initial push so it won't be recaught before gravity accelerates it downwards
this.attachedBottles.forEach(bottle => bottle.position.y += 5)
this.attachedBottles.clear()
}
if (this.cursors.left.isDown) {
// Move to the left
if (this.blabberDirection != -1) {
directionChanged = true
this.blabberDirection = -1
}
} else if (this.cursors.right.isDown) {
// Move to the right
if (this.blabberDirection != 1) {
directionChanged = true
this.blabberDirection = 1
}
}
if (directionChanged && this.blabberDirection < 0) {
this.attachedBottles.forEach(bottle => {
let playerCenter = this.playerTorso.position.x + (this.playerTorso.width / 2) - 20
console.log(`playerCenter = ${playerCenter}`)
bottle.position.x = playerCenter - (bottle.position.x - playerCenter)
}, this)
}
if (directionChanged && this.blabberDirection > 0) {
this.attachedBottles.forEach(bottle => {
let playerCenter = this.playerTorso.position.x + (this.playerTorso.width / 2) - 20
console.log(`playerCenter = ${playerCenter}`)
bottle.position.x = playerCenter + (playerCenter - bottle.position.x)
}, this)
}
// Reset the players velocity (movement)
if (this.cursors.left.isDown) {
this.playerArm.body.velocity.x = -PLAYER_DEFAULT_VELOCITY
this.playerTorso.body.velocity.x = -PLAYER_DEFAULT_VELOCITY
this.playerTorso.animations.play('left')
this.playerArm.animations.play('left')
} else if (this.cursors.right.isDown) {
this.playerArm.body.velocity.x = PLAYER_DEFAULT_VELOCITY
this.playerTorso.body.velocity.x = PLAYER_DEFAULT_VELOCITY
this.playerTorso.animations.play('right')
this.playerArm.animations.play('right')
} else {
// Stand still
this.playerTorso.animations.stop()
if (this.blabberDirection > 0) {
this.playerTorso.frame = 3
}
if (this.blabberDirection < 0) {
this.playerTorso.frame = 0
}
this.playerArm.body.velocity.x = 0
this.playerTorso.body.velocity.x = 0
}
this.playerArm.body.velocity = this.playerTorso.body.velocity
// process direction changes
if (this.blabberDirection < 0) {
this.playerArm.position.x = this.playerTorso.position.x - 60
this.playerArm.position.y = this.playerTorso.position.y + 90
this.playerTorso.body.setSize(55, 200, 10, 0);
this.playerBasket.position.x = this.playerArm.position.x - 1
this.playerBasket.position.y = this.playerArm.position.y - 100
}
if (this.blabberDirection > 0) {
this.playerArm.position.x = this.playerTorso.position.x + 50
this.playerArm.position.y = this.playerTorso.position.y + 90
this.playerTorso.body.setSize(55, 200, 0, 0);
this.playerBasket.position.x = this.playerArm.position.x + this.playerArm.width
this.playerBasket.position.y = this.playerArm.position.y - 100
}
if ((this.game.time.now - this.lastThrowTime > 5000 && this.bottleSprites.length < 5) || this.bottleSprites.length == 0) {
this.lastThrowTime = this.game.time.now
this.throwBottle()
}
this.scoreBanner.setText(`SCORE ${this.score}`)
}
throwBottle() {
let bottle = this.createBottle(this.boss.centerX, this.boss.centerY)
let body = this.arcadeBodyOf(bottle)
if (this.boss.centerX > this.world.centerX)
body.velocity.x = 50 + Phaser.Math.random(0, 300)
else
body.velocity.x = -50 - Phaser.Math.random(0, 300)
body.velocity.y = -50
this.throw_sound.play()
}
initiateBottleDestruction(bottle: Phaser.Sprite) {
if (this.bottlesToDestroy.has(bottle))
return
this.bottlesToDestroy.add(bottle)
bottle.alpha = 0.5
bottle.body.angularVelocity = 150
bottle.anchor.set(0.5, 0.5)
bottle.position.add(0, bottle.height / 2)
if (bottle.key === 'pcb') {
this.pcb_sound.play()
} else {
this.bottle_sound.play()
}
setTimeout(_ => {
bottle.destroy()
this.bottlesToDestroy.delete(bottle)
}, 1000)
}
render() {
// this.game.debug.text(`player velocity=${this.playerTorso.body.velocity} attachedBottles.size=${this.attachedBottles.size}`, 16, 16)
// this.game.debug.text(`boss velocity.x=${this.boss.body.velocity.x}`, 16, 32)
// this.game.debug.text(`score=${this.score}`, 16, 48)
// this.game.debug.body(this.playerTorso)
// this.game.debug.body(this.playerArm)
// this.game.debug.body(this.playerBasket)
// this.bottleSprites.forEach(s => this.game.debug.body(s), this)
}
}
}