silphius/resources/magic-swords/start.js
2024-10-16 06:05:06 -05:00

91 lines
2.1 KiB
JavaScript

const {Player, Position} = wielder;
const EVERY = 0.03;
const N = 14;
const SPREAD = 1;
const creating = [];
const offset = Math.random() * Math.TAU;
for (let i = 0; i < N; ++i) {
creating.push(ecs.get(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: offset + 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: {},
})));
}
const shot = creating.shift();
shot.Sprite.alpha = 1;
const shots = [
{
accumulated: 0,
entity: shot,
},
];
let spawner = 0;
while (shots.length > 0) {
spawner += elapsed;
if (creating.length > 0 && spawner >= EVERY) {
const entity = creating.shift();
entity.Sprite.alpha = 1;
shots.push({accumulated: 0, entity})
spawner -= EVERY;
}
const destroying = [];
for (const shot of shots) {
shot.accumulated += elapsed;
if (shot.accumulated <= SPREAD) {
shot.entity.Speed.speed = 100 * (1 - (shot.accumulated / SPREAD))
}
else {
if (!shot.oriented) {
const toward = Math.atan2(
where.y - shot.entity.Position.y,
where.x - shot.entity.Position.x,
)
shot.entity.Speed.speed = 400;
shot.entity.Direction.direction = (Math.TAU + toward) % Math.TAU;
shot.oriented = true;
}
if (shot.accumulated > 1.5) {
shot.entity.Sprite.alpha = 0;
ecs.destroy(shot.entity.id);
destroying.push(shot);
}
}
shot.entity.Controlled.directionMove(shot.entity.Direction.direction);
}
for (let i = 0; i < destroying.length; ++i) {
shots.splice(shots.indexOf(destroying[i]), 1);
}
await wait();
}