|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +03/22/2026 |
| 5 | +Ship.js |
| 6 | +*/ |
| 7 | +import { wrap } from "../../asteroids/utils/math.js"; |
| 8 | +import { transformPoints } from '../../../src/engine/vector/index.js'; |
| 9 | + |
| 10 | +const SMALL_VECTOR_MAP = [ |
| 11 | + { x: 14, y: 0 }, |
| 12 | + { x: -10, y: -8 }, |
| 13 | + { x: -6, y: -3 }, |
| 14 | + { x: -6, y: 3 }, |
| 15 | + { x: -10, y: 8 }, |
| 16 | + { x: 14, y: 0 }, |
| 17 | +]; |
| 18 | + |
| 19 | +export default class Ship { |
| 20 | + constructor(x, y) { |
| 21 | + this.spawnX = x; |
| 22 | + this.spawnY = y; |
| 23 | + this.reset(); |
| 24 | + } |
| 25 | + |
| 26 | + reset() { |
| 27 | + this.x = this.spawnX; |
| 28 | + this.y = this.spawnY; |
| 29 | + this.vx = 0; |
| 30 | + this.vy = 0; |
| 31 | + this.angle = -Math.PI / 2; |
| 32 | + this.invulnerable = 2; |
| 33 | + this.thrusting = false; |
| 34 | + } |
| 35 | + |
| 36 | + update(dtSeconds, bounds, input) { |
| 37 | + if (input?.isDown('ArrowLeft')) { |
| 38 | + this.angle -= 3.8 * dtSeconds; |
| 39 | + } |
| 40 | + if (input?.isDown('ArrowRight')) { |
| 41 | + this.angle += 3.8 * dtSeconds; |
| 42 | + } |
| 43 | + |
| 44 | + this.thrusting = Boolean(input?.isDown('ArrowUp')); |
| 45 | + if (this.thrusting) { |
| 46 | + this.vx += Math.cos(this.angle) * 170 * dtSeconds; |
| 47 | + this.vy += Math.sin(this.angle) * 170 * dtSeconds; |
| 48 | + } |
| 49 | + |
| 50 | + this.vx *= 0.992; |
| 51 | + this.vy *= 0.992; |
| 52 | + this.x = wrap(this.x + this.vx * dtSeconds, bounds.width); |
| 53 | + this.y = wrap(this.y + this.vy * dtSeconds, bounds.height); |
| 54 | + this.invulnerable = Math.max(0, this.invulnerable - dtSeconds); |
| 55 | + } |
| 56 | + |
| 57 | + getPoints() { |
| 58 | + return transformPoints(SMALL_VECTOR_MAP, { |
| 59 | + x: this.x, |
| 60 | + y: this.y, |
| 61 | + rotation: this.angle, |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + getFlamePoints(pulse = 1) { |
| 66 | + const rear = this.angle + Math.PI; |
| 67 | + const flameReach = (18 + pulse * 9) * 0.5; |
| 68 | + const wingSpread = (8 + pulse * 2) * 0.5; |
| 69 | + return [ |
| 70 | + { x: this.x + Math.cos(rear) * flameReach, y: this.y + Math.sin(rear) * flameReach }, |
| 71 | + { x: this.x + Math.cos(this.angle + 2.7) * wingSpread, y: this.y + Math.sin(this.angle + 2.7) * wingSpread }, |
| 72 | + { x: this.x + Math.cos(this.angle - 2.7) * wingSpread, y: this.y + Math.sin(this.angle - 2.7) * wingSpread }, |
| 73 | + ]; |
| 74 | + } |
| 75 | +} |
0 commit comments