44 lines
1.5 KiB
JavaScript
44 lines
1.5 KiB
JavaScript
// size -> { textureKey, radius, texSize, minSpeed, maxSpeed }
|
|
export const ASTEROID_CONFIG = {
|
|
large: { textureKey: 'asteroid_large', radius: 50, texSize: 120, minSpeed: 20, maxSpeed: 60 },
|
|
medium: { textureKey: 'asteroid_medium', radius: 26, texSize: 64, minSpeed: 40, maxSpeed: 100 },
|
|
small: { textureKey: 'asteroid_small', radius: 12, texSize: 32, minSpeed: 60, maxSpeed: 140 }
|
|
};
|
|
|
|
export default class Asteroid {
|
|
constructor(scene, x, y, size, group) {
|
|
this.scene = scene;
|
|
this.size = size;
|
|
this.alive = true;
|
|
|
|
const cfg = ASTEROID_CONFIG[size];
|
|
this.sprite = scene.physics.add.sprite(x, y, cfg.textureKey);
|
|
group.add(this.sprite);
|
|
this.sprite.gameEntity = this;
|
|
this.sprite.body.setAllowGravity(false);
|
|
|
|
// Circular hitbox centered in texture
|
|
const offset = cfg.texSize / 2 - cfg.radius;
|
|
this.sprite.body.setCircle(cfg.radius, offset, offset);
|
|
|
|
// Random velocity and slow spin
|
|
const speed = cfg.minSpeed + Math.random() * (cfg.maxSpeed - cfg.minSpeed);
|
|
const angle = Math.random() * Math.PI * 2;
|
|
this.sprite.setVelocity(Math.cos(angle) * speed, Math.sin(angle) * speed);
|
|
this.sprite.setAngularVelocity((Math.random() - 0.5) * 60);
|
|
}
|
|
|
|
update() {
|
|
if (!this.alive) return;
|
|
this.scene.physics.world.wrap(this.sprite, 64);
|
|
}
|
|
|
|
destroy() {
|
|
if (!this.alive) return;
|
|
this.alive = false;
|
|
if (this.sprite && this.sprite.active) {
|
|
this.sprite.destroy();
|
|
}
|
|
}
|
|
}
|