-
Notifications
You must be signed in to change notification settings - Fork 7
/
lantern.ts
207 lines (170 loc) · 7.08 KB
/
lantern.ts
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//% icon="\uf185" color="#8f1fff"
namespace lantern {
let bandPalettes: Buffer[];
// The top row is just the palette, each row gets darker
const palette_ramps = image.ofBuffer(hex`e4100400ffff0000d1cb0000a2ff0000b3fc0000e4fc000045ce000086fc000067c80000c8ff000069c80000bafc0000cbff0000fcff0000bdfc0000ceff0000ffff0000`);
export interface LightAnchor {
x: number;
y: number;
}
export class LightSource {
anchor: LightAnchor;
offsetTable: Buffer;
width: number;
height: number;
constructor(public rings: number, public bandWidth: number, public centerRadius: number) {
const halfh = centerRadius + rings * bandWidth;
this.offsetTable = control.createBuffer((rings + 1) * halfh);
// Approach is roughly based on https://hackernoon.com/pico-8-lighting-part-1-thin-dark-line-8ea15d21fed7
let x: number;
let band: number;
let y2: number;
for (let y = 0; y < halfh; y++) {
y2 = Math.pow(y, 2);
// Store the offsets where the bands switch light levels for each row. We only need to
// do one quadrant which we can mirror in x/y
for (band = 0; band < rings; band++) {
x = Math.sqrt(Math.pow(centerRadius + bandWidth * (band + 1), 2) - y2) | 0;
this.offsetTable[y * rings + band] = x;
}
}
this.width = halfh;
this.height = halfh;
}
apply() {
const camera = game.currentScene().camera;
const halfh = this.width;
const cx = this.anchor.x - camera.drawOffsetX;
const cy = this.anchor.y - camera.drawOffsetY;
let prev: number;
let offset: number;
let band: number;
// First, black out the completely dark areas of the screen
screen.fillRect(0, 0, screen.width, cy - halfh + 1, 15)
screen.fillRect(0, cy - halfh + 1, cx - halfh, halfh << 1, 15)
screen.fillRect(cx + halfh, cy - halfh + 1, screen.width - cx - halfh, halfh << 1, 15)
screen.fillRect(0, cy + halfh, screen.width, screen.height - (cy + halfh), 15)
// Go over each row and darken the colors
for (let y = 0; y < halfh; y++) {
band = this.rings;
prev = 0;
offset = this.offsetTable[y * this.rings + band - 1]
// Black out the region outside the darkest light band
screen.mapRect(cx - halfh, cy + y + 1, halfh - offset, 1, bandPalettes[bandPalettes.length - 1])
screen.mapRect(cx - halfh, cy - y, halfh - offset, 1, bandPalettes[bandPalettes.length - 1])
screen.mapRect(cx + offset, cy + y + 1, halfh - offset, 1, bandPalettes[bandPalettes.length - 1])
screen.mapRect(cx + offset, cy - y, halfh - offset, 1, bandPalettes[bandPalettes.length - 1])
// Darken each concentric circle by remapping the colors
while (band > 0) {
offset = this.offsetTable[y * this.rings + band - 1]
if (offset) {
offset += (Math.idiv(Math.randomRange(0, 11), 5))
}
// We reflect the circle-quadrant horizontally and vertically
screen.mapRect(cx + offset, cy + y + 1, prev - offset, 1, bandPalettes[band - 1])
screen.mapRect(cx - prev, cy + y + 1, prev - offset, 1, bandPalettes[band - 1])
screen.mapRect(cx + offset, cy - y, prev - offset, 1, bandPalettes[band - 1])
screen.mapRect(cx - prev, cy - y, prev - offset, 1, bandPalettes[band - 1])
prev = offset;
band--;
}
}
}
}
export class LanternEffect implements effects.BackgroundEffect {
protected sources: LightSource[];
protected static instance: LanternEffect;
protected anchor: LightAnchor;
protected init: boolean;
protected running: boolean;
protected breathing: boolean;
public static getInstance() {
if (!LanternEffect.instance) LanternEffect.instance = new LanternEffect();
return LanternEffect.instance;
}
private constructor() {
bandPalettes = [];
for (let band = 0; band < 6; band++) {
const buffer = control.createBuffer(16);
for (let i = 0; i < 16; i++) {
buffer[i] = palette_ramps.getPixel(i, band + 1);
}
bandPalettes.push(buffer);
}
this.setBandWidth(13);
this.setAnchor({ x: screen.width >> 1, y: screen.height >> 1 });
this.running = false;
this.breathing = true;
}
startScreenEffect() {
this.running = true;
if (this.init) return;
this.init = true;
let index = 0;
scene.createRenderable(91, () => {
if (!this.running) return;
this.sources[index].apply();
})
let up = true;
game.onUpdateInterval(1000, () => {
if (!this.running) return;
if (!this.breathing) {
index = 1;
return;
}
if (up) index++;
else index--;
if (index < 0) {
index = 1;
up = true;
}
else if (index >= this.sources.length) {
index = this.sources.length - 2;
up = false;
}
})
}
stopScreenEffect() {
this.running = false;
}
setAnchor(anchor: LightAnchor) {
this.anchor = anchor;
this.sources.forEach((value: LightSource, index: number) => {
value.anchor = this.anchor;
});
}
setBandWidth(width: number) {
this.sources = [
new LightSource(4, width - 1, 2),
new LightSource(4, width, 1),
new LightSource(4, width + 1, 2)
];
this.setAnchor(this.anchor)
}
setBreathingEnabled(enabled: boolean) {
this.breathing = enabled;
}
}
//% block
export function startLanternEffect(anchor: Sprite) {
if (!anchor) {
stopLanternEffect();
return;
}
const effect = LanternEffect.getInstance();
effect.setAnchor(anchor);
effect.startScreenEffect();
}
//% block
export function stopLanternEffect() {
LanternEffect.getInstance().stopScreenEffect();
}
//% block
export function setLightBandWidth(width: number) {
LanternEffect.getInstance().setBandWidth(width);
}
//% block
export function setBreathingEnabled(enabled: boolean) {
LanternEffect.getInstance().setBreathingEnabled(enabled);
}
}