perf: magic swords
This commit is contained in:
parent
3a37cf4938
commit
0bc8d05706
|
@ -1,7 +1,5 @@
|
|||
const {Player, Position} = wielder;
|
||||
|
||||
const shots = [];
|
||||
|
||||
const EVERY = 0.03;
|
||||
const N = 14;
|
||||
const SPREAD = 1;
|
||||
|
@ -48,43 +46,47 @@ 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)
|
||||
const shots = [
|
||||
{
|
||||
accumulated: 0,
|
||||
entity: 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)
|
||||
const entity = creating.shift();
|
||||
entity.Sprite.alpha = 1;
|
||||
shots.push({accumulated: 0, entity})
|
||||
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))
|
||||
shot.accumulated += elapsed;
|
||||
if (shot.accumulated <= SPREAD) {
|
||||
shot.entity.Speed.speed = 100 * (1 - (shot.accumulated / 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);
|
||||
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.Controlled.directionMove(shot.Direction.direction);
|
||||
shot.entity.Controlled.directionMove(shot.entity.Direction.direction);
|
||||
}
|
||||
for (let i = 0; i < destroying.length; ++i) {
|
||||
shots.splice(shots.indexOf(destroying[i]), 1);
|
||||
|
|
Loading…
Reference in New Issue
Block a user