-
Notifications
You must be signed in to change notification settings - Fork 0
/
sceneControl.js
140 lines (126 loc) · 3.68 KB
/
sceneControl.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
let scene = "TITLE";
let sphereInfo = {
rVel: [0, 0],
pos: [-1000, 1000],
scale: 1,
light: 0,
};
function updateSphere(rVel, pos, scale, lightUp) {
sphereInfo.rVel[0] += (rVel[0] - sphereInfo.rVel[0] < 0 ? -1 : 1) * 0.01;
sphereInfo.rVel[1] += (rVel[1] - sphereInfo.rVel[1] < 0 ? -1 : 1) * 0.01;
applyRotation(rVel[0], rVel[1]);
sphereInfo.pos[0] += (pos[0] - sphereInfo.pos[0]) * SPHERE_TRANSITION_SPEED;
sphereInfo.pos[1] += (pos[1] - sphereInfo.pos[1]) * SPHERE_TRANSITION_SPEED;
const scaleChange = (scale - sphereInfo.scale) * SPHERE_TRANSITION_SPEED;
sphereInfo.scale += scaleChange > 0.1 ? 0.1 : scaleChange;
if (lightUp) {
sphereInfo.light = min(sphereInfo.light + 0.012, 1);
}
}
function renderSphere() {
push();
translate(sphereInfo.pos[0], sphereInfo.pos[1]);
scale(sphereInfo.scale);
stroke(...COLORS.GRID);
strokeWeight(3.5);
for (const ue of uniqueEdges) {
if (ue.smallFaces[0].isVisible || ue.smallFaces[1].isVisible) {
line(ue.v0[0], ue.v0[1], ue.v1[0], ue.v1[1]);
}
}
if (sphereInfo.light > 0) {
noStroke();
fill(...COLORS.GRID, 255 * sphereInfo.light);
for (let i = 0; i < allSmallFaces.length; i++) {
const sf = allSmallFaces[i];
if (!sf.isVisible) continue;
const vs = sf.vertices;
triangle(vs[0][0], vs[0][1], vs[1][0], vs[1][1], vs[2][0], vs[2][1]);
}
}
pop();
}
const playBtn = new GameButton(500, 480, 140, 50, 36, "Play", function () {
scene = "SELECT";
});
function titleScene() {
updateSphere([0.2, 0.2], [90, 650], 1.2);
playBtn.render();
noStroke();
textSize(120);
fill(...COLORS.GRID);
text("LASER", 300, 100);
text("LOOP", 300, 220);
fill(...COLORS.LASER);
textSize(220);
text("4", 500, 260);
}
function titleSceneTouchEnded() {
if (playBtn.isHovered) playBtn.clicked();
}
function startGame(d) {
difficultyLevel = d;
generator.generate();
generateCountdown = 100;
}
const easyBtn = new GameButton(120, 150, 200, 60, 38, "Short", function () {
startGame(0);
});
const mediumBtn = new GameButton(120, 250, 200, 60, 38, "Medium", function () {
startGame(1);
});
const hardBtn = new GameButton(120, 350, 200, 60, 38, "Long", function () {
startGame(2);
});
function selectScene() {
updateSphere([0, -0.1], [300, 900], 1.5);
easyBtn.render();
mediumBtn.render();
hardBtn.render();
// Draw stats
fill(...COLORS.GRID);
textSize(28);
text("Completed", 330, 80);
text("Best time", 500, 80);
textSize(36);
for (let i = 0; i < STATS.length; i++) {
text(STATS[i].completeCount, 330, 150 + i * 100);
const bt = STATS[i].bestTime;
let displayTime = "-";
if (bt !== null) {
let minute = floor(bt / 60000);
let sec = floor((bt % 60000) / 1000) + "";
if (sec.length === 1) sec = "0" + sec;
displayTime = `${minute}:${sec}`;
}
text(displayTime, 500, 150 + i * 100);
}
}
function selectSceneTouchEnded() {
if (easyBtn.isHovered) easyBtn.clicked();
else if (mediumBtn.isHovered) mediumBtn.clicked();
else if (hardBtn.isHovered) hardBtn.clicked();
}
let generateCountdown = 0;
function generatingScene() {
generateCountdown--;
if (generateCountdown > 0 || !generator.isDoneGenerating) {
updateSphere([8, 0], [300, 300], 0.5);
if (!generator.isDoneGenerating) {
// multi stepping
if (generator.isGeneratingLaser) {
for (let i = 0; i < 70; i++) {
if (!generator.isGeneratingLaser) break;
generator.stepGenerateLaser();
}
}
}
} else {
updateSphere([(1 - sphereInfo.scale) * 15, 0], [300, 300], 1, true);
// done fake timer & sphere normal size?
if (1 - sphereInfo.scale < 0.001) {
scene = "PLAY";
startTime = Date.now();
}
}
}