-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnow.ts
80 lines (69 loc) · 1.79 KB
/
snow.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
class Flake {
private object: Phaser.GameObjects.Graphics;
private width: number;
private height: number;
private x = -1;
private y = -1;
private drift = 0;
private fallSpeed = 0;
prevFlake?: Flake;
constructor(scene: Phaser.Scene, w: number, h: number) {
this.object = scene.add.graphics()
this.object.fillStyle(0xffffff, 1);
this.object.fillRect(0, 0, 7, 7);
this.width = w;
this.height = h;
this.y = Phaser.Math.Between(0, h);
this.reset();
}
reset() {
this.x = Phaser.Math.Between(0, this.width);
this.drift = Phaser.Math.Between(-1, 1) * (.05 + Math.random() * .1);
this.fallSpeed = 1 + Math.random() * 10;
this.object.scaleX = .1 + Math.random();
this.object.scaleY = this.object.scaleX;
this.object.alpha = .1 + Math.random();
this.object.depth = 5;
}
move() {
this.x += this.drift;
this.y += this.fallSpeed;
if (this.y > this.height) {
this.y = -10;
this.reset();
}
this.object.setPosition(this.x, this.y);
this.prevFlake?.move();
}
destroy() {
this.object.visible = true;
this.object.destroy();
this.prevFlake?.object.destroy();
}
}
export class Snow {
private width: number;
private height: number;
private lastFlake?: Flake;
private maxFlakes = 250;
constructor(scene: Phaser.Scene) {
this.width = +scene.game.config.width;
this.height = +scene.game.config.height;
this.createFlakes(scene);
}
createFlakes(scene: Phaser.Scene) {
for (let i = 0; i < this.maxFlakes; i++) {
const flake = new Flake(scene, this.width, this.height);
if (this.lastFlake) {
flake.prevFlake = this.lastFlake;
}
this.lastFlake = flake;
}
}
update() {
this.lastFlake?.move();
}
destroy() {
this.lastFlake?.destroy();
}
}