silphius/resources/magic-swords/start.js
2024-09-17 01:25:39 -05:00

92 lines
2.2 KiB
JavaScript

const {Player, 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: [
{
group: -1,
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,
},
],
collisionStartScript: '/resources/magic-swords/collision-start.js',
},
Controlled: {},
Direction: {direction: Math.TAU * (i / N)},
Forces: {},
Light: {brightness: 0},
Owned: {owner: Player ? Player.id : 0},
Position: {x: Position.x, y: Position.y},
Speed: {},
Sprite: {
alpha: 0,
source: '/resources/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 (accumulated[shot.id] > 1.5 || Math.distance(where, shot.Position) < 4) {
delete accumulated[shot.id];
shot.Sprite.alpha = 0;
ecs.destroy(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);
}
await wait();
}