Back to Arañas Pistoleras
SOURCE / PINNED RELEASE

Made of little things.

Arañas Pistoleras

Release
2578a037ceaf…
Author-recorded commit
e35cc0733a7b…
License
LICENSE
Author’s source reference
nostr://npub1eg5pfnws6tg6rtp33tugpppqtv5u05crmf983qp438cdycwzngys0gxzcs/wss%3A%2F%2Fgit.napplet.soy%2F/n-7bf0a402584

Archive hash verified: 52cdda8c063eaf88…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/butterfly.ts
// The gentle side of the game: butterflies in place of the spiders. They take
// the same pose as a spider, so the rules and the frame loop do not change.

import type { Kind } from './game.js';
import { drawHat, glow, type SpiderPose } from './spider.js';

export interface WingColors {
  wing: string;
  wingHi: string;
  edge: string;
  spot: string;
  body: string;
  glow: string;
}

export const WINGS: Record<Kind, WingColors> = {
  widow: { wing: '#ff8a1e', wingHi: '#ffd35a', edge: '#2a1608', spot: '#ffffff', body: '#3a2414', glow: '#ffb84a' },
  hat: { wing: '#2f7df0', wingHi: '#7fe3ff', edge: '#0d1b4a', spot: '#d9f6ff', body: '#22305e', glow: '#6fd0ff' },
  mini: { wing: '#ff6fb4', wingHi: '#ffe66a', edge: '#6a1e4a', spot: '#ffffff', body: '#5a2a4a', glow: '#ff9ad0' },
  boss: { wing: '#b04cf0', wingHi: '#ff7ad8', edge: '#2e0f52', spot: '#ffe58a', body: '#3a1a5e', glow: '#e09cff' },
};

const WHITE: WingColors = { wing: '#ffffff', wingHi: '#ffffff', edge: '#fff3d0', spot: '#ffffff', body: '#ffffff', glow: '#ffffff' };

const TAU = Math.PI * 2;
type Ctx = CanvasRenderingContext2D;

/** How open the wings are, 0 folded to 1 flat, and how fast they beat. */
function flap(pose: SpiderPose): number {
  const rate = 6.5 + 9 * pose.aim + 10 * pose.heat + 8 * pose.walk;
  const beat = 0.5 + 0.5 * Math.sin(pose.time * rate + pose.seed * 9);
  // Wide open at the moment it slips away.
  return Math.max(0.3 + 0.7 * beat, pose.flash);
}

/** Where the body floats: butterflies bob, and rise a little when restless. */
function lift(pose: SpiderPose): number {
  return Math.sin(pose.time * 4.6 + pose.seed * 5) * 7 - 16 * pose.aim;
}

function wing(ctx: Ctx, c: WingColors, side: number, open: number, queen: boolean): void {
  ctx.save();
  ctx.scale(side * open, 1);
  ctx.lineJoin = 'round';
  // Lower wing, behind the upper one.
  ctx.beginPath();
  ctx.moveTo(6, 0);
  ctx.bezierCurveTo(70, 6, 132, 34, 112, 86);
  ctx.bezierCurveTo(98, 126, 54, 130, 30, 96);
  ctx.bezierCurveTo(16, 74, 8, 46, 6, 30);
  ctx.closePath();
  const lg = ctx.createRadialGradient(20, 20, 6, 60, 60, 110);
  lg.addColorStop(0, c.wingHi);
  lg.addColorStop(1, c.wing);
  ctx.fillStyle = lg;
  ctx.fill();
  ctx.strokeStyle = c.edge;
  ctx.lineWidth = 9;
  ctx.stroke();
  if (queen) {
    // The queen's wings end in long tails.
    ctx.beginPath();
    ctx.moveTo(66, 112);
    ctx.quadraticCurveTo(104, 172, 60, 222);
    ctx.quadraticCurveTo(54, 170, 22, 104);
    ctx.closePath();
    ctx.fillStyle = c.wing;
    ctx.fill();
    ctx.stroke();
  }
  ctx.fillStyle = c.spot;
  for (const [x, y, r] of [[84, 66, 9], [62, 98, 7], [100, 96, 5]]) {
    ctx.beginPath();
    ctx.arc(x, y, r, 0, TAU);
    ctx.fill();
  }
  // Upper wing.
  ctx.beginPath();
  ctx.moveTo(6, -6);
  ctx.bezierCurveTo(40, -92, 118, -150, 166, -118);
  ctx.bezierCurveTo(196, -96, 178, -22, 126, 4);
  ctx.bezierCurveTo(84, 22, 30, 18, 6, 8);
  ctx.closePath();
  const ug = ctx.createRadialGradient(24, -10, 8, 80, -50, 150);
  ug.addColorStop(0, c.wingHi);
  ug.addColorStop(1, c.wing);
  ctx.fillStyle = ug;
  ctx.fill();
  ctx.strokeStyle = c.edge;
  ctx.lineWidth = 10;
  ctx.stroke();
  // Veins.
  ctx.lineWidth = 3;
  ctx.globalAlpha = 0.45;
  ctx.beginPath();
  ctx.moveTo(10, -2);
  ctx.quadraticCurveTo(70, -60, 150, -104);
  ctx.moveTo(10, 2);
  ctx.quadraticCurveTo(84, -24, 156, -52);
  ctx.moveTo(10, 4);
  ctx.quadraticCurveTo(70, 6, 122, -4);
  ctx.stroke();
  ctx.globalAlpha = 1;
  // An eye spot and a row of dots along the edge.
  ctx.fillStyle = c.edge;
  ctx.beginPath();
  ctx.arc(118, -70, 23, 0, TAU);
  ctx.fill();
  ctx.fillStyle = c.spot;
  ctx.beginPath();
  ctx.arc(118, -70, 14, 0, TAU);
  ctx.fill();
  ctx.fillStyle = c.wingHi;
  ctx.beginPath();
  ctx.arc(114, -74, 6, 0, TAU);
  ctx.fill();
  ctx.fillStyle = c.spot;
  for (const [x, y, r] of [[160, -104, 6], [172, -78, 5], [168, -50, 5], [150, -22, 4.5]]) {
    ctx.beginPath();
    ctx.arc(x, y, r, 0, TAU);
    ctx.fill();
  }
  ctx.restore();
}

export function drawButterfly(ctx: Ctx, pose: SpiderPose): void {
  const c = pose.hurt ? WHITE : WINGS[pose.kind];
  const queen = pose.kind === 'boss';
  const open = flap(pose);
  ctx.save();
  const shake = pose.heat > 0.72 ? (pose.heat - 0.72) * 9 : 0;
  ctx.translate(pose.x + Math.sin(pose.time * 61) * shake, pose.y + Math.cos(pose.time * 53) * shake);
  ctx.scale(pose.scale * pose.squashX, pose.scale * pose.squashY);
  if (pose.grounded) {
    ctx.fillStyle = 'rgba(0,0,0,0.25)';
    ctx.beginPath();
    ctx.ellipse(0, 130, 110 * (0.6 + 0.4 * open), 13, 0, 0, TAU);
    ctx.fill();
  }
  ctx.translate(0, lift(pose));
  // A little tilt as it hovers.
  ctx.rotate(Math.sin(pose.time * 2.3 + pose.seed * 3) * 0.06);

  wing(ctx, c, -1, open, queen);
  wing(ctx, c, 1, open, queen);

  // Body: a soft striped abdomen and a round thorax.
  ctx.fillStyle = c.body;
  ctx.beginPath();
  ctx.ellipse(0, 46, 15, 50, 0, 0, TAU);
  ctx.fill();
  ctx.strokeStyle = c.wingHi;
  ctx.globalAlpha = 0.5;
  ctx.lineWidth = 4;
  for (let i = 0; i < 4; i++) {
    ctx.beginPath();
    ctx.moveTo(-12 + i * 1.5, 34 + i * 15);
    ctx.lineTo(12 - i * 1.5, 34 + i * 15);
    ctx.stroke();
  }
  ctx.globalAlpha = 1;
  ctx.fillStyle = c.body;
  ctx.beginPath();
  ctx.ellipse(0, -2, 19, 24, 0, 0, TAU);
  ctx.fill();

  // Antennae with little balls, bobbing.
  ctx.strokeStyle = c.body;
  ctx.lineWidth = 4.5;
  ctx.lineCap = 'round';
  for (const side of [-1, 1]) {
    const sway = Math.sin(pose.time * 3.1 + side + pose.seed * 4) * 6;
    ctx.beginPath();
    ctx.moveTo(side * 10, -62);
    ctx.quadraticCurveTo(side * 16, -104, side * (40 + sway), -112);
    ctx.stroke();
    ctx.fillStyle = c.wingHi;
    ctx.beginPath();
    ctx.arc(side * (40 + sway), -112, 7, 0, TAU);
    ctx.fill();
  }

  // Head with a face.
  ctx.fillStyle = c.body;
  ctx.beginPath();
  ctx.arc(0, -44, 31, 0, TAU);
  ctx.fill();
  ctx.strokeStyle = c.wingHi;
  ctx.globalAlpha = 0.6;
  ctx.lineWidth = 3;
  ctx.stroke();
  ctx.globalAlpha = 1;
  const blinkPhase = (pose.time * 0.33 + pose.seed * 5) % 1;
  const blink = blinkPhase > 0.96 ? 0.12 : 1;
  for (const side of [-1, 1]) {
    ctx.save();
    ctx.translate(side * 13, -48);
    ctx.scale(1, blink);
    ctx.fillStyle = '#ffffff';
    ctx.beginPath();
    ctx.ellipse(0, 0, 10.5, 12.5, 0, 0, TAU);
    ctx.fill();
    if (pose.dizzy) {
      ctx.strokeStyle = '#1a1020';
      ctx.lineWidth = 2.2;
      ctx.beginPath();
      for (let i = 0; i <= 20; i++) {
        const a = (i / 20) * TAU * 1.6 + pose.time * 9;
        const r = (i / 20) * 8;
        if (i === 0) ctx.moveTo(Math.cos(a) * r, Math.sin(a) * r);
        else ctx.lineTo(Math.cos(a) * r, Math.sin(a) * r);
      }
      ctx.stroke();
    } else {
      const px = pose.lookX * 3.6;
      const py = pose.lookY * 4;
      ctx.fillStyle = '#1a1020';
      ctx.beginPath();
      ctx.arc(px, py + 1, 6.2, 0, TAU);
      ctx.fill();
      ctx.fillStyle = '#ffffff';
      ctx.beginPath();
      ctx.arc(px - 2.2, py - 1.6, 2.3, 0, TAU);
      ctx.fill();
    }
    ctx.restore();
    // Rosy cheeks.
    ctx.fillStyle = 'rgba(255,120,150,0.55)';
    ctx.beginPath();
    ctx.ellipse(side * 21, -33, 6, 4, 0, 0, TAU);
    ctx.fill();
  }
  // A smile; a cheeky "o" when it is about to slip away.
  ctx.strokeStyle = '#ffffff';
  ctx.lineWidth = 2.6;
  ctx.beginPath();
  if (pose.aim > 0.5) ctx.arc(0, -29, 4.5, 0, TAU);
  else ctx.arc(0, -34, 9, 0.25 * Math.PI, 0.75 * Math.PI);
  ctx.stroke();

  if (pose.hat >= 0 || queen) {
    ctx.save();
    ctx.translate(0, -70);
    ctx.scale(0.62, 0.62);
    ctx.rotate(Math.sin(pose.time * 1.9 + pose.seed) * 0.05);
    drawHat(ctx, queen ? 9 : pose.hat, pose.time + pose.seed * 4);
    ctx.restore();
  }
  ctx.restore();
}

/** Shimmer: a glow in the wings, glitter circling as it gets restless, and a
 *  burst when it slips away. Drawn after the darkness, with additive blending. */
export function drawButterflyGlow(ctx: Ctx, pose: SpiderPose, sprite: CanvasImageSource): void {
  const c = WINGS[pose.kind];
  ctx.save();
  ctx.translate(pose.x, pose.y);
  ctx.scale(pose.scale, pose.scale);
  ctx.translate(0, lift(pose));
  ctx.globalAlpha = 0.16 + 0.2 * pose.aim;
  for (const side of [-1, 1]) glow(ctx, sprite, side * 80, -40, 120, c.glow);
  if (pose.heat > 0.02) {
    const pulse = pose.heat > 0.6 ? 0.75 + 0.25 * Math.sin(pose.time * 26) : 1;
    ctx.globalAlpha = Math.min(1, (0.2 + 0.6 * pose.heat) * pulse);
    glow(ctx, sprite, 0, -10, 70 + 130 * pose.heat, '#fff0a0');
    // Glitter going round, faster and closer as the moment comes.
    const n = 7;
    for (let i = 0; i < n; i++) {
      const a = pose.time * (2 + 5 * pose.heat) + (i / n) * TAU;
      const r = 190 - 50 * pose.heat;
      ctx.globalAlpha = Math.min(1, pose.heat * 1.6) * (0.5 + 0.5 * Math.sin(pose.time * 9 + i * 2));
      glow(ctx, sprite, Math.cos(a) * r, -10 + Math.sin(a) * r * 0.62, 16, '#ffffff');
    }
  }
  if (pose.flash > 0.02) {
    ctx.globalAlpha = Math.min(1, pose.flash * 1.3);
    glow(ctx, sprite, 0, -10, 150 + 240 * pose.flash, '#fff6d0');
  }
  ctx.restore();
}

/** A small butterfly for the effects: one that was caught and flies to the jar. */
export function drawTinyButterfly(ctx: Ctx, kind: Kind, x: number, y: number, scale: number, time: number, alpha: number): void {
  const c = WINGS[kind];
  ctx.save();
  ctx.translate(x, y);
  ctx.scale(scale, scale);
  ctx.globalAlpha = alpha;
  const open = 0.3 + 0.7 * (0.5 + 0.5 * Math.sin(time * 26));
  wing(ctx, c, -1, open, false);
  wing(ctx, c, 1, open, false);
  ctx.fillStyle = c.body;
  ctx.beginPath();
  ctx.ellipse(0, 30, 15, 64, 0, 0, TAU);
  ctx.fill();
  ctx.beginPath();
  ctx.arc(0, -44, 28, 0, TAU);
  ctx.fill();
  ctx.restore();
}