[READ-ONLY] Mirror of https://github.com/flo-bit/particle-surfer.
flo-bit.dev/particle-surfer/
1.7 kB
65 lines
1import * as THREE from "three";
2
3export class Particle {
4 pos: THREE.Vector2;
5 speed: THREE.Vector2;
6 acc: THREE.Vector2;
7 time: number;
8
9 normalizedSpeed: THREE.Vector2;
10
11 constructor() {
12 this.pos = new THREE.Vector2(0, 0);
13 this.speed = new THREE.Vector2(0, 0);
14 this.acc = new THREE.Vector2(0, 0);
15 this.time = Math.random() * 1.8 + 0.3;
16
17 this.normalizedSpeed = new THREE.Vector2(0, 0);
18 }
19 reset(
20 minX: number,
21 maxX: number,
22 minY: number,
23 maxY: number,
24 innerRingRadiusSquared: number,
25 outerRingRadiusSquared: number,
26 ) {
27 this.pos.set(
28 Math.random() * (maxX - minX) + minX,
29 Math.random() * (maxY - minY) + minY,
30 );
31 this.acc.set(0, 0);
32 this.speed.set(0, 0);
33 this.time = Math.random() * 1.8 + 0.3;
34 }
35 update(dt: number, maxSpeed: number) {
36 this.speed.add(this.acc);
37 this.speed.clampLength(0, maxSpeed);
38 this.pos.x += this.speed.x * dt * 50;
39 this.pos.y += this.speed.y * dt * 50;
40 this.time -= dt;
41
42 this.normalizedSpeed.set(this.speed.x / maxSpeed, this.speed.y / maxSpeed);
43 }
44 isDead(
45 minX: number,
46 maxX: number,
47 minY: number,
48 maxY: number,
49 innerRingRadiusSquared: number,
50 outerRingRadiusSquared: number,
51 ): boolean {
52 // check if within camera bounds
53
54 if (this.time < 0) return true;
55
56 if (this.pos.x < minX || this.pos.x > maxX) return true;
57 if (this.pos.y < minY || this.pos.y > maxY) return true;
58
59 const distSquared = this.pos.x * this.pos.x + this.pos.y * this.pos.y;
60 if (distSquared < innerRingRadiusSquared) return true;
61 if (distSquared > outerRingRadiusSquared) return true;
62
63 return false;
64 }
65}