Skip to content

Commit 5c04b3a

Browse files
committed
Add new types of obstacle movement and difficulty amplification
1 parent 3dc40cb commit 5c04b3a

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

src/game/Obstacle.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,55 @@ export class Obstacle {
44
private scene: Phaser.Scene;
55
private sprite: Phaser.Physics.Arcade.Sprite;
66
private speed: number;
7+
private obstacleType: string;
8+
private movementType: string;
9+
private acceleration: number = 0;
10+
private flutterAmplitude: number = 0;
11+
private flutterFrequency: number = 0;
12+
private flutterOffset: number = 0;
713

8-
constructor(scene: Phaser.Scene, x: number, type: string) {
14+
constructor(
15+
scene: Phaser.Scene,
16+
x: number,
17+
obstacleType: string,
18+
movementType: string,
19+
score: number
20+
) {
921
this.scene = scene;
10-
this.sprite = this.scene.physics.add.sprite(x, -128, type);
11-
this.sprite.play(`${type}-fall`, true);
22+
this.obstacleType = obstacleType;
23+
this.movementType = movementType;
24+
this.sprite = this.scene.physics.add.sprite(x, -16, obstacleType);
25+
this.sprite.play(`${obstacleType}-fall`, true);
1226
this.sprite.setOrigin(0.5, 1);
1327

14-
this.speed = Phaser.Math.Between(30, 60);
28+
const minSpeed = 30 + score * 0.5;
29+
const maxSpeed = 60 + score;
30+
31+
this.speed = Phaser.Math.Between(minSpeed, maxSpeed);
32+
this.acceleration = 0;
33+
if (movementType === "fast") {
34+
this.acceleration = 200 + score * 5;
35+
}
36+
if (movementType === "flutter") {
37+
this.flutterAmplitude = Phaser.Math.Between(5, 15);
38+
this.flutterFrequency = Phaser.Math.FloatBetween(3, 6);
39+
this.flutterOffset = Phaser.Math.FloatBetween(0, Math.PI * 2);
40+
}
1541
}
1642

1743
update(deltaSeconds: number) {
18-
this.sprite.setVelocityY(this.speed);
44+
if (!this.sprite.active) return;
45+
if (this.movementType === "fast") {
46+
this.speed += this.acceleration * deltaSeconds;
47+
}
48+
if (this.movementType === "flutter") {
49+
const time = this.scene.time.now / 1000;
50+
const offset =
51+
Math.sin(time * this.flutterFrequency + this.flutterOffset) *
52+
this.flutterAmplitude;
53+
this.sprite.x += offset * deltaSeconds;
54+
}
55+
this.sprite.y += this.speed * deltaSeconds;
1956

2057
if (this.sprite.y > this.scene.cameras.main.height + 128) {
2158
this.sprite.destroy();

src/game/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const gameConfig: Phaser.Types.Core.GameConfig = {
1010
backgroundColor: "#9bbc0f",
1111
width: 160,
1212
height: 144,
13-
zoom: 6,
13+
zoom: 4,
1414
physics: {
1515
default: "arcade",
1616
arcade: {

src/game/scenes/GameScene.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,31 @@ export class GameScene extends Phaser.Scene {
144144

145145
spawnObstacle() {
146146
const obstacleTypes = ["paper", "mail", "stapler", "taxman", "briefcase"];
147-
const type = Phaser.Utils.Array.GetRandom(obstacleTypes);
148-
const x = Phaser.Math.Between(0, this.cameras.main.width - 16);
149-
const obstacle = new Obstacle(this, x, type);
147+
const weights = [
148+
{ type: "normal", weight: 100 },
149+
{ type: "fast", weight: Math.min(this.score * 10, 40) },
150+
{ type: "flutter", weight: Math.min(this.score * 10, 60) },
151+
];
152+
const totalWeight = weights.reduce((acc, w) => acc + w.weight, 0);
153+
const roll = Phaser.Math.Between(0, totalWeight);
154+
let sum = 0;
155+
let movementType = "normal";
156+
for (const w of weights) {
157+
sum += w.weight;
158+
if (roll <= sum) {
159+
movementType = w.type;
160+
break;
161+
}
162+
}
163+
const obstacleType = Phaser.Utils.Array.GetRandom(obstacleTypes);
164+
const x = Phaser.Math.Between(0, this.cameras.main.width);
165+
const obstacle = new Obstacle(
166+
this,
167+
x,
168+
obstacleType,
169+
movementType,
170+
this.score
171+
);
150172
this.obstacles.push(obstacle);
151173
}
152174

0 commit comments

Comments
 (0)