diff --git a/calm-island/README.md b/calm-island/README.md index a617d48..add5541 100644 --- a/calm-island/README.md +++ b/calm-island/README.md @@ -29,6 +29,7 @@ Open http://localhost:5173 and press **Begin wandering**. | `E` | Say hello to a Calmling. Once met, `E` invites them to walk with you (one at a time) or says goodbye | | `Q` | Near a Calmling you have met: learn the small practice they teach | | `E` at a garden plot | Plant a seed or water it. Plants grow in real time, even while the game is closed | +| `F` | Beside water: skim a stone and count the skips | | `M` | Map. Mist covers what you have not explored; discovering places and breathing clears it | | `P` | Photo mode: frame a shot and save it | | `L` | Calmling log: who you have met, with a photo and what their name means | diff --git a/calm-island/index.html b/calm-island/index.html index ca9f852..e938093 100644 --- a/calm-island/index.html +++ b/calm-island/index.html @@ -18,6 +18,7 @@ B sit & breathe E say hello L log + F skim a stone M map P photo Esc menu @@ -35,6 +36,7 @@ + @@ -67,6 +69,7 @@

Calm Island

E say hello to a Calmling, or ask them along Q learn a friend's practice L Calmling log + F skim a stone across water M map P photo mode Esc menu @@ -90,6 +93,7 @@

Taking a breath

+ diff --git a/calm-island/src/core/Ambience.ts b/calm-island/src/core/Ambience.ts new file mode 100644 index 0000000..ff6d0ac --- /dev/null +++ b/calm-island/src/core/Ambience.ts @@ -0,0 +1,182 @@ +/** + * The island's sound, made entirely in Web Audio: wind, running water, the + * waterfall, rain, birdsong by day and crickets at night. Nothing is recorded; + * every layer is shaped noise or a few sine chirps, mixed by where you stand. + */ +export interface AmbienceMix { + /** 0..1 closeness to the sea, a lake or the river */ + water: number; + /** 0..1 closeness to the waterfall */ + falls: number; + /** 0..1, higher up means more wind */ + height: number; + night: number; + rain: number; + /** 0..1 how wooded it is here */ + trees: number; +} + +export class Ambience { + private ctx: AudioContext | null = null; + private master: GainNode | null = null; + private wind: GainNode | null = null; + private water: GainNode | null = null; + private falls: GainNode | null = null; + private rain: GainNode | null = null; + private volume = 0.6; + private ducked = false; + private nextChirp = 0; + /** how many bird or cricket calls have been scheduled (for tests) */ + calls = 0; + + start(): void { + if (this.ctx) { + void this.ctx.resume(); + return; + } + try { + const ctx = new AudioContext(); + this.ctx = ctx; + const master = ctx.createGain(); + master.gain.value = 0; + master.connect(ctx.destination); + this.master = master; + // one long loop of soft noise feeds every layer + const len = ctx.sampleRate * 4; + const buf = ctx.createBuffer(1, len, ctx.sampleRate); + const d = buf.getChannelData(0); + let last = 0; + for (let i = 0; i < len; i++) { + last = last * 0.6 + (Math.random() * 2 - 1) * 0.4; + d[i] = last; + } + const layer = (type: BiquadFilterType, freq: number, q: number, rate: number): GainNode => { + const src = ctx.createBufferSource(); + src.buffer = buf; + src.loop = true; + src.playbackRate.value = rate; + const f = ctx.createBiquadFilter(); + f.type = type; + f.frequency.value = freq; + f.Q.value = q; + const g = ctx.createGain(); + g.gain.value = 0; + src.connect(f).connect(g).connect(master); + src.start(0, Math.random() * 3); + return g; + }; + this.wind = layer('bandpass', 420, 0.5, 0.55); + this.water = layer('bandpass', 1500, 0.4, 1.0); + this.falls = layer('lowpass', 900, 0.3, 0.8); + this.rain = layer('highpass', 2600, 0.3, 1.3); + this.apply(); + } catch (err) { + console.info('Ambient sound unavailable:', err); + } + } + + setVolume(v: number): void { + this.volume = v; + this.apply(); + } + + duck(on: boolean): void { + this.ducked = on; + this.apply(); + } + + private apply(): void { + if (!this.ctx || !this.master) return; + this.master.gain.setTargetAtTime(this.volume * this.volume * (this.ducked ? 0.3 : 1), this.ctx.currentTime, 0.4); + } + + update(mix: AmbienceMix, time: number): void { + const ctx = this.ctx; + if (!ctx || !this.wind || !this.water || !this.falls || !this.rain || !this.master) return; + const now = ctx.currentTime; + const set = (g: GainNode, v: number): void => { + g.gain.setTargetAtTime(v, now, 0.5); + }; + // the wind breathes: gusts come and go, stronger on high ground + const gust = 0.55 + 0.45 * Math.sin(time * 0.31) * Math.sin(time * 0.13 + 1); + set(this.wind, (0.05 + 0.16 * mix.height) * gust * (1 - mix.trees * 0.4)); + set(this.water, 0.11 * mix.water); + set(this.falls, 0.5 * mix.falls * mix.falls); + set(this.rain, 0.16 * mix.rain); + + if (time < this.nextChirp || mix.rain > 0.5) return; + if (mix.night > 0.6) { + this.cricket(now); + this.nextChirp = time + 0.9 + Math.random() * 1.6; + } else if (mix.night < 0.3) { + this.bird(now); + this.nextChirp = time + (2.2 + Math.random() * 5) / (0.6 + mix.trees); + } else { + this.nextChirp = time + 2; + } + this.calls++; + } + + /** A short run of falling or rising chirps, panned somewhere around you. */ + private bird(now: number): void { + const ctx = this.ctx!; + const pan = ctx.createStereoPanner(); + pan.pan.value = Math.random() * 1.6 - 0.8; + pan.connect(this.master!); + const base = 2200 + Math.random() * 2400; + const notes = 2 + Math.floor(Math.random() * 4); + const up = Math.random() < 0.5; + const loud = 0.03 + Math.random() * 0.05; + for (let i = 0; i < notes; i++) { + const t = now + i * (0.09 + Math.random() * 0.05); + const o = ctx.createOscillator(); + const g = ctx.createGain(); + const f = base * (1 + (up ? i : -i) * 0.06); + o.frequency.setValueAtTime(f, t); + o.frequency.exponentialRampToValueAtTime(f * (up ? 1.25 : 0.8), t + 0.07); + g.gain.setValueAtTime(0, t); + g.gain.linearRampToValueAtTime(loud, t + 0.012); + g.gain.exponentialRampToValueAtTime(0.0005, t + 0.085); + o.connect(g).connect(pan); + o.start(t); + o.stop(t + 0.1); + } + } + + private cricket(now: number): void { + const ctx = this.ctx!; + const pan = ctx.createStereoPanner(); + pan.pan.value = Math.random() * 1.8 - 0.9; + pan.connect(this.master!); + const f = 3900 + Math.random() * 700; + for (let i = 0; i < 3 + Math.floor(Math.random() * 3); i++) { + const t = now + i * 0.055; + const o = ctx.createOscillator(); + const g = ctx.createGain(); + o.frequency.value = f; + g.gain.setValueAtTime(0, t); + g.gain.linearRampToValueAtTime(0.022, t + 0.008); + g.gain.linearRampToValueAtTime(0, t + 0.035); + o.connect(g).connect(pan); + o.start(t); + o.stop(t + 0.04); + } + } + + /** A small watery plip, for skimming stones. */ + plip(strength: number): void { + const ctx = this.ctx; + if (!ctx || !this.master) return; + const now = ctx.currentTime; + const o = ctx.createOscillator(); + const g = ctx.createGain(); + o.frequency.setValueAtTime(520 + strength * 380, now); + o.frequency.exponentialRampToValueAtTime(180, now + 0.12); + g.gain.setValueAtTime(0.0001, now); + g.gain.linearRampToValueAtTime(0.14, now + 0.008); + g.gain.exponentialRampToValueAtTime(0.0005, now + 0.16); + o.connect(g).connect(this.master); + o.start(now); + o.stop(now + 0.18); + } +} diff --git a/calm-island/src/core/Game.ts b/calm-island/src/core/Game.ts index 5d68761..5a2d054 100644 --- a/calm-island/src/core/Game.ts +++ b/calm-island/src/core/Game.ts @@ -8,6 +8,7 @@ import { Ambient } from '../world/Ambient'; import { DayCycle } from '../world/DayCycle'; import { Garden } from '../world/Garden'; import { Seeds } from '../world/Seeds'; +import { Skimming } from '../world/Skimming'; import { CELL, GRID, HALF, WORLD_SIZE } from '../world/islandMap'; import { Weather } from '../world/Weather'; import { Grass } from '../world/Grass'; @@ -17,6 +18,7 @@ import { Terrain, bakeGroundTexture } from '../world/Terrain'; import { Vegetation } from '../world/Vegetation'; import { Water } from '../world/Water'; import { SUN_DIR, globalUniforms, makeHeightTexture } from '../world/shared'; +import { Ambience } from './Ambience'; import { BREATHS, Breathing } from './Breathing'; import { Input } from './Input'; import { Music } from './Music'; @@ -30,6 +32,8 @@ const nextFrame = (): Promise => new Promise((r) => requestAnimationFrame( export interface GameOptions { /** automated runs: no pointer lock, no persistence */ test: boolean; + /** allow ambient sound even in test mode */ + sound?: boolean; quality?: Quality; } @@ -68,6 +72,11 @@ export class Game { private weather = new Weather(); private garden!: Garden; private seeds!: Seeds; + private ambience = new Ambience(); + private skimming!: Skimming; + private bestSkips = 0; + private waterNear = 0; + private waterCheck = 0; private reveals: number[][] = [[0, 0, 80]]; private mapBase: HTMLCanvasElement | null = null; private groundTex!: THREE.DataTexture; @@ -218,6 +227,8 @@ export class Game { this.ambient = new Ambient(this.island); this.garden = new Garden(this.island); this.seeds = new Seeds(this.island); + this.skimming = new Skimming(this.island); + this.scene.add(this.skimming.group); this.scene.add(this.seeds.group); this.scene.add(this.garden.group, this.weather.mesh); this.scene.add(this.ambient.group); @@ -257,6 +268,7 @@ export class Game { this.garden.load(save.garden); this.seeds.load(save.seeds); if (save.reveals?.length) this.reveals = save.reveals; + this.bestSkips = save.bestSkips ?? 0; const friend = this.npcs.find((n) => n.who.id === save.companion); if (friend) { this.setCompanion(friend); @@ -286,7 +298,9 @@ export class Game { this.input.cameraOnly = false; this.input.enabled = true; if (!this.opts.test) void this.music.start(); + if (!this.opts.test || this.opts.sound) this.ambience.start(); this.music.duck(false); + this.ambience.duck(false); this.ui.showGame(); this.input.requestLock(); this.canvas.focus(); @@ -298,6 +312,7 @@ export class Game { this.input.enabled = false; this.input.releaseLock(); this.music.duck(true); + this.ambience.duck(true); this.save(); const mins = Math.max(1, Math.round(this.playTime / 60)); this.stopBreathing(); @@ -323,6 +338,7 @@ export class Game { }); } this.music.setVolume(s.musicVolume); + this.ambience.setVolume(s.soundVolume); if (this.post) this.post.enabled = s.quality !== 'low'; this.grassNear?.setDensity(preset.grass * s.grassDensity); this.grassFar?.setDensity(preset.grass * s.grassDensity); @@ -353,6 +369,7 @@ export class Game { this.updateMeetings(); this.updateFriendship(dt); this.updatePractice(dt); + this.updateShore(dt); const seed = this.seeds.update(globalUniforms.uTime.value, this.player.pos); if (seed >= 0) { const n = this.seeds.foundIds.length; @@ -524,6 +541,49 @@ export class Game { return name; } + /** How close water is (for sound and skimming), and stones thrown across it. */ + private updateShore(dt: number): void { + const p = this.player.pos; + const I = this.island; + this.waterCheck -= dt; + if (this.waterCheck <= 0) { + this.waterCheck = 0.25; + let near = 0; + for (let i = 0; i < 12; i++) { + const a = (i / 12) * Math.PI * 2; + const r = i % 2 ? 5 : 11; + const x = p.x + Math.cos(a) * r; + const z = p.z + Math.sin(a) * r; + if (I.waterLevelAt(x, z) > I.heightAt(x, z) + 0.15 && Math.abs(I.waterLevelAt(x, z) - p.y) < 4) near += i % 2 ? 2 : 1; + } + this.waterNear = Math.min(1, near / 7); + this.ui.skim(this.waterNear > 0.12 && this.player.grounded && !this.practice && !this.breathing.active); + } + if (this.input.skimPressed() && this.waterNear > 0.12 && this.player.grounded && !this.player.sitting) this.skimming.throw(p, this.player.yaw); + const event = this.skimming.update(dt); + if (event) this.ambience.plip(event === 'skip' ? 1 : 0.2); + if (event === 'done' && this.skimming.skips > 0) { + const n = this.skimming.skips; + const best = n > this.bestSkips; + if (best) this.bestSkips = n; + this.ui.toast(best && n > 1 ? 'A new best' : '', `${n} skip${n === 1 ? '' : 's'}`, n >= 5 ? 'Beautiful throw' : n >= 3 ? 'Nicely done' : 'Try a longer stretch of water'); + if (best) this.save(); + } + const fx = I.falls.base.x - p.x; + const fz = I.falls.base.z - p.z; + this.ambience.update( + { + water: this.waterNear, + falls: Math.max(0, 1 - Math.hypot(fx, fz) / 110), + height: Math.min(1, Math.max(0, p.y / 120)), + night: this.day.night, + rain: this.weather.rain, + trees: I.sample(I.forest, p.x, p.z), + }, + globalUniforms.uTime.value, + ); + } + private reveal(x: number, z: number, r: number): void { this.reveals.push([Math.round(x), Math.round(z), Math.round(r)]); if (this.reveals.length > 80) this.reveals.splice(1, 1); @@ -884,6 +944,7 @@ export class Game { garden: this.garden.serialize(), seeds: this.seeds.foundIds, reveals: this.reveals, + bestSkips: this.bestSkips, }); } @@ -915,6 +976,10 @@ export class Game { reveals: this.reveals.length, photo: this.photo, photosSaved: this.photosSaved, + waterNear: this.waterNear, + skips: this.skimming.skips, + stoneFlying: this.skimming.flying, + birdCalls: this.ambience.calls, absent: this.npcs.filter((n) => n.absent).map((n) => n.who.id), hour: this.day.hour, vy: this.player.vel.y, diff --git a/calm-island/src/core/Input.ts b/calm-island/src/core/Input.ts index 6f64988..97bac9c 100644 --- a/calm-island/src/core/Input.ts +++ b/calm-island/src/core/Input.ts @@ -140,6 +140,10 @@ export class Input { /** Photo mode keeps the camera controls but nothing else. */ cameraOnly = false; + skimPressed(): boolean { + return this.enabled && (this.pressed.has('KeyF') || this.virtualHit.has('skim')); + } + breathePressed(): boolean { return this.enabled && (this.pressed.has('KeyB') || this.virtualHit.has('breathe')); } diff --git a/calm-island/src/core/Save.ts b/calm-island/src/core/Save.ts index 46cd5a1..1c774ac 100644 --- a/calm-island/src/core/Save.ts +++ b/calm-island/src/core/Save.ts @@ -18,6 +18,7 @@ export interface SaveData { practices?: string[]; garden?: { t: number; w: number }[]; seeds?: number[]; + bestSkips?: number; /** patches of the map cleared of mist: x, z, radius */ reveals?: number[][]; } diff --git a/calm-island/src/core/Settings.ts b/calm-island/src/core/Settings.ts index 04f22e5..9576563 100644 --- a/calm-island/src/core/Settings.ts +++ b/calm-island/src/core/Settings.ts @@ -9,6 +9,8 @@ export interface Settings { invertY: boolean; showFps: boolean; musicVolume: number; // 0..1 + /** wind, water, birds */ + soundVolume: number; /** lower the graphics preset automatically if the frame rate is poor */ autoQuality: boolean; } @@ -30,6 +32,7 @@ const DEFAULTS: Settings = { invertY: false, showFps: false, musicVolume: 0.6, + soundVolume: 0.7, autoQuality: true, }; diff --git a/calm-island/src/core/TouchControls.ts b/calm-island/src/core/TouchControls.ts index 82d5a25..dc1f283 100644 --- a/calm-island/src/core/TouchControls.ts +++ b/calm-island/src/core/TouchControls.ts @@ -84,6 +84,7 @@ export class TouchControls { button('btn-breathe', 'breathe'); button('btn-hello', 'interact'); button('btn-practise', 'practise'); + button('btn-skim', 'skim'); document.getElementById('btn-menu')!.addEventListener('click', onMenu); } } diff --git a/calm-island/src/main.ts b/calm-island/src/main.ts index 8455fbb..601b5b8 100644 --- a/calm-island/src/main.ts +++ b/calm-island/src/main.ts @@ -12,6 +12,7 @@ const params = new URLSearchParams(location.search); const quality = params.get('quality'); const game = new Game(document.getElementById('game') as HTMLCanvasElement, { test: params.has('test'), + sound: params.has('sound'), quality: quality === 'low' || quality === 'medium' || quality === 'high' ? (quality as Quality) : undefined, }); window.__calm = game; diff --git a/calm-island/src/ui/UI.ts b/calm-island/src/ui/UI.ts index d7873c7..d7ed6c8 100644 --- a/calm-island/src/ui/UI.ts +++ b/calm-island/src/ui/UI.ts @@ -64,6 +64,7 @@ export class UI { handlers.onSettings(this.settings); }); bind('set-music', (el) => (this.settings.musicVolume = +el.value)); + bind('set-sound', (el) => (this.settings.soundVolume = +el.value)); bind('set-shadows', (el) => (this.settings.shadows = el.checked)); bind('set-grass', (el) => (this.settings.grassDensity = +el.value)); bind('set-scale', (el) => (this.settings.renderScale = +el.value)); @@ -77,6 +78,7 @@ export class UI { const s = this.settings; $('set-quality').value = s.quality; $('set-music').value = String(s.musicVolume); + $('set-sound').value = String(s.soundVolume); $('set-shadows').checked = s.shadows; $('set-grass').value = String(s.grassDensity); $('set-scale').value = String(s.renderScale); @@ -194,6 +196,11 @@ export class UI { $('log-close').focus(); } + /** The Skim button only shows beside water. */ + skim(show: boolean): void { + $('btn-skim').classList.toggle('hidden', !show); + } + seeds(found: number, total: number): void { $('seeds').classList.toggle('hidden', found === 0); $('seeds').textContent = `✦ ${found} of ${total} calm seeds`; diff --git a/calm-island/src/ui/style.css b/calm-island/src/ui/style.css index 185a87a..2f64ca3 100644 --- a/calm-island/src/ui/style.css +++ b/calm-island/src/ui/style.css @@ -144,3 +144,5 @@ body.is-touch #seeds { left: 50%; transform: translateX(-50%); } #btn-map { left: max(86px, calc(env(safe-area-inset-left) + 70px)); top: max(14px, env(safe-area-inset-top)); width: 62px; height: 62px; font-size: 13px !important; } #photo-bar { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); display: flex; align-items: center; gap: 12px; padding: 10px 12px 10px 20px; border-radius: 99px; font-weight: 700; font-size: 14px; color: var(--ink); background: rgba(255,255,255,.86); box-shadow: 0 8px 26px rgba(20,70,130,.3); z-index: 5; } #photo-bar button { padding: 9px 18px; font-size: 14px; } + +#btn-skim { right: max(214px, calc(env(safe-area-inset-right) + 190px)); bottom: max(26px, env(safe-area-inset-bottom)); width: 70px; height: 70px; } diff --git a/calm-island/src/world/Skimming.ts b/calm-island/src/world/Skimming.ts new file mode 100644 index 0000000..4ee63a6 --- /dev/null +++ b/calm-island/src/world/Skimming.ts @@ -0,0 +1,80 @@ +import * as THREE from 'three'; +import type { Island } from './islandMap'; + +/** Throw a flat stone across the water and count how many times it skips. */ +export class Skimming { + readonly group = new THREE.Group(); + /** skips of the stone in flight, or of the last throw */ + skips = 0; + flying = false; + private stone: THREE.Mesh; + private vel = new THREE.Vector3(); + private rings: { mesh: THREE.Mesh; age: number }[] = []; + private nextRing = 0; + + constructor(private island: Island) { + this.stone = new THREE.Mesh(new THREE.SphereGeometry(1, 10, 8).scale(0.13, 0.04, 0.11), new THREE.MeshLambertMaterial({ color: 0x8d8a86 })); + this.stone.visible = false; + this.stone.castShadow = true; + this.group.add(this.stone); + const ring = new THREE.RingGeometry(0.82, 1, 28).rotateX(-Math.PI / 2); + for (let i = 0; i < 10; i++) { + const mesh = new THREE.Mesh(ring, new THREE.MeshBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0, depthWrite: false })); + mesh.visible = false; + mesh.renderOrder = 4; + this.group.add(mesh); + this.rings.push({ mesh, age: 9 }); + } + } + + throw(from: THREE.Vector3, yaw: number): void { + if (this.flying) return; + this.flying = true; + this.skips = 0; + this.stone.visible = true; + this.stone.position.set(from.x + Math.sin(yaw) * 0.5, from.y + 0.75, from.z + Math.cos(yaw) * 0.5); + this.vel.set(Math.sin(yaw) * 11, 1.6, Math.cos(yaw) * 11); + } + + /** Returns 'skip' on each bounce and 'done' when the stone comes to rest. */ + update(dt: number): 'skip' | 'done' | null { + for (const r of this.rings) { + if (r.age > 1.6) continue; + r.age += dt; + r.mesh.scale.setScalar(0.25 + r.age * 1.5); + (r.mesh.material as THREE.MeshBasicMaterial).opacity = Math.max(0, 0.75 * (1 - r.age / 1.6)); + r.mesh.visible = r.age <= 1.6; + } + if (!this.flying) return null; + const p = this.stone.position; + this.vel.y -= 13 * dt; + p.addScaledVector(this.vel, dt); + this.stone.rotation.y += dt * 14; + const ground = this.island.heightAt(p.x, p.z); + const water = this.island.waterLevelAt(p.x, p.z); + const speed = Math.hypot(this.vel.x, this.vel.z); + if (ground > water && p.y <= ground + 0.05) { + this.flying = false; // landed on the bank + p.y = ground + 0.04; + return 'done'; + } + if (ground <= water && p.y <= water && this.vel.y < 0) { + const r = this.rings[(this.nextRing = (this.nextRing + 1) % this.rings.length)]; + r.age = 0; + r.mesh.position.set(p.x, water + 0.04, p.z); + if (speed > 3.4) { + // a clean skip: lose a little pace each time + this.skips++; + p.y = water; + this.vel.y = 1.1 + speed * 0.13; + this.vel.x *= 0.8; + this.vel.z *= 0.8; + return 'skip'; + } + this.flying = false; + this.stone.visible = false; + return 'done'; + } + return null; + } +}