-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
190 lines (177 loc) · 4.61 KB
/
main.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
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
let canvas = document.getElementById("draw");
canvas.width = document.body.clientHeight;
canvas.height = document.body.clientHeight;
let rps = null;
let recorder = null;
let saveName = null;
let matrixSize = 128;
let preset = Object.keys(saves)[0];
// GUI Object
let gui = new guify({
title: "Rock Paper Scissors",
open: true,
panelOverflowBehavior: 'overflow'
});
async function start(saveName) {
// recorder = new GIF({
// workers: 2,
// quality: Math.floor(canvas.height / matrixSize)
// });
// recorder.on('finished', function(blob) {
// window.open(URL.createObjectURL(blob));
// });
if (saves[saveName]["init_random"] == false &&
saves[saveName]['initial_matrix'] == undefined)
{
saves[saveName]['initial_matrix'] = await readNibbleFile(saveName);
}
rps = new RPSCanvasWrapper(canvas, saves[saveName]);
rps.play();
}
function getSaveName() {
let v = window.location.search;
return v.slice(1);
}
let guiFeatures = structuredClone(defaultFeatures);
guiFeatures.locked = true;
// Set up the user interface for controlling relevant RPS parameters
function configureGUI() {
gui.Register(
[
{
type: 'title',
label: 'Simulation'
},
// Preset selector
{
type: 'select',
label: 'Preset',
options: Object.keys(saves),
onChange: (value) => {
// Update preset
preset = value;
// Pause; start; reinit color pickers
rps.pause();
start(value);
colorPickers();
// Load the existing non-competitor properties if not locked
if (guiFeatures.locked) {
// Overwrite
rps.features.smoothing = guiFeatures.smoothing;
rps.features.fps = guiFeatures.fps;
rps.features.ifdead = guiFeatures.ifdead;
}
}
},
// Pause the simulation
{
type: 'button', label: 'Play / Pause',
action: () => {
// Invert the paused variable
if (rps.paused) { rps.play(); }
else { rps.pause(); }
}
},
// Reset the simulation
{
type: 'button', label: 'Reset',
action: () => {
// Pause
rps.pause();
rps.matrixIndex = 0;
// If the matrix is initialized randomly
if (rps.features.init_random) {
rps.randomizeMatrix();
}
// If there is an initial state
else {
rps.matrix[0] = structuredClone(rps.features.initial_matrix);
}
// Resume
rps.play();
}
},
// Step the simulation
{
type: 'button', label: 'Step',
action: () => { rps.step(); }
},
{
type: 'title',
label: 'Rendering'
},
// Lock the relevant parameters
{
type: 'checkbox', label: 'Locked',
object: guiFeatures,
property: 'locked'
},
// Smooth between pixels
{
type: 'checkbox', label: 'Smoothing',
object: guiFeatures,
property: 'smoothing',
onChange: (value) => {
rps.features.smoothing = value;
}
},
// Initialize randomly
{
type: 'checkbox', label: 'Border',
object: guiFeatures,
property: 'ifdead',
onChange: (value) => {
rps.features.ifdead = value;
}
},
// Frames per second
{
type: 'range', label: 'FPS',
min: 1, max: 60, step: 1,
initial: 24,
object: guiFeatures,
property: 'fps',
onChange: (value) => {
rps.features.fps = value;
}
},
// Attackers
{
type: 'range', label: 'Weapons (?)',
min: 3, max: 16, step: 1,
onChange: (value) => {
// Print
}
}
]
);
}
let removableFolder = null;
function colorPickers() {
// If there's an existing set of color pickers
if (removableFolder != null) {
// Remove them
gui.Remove(removableFolder);
}
// Add the new folder
removableFolder = gui.Register({
// Color Picker Folder
type: 'folder', label: 'Colors', open: true
});
// Add the new individual pickers
for (let i = 0; i < rps.features.competitors.length; i++) {
gui.Register({
type: 'color',
label: 'Color ' + (i+1),
format: 'hex',
initial: rps.features.competitors[i],
folder: 'Colors',
onChange: (value) => {
// Update the value of the competitor
rps.features.competitors[i] = value;
}
});
}
}
// Start the game then load the colors
start(preset).then(configureGUI).then(colorPickers);