Download js/patterns.js from bebechien/cats-eye: direct link, hf CLI and curl.
- Browser
- Download file 11.9 kB
-
https://huggingface.co/spaces/bebechien/cats-eye/resolve/main/js/patterns.js
- Command line
-
hf download hf://spaces/bebechien/cats-eye/js/patterns.js
-
curl -L -o patterns.js https://huggingface.co/spaces/bebechien/cats-eye/resolve/main/js/patterns.js
11.9 kB
| // Bullet patterns (GDD §4) written as generators: `yield seconds` pauses the pattern. | |
| // Each pattern receives a context with: player, spawn(), fx, audio, threat(), falseThreat(), | |
| // run(childGenerator), speedMul (Endless speed-up) and beatDur (Rhythm mode). | |
| import { SHAPE } from './gfx.js'; | |
| import { ARENA, COLORS } from './config.js'; | |
| import { rand, pick, deg, TAU, clamp, angleDiff, rgb } from './utils.js'; | |
| const C = { | |
| pink: rgb(COLORS.pink), red: rgb(COLORS.red), cyan: rgb(COLORS.cyan), yellow: rgb(COLORS.yellow), | |
| white: rgb(COLORS.white), mouse: rgb(COLORS.mouse), hot: [1, 1, 0.75], | |
| }; | |
| const HW = ARENA.hw, HH = ARENA.hh; | |
| /** A point just outside the arena: the top edge, or the upper part of a side. */ | |
| export function edgePoint(topOnly = false) { | |
| if (topOnly || Math.random() < 0.6) return { x: rand(-HW + 30, HW - 30), y: HH + 24 }; | |
| const side = Math.random() < 0.5 ? -1 : 1; | |
| return { x: side * (HW + 24), y: rand(0, HH - 40) }; | |
| } | |
| export const aimAt = (x, y, tx, ty) => Math.atan2(ty - y, tx - x); | |
| // ======================================================================= Phase 1 | |
| /** Red yarn balls: aimed straight shots from the edges. Teaches "tap dodging". */ | |
| export function* yarnSniper(ctx, o = {}) { | |
| const shots = o.shots ?? 4; | |
| const gap = o.gap ?? 0.42; | |
| const speed = (o.speed ?? 230) * ctx.speedMul; | |
| for (let i = 0; i < shots; i++) { | |
| const p = edgePoint(false); | |
| ctx.fx.muzzle(p.x, p.y, C.red); | |
| yield 0.18; | |
| const ang = aimAt(p.x, p.y, ctx.player.x, ctx.player.y); | |
| ctx.spawn({ kind: 'yarn', x: p.x, y: p.y, angle: ang, speed, r: 7, shape: SHAPE.ORB, color: C.red, soft: 9, trail: true }); | |
| ctx.audio.shoot(); | |
| ctx.threat(); | |
| yield gap; | |
| } | |
| } | |
| /** One guide laser followed by a thick, very fast needle along the same line. */ | |
| export function* singleNeedle(ctx, o) { | |
| const p = o.from || edgePoint(false); | |
| const ang = aimAt(p.x, p.y, ctx.player.x, ctx.player.y); | |
| const L = 1000; | |
| ctx.fx.telegraph(p.x, p.y, p.x + Math.cos(ang) * L, p.y + Math.sin(ang) * L, { color: C.yellow, life: o.warn, width: 1.2 }); | |
| ctx.audio.telegraph(o.warn); | |
| ctx.threat(); | |
| yield o.warn; | |
| ctx.spawn({ kind: 'needle', x: p.x, y: p.y, angle: ang, speed: o.speed, r: 6, len: 34, shape: SHAPE.CAPSULE, color: C.yellow, soft: 10, trail: true, life: 4 }); | |
| ctx.audio.beam(); | |
| } | |
| /** Straight beam / fast needle shots with telegraphed guide lines. */ | |
| export function* needleBeam(ctx, o = {}) { | |
| const count = o.count ?? 2; | |
| const warn = o.warn ?? 0.7; | |
| const speed = (o.speed ?? 950) * ctx.speedMul; | |
| for (let i = 0; i < count; i++) { | |
| ctx.run(singleNeedle(ctx, { warn, speed })); | |
| yield o.stagger ?? 0.4; | |
| } | |
| } | |
| // ======================================================================= Phase 2 | |
| /** Radial rings expanding from an emitter. Trains distributed vision / finding safe zones. */ | |
| export function* spreadRing(ctx, o = {}) { | |
| const rings = o.rings ?? 3; | |
| const n = o.n ?? 22; | |
| const charge = o.charge ?? 0.6; | |
| const gap = o.gap ?? 0.45; | |
| const x = o.x ?? rand(-150, 150); | |
| const y = o.y ?? rand(60, 220); | |
| const v0 = 40 * ctx.speedMul; | |
| const acc = 260 * ctx.speedMul; | |
| const cap = (o.speed ?? 200) * ctx.speedMul; | |
| ctx.fx.emitter(x, y, C.cyan, charge); | |
| ctx.audio.charge(charge); | |
| yield charge; | |
| for (let k = 0; k < rings; k++) { | |
| const off = (k % 2) * (Math.PI / n) + rand(0, 0.25); | |
| for (let i = 0; i < n; i++) { | |
| ctx.spawn({ kind: 'ring', x, y, angle: off + (i * TAU) / n, speed: v0, accel: acc, speedCap: cap, r: 5, shape: SHAPE.ORB, color: C.cyan, soft: 7, trail: true }); | |
| } | |
| ctx.audio.ringShot(); | |
| ctx.fx.ring(x, y, C.cyan, 4, 60, 0.4); | |
| yield gap; | |
| } | |
| } | |
| /** | |
| * Odd/even aimed volleys. Odd counts (pink) put a bullet straight on the player -> must move. | |
| * Even counts (cyan) straddle the player -> staying still is correct. Moving anyway is an over-reaction. | |
| */ | |
| export function* oddEven(ctx, o = {}) { | |
| const volleys = o.volleys ?? 6; | |
| const gap = o.gap ?? 0.62; | |
| const charge = o.charge ?? 0.5; | |
| const speed = (o.speed ?? 240) * ctx.speedMul; | |
| const x = o.x ?? clamp(ctx.player.x + rand(-120, 120), -HW + 40, HW - 40); | |
| const y = o.y ?? HH - 50; | |
| if (charge > 0) { | |
| ctx.fx.emitter(x, y, C.pink, charge); | |
| yield charge; | |
| } | |
| let odd = o.odd ?? Math.random() < 0.5; | |
| for (let v = 0; v < volleys; v++) { | |
| const n = odd ? pick([3, 5, 7]) : pick([4, 6]); | |
| const base = aimAt(x, y, ctx.player.x, ctx.player.y); | |
| const spread = deg(13); | |
| const color = odd ? C.pink : C.cyan; | |
| const start = -(n - 1) / 2; | |
| for (let i = 0; i < n; i++) { | |
| ctx.spawn({ kind: 'aimed', x, y, angle: base + (start + i) * spread, speed, r: 6, shape: SHAPE.ORB, color, soft: 8, trail: true }); | |
| } | |
| ctx.audio.shoot(); | |
| ctx.fx.muzzle(x, y, color); | |
| if (odd) ctx.threat(); else ctx.falseThreat(); | |
| odd = !odd; | |
| if (v < volleys - 1) yield gap; | |
| } | |
| } | |
| // ======================================================================= Phase 3 | |
| function mouseUpdate(b, dt, game) { | |
| const d = b.data; | |
| if (d.turned) return; | |
| const p = game.player; | |
| if (Math.hypot(p.x - b.x, p.y - b.y) < d.trigger) { | |
| d.turned = true; | |
| const want = Math.atan2(p.y - b.y, p.x - b.x); | |
| b.angle += clamp(angleDiff(b.angle, want), -deg(95), deg(95)); | |
| b.speed *= 1.35; | |
| b.color = C.white; | |
| game.audio.beep(); | |
| game.threat(); | |
| game.effects.flash(b.x, b.y, C.mouse, 16, 0.3); | |
| game.effects.ring(b.x, b.y, C.pink, 4, 40, 0.3); | |
| } | |
| } | |
| /** | |
| * Mouse missiles fly straight past the player, then snap 45-90 degrees toward the player's | |
| * *new* position once within range (with a "beep" warning). | |
| */ | |
| export function* mouseMissile(ctx, o = {}) { | |
| const count = o.count ?? 2; | |
| const gap = o.gap ?? 0.55; | |
| const speed = (o.speed ?? 210) * ctx.speedMul; | |
| for (let i = 0; i < count; i++) { | |
| const side = (i + (o.side ?? 0)) % 2 === 0 ? -1 : 1; | |
| const sx = side * (HW + 24), sy = rand(100, HH - 20); | |
| const px = ctx.player.x, py = ctx.player.y; | |
| const dir = aimAt(sx, sy, px, py); | |
| const perp = dir + (Math.random() < 0.5 ? 1 : -1) * (Math.PI / 2); | |
| const tx = px + Math.cos(perp) * 140, ty = py + Math.sin(perp) * 140; | |
| ctx.fx.muzzle(sx, sy, C.mouse); | |
| ctx.spawn({ | |
| kind: 'mouse', x: sx, y: sy, angle: aimAt(sx, sy, tx, ty), speed, r: 7, len: 9, shape: SHAPE.MOUSE, | |
| color: C.mouse, soft: 9, trail: true, data: { turned: false, trigger: 150 }, update: mouseUpdate, | |
| }); | |
| ctx.audio.shoot(); | |
| yield gap; | |
| } | |
| } | |
| function laserUpdate(b, dt, game) { | |
| const d = b.data; | |
| d.t += dt; | |
| if (d.locked) return; | |
| const p = game.player; | |
| const want = Math.atan2(p.y - b.y, p.x - b.x); | |
| b.angle += clamp(angleDiff(b.angle, want), -d.turnRate * dt, d.turnRate * dt); | |
| d.dropT += dt; | |
| if (d.dropT > 0.045) { | |
| d.dropT = 0; | |
| game.effects.yarnDot(b.x - Math.cos(b.angle) * b.len, b.y - Math.sin(b.angle) * b.len); | |
| } | |
| if (d.t >= d.lockAt) { | |
| d.locked = true; | |
| b.color = C.hot; | |
| b.accel = d.accel; | |
| b.speedCap = d.cap; | |
| b.len = 24; | |
| b.soft = 14; | |
| game.audio.lock(); | |
| game.threat(); | |
| game.effects.flash(b.x, b.y, C.yellow, 18, 0.3); | |
| } | |
| } | |
| /** Smart laser yarn: curves gently toward the player, then locks its heading and accelerates. */ | |
| export function* laserYarn(ctx, o = {}) { | |
| const count = o.count ?? 2; | |
| const gap = o.gap ?? 0.7; | |
| const speed = (o.speed ?? 150) * ctx.speedMul; | |
| for (let i = 0; i < count; i++) { | |
| const sx = rand(-200, 200), sy = HH + 24; | |
| ctx.fx.muzzle(sx, sy, C.yellow); | |
| ctx.spawn({ | |
| kind: 'laser', x: sx, y: sy, angle: -Math.PI / 2 + rand(-0.6, 0.6), speed, r: 4, len: 14, shape: SHAPE.CAPSULE, | |
| color: C.yellow, soft: 8, grazeContinuous: true, life: 9, | |
| data: { | |
| t: 0, lockAt: o.lockAt ?? 1.6, locked: false, turnRate: deg(o.turnRate ?? 110), dropT: 0, | |
| accel: 1600 * ctx.speedMul, cap: (o.lockSpeed ?? 560) * ctx.speedMul, | |
| }, | |
| update: laserUpdate, | |
| }); | |
| ctx.audio.shoot(); | |
| yield gap; | |
| } | |
| } | |
| function splitterUpdate(b, dt, game) { | |
| const d = b.data; | |
| d.t += dt; | |
| if (d.t >= d.travel && !d.charging) { | |
| d.charging = true; | |
| b.speed = 0; | |
| b.accel = 0; | |
| game.audio.charge(d.charge); | |
| game.effects.emitter(b.x, b.y, C.pink, d.charge); | |
| } | |
| if (d.charging) { | |
| const ct = d.t - d.travel; | |
| b.scale = 1 + 0.25 * Math.sin(ct * 30) * (ct / d.charge); | |
| if (ct >= d.charge) { | |
| const p = game.player; | |
| const base = Math.atan2(p.y - b.y, p.x - b.x); | |
| for (let k = 0; k < 8; k++) { | |
| game.spawnBullet({ kind: 'frag', x: b.x, y: b.y, angle: base + k * (Math.PI / 4), speed: d.fragSpeed, r: 5, shape: SHAPE.ORB, color: k % 2 ? C.cyan : C.pink, soft: 7, trail: true }); | |
| } | |
| game.audio.burst(); | |
| game.threat(); | |
| game.effects.ring(b.x, b.y, C.pink, 6, 70, 0.4); | |
| game.effects.burst(b.x, b.y, C.pink, 10, 120, 0.4, 2); | |
| b.dead = true; | |
| } | |
| } | |
| } | |
| /** Split bullet: decelerates to a stop near the player, charges, then bursts into 8 fragments. */ | |
| export function* splitter(ctx, o = {}) { | |
| const count = o.count ?? 2; | |
| const gap = o.gap ?? 0.6; | |
| const charge = o.charge ?? 0.65; | |
| const travel = o.travel ?? 1.1; | |
| const fragSpeed = (o.fragSpeed ?? 250) * ctx.speedMul; | |
| for (let i = 0; i < count; i++) { | |
| const p = edgePoint(true); | |
| const a = rand(0, TAU), dist = rand(80, 140); | |
| const tx = clamp(ctx.player.x + Math.cos(a) * dist, -HW + 40, HW - 40); | |
| const ty = clamp(ctx.player.y + Math.sin(a) * dist + 50, -HH + 90, HH - 60); | |
| const L = Math.hypot(tx - p.x, ty - p.y); | |
| const v0 = (2 * L) / travel; // decelerating uniformly, it stops exactly at (tx, ty) | |
| ctx.fx.muzzle(p.x, p.y, C.pink); | |
| ctx.spawn({ | |
| kind: 'splitter', x: p.x, y: p.y, angle: aimAt(p.x, p.y, tx, ty), speed: v0, accel: -v0 / travel, speedCap: v0, | |
| r: 11, shape: SHAPE.ORB, color: C.pink, soft: 12, life: 10, | |
| data: { t: 0, travel, charge, charging: false, fragSpeed }, update: splitterUpdate, | |
| }); | |
| ctx.audio.shoot(); | |
| yield gap; | |
| } | |
| } | |
| // ======================================================================= Rhythm-mode variants | |
| /** Aimed yarn balls whose speed is chosen so they reach the player's position exactly `beats` later. */ | |
| export function* rhythmAimed(ctx, o = {}) { | |
| const beats = o.beats ?? 4; | |
| const n = o.count ?? 1; | |
| for (let i = 0; i < n; i++) { | |
| const px = ctx.player.x, py = ctx.player.y; | |
| const sx = clamp(px + rand(-140, 140) + (i ? rand(-80, 80) : 0), -HW + 20, HW - 20); | |
| const sy = HH + 20; | |
| const speed = Math.hypot(px - sx, py - sy) / (beats * ctx.beatDur); | |
| ctx.fx.muzzle(sx, sy, C.red); | |
| ctx.spawn({ kind: 'yarn', x: sx, y: sy, angle: aimAt(sx, sy, px, py), speed, r: 7, shape: SHAPE.ORB, color: C.red, soft: 9, trail: true }); | |
| ctx.audio.shoot(); | |
| ctx.threat(); | |
| } | |
| } | |
| /** Needle whose guide line lasts `warnBeats` and whose beam lands within a third of a beat. */ | |
| export function* rhythmNeedle(ctx, o = {}) { | |
| const warn = (o.warnBeats ?? 1) * ctx.beatDur; | |
| const p = o.from || edgePoint(false); | |
| const D = Math.hypot(ctx.player.x - p.x, ctx.player.y - p.y); | |
| const speed = clamp(D / (0.3 * ctx.beatDur), 500, 1500); | |
| yield* singleNeedle(ctx, { from: p, warn, speed }); | |
| } | |
| // ======================================================================= registry | |
| export const PATTERNS = { | |
| yarn: { fn: yarnSniper, name: 'pYarn', phase: 1, hard: false }, | |
| needle: { fn: needleBeam, name: 'pNeedle', phase: 1, hard: false }, | |
| ring: { fn: spreadRing, name: 'pRing', phase: 2, hard: false }, | |
| oddeven: { fn: oddEven, name: 'pOddEven', phase: 2, hard: false }, | |
| missile: { fn: mouseMissile, name: 'pMissile', phase: 3, hard: true }, | |
| laser: { fn: laserYarn, name: 'pLaser', phase: 3, hard: true }, | |
| splitter: { fn: splitter, name: 'pSplitter', phase: 3, hard: true }, | |
| }; | |
| export const PATTERN_KEYS = Object.keys(PATTERNS); | |