Skip to content

Commit f3313f4

Browse files
Added settling effect to leaves in autumn theme (#798)
* Added settling effect to leaves in autumn theme * Used prettier to format the code style * Implemented falling effect based on geometrical center of leaves * Change settleDuration range from 2-5 to 4-7 --------- Co-authored-by: Anthony Shaw <anthony.p.shaw@gmail.com>
1 parent c59fb82 commit f3313f4

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/panel/effects/leaves.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class Leaf {
3333
color: string;
3434
rotation: number;
3535
rotationSpeed: number;
36+
settled: boolean;
37+
settleTime: number;
38+
settleDuration: number;
3639

3740
constructor(
3841
origin: Vector2,
@@ -50,9 +53,17 @@ class Leaf {
5053
// randomize start values a bit
5154
this.dx = Math.random() * 100;
5255
this.rotation = Math.random() * Math.PI * 2; // Random initial rotation
56+
57+
this.settled = false;
58+
this.settleTime = 0;
59+
this.settleDuration = floorRandom(4, 7);
5360
}
5461

5562
update(timeDelta: number) {
63+
if (this.settled) {
64+
return;
65+
}
66+
5667
this.position.y += this.velocity.y * timeDelta;
5768

5869
// oscillate the x value between -amplitude and +amplitude
@@ -195,14 +206,29 @@ export class LeafEffect implements Effect {
195206
var particle = this.particles[i];
196207
particle.update(timeDelta);
197208

198-
if (particle.position.y > this.canvas.height - this.floor) {
199-
// reset the particle to the top and a random x position
200-
particle.position.y = this.canvas.height - this.treeLine;
201-
particle.position.x = particle.origin.x =
202-
Math.random() * this.canvas.width;
203-
particle.dx = Math.random() * 100;
204-
// Reset rotation to a random value for variety
205-
particle.rotation = Math.random() * Math.PI * 2;
209+
var leafCenterY = particle.position.y + 119.5 * this.scale;
210+
211+
if (leafCenterY >= this.canvas.height - this.floor) {
212+
if (!particle.settled) {
213+
particle.settled = true;
214+
particle.settleTime = timeNow;
215+
particle.position.y =
216+
this.canvas.height - this.floor - 119.5 * this.scale;
217+
} else {
218+
if (
219+
timeNow - particle.settleTime >=
220+
particle.settleDuration
221+
) {
222+
particle.position.y = particle.origin.y =
223+
this.canvas.height - this.treeLine;
224+
particle.position.x = particle.origin.x =
225+
Math.random() * this.canvas.width;
226+
particle.dx = Math.random() * 100;
227+
particle.rotation = Math.random() * Math.PI * 2;
228+
particle.settled = false;
229+
particle.settleDuration = floorRandom(2, 5);
230+
}
231+
}
206232
}
207233
}
208234

0 commit comments

Comments
 (0)