-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisc-tunnel-scene.js
143 lines (118 loc) · 4.97 KB
/
disc-tunnel-scene.js
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
import { sinus, solidColor, Scene } from './js-demo.js';
const discColors = [
{r : 255, g : 255, b : 255},
{r : 255, g : 64, b : 128},
{r : 255, g : 255, b : 255},
{r : 96, g : 240, b : 255}
];
export class DiscTunnelScene extends Scene {
constructor() {
super();
this.name = 'Disc Tunnel';
this.part = -1;
this.discFarA = 32768;
this.discZOffset = 0;
this.discRenderMinIndex = 9;
this.discRenderMaxIndex = 9;
this.discColoredMinIndex = 10;
this.discIndexOffset = 0;
this.discRotation = 0;
this.discOffsetMinZ = 1050;
}
async prepare(timeSource, progressCallback) {
timeSource.watch(18, 0);
timeSource.watch(19, 0);
timeSource.watch(20, 0);
timeSource.watch(21, 0);
timeSource.watch(22, 48);
progressCallback(1, 1);
}
handleTime(pos, row) {
if (pos === 18 && row === 0) {
this.part = 0;
}
if (pos === 19 && row === 0) {
this.part = 1;
}
if (pos === 20 && row === 0) {
this.part = 2;
}
if (pos === 21 && row === 0) {
this.part = 3;
}
if (pos === 22 && row === 48) {
this.part = 4;
}
}
render(canvas, context, time) {
const frameDiff = (this.lastTime ? time - this.lastTime : 16) / 1000 * 60;
context.fillStyle = "#240000";
context.fillRect(0, 0, canvas.width, canvas.height);
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const discFarX = centerX + canvas.width / 2 * sinus[this.discFarA];
const discFarY = centerY + canvas.width / 2 * sinus[(this.discFarA + 16384) % 65536];
const zOffset = (this.part == 0) ? ((sinus[(time * 32768 / 3840) & 0xffff]) * 800) : this.discZOffset;
const maxDisc = (this.part == 0) ? 0 : this.discRenderMaxIndex;
const minDisc = (this.part == 0) ? 0 : this.discRenderMinIndex;
for (let disc = maxDisc; disc >= minDisc; --disc) {
const z = 50 + 100 * disc + zOffset; // From 50 to (50 + 10 * 10) = 1050
const alpha = (this.part == 0) ? 1 : 1 - (z - 50) / 1000;
const zForOffset = z - this.discOffsetMinZ;
const alphaForOffset = Math.min(1, 1 - (zForOffset - 50) / 1000);
const colorIndex = (disc >= this.discColoredMinIndex) ? (disc + this.discIndexOffset) % 4 : 0;
const color = discColors[colorIndex];
const discX = (centerX * alphaForOffset) + (discFarX * (1 - alphaForOffset));
const discY = (centerY * alphaForOffset) + (discFarY * (1 - alphaForOffset));
context.beginPath();
for (let a = 0; a <= 65536; a += 256) {
const outerA = (a * 10) % 65536;
const radius = (1 + sinus[outerA] * 0.3) * canvas.width / 2;
const x = discX + ((100 / z) * radius) * sinus[(a + this.discRotation) % 65536];
const y = discY + ((100 / z) * radius) * sinus[(a + 16384 + this.discRotation) % 65536];
if (a) {
context.lineTo(x, y);
} else {
context.moveTo(x, y);
}
}
for (let a = 65536; a >= 0; a -= 4096) {
const radius = canvas.width / 2;
const x = discX + ((100 / z) * radius) * sinus[(a + this.discRotation) % 65536];
const y = discY + ((100 / z) * radius) * sinus[(a + 16384 + this.discRotation) % 65536];
context.lineTo(x, y);
}
context.closePath();
const r = (color.r * alpha) + (0x24 * (1 - alpha));
const g = (color.g * alpha);
const b = (color.b * alpha);
context.fillStyle = solidColor(r, g, b);
context.fill();
}
let newDiscZOffset = (this.discZOffset - 9 * frameDiff);
while (newDiscZOffset < 0) {
++this.discIndexOffset;
newDiscZOffset += 100;
if (this.part >= 2 && this.discColoredMinIndex > 0) {
--this.discColoredMinIndex;
}
if (this.part >= 1 && this.discRenderMinIndex > 0) {
--this.discRenderMinIndex;
}
if (this.part == 4) {
--this.discRenderMaxIndex;
}
}
this.discZOffset = newDiscZOffset | 0;
this.discRotation = (this.discRotation + 65536 - 473 * frameDiff) & 0xffff;
if (this.part >= 3) {
if (this.discOffsetMinZ > 0) {
this.discOffsetMinZ -= 9 * frameDiff;
} else {
this.discOffsetMinZ = 0;
this.discFarA = (this.discFarA + 65536 - 100 * frameDiff) & 0xffff;
}
}
this.lastTime = time;
}
}