|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +03/22/2026 |
| 5 | +Ufo.js |
| 6 | +*/ |
| 7 | +import Bullet from './Bullet.js'; |
| 8 | +import { distance } from '../../../src/engine/utils/index.js'; |
| 9 | +import { transformPoints } from '../../../src/engine/vector/index.js'; |
| 10 | +import { randomRange } from '../../Asteroids/utils/math.js'; |
| 11 | + |
| 12 | +const UFO_PROFILES = { |
| 13 | + large: { |
| 14 | + radius: 26, |
| 15 | + speed: 88, |
| 16 | + points: 200, |
| 17 | + fireInterval: [1.8, 2.8], |
| 18 | + aimJitter: 0.55, |
| 19 | + }, |
| 20 | + small: { |
| 21 | + radius: 18, |
| 22 | + speed: 126, |
| 23 | + points: 1000, |
| 24 | + fireInterval: [1.1, 1.8], |
| 25 | + aimJitter: 0.16, |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +const VECTOR_MAPS = { |
| 30 | + small: [ |
| 31 | + { x: -14, y: 3 }, { x: 14, y: 3 }, { x: 9, y: 9 }, { x: -9, y: 9 }, { x: -14, y: 3 }, { x: -9, y: -3 }, |
| 32 | + { x: -5, y: -3 }, { x: -5, y: -6 }, { x: -2, y: -9 }, { x: 2, y: -9 }, { x: 5, y: -6 }, { x: 5, y: -3 }, { x: -5, y: -3 }, |
| 33 | + { x: 9, y: -3 }, { x: 14, y: 3 }, |
| 34 | + ], |
| 35 | + large: [ |
| 36 | + { x: -21, y: 4.5 }, { x: 21, y: 4.5 }, { x: 13.5, y: 13.5 }, { x: -13.5, y: 13.5 }, { x: -21, y: 4.5 }, { x: -13.5, y: -4.5 }, |
| 37 | + { x: -7.5, y: -4.5 }, { x: -7.5, y: -9 }, { x: -3, y: -13.5 }, { x: 3, y: -13.5 }, { x: 7.5, y: -9 }, { x: 7.5, y: -4.5 }, { x: -7.5, y: -4.5 }, |
| 38 | + { x: 13.5, y: -4.5 }, { x: 21, y: 4.5 }, |
| 39 | + ], |
| 40 | +}; |
| 41 | + |
| 42 | +export default class Ufo { |
| 43 | + constructor(bounds, type = 'large', level = 1, rng = Math.random) { |
| 44 | + this.bounds = bounds; |
| 45 | + this.rng = typeof rng === 'function' ? rng : Math.random; |
| 46 | + this.type = UFO_PROFILES[type] ? type : 'large'; |
| 47 | + this.profile = UFO_PROFILES[this.type]; |
| 48 | + this.direction = this.rng() > 0.5 ? 1 : -1; |
| 49 | + this.x = this.direction > 0 ? -48 : bounds.width + 48; |
| 50 | + this.y = randomRange(120, bounds.height - 140, this.rng); |
| 51 | + this.vx = this.profile.speed * (1 + (level - 1) * 0.05) * this.direction; |
| 52 | + this.vy = randomRange(-26, 26, this.rng); |
| 53 | + this.radius = this.profile.radius; |
| 54 | + this.points = this.profile.points; |
| 55 | + this.turnTimer = randomRange(1.2, 2.4, this.rng); |
| 56 | + this.fireTimer = randomRange(...this.profile.fireInterval, this.rng); |
| 57 | + this.alive = true; |
| 58 | + } |
| 59 | + |
| 60 | + update(dtSeconds) { |
| 61 | + this.x += this.vx * dtSeconds; |
| 62 | + this.y += this.vy * dtSeconds; |
| 63 | + |
| 64 | + this.turnTimer -= dtSeconds; |
| 65 | + if (this.turnTimer <= 0) { |
| 66 | + this.turnTimer = randomRange(1.1, 2.2, this.rng); |
| 67 | + this.vy = randomRange(-52, 52, this.rng); |
| 68 | + } |
| 69 | + |
| 70 | + if (this.y < 88) { |
| 71 | + this.y = 88; |
| 72 | + this.vy = Math.abs(this.vy); |
| 73 | + } else if (this.y > this.bounds.height - 120) { |
| 74 | + this.y = this.bounds.height - 120; |
| 75 | + this.vy = -Math.abs(this.vy); |
| 76 | + } |
| 77 | + |
| 78 | + this.fireTimer -= dtSeconds; |
| 79 | + if (this.direction > 0 && this.x > this.bounds.width + 60) { |
| 80 | + this.alive = false; |
| 81 | + } else if (this.direction < 0 && this.x < -60) { |
| 82 | + this.alive = false; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + canFire() { |
| 87 | + return this.fireTimer <= 0; |
| 88 | + } |
| 89 | + |
| 90 | + fireAt(target) { |
| 91 | + this.fireTimer = randomRange(...this.profile.fireInterval, this.rng); |
| 92 | + const aimAngle = Math.atan2(target.y - this.y, target.x - this.x) + randomRange(-this.profile.aimJitter, this.profile.aimJitter, this.rng); |
| 93 | + const muzzleX = this.x + Math.cos(aimAngle) * (this.radius - 2); |
| 94 | + const muzzleY = this.y + Math.sin(aimAngle) * (this.radius - 2); |
| 95 | + const shotSpeed = 250; |
| 96 | + const fullScreenLife = Math.max(1.1, this.bounds.width / shotSpeed); |
| 97 | + return new Bullet( |
| 98 | + muzzleX, |
| 99 | + muzzleY, |
| 100 | + Math.cos(aimAngle) * shotSpeed, |
| 101 | + Math.sin(aimAngle) * shotSpeed, |
| 102 | + fullScreenLife, |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + collidesWithPoint(point, padding = 0) { |
| 107 | + return distance(this, point) <= this.radius + padding; |
| 108 | + } |
| 109 | + |
| 110 | + getCollisionPolygon() { |
| 111 | + return transformPoints(VECTOR_MAPS[this.type], { |
| 112 | + x: this.x, |
| 113 | + y: this.y, |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + getBodyLines() { |
| 118 | + return [ |
| 119 | + this.getCollisionPolygon(), |
| 120 | + ]; |
| 121 | + } |
| 122 | +} |
0 commit comments