-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisual-corruption.ts
More file actions
169 lines (157 loc) · 6.42 KB
/
Copy pathvisual-corruption.ts
File metadata and controls
169 lines (157 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Renderer, Cell } from '../render/renderer';
import { CorruptionRenderer, GlitchSnapshot } from '../render/corruption';
import { CorruptionAudio } from '../audio/corruption';
import { DesignTunables as T } from '../ui/design-tunables';
import { VoxelType } from '../world/types';
interface FrozenStripe {
x: number;
width: number;
yTop: number;
yBottom: number;
remaining: number;
cells: Cell[][] | null;
}
export class VisualCorruption {
intensity = 0;
grayscaleActive = 0;
frozenStripeCount = 0;
private time = 0;
private refreshTimer = 0;
private snapshot: GlitchSnapshot | null = null;
private frozenStripes: FrozenStripe[] = [];
private grayscaleRemaining = 0;
// Glitch snapshot is rebuilt at this cadence (seconds). Slower than a
// render frame so individual flickers/blocks actually hang on screen long
// enough for the eye to resolve them, and the apparent glitch rate is the
// same on a 60 Hz and a 144 Hz display.
private static readonly REFRESH_INTERVAL = 0.04;
constructor(
private cols: number,
private rows: number,
private corruptionAudio: CorruptionAudio | null = null,
) {}
update(dt: number, sanity: number, entityProximity: number) {
const V = T.visualCorruption;
this.time += dt;
this.corruptionAudio?.update(dt);
// More aggressive ramp — sanity drops below sanityIdleAbove already produces noticeable effects
const sanityCorruption =
sanity < V.sanityIdleAbove ? (V.sanityIdleAbove - sanity) / V.sanityRampLen : 0;
const entityCorruption = entityProximity * V.entityScale;
this.intensity = Math.min(1, Math.max(sanityCorruption, entityCorruption));
this.refreshTimer -= dt;
if (this.refreshTimer <= 0) {
this.refreshTimer += VisualCorruption.REFRESH_INTERVAL;
if (this.refreshTimer < 0) this.refreshTimer = VisualCorruption.REFRESH_INTERVAL;
this.snapshot = CorruptionRenderer.buildGlitchSnapshot(
this.intensity,
this.time,
this.cols,
this.rows
);
}
// Data-moshing: at high intensity, spawn short-lived frozen vertical
// stripes. Age existing stripes and drop expired ones.
for (let i = this.frozenStripes.length - 1; i >= 0; i--) {
this.frozenStripes[i].remaining -= dt;
if (this.frozenStripes[i].remaining <= 0) {
this.frozenStripes.splice(i, 1);
}
}
this.frozenStripeCount = this.frozenStripes.length;
// Rare full-screen grayscale flash. Expected once per ~15s when
// active; scales slightly with intensity above threshold.
this.grayscaleRemaining -= dt;
if (this.grayscaleRemaining <= 0 && this.intensity > V.grayscaleThreshold) {
const over = this.intensity - V.grayscaleThreshold;
const spawnRate = 0.05 + over * 0.15; // per second
if (Math.random() < spawnRate * dt) {
this.grayscaleRemaining = 0.1 + (Math.random() - 0.5) * 0.05;
}
}
this.grayscaleActive = this.grayscaleRemaining > 0 ? 1 : 0;
if (this.intensity > V.freezeThreshold) {
const over = this.intensity - V.freezeThreshold;
// Expected spawns per second grows with how far past threshold we are.
const spawnRate = over * 35;
if (Math.random() < spawnRate * dt) {
const viewTop = 4;
const viewBottom = this.rows - 2;
const width = 1 + Math.floor(Math.random() * 3);
const x = Math.floor(Math.random() * (this.cols - width));
// Stripe covers a tall slice, not always the full viewport
const sliceH = Math.floor((viewBottom - viewTop) * (0.4 + Math.random() * 0.6));
const yTop = viewTop + Math.floor(Math.random() * (viewBottom - viewTop - sliceH));
const yBottom = yTop + sliceH;
this.frozenStripes.push({
x,
width,
yTop,
yBottom,
remaining: 0.08 + Math.random() * 0.17,
cells: null,
});
this.corruptionAudio?.playStripeFreeze();
}
}
}
apply(renderer: Renderer, _entityProximity: number) {
if (!this.snapshot) return;
CorruptionRenderer.applyGlitches(renderer, this.snapshot);
// Frozen stripes go last so they stomp over any other corruption in
// the slice — the whole point is they're stuck pixels.
const grid = renderer.grid;
for (const s of this.frozenStripes) {
if (s.cells === null) {
// First frame: capture current slice into the buffer.
const buf: Cell[][] = [];
for (let y = s.yTop; y < s.yBottom; y++) {
const row: Cell[] = [];
for (let dx = 0; dx < s.width; dx++) {
row.push({ ...grid[y][s.x + dx] });
}
buf.push(row);
}
s.cells = buf;
} else {
// Subsequent frames: paste the frozen buffer back over the grid.
// Fog cells in the slice are re-sampled live so the pool keeps
// animating inside an otherwise frozen stripe.
for (let y = s.yTop; y < s.yBottom; y++) {
const row = s.cells[y - s.yTop];
for (let dx = 0; dx < s.width; dx++) {
const liveCell = grid[y][s.x + dx];
if (liveCell.material === VoxelType.FOG) continue;
grid[y][s.x + dx] = { ...row[dx] };
}
}
}
}
// Full-screen grayscale flash — runs last so it drains color from
// everything, including frozen stripes and other corruption.
if (this.grayscaleRemaining > 0) {
const rgbRe = /rgb\((\d+),\s*(\d+),\s*(\d+)\)/;
for (let y = 0; y < renderer.rows; y++) {
const row = grid[y];
for (let x = 0; x < renderer.cols; x++) {
const cell = row[x];
if (cell.char === ' ') continue;
if (cell.material === VoxelType.FOG) continue;
const m = rgbRe.exec(cell.fg);
if (!m) continue;
const r = parseInt(m[1], 10);
const g = parseInt(m[2], 10);
const b = parseInt(m[3], 10);
// Red-dominant cells (engravings, cursed symbols, corrupted voxels
// — all use the CORRUPTED tint [1.20, 0.30, 0.55]) stay red. The
// rest of the screen drains to luma grayscale, so the horror text
// suddenly POPS out of the desaturated frame.
if (r > g * 1.4 && r > b * 1.4 && r > 60) continue;
// Rec. 601 luma — the standard perceptual grayscale weights.
const lum = Math.min(255, Math.round(0.299 * r + 0.587 * g + 0.114 * b));
cell.fg = `rgb(${lum}, ${lum}, ${lum})`;
}
}
}
}
}