-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubatomic.ino
More file actions
454 lines (418 loc) · 11.7 KB
/
Subatomic.ino
File metadata and controls
454 lines (418 loc) · 11.7 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
Subatomic
for Move38 Blinks
By Dillon Hall and Dominik Gnirs
*/
/* To Dos:
* - Better energy balancing (more subtlety? danger? Too easy once you find a "good" particle)
* - Write better instructions in README
*
* Eventually:
* - Test startup and finale "waveform" mechanic?
*/
// Ship charge
#define STARTINGENERGY 300
#define ENERGYDECREMENT 1
#define LEVELBOOST 1
#define ENERGYTIMERDELAYMS 100
#define ENERGYMAX 720
#define ENERGYTARGET 1440
#define ENERGYRARITY 16 // Larger is more rare
// Gameplay Rates
#define SCANDECAYMS 5000
#define ABSORBTIMEMS 200
#define PULSERATE 1000
#define DEATHANIMTIME 2000
#define WINANIMTIME 5000
// Color defaults
#define STARTINGENERGYCOLOR WHITE
#define ENERGYACOLOR MAGENTA
#define ENERGYBCOLOR YELLOW
#define ENERGYCCOLOR GREEN
#define ENERGYDCOLOR CYAN
#define ENERGYECOLOR BLUE
#define PARTICLECOLOR OFF //makeColorRGB (150, 75, 0) //Brown
#define UNSCANNED OFF //makeColorRGB (20, 10, 0) //Dark Brown?
#define BUBBLEMAX 30
#define EXPLOSIONRANGE 20
// State and Comms Variables
enum objectTypes {PARTICLE, SHIP};
byte objectType = PARTICLE;
enum gameModes {SETUP, LOADING, INGAME, FINALE};
byte gameMode = SETUP;
enum energyTypes {NONE, ENERGYA, ENERGYB, ENERGYC, ENERGYD, ENERGYE};
Color energyColors[] = {PARTICLECOLOR,ENERGYACOLOR,ENERGYBCOLOR,ENERGYCCOLOR,ENERGYDCOLOR,ENERGYECOLOR};
int energyLoads[] = {60,180,120,60,120};
int energyDecays[] = {4,4,2,1,1};
int energyIndexShift = 0;
/* IN-GAME COMMS SCHEME
* Particles:
* 32 16 8 4 2 1
* <-Energy Type on this face?-> <-Game Mode-> <-0->
* Ship(s):
* 32 16 8 4 2 1
* <-Game Mode-> <-1->
*/
// Ship-specific variables
Timer energyDecayRate;
Timer deathTimer;
Timer overchargeTimer;
int overchargeTarget = 0;
bool isWinning = false;
bool hasWon = false;
bool isDying = false;
bool isDead = false;
int energy = STARTINGENERGY;
int decayRate = ENERGYDECREMENT;
int difficultyBoost = 0;
bool hasBoosted = false;
byte energyType = NONE;
Color energyColor = STARTINGENERGYCOLOR;
bool receivedOnFace[] = {false,false,false,false,false,false};
// Particle-specific variables
Timer scanDecay;
Timer absorbTimer;
bool isScanned = false;
bool scanFading = true;
bool freshScan = false;
int energyContents[] = {NONE, NONE, NONE, NONE, NONE, NONE};
bool sendingOnFace[] = {false,false,false,false,false,false};
// Functions and processes that run at startup, then never again.
void setup() {
randomize(); // Seeds randomness for any RNG use
makeParticle(); // Setup and seed this particle
}
// Main process loop. Each Blink runs this over and over after setup.
void loop() {
// Check for switch object type
checkObjectSwap();
// Run functions specific to object types
switch (objectType) {
case PARTICLE:
checkScan();
checkEnergySend();
break;
case SHIP:
checkEnergyDecay();
checkEnergyReceive();
checkOverchargeProgress();
break;
}
// Run general functions
commsHandler();
displayHandler();
}
// Creating this object as a particle
void makeParticle () {
objectType = PARTICLE;
seedEnergy();
}
// Randomize and distribute energy
void seedEnergy () {
FOREACH_FACE(f) {
int randomEnergy = random(ENERGYRARITY) - (ENERGYRARITY-5);
if (randomEnergy < 0) {
randomEnergy = 0;
}
energyContents[f] = randomEnergy;
}
}
// Switch objects on long press
void checkObjectSwap () {
if (buttonLongPressed()) {
if (objectType == PARTICLE) {
objectType = SHIP;
energy = STARTINGENERGY;
energyColor = STARTINGENERGYCOLOR;
energyDecayRate.set(ENERGYTIMERDELAYMS);
decayRate = ENERGYDECREMENT;
energyIndexShift = random(4);
isDying = false;
isDead = false;
isWinning = false;
hasWon = false;
difficultyBoost = 0;
} else {
makeParticle();
}
}
}
// Handle scan display
void checkScan () {
if (shipConnected() && !particleConnected()) {
if (!isScanned){
seedEnergy();
}
scanFading = false;
isScanned = true;
freshScan = true;
} else {
if (freshScan == true) {
freshScan = false;
scanFading = true;
scanDecay.set(SCANDECAYMS);
} else {
if (scanFading == true && scanDecay.isExpired()) {
scanFading = false;
isScanned = false;
}
}
}
}
// Return true if ship is connected
bool shipConnected () {
bool shipFound = false;
FOREACH_FACE(f) {
if (isShipOnFace(f)) {
shipFound = true;
}
}
return shipFound;
}
// Return true if particle is connected
bool particleConnected () {
bool particleFound = false;
FOREACH_FACE(f) {
if (isParticleOnFace(f)) {
particleFound = true;
}
}
return particleFound;
}
void checkEnergySend () {
FOREACH_FACE(f) {
if (absorbTimer.isExpired() && sendingOnFace[f]) {
sendingOnFace[f] = false;
energyContents[f] = NONE;
}
}
if (shipConnected() && buttonPressed()) {
FOREACH_FACE(f) {
if (energyContents[f] != NONE && isShipOnFace(f)) {
sendingOnFace[f] = true;
absorbTimer.set(ABSORBTIMEMS);
}
}
}
if (!shipConnected()) {
FOREACH_FACE(f) {
sendingOnFace[f] = false;
}
}
}
void checkEnergyReceive() {
FOREACH_FACE(f) {
if (isParticleOnFace(f) && sendingEnergyOnFace(f) && !receivedOnFace[f]) {
int receivedEnergyType = getEnergyContents(getLastValueReceivedOnFace(f));
// Set energy color to new energy color
energyColor = energyColors[receivedEnergyType];
// Determine actual shifted index
receivedEnergyType = ((receivedEnergyType - 1) + energyIndexShift) % 5;
// Add energy load
energy = energy + energyLoads[receivedEnergyType];
// Set decay rate
decayRate = energyDecays[receivedEnergyType];
// Mark received on face
receivedOnFace[f] = true;
}
}
if (!particleConnected()) {
FOREACH_FACE(f) {
receivedOnFace[f] = false;
}
}
}
// Check face for ship
bool isShipOnFace (int f) {
if (!isValueReceivedOnFaceExpired(f)){
if (getObjectType(getLastValueReceivedOnFace(f)) == 1) {
return true;
} else {
return false;
}
} else {
return false;
}
}
// Check face for particle
bool isParticleOnFace (int f) {
if (!isValueReceivedOnFaceExpired(f)){
if (getObjectType(getLastValueReceivedOnFace(f)) == 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}
bool sendingEnergyOnFace (int f) {
if (!isValueReceivedOnFaceExpired(f)){
if (getEnergyContents(getLastValueReceivedOnFace(f)) != NONE) {
return true;
} else {
return false;
}
} else {
return false;
}
}
// See if energy should be decreased or is now empty
void checkEnergyDecay () {
if ( energy > 0 && !isDying && !isDead) {
if (energyDecayRate.isExpired()) {
energy = energy - (decayRate + difficultyBoost);
energyDecayRate.set(ENERGYTIMERDELAYMS);
}
} else {
energy = 0;
destroyShip();
}
}
void checkOverchargeProgress () {
if (isWinning && deathTimer.isExpired()){
hasWon = true;
}
if (energy > ENERGYTARGET) {
if (!isWinning){
isWinning = true;
decayRate = 0;
difficultyBoost = 0;
deathTimer.set(WINANIMTIME);
}
} else if (energy > ENERGYMAX) {
if (!hasBoosted){
hasBoosted = true;
difficultyBoost = LEVELBOOST;
}
if (overchargeTimer.isExpired()) {
overchargeTarget = (overchargeTarget + 1) % 6;
overchargeTimer.set(ENERGYTARGET - energy - 150);
}
}
}
// Out of fuel, game over.
void destroyShip () {
if (!isDying & !isDead){
deathTimer.set(DEATHANIMTIME);
isDying = true;
}
if (deathTimer.isExpired()){
isDying = false;
isDead = true;
makeParticle();
}
}
// Handle outgoing communications
void commsHandler () {
if (objectType == SHIP) {
byte numParticles = 0;
FOREACH_FACE(f) {
if (isParticleOnFace(f)){
numParticles++;
}
}
if (numParticles < 2) {
setValueSentOnAllFaces((gameMode << 1) + 1);
} else {
setValueSentOnAllFaces((gameMode << 1) + 0);
}
} else {
FOREACH_FACE(f) {
if (sendingOnFace[f]) {
setValueSentOnFace((energyContents[f] << 3) + (gameMode << 1) + 0, f);
} else {
setValueSentOnFace((NONE << 3) + (gameMode << 1) + 0, f);
}
}
}
}
byte getObjectType (byte data) {
return (data & 1);
}
byte getGameMode (byte data) {
return ((data >> 1) & 3);
}
byte getEnergyContents (byte data) {
return ((data >> 3) & 7);
}
// Handle LED display
void displayHandler () {
if (objectType == SHIP) { // Ship display handler
// Sliding energy display calculations
int energyPerLED = round(ENERGYMAX / 6); // Amount of energy per LED
int fullLEDs = round(energy / energyPerLED); // How many LEDs are 100% full
int remainder = round(energy % energyPerLED); // remaining energy in partially full LED
FOREACH_FACE(f) {
if (hasWon){
int explosionHue = sin8_C(millis() % EXPLOSIONRANGE) + random(20);
setColorOnFace(makeColorHSB(explosionHue,255,255),f);
} else if (isWinning){
if (deathTimer.getRemaining() > 3000){
if (f == 3) {
setColorOnFace(WHITE,f);
} else {
setColorOnFace(OFF,f);
}
} else if (deathTimer.getRemaining() > 2000){
if (f == 2 || f == 3 || f == 4) {
setColorOnFace(WHITE,f);
} else {
setColorOnFace(OFF,f);
}
} else if (deathTimer.getRemaining() > 1000){
if (f == 1 || f == 2 || f == 3 || f == 4 || f == 5) {
setColorOnFace(WHITE,f);
} else {
setColorOnFace(OFF,f);
}
}
} else if (!isDying && !isDead){
if (fullLEDs > 6) {
if (f == overchargeTarget){
setColorOnFace(WHITE,f);
} else {
setColorOnFace(energyColor, f);
}
} else if (f < (fullLEDs)) {
setColorOnFace(energyColor, f); // Make full LEDs lit
} else if (f > (fullLEDs)){
setColorOnFace(OFF, f); // Make empty LEDs off
} else {
int pulseInvert = PULSERATE / decayRate;
int pulseRemaining = millis() % pulseInvert;
byte pulseMapped = map(pulseRemaining, 0, pulseInvert, 0, 255);
byte dimness = sin8_C(pulseMapped);
setColorOnFace(dim(energyColor,dimness), f);
//setColorOnFace(dim(energyColor,map(remainder,0,energyPerLED,0,255)), f); // set partial brightness to partially full LED, mapped to 255 scale
}
} else if (isDying) {
if (deathTimer.getRemaining() > 400){
int explosionHue = sin8_C(millis() % EXPLOSIONRANGE);
setColorOnFace(makeColorHSB(explosionHue + 110,255,255),f);
} else {
int noise = random(deathTimer.getRemaining());
setColorOnFace(makeColorRGB(noise,noise,noise),f);
}
} else {
setColorOnFace(OFF,f);
}
}
} else { // Particle display handler
if (!isScanned) { // Unscanned particles display as blank (for now)
FOREACH_FACE (f) {
int bubbleNoise = 40; //random(BUBBLEMAX);
setColorOnFace(makeColorRGB(bubbleNoise,bubbleNoise,bubbleNoise), f);
}
} else if (!scanFading) { // Scanned but not fading (usually because ship still connected) displays as full brightness
FOREACH_FACE (f) {
setColorOnFace(energyColors[energyContents[f]],f);
}
} else { // Scanned and fading, dim to match fading scan
int scanRemaining = map(scanDecay.getRemaining(),0,SCANDECAYMS,0,255);
FOREACH_FACE (f) {
setColorOnFace(dim(energyColors[energyContents[f]],scanRemaining),f);
}
}
}
}