summaryrefslogtreecommitdiff
path: root/src/engine/entities/Particles.ts
blob: 5381b23010b031d86c85ed4f8b266a83775abe37 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { Entity, EntityNames } from ".";
import {
  BoundingBox,
  Component,
  ComponentNames,
  Life,
  Renderable,
} from "../components";
import { Coord2D, Dimension2D, DrawArgs } from "../interfaces";
import { colors } from "../utils";
import { normalRandom } from "../utils/random";

export interface ParticleSpawnOptions {
  spawnerDimensions: Dimension2D;
  center: Coord2D;
  spawnerShape: "circle" | "rectangle";
  particleShape: "circle" | "rectangle";
  particleCount: number;
  particleMeanLife: number;
  particleLifeVariance: number;
  particleMeanSize: number;
  particleSizeVariance: number;
  particleMeanSpeed: number;
  particleSpeedVariance: number;
  particleColors: Array<string>;
}

const DEFAULT_PARTICLE_SPAWN_OPTIONS: ParticleSpawnOptions = {
  spawnerDimensions: { width: 0, height: 0 },
  center: { x: 0, y: 0 },
  spawnerShape: "circle",
  particleShape: "circle",
  particleCount: 50,
  particleMeanLife: 200,
  particleLifeVariance: 50,
  particleMeanSize: 12,
  particleSizeVariance: 1,
  particleMeanSpeed: 2,
  particleSpeedVariance: 1,
  particleColors: [colors.gray, colors.aqua, colors.lightAqua],
};

interface Particle {
  position: Coord2D;
  velocity: Coord2D;
  dimension: Dimension2D;
  color: string;
  life: number;
  shape: "circle" | "rectangle";
}

class ParticleRenderer extends Component implements Renderable {
  private particles: Array<Particle>;
  private onDeath?: () => void;

  constructor(particles: Array<Particle> = [], onDeath?: () => void) {
    super(ComponentNames.Sprite);

    this.particles = particles;
    this.onDeath = onDeath;
  }

  public update(dt: number) {
    this.particles = this.particles.filter((particle) => {
      particle.position.x += particle.velocity.x * dt;
      particle.position.y += particle.velocity.y * dt;
      particle.life -= dt;
      return particle.life > 0;
    });

    if (this.particles.length === 0 && this.onDeath) {
      this.onDeath();
    }
  }

  public draw(ctx: CanvasRenderingContext2D, _drawArgs: DrawArgs) {
    for (const particle of this.particles) {
      ctx.fillStyle = particle.color;
      if (particle.shape === "circle") {
        ctx.beginPath();

        ctx.ellipse(
          particle.position.x,
          particle.position.y,
          particle.dimension.width / 2,
          particle.dimension.height / 2,
          0,
          0,
          Math.PI * 2,
        );
        ctx.fill();
      } else {
        ctx.fillRect(
          particle.position.x - particle.dimension.width / 2,
          particle.position.y - particle.dimension.height / 2,
          particle.dimension.width,
          particle.dimension.height,
        );
      }
    }
  }
}

export class Particles extends Entity {
  constructor(options: Partial<ParticleSpawnOptions>) {
    super(EntityNames.Particles);

    const spawnOptions = {
      ...DEFAULT_PARTICLE_SPAWN_OPTIONS,
      ...options,
    };
    const particles = Array(options.particleCount)
      .fill(0)
      .map(() => Particles.spawnParticle(spawnOptions));

    this.addComponent(new Life(true));
    this.addComponent(
      new ParticleRenderer(particles, () => {
        const life = this.getComponent<Life>(ComponentNames.Life);
        life.alive = false;
        this.addComponent(life);
      }),
    );

    this.addComponent(
      new BoundingBox(
        {
          x: 0,
          y: 0,
        },
        {
          width: spawnOptions.spawnerDimensions.width,
          height: spawnOptions.spawnerDimensions.height,
        },
        0,
      ),
    );
  }

  static spawnParticle(options: ParticleSpawnOptions) {
    const angle = Math.random() * Math.PI * 2;
    const speed = normalRandom(
      options.particleMeanSpeed,
      options.particleSpeedVariance,
    );
    const life = normalRandom(
      options.particleMeanLife,
      options.particleLifeVariance,
    );
    const size = normalRandom(
      options.particleMeanSize,
      options.particleSizeVariance,
    );
    const color =
      options.particleColors[
        Math.floor(Math.random() * options.particleColors.length)
      ];
    const position = {
      x:
        options.center.x +
        (Math.cos(angle) * options.spawnerDimensions.width) / 2,
      y:
        options.center.y +
        (Math.sin(angle) * options.spawnerDimensions.height) / 2,
    };
    if (options.spawnerShape === "rectangle") {
      // determine a random position on the edge of the spawner based on the angle
      const halfWidth = options.spawnerDimensions.width / 2;
      const halfHeight = options.spawnerDimensions.height / 2;

      if (angle < Math.PI / 4 || angle > (Math.PI * 7) / 4) {
        position.x += halfWidth;
        position.y += Math.tan(angle) * halfWidth;
      } else if (angle < (Math.PI * 3) / 4) {
        position.y += halfHeight;
        position.x += halfHeight / Math.tan(angle);
      } else if (angle < (Math.PI * 5) / 4) {
        position.x -= halfWidth;
        position.y -= Math.tan(angle) * halfWidth;
      } else {
        position.y -= halfHeight;
        position.x -= halfHeight / Math.tan(angle);
      }
    }

    return {
      position,
      velocity: {
        x: Math.cos(angle) * speed,
        y: Math.sin(angle) * speed,
      },
      color,
      life,
      dimension: { width: size, height: size },
      shape: options.particleShape,
    };
  }
}