Mercurial > code > home > repos > typorama
annotate src/first-scene.ts @ 0:6d2e2a8ac6f0 default tip
start from https://github.com/supertorpe/vite-ts-phaser-starter
author | drewp@bigasterisk.com |
---|---|
date | Sun, 13 Nov 2022 13:24:40 -0800 |
parents | |
children |
rev | line source |
---|---|
0
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
1 import Phaser from 'phaser'; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
2 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
3 /** |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
4 * FirstGameScene is an example Phaser Scene |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
5 * @class |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
6 * @constructor |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
7 * @public |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
8 */ |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
9 export class FirstGameScene extends Phaser.Scene { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
10 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
11 private gameOver!: boolean; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
12 private score!: number; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
13 private platforms!: Phaser.Physics.Arcade.StaticGroup; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
14 private player!: Phaser.Physics.Arcade.Sprite; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
15 private cursors!: Phaser.Types.Input.Keyboard.CursorKeys; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
16 private stars!: Phaser.Physics.Arcade.Group; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
17 private bombs!: Phaser.Physics.Arcade.Group; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
18 private scoreText!: Phaser.GameObjects.Text; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
19 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
20 constructor() { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
21 super('FirstGameScene'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
22 console.log('FirstGameScene.constructor()'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
23 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
24 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
25 preload() { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
26 console.log('FirstGameScene.preload'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
27 this.load.image('sky', 'assets/sky.png'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
28 this.load.image('ground', 'assets/platform.png'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
29 this.load.image('star', 'assets/star.png'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
30 this.load.image('bomb', 'assets/bomb.png'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
31 this.load.spritesheet('dude', 'assets/dude.png', { frameWidth: 32, frameHeight: 48 }); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
32 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
33 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
34 create() { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
35 console.log('FirstGameScene.create'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
36 // initialize variables |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
37 this.gameOver = false; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
38 this.score = 0; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
39 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
40 // A simple background for our game |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
41 this.add.image(400, 300, 'sky'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
42 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
43 // The platforms group contains the ground and the 2 ledges we can jump on |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
44 this.platforms = this.physics.add.staticGroup(); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
45 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
46 // Here we create the ground. |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
47 // Scale it to fit the width of the game (the original sprite is 400x32 in size) |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
48 this.platforms.create(400, 568, 'ground').setScale(2).refreshBody(); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
49 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
50 // Now let's create some ledges |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
51 this.platforms.create(600, 400, 'ground'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
52 this.platforms.create(50, 250, 'ground'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
53 this.platforms.create(750, 220, 'ground'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
54 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
55 // The player and its settings |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
56 this.player = this.physics.add.sprite(100, 450, 'dude'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
57 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
58 // Player physics properties. Give the little guy a slight bounce. |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
59 this.player.setBounce(0.2); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
60 this.player.setCollideWorldBounds(true); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
61 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
62 // Our player animations, turning, walking left and walking right. |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
63 this.anims.create({ |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
64 key: 'left', |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
65 frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }), |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
66 frameRate: 10, |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
67 repeat: -1 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
68 }); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
69 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
70 this.anims.create({ |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
71 key: 'turn', |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
72 frames: [{ key: 'dude', frame: 4 }], |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
73 frameRate: 20 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
74 }); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
75 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
76 this.anims.create({ |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
77 key: 'right', |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
78 frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }), |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
79 frameRate: 10, |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
80 repeat: -1 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
81 }); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
82 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
83 // Input Events |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
84 this.cursors = this.input.keyboard.createCursorKeys(); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
85 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
86 // Some stars to collect, 12 in total, evenly spaced 70 pixels apart along the x axis |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
87 this.stars = this.physics.add.group({ |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
88 key: 'star', |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
89 repeat: 11, |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
90 setXY: { x: 12, y: 0, stepX: 70 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
91 }); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
92 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
93 this.stars.children.iterate((child: Phaser.GameObjects.GameObject) => { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
94 // Give each star a slightly different bounce |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
95 (child as Phaser.Physics.Arcade.Sprite).setBounceY(Phaser.Math.FloatBetween(0.4, 0.8)); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
96 }); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
97 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
98 this.bombs = this.physics.add.group(); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
99 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
100 // The score |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
101 this.scoreText = this.add.text(16, 16, 'score: 0'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
102 this.scoreText.style.fontSize = '32px'; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
103 this.scoreText.style.setFill('#000'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
104 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
105 // Collide the player and the stars with the platforms |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
106 this.physics.add.collider(this.player, this.platforms); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
107 this.physics.add.collider(this.stars, this.platforms); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
108 this.physics.add.collider(this.bombs, this.platforms); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
109 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
110 // Checks to see if the player overlaps with any of the stars, if he does call the collectStar function |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
111 this.physics.add.overlap(this.player, this.stars, this.collectStar, undefined, this); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
112 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
113 this.physics.add.collider(this.player, this.bombs, this.hitBomb, undefined, this); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
114 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
115 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
116 update() { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
117 if (this.gameOver) { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
118 return; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
119 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
120 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
121 if (this.cursors.left.isDown) { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
122 this.player.setVelocityX(-160); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
123 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
124 this.player.anims.play('left', true); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
125 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
126 else if (this.cursors.right.isDown) { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
127 this.player.setVelocityX(160); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
128 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
129 this.player.anims.play('right', true); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
130 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
131 else { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
132 this.player.setVelocityX(0); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
133 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
134 this.player.anims.play('turn'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
135 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
136 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
137 if (this.cursors.up.isDown && this.player.body.touching.down) { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
138 this.player.setVelocityY(-330); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
139 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
140 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
141 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
142 collectStar(playerGO: Phaser.GameObjects.GameObject, starGO: Phaser.GameObjects.GameObject) { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
143 const player = playerGO as Phaser.Physics.Arcade.Sprite; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
144 const star = starGO as Phaser.Physics.Arcade.Sprite; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
145 star.disableBody(true, true); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
146 // Add and update the score |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
147 this.score += 10; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
148 this.scoreText.setText('Score: ' + this.score); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
149 if (this.stars.countActive(true) === 0) { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
150 // A new batch of stars to collect |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
151 this.stars.children.iterate((childGO: Phaser.GameObjects.GameObject) => { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
152 const child = childGO as Phaser.Physics.Arcade.Sprite; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
153 child.enableBody(true, child.x, 0, true, true); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
154 }); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
155 const x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
156 const bomb = this.bombs.create(x, 16, 'bomb'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
157 bomb.setBounce(1); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
158 bomb.setCollideWorldBounds(true); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
159 bomb.setVelocity(Phaser.Math.Between(-200, 200), 20); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
160 bomb.allowGravity = false; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
161 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
162 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
163 |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
164 hitBomb(playerGO: Phaser.GameObjects.GameObject) { |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
165 const player = playerGO as Phaser.Physics.Arcade.Sprite; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
166 this.physics.pause(); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
167 player.setTint(0xff0000); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
168 player.anims.play('turn'); |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
169 this.gameOver = true; |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
170 } |
6d2e2a8ac6f0
start from https://github.com/supertorpe/vite-ts-phaser-starter
drewp@bigasterisk.com
parents:
diff
changeset
|
171 } |