silphius/public/assets/magic-swords/start.js
2024-07-25 16:39:05 -05:00

88 lines
2.0 KiB
JavaScript

const {Position} = wielder;
const shots = [];
const EVERY = 0.03;
const N = 14;
const SPREAD = 1;
const creating = [];
const promises = []
for (let i = 0; i < N; ++i) {
promises.push(ecs.create({
Collider: {
bodies: [
{
points: [
{x: -2.5, y: -2.5},
{x: 14, y: -2.5},
{x: 14, y: 2.5},
{x: -2.5, y: 2.5},
],
unstoppable: 1,
},
],
},
Controlled: {},
Direction: {direction: Math.TAU * (i / N)},
Forces: {},
Position: {x: Position.x, y: Position.y},
Speed: {},
Sprite: {
alpha: 0,
source: '/assets/magic-swords/magic-sword-shot.json',
},
Ticking: {},
VisibleAabb: {},
}));
}
for (const id of await Promise.all(promises)) {
creating.push(ecs.get(id));
}
const accumulated = {};
const shot = creating.shift();
shot.Sprite.alpha = 1;
accumulated[shot.id] = 0;
shots.push(shot)
let spawner = 0;
while (shots.length > 0) {
spawner += elapsed;
if (creating.length > 0 && spawner >= EVERY) {
const shot = creating.shift();
shot.Sprite.alpha = 1;
accumulated[shot.id] = 0;
shots.push(shot)
spawner -= EVERY;
}
const destroying = [];
for (const shot of shots) {
accumulated[shot.id] += elapsed;
if (accumulated[shot.id] <= SPREAD) {
shot.Speed.speed = 100 * (1 - (accumulated[shot.id] / SPREAD))
}
else {
const toward = Math.atan2(
where.y - shot.Position.y,
where.x - shot.Position.x,
)
shot.Speed.speed = 400;
shot.Direction.direction = (Math.TAU + toward) % Math.TAU;
if (Math.distance(where, shot.Position) < 4) {
delete accumulated[shot.id];
destroying.push(shot);
}
}
shot.Controlled.directionMove(shot.Direction.direction);
}
for (let i = 0; i < destroying.length; ++i) {
shots.splice(shots.indexOf(destroying[i]), 1);
destroying[i] = destroying[i].id;
}
ecs.destroyMany(destroying);
await wait(0)
}