Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions calm-island/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
4 changes: 4 additions & 0 deletions calm-island/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<span><kbd>B</kbd> sit &amp; breathe</span>
<span><kbd>E</kbd> say hello</span>
<span><kbd>L</kbd> log</span>
<span><kbd>F</kbd> skim a stone</span>
<span><kbd>M</kbd> map</span>
<span><kbd>P</kbd> photo</span>
<span><kbd>Esc</kbd> menu</span>
Expand All @@ -35,6 +36,7 @@
<button id="btn-breathe">Breathe</button>
<button id="btn-hello" class="hidden">Hello</button>
<button id="btn-practise" class="hidden">Practise</button>
<button id="btn-skim" class="hidden">Skim</button>
<button id="btn-log" aria-label="Calmling log">Log</button>
<button id="btn-map" aria-label="Map">Map</button>
<button id="btn-hop">Hop</button>
Expand Down Expand Up @@ -67,6 +69,7 @@ <h1>Calm Island</h1>
<span class="desk"><kbd>E</kbd> say hello to a Calmling, or ask them along</span>
<span class="desk"><kbd>Q</kbd> learn a friend's practice</span>
<span class="desk"><kbd>L</kbd> Calmling log</span>
<span class="desk"><kbd>F</kbd> skim a stone across water</span>
<span class="desk"><kbd>M</kbd> map</span>
<span class="desk"><kbd>P</kbd> photo mode</span>
<span class="desk"><kbd>Esc</kbd> menu</span>
Expand All @@ -90,6 +93,7 @@ <h2>Taking a breath</h2>
</select>
</label>
<label>Music <input type="range" id="set-music" min="0" max="1" step="0.05" /></label>
<label>Nature sounds <input type="range" id="set-sound" min="0" max="1" step="0.05" /></label>
<label>Shadows <input type="checkbox" id="set-shadows" /></label>
<label>Grass <input type="range" id="set-grass" min="0.1" max="1" step="0.05" /></label>
<label>Sharpness <input type="range" id="set-scale" min="0.5" max="1" step="0.05" /></label>
Expand Down
182 changes: 182 additions & 0 deletions calm-island/src/core/Ambience.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
65 changes: 65 additions & 0 deletions calm-island/src/core/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand All @@ -30,6 +32,8 @@ const nextFrame = (): Promise<void> => 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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -884,6 +944,7 @@ export class Game {
garden: this.garden.serialize(),
seeds: this.seeds.foundIds,
reveals: this.reveals,
bestSkips: this.bestSkips,
});
}

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions calm-island/src/core/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
1 change: 1 addition & 0 deletions calm-island/src/core/Save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[][];
}
Expand Down
Loading