silphius/public/assets/magic-swords/start.js

92 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-07-26 10:36:59 -05:00
const {Player, Position} = wielder;
2024-07-25 11:00:25 -05:00
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: [
{
2024-07-26 19:28:28 -05:00
group: -1,
2024-07-25 11:00:25 -05:00
points: [
2024-07-25 16:39:05 -05:00
{x: -2.5, y: -2.5},
{x: 14, y: -2.5},
{x: 14, y: 2.5},
{x: -2.5, y: 2.5},
2024-07-25 11:00:25 -05:00
],
unstoppable: 1,
},
],
2024-07-26 10:36:59 -05:00
collisionStartScript: '/assets/magic-swords/collision-start.js',
2024-07-25 11:00:25 -05:00
},
Controlled: {},
Direction: {direction: Math.TAU * (i / N)},
Forces: {},
2024-07-27 15:28:08 -05:00
Light: {brightness: 0.1},
2024-07-26 10:36:59 -05:00
Owned: {owner: Player ? Player.id : 0},
2024-07-25 11:00:25 -05:00
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];
2024-07-26 10:36:59 -05:00
shot.Sprite.alpha = 0;
ecs.destroy(shot.id);
2024-07-25 11:00:25 -05:00
destroying.push(shot);
}
}
shot.Controlled.directionMove(shot.Direction.direction);
}
for (let i = 0; i < destroying.length; ++i) {
shots.splice(shots.indexOf(destroying[i]), 1);
}
2024-07-26 10:36:59 -05:00
await wait(0);
2024-07-25 11:00:25 -05:00
}