-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
executable file
·2348 lines (1823 loc) · 56.2 KB
/
engine.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var canvas;
var canvasContainer;
var outOfCanvas;
var onInterface;
var gameState;
var FPS = 30;
var frames = 0;
var TILE = 64;
var TILE_W = 60;
var TILE_H = 30;
var ROWS = 30;
var COLS = 30;
var TURN = 0.1;
var PIXEL_DENSITY;
var mousePosition;
var turnCounter;
var turns;
var time;
var deltaTime;
var lastMillis;
var pMouseIsPressed;
var interpolator;
var counter;
var restartDialog;
var PRE_TURNS = 10; //let the sim run x times before it gets visualized
//Ecology variables
//the file
var ECOLOGY_FILE = "ecology.csv";
//the table
var ecology;
var CITY_RESILIENCE = 15; //x turns before it turns into ruin
var DESERTIFICATION_LEVELS = 0;
var OASIFY_LEVELS = 3;
var FOREST_LEVELS = 6;
var DEFOREST_LEVELS = 8;
var REGROWTH_TIME = 50;
var WEED_POWER = 10;
var WETLAND_STRENGTH = 6;
var ANNIHILATION = 1; //
var ANNIHILATION_SIZE = ANNIHILATION * 6; //
var GLITCH_DURATION = 60;
var TRANSFORM_DURATION = 10;
var PLASTIC_FREQUENCY = 14; //every % ruins/concrete converted up to 16
var LOW_TRASH = 20; //to count for clean
//Disasters
var HEAT_WAVE_DURATION = 30; //in turns
var HEAT_WAVE_SEVERITY = 0.1; //chances of desertification over a dice roll of HEAT_WAVE_DURATION
var WILDFIRE_DURATION = 32;
var FLOOD_DURATION = 50;
var FLOOD_VIZ = 60;
var FLOOD_SEVERITY = 5; //chances of flooding on a dice roll of HEAT_WAVE_DURATION
var OIL_BREAKDOWN = 0.01;
var disasterCounter = 0;
var lastDisaster = 0;
var DISASTER_FREQUENCY_MIN = 500;
var DISASTER_FREQUENCY_MAX = 1000;
var seaLevelRise = 0;
var SEA_RISE = 2; //after how many floods
//corresponding to row in the table
var GRASS = 0;
var WATER = 1;
var CONCRETE = 2;
var WASTELAND = 3;
var FIRE = 4;
var GREEN_CUBE = 5;
var BURNED = 6;
var BURNED_FOREST = 7;
var RUIN = 8;
var BLACK_BURN = 9;
var CITY = 10;
var FOREST = 11;
var PLASTIC = 12;
var WEED = 13;
var BLACK_CUBE = 14;
var GLITCH_CITY = 15;
var GLITCH_CUBE = 16;
var STONE_CUBE = 17;
var BLUE_CUBE = 18;
var FLOOD = 19;
var WHITE_CUBE = 20;
var OIL = 21;
var WETLAND = 22;
var ROAD = 23;
var ROAD_CUBE = 24;
var id = [];
//as they appear in menu and right click sequence - the properties of the obj below
var cubeLabels = ["green", "blue", "stone", "white", "road", "glitch", "black"];
var cubeIndex = 0;
var currentCube = null; //reference to an object
var cubes = {
fire: {
id: FIRE,
tile: null,
sprite: "blocks_25", //the sprite
buttonSprite: "fireButton"
},
green: {
id: GREEN_CUBE,
tile: null,
sprite: "blocks_11",
buttonSprite: "greenButton"
},
blue: {
id: BLUE_CUBE,
tile: null,
sprite: "blocks_23",
buttonSprite: "blueButton"
},
stone: {
id: STONE_CUBE,
tile: null,
sprite: "blocks_33",
buttonSprite: "stoneButton"
},
glitch: {
id: GLITCH_CUBE,
tile: null,
sprite: "blocks_05",
buttonSprite: "glitchButton"
},
black: {
id: BLACK_CUBE,
tile: null,
sprite: "blocks_41",
buttonSprite: "blackButton"
},
white: {
id: WHITE_CUBE,
tile: null,
sprite: "blocks_56",
buttonSprite: "whiteButton"
},
road: {
id: ROAD_CUBE,
tile: null,
sprite: "roadCube",
buttonSprite: "roadButton"
}
};
//Terrain generation
var TERRAIN_NOISE_DETAIL = 0.2;
var CIVILIZATION_NOISE_DETAIL = 0.2;
var CIVILIZATION_OFFSET = 1000; //noise offset
var terrain;
//0-1 scale terrain gen
var terrainScale = [WATER, GRASS, GRASS];
var civilizationScale = [null, null, WASTELAND, CONCRETE, CONCRETE];
/////////GUI variables
var TITLE_W = 600;
var TITLE_H = 145;
var MARGIN = 20;
var MENU_Y = ROWS * TILE + MARGIN;
var zoomCounter = 0;
var scrollCounter;
var SCROLL_MARGIN = 8;
var SCROLL_SPEED = 1;
var MIN_ZOOM = 0.5;
var MAX_ZOOM = 2;
var ZOOM_DRAG = 40; //sensitivity
//where it starts the drag
var dragZoomPoint;
var viewZoom;
var previousZoom;
var buttons = [];
var buttonGroup;
var heatGroup;
var markerGroup;
var okButton;
var cancelButton;
//confirm dialog
var BOX_W = 300;
var BOX_H = 90;
var dragTile;
var dragPoint; //further correction to avoid false dnd on cube extended hit area
var scrollPoint;
//menu
var MENU_W = 100;
var BTN = 68; //tile as seen in GUI
var ICON = 80;
var RESTART_BTN = 56;
var MARKER = 40;
var buttonNormal;
var buttonRoll;
var buttonSelected;
var glitchButton, blackButton;
//top bar
var BAR_W = 800;
var BAR_H = 4;
var BAR_Y = 14;
var barY = -10;
var targetBarY = -10;
var BAR_BORDER = 1.5;
var BAR_SCALE = 200;
var CITY_COLOR_1, CITY_COLOR_2, RUIN_COLOR_1, RUIN_COLOR_2;
//text messages lots of var to prevent lots of calculations
var messages; //the queue
var currentMessage; //the final string
var messageWidth; //the calculated lenght in px
var messageIndex; //the substring index
var messageCounter; //the time before it disappears
var POST_DELAY = 0.06; //msg.length multiplier time before disappearing
var POST_DELAY_FIXED = 1;
var subMessage; //the substring of the current message
var subWidth; //the width of the substring
var MSG_SIZE = 24; //font size
var MSG_H = MSG_SIZE / 2;
var MSG_PADDING = 14;
var MSG_Y_OFFSET = 4;
var MSG_MARGIN = 26; //from bottom
var cameraMoved = true;
var titleImage;
var titleMask;
var titleNoise;
var noiseCount;
////// vars across session
var firstPlay = true;
var glitchUnlocked = false;
var blackUnlocked = false;
//////global game vars
var natural = 0;
var artificial = 0;
var nW = 0;
var aW = 0;
//resources are necessary to sustain cities
var resources = 0;
var transition;
var transitions = {};
var TRANSITION_FRAMES = 8;
var plasticCounter = 0;
var plastiglomerates = 0;
var heatWaveCounter = 0;
var wildFireCounter = 0;
var floodCounter = 0;
var floodViz = 0;
var plasticSequence;
var dissolve; //animation
//refs to current cube tiles
var stoneCubeTile, greenCubeTile, blueCubeTile, glitchCubeTile, blackCubeTile;
//the one currently hovering
var currentTile;
//graphics objects
var bg; //background image
var bgDisaster;
var overlay;
var graphics; //graphics container
var bgX = 0;
var bgY = 0;
var bgNoise = 0;
var bgA = 0;
var fps = FPS; //viz framerate with easing
var NOISE_SHAKE = 50;
var glitchPreview = false; //freeze glitches
var BG_FLOOD;
var BG_HEAT;
var BG_FIRE;
//randomizing array
var shuffle4 = [0, 1, 2, 3];
var shuffle8 = [0, 1, 2, 3, 4, 5, 6, 7];
///////Sound stuff
var VOLUME = 0.5; //master multiplier
var VOLUME_OFFSCREEN = 0.1; //reducer for offscreen sounds
var sounds = {};
var messageFont;
//debugging
var debugMillis = 0;
function preload() {
messageFont = loadFont('fonts/Mecha.ttf');
BG_FLOOD = color(56, 78, 120, 10);
BG_HEAT = color("#f4e0b710");
BG_FIRE = color("#f2a86c10");
CITY_COLOR_1 = color("#f2ecd8");
CITY_COLOR_2 = color("#dbccb4");
RUIN_COLOR_1 = color("#696774");
RUIN_COLOR_2 = color("#476265");
ecology = loadTable(ECOLOGY_FILE, "csv", "header");
graphics = {};
tileGraphics = {};
for (var i = 1; i <= 56; i++) {
var n = nf(i, 2, 0);
tileGraphics["blocks_" + n] = loadImage("images/blocks_" + n + ".png");
}
for (var i = 1; i <= 56; i++) {
var n = nf(i, 2, 0);
//print(n);
tileGraphics["ground_" + n] = loadImage("images/ground_" + n + ".png");
}
for (var i = 1; i <= 56; i++) {
var n = nf(i, 2, 0);
//print(n);
tileGraphics["water_" + n] = loadImage("images/water_" + n + ".png");
}
for (var i = 1; i <= 72; i++) {
var n = nf(i, 2, 0);
//print(n);
tileGraphics["buildings_" + n] = loadImage("images/buildings_" + n + ".png");
}
for (var i = 1; i <= 24; i++) {
var n = nf(i, 2, 0);
//print(n);
tileGraphics["flooring_" + n] = loadImage("images/flooring_" + n + ".png");
}
for (var i = 1; i <= 40; i++) {
var n = nf(i, 2, 0);
//print(n);
tileGraphics["plants_" + n] = loadImage("images/plants_" + n + ".png");
}
//plastiglomerate
for (var i = 1; i <= 16; i++) {
var n = nf(i, 2, 0);
//print(n);
tileGraphics["plastic_" + n] = loadImage("images/plastic_" + n + ".png");
}
//marsh
for (var i = 1; i <= 14; i++) {
var n = nf(i, 2, 0);
//print(n);
tileGraphics["wetland_" + n] = loadImage("images/wetland_" + n + ".png");
}
tileGraphics["blackButton"] = loadImage("images/blackButton.png");
tileGraphics["greenButton"] = loadImage("images/greenButton.png");
tileGraphics["blueButton"] = loadImage("images/blueButton.png");
tileGraphics["stoneButton"] = loadImage("images/stoneButton.png");
tileGraphics["fireButton"] = loadImage("images/fireButton.png");
tileGraphics["glitchButton"] = loadImage("images/glitchButton.png");
tileGraphics["whiteButton"] = loadImage("images/whiteButton.png");
tileGraphics["roadButton"] = loadImage("images/roadButton.png");
tileGraphics["roadCube"] = loadImage("images/roadCube.png");
buttonSprite = loadImage("images/buttonNormal.png");
buttonRoll = loadImage("images/buttonRoll.png");
buttonSelected = loadImage("images/buttonSelected.png");
titleImage = loadImage("images/title.png");
titleMask = loadImage("images/titleMask.png");
titleNoise = loadImage("images/noise.png");
graphics.drop = loadImage("images/drop.png");
graphics.tutorial = loadImage("images/tutorial.png");
graphics.credits = loadImage("images/credits.png");
graphics.restart = loadImage("images/restartBtn.png");
graphics.restartRoll = loadImage("images/restartBtnRoll.png");
graphics.fireIcon = loadImage("images/fireIcon.png");
graphics.floodIcon = loadImage("images/floodIcon.png");
graphics.droughtIcon = loadImage("images/droughtIcon.png");
graphics.marker = loadImage("images/marker.png");
graphics.markerRoll = loadImage("images/markerRoll.png");
graphics.okBtn = loadImage("images/okBtn.png");
graphics.okBtnRoll = loadImage("images/okBtnRoll.png");
graphics.cancelBtn = loadImage("images/cancelBtn.png");
graphics.cancelBtnRoll = loadImage("images/cancelBtnRoll.png");
transitions = {};
transitions["grass"] = loadAnimation("images/transitions/grass0000.png", "images/transitions/grass0007.png");
transitions["tree"] = loadAnimation("images/transitions/tree0000.png", "images/transitions/tree0007.png");
transitions["water"] = loadAnimation("images/transitions/water0000.png", "images/transitions/water0007.png");
transitions["building"] = loadAnimation("images/transitions/building0000.png", "images/transitions/building0007.png");
dissolve = loadAnimation("images/dissolve_01.png", "images/dissolve_08.png");
dissolve.frameDelay = 3;
sounds = {};
sounds.bricks = [];
for (var i = 1; i <= 13; i++) {
sounds.bricks.push(loadSound("sounds/city" + i + ".mp3"));
}
sounds.fire = [];
for (var i = 1; i <= 6; i++) {
sounds.fire.push(loadSound("sounds/fire" + i + ".mp3"));
}
sounds.foliage = [];
for (var i = 1; i <= 8; i++) {
sounds.foliage.push(loadSound("sounds/foliage" + i + ".mp3"));
}
sounds.grass = [];
for (var i = 1; i <= 9; i++) {
sounds.grass.push(loadSound("sounds/grass" + i + ".mp3"));
}
sounds.water = [];
for (var i = 1; i <= 8; i++) {
sounds.water.push(loadSound("sounds/water" + i + ".mp3"));
}
sounds.weed = [];
for (var i = 1; i <= 5; i++) {
sounds.weed.push(loadSound("sounds/weed" + i + ".mp3"));
}
sounds.ruins = [];
for (var i = 1; i <= 9; i++) {
sounds.ruins.push(loadSound("sounds/ruins" + i + ".mp3"));
}
sounds.glitch = [];
for (var i = 1; i <= 10; i++) {
sounds.glitch.push(loadSound("sounds/glitch" + i + ".mp3"));
}
sounds.tones = [];
for (var i = 1; i <= 8; i++) {
sounds.tones.push(loadSound("sounds/tone" + i + ".mp3"));
}
sounds.annihilation = loadSound("sounds/annihilate.mp3");
sounds.flood = loadSound("sounds/flood.mp3");
sounds.heatWave = loadSound("sounds/heatwave.mp3");
sounds.fireWave = loadSound("sounds/fireSound.mp3");
var is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
if (is_safari) {
print("Safari detected: using aac files");
sounds.forestLoop = loadSound("sounds/forestLoop.aac");
sounds.desertLoop = loadSound("sounds/desertLoop.aac");
sounds.cityLoop = loadSound("sounds/cityLoop.aac");
} else {
print("Not safari");
sounds.forestLoop = loadSound("sounds/forestLoop.ogg");
sounds.desertLoop = loadSound("sounds/desertLoop.ogg");
sounds.cityLoop = loadSound("sounds/cityLoop.ogg");
}
sounds.glitchBuzz = loadSound("sounds/glitchBuzz.mp3");
sounds.theme = loadSound("sounds/theme2.mp3");
sounds.opening = loadSound("sounds/opening.mp3");
}
function setup() {
frameRate(FPS);
canvasContainer = document.getElementById('canvasContainer');
if (canvasContainer != null) {
canvas = createCanvas(canvasContainer.offsetWidth, canvasContainer.offsetHeight);
canvas.parent("canvasContainer");
} else
canvas = createCanvas(windowWidth, windowHeight);
PIXEL_DENSITY = pixelDensity();
noStroke();
noSmooth();
canvas.elt.addEventListener("mouseout", function() {
outOfCanvas = true;
}, false);
canvas.elt.addEventListener("mouseover", function() {
outOfCanvas = false;
}, false);
id = [];
//check tile types and ids from table
for (var i = 0; i < ecology.getRowCount(); i++) {
var row = ecology.getRow(i);
var nid = row.getString("id");
id[i] = nid;
}
//narrative reset
for (var p in story) {
if (story.hasOwnProperty(p)) {
// Do things here
story[p].state = 0;
story[p].counter = 0;
}
}
lastMillis = 0;
counter = 0;
gameState = "credits";
//testing
//startGame();
} //end setup
//fullscreened or whatever (only minor thing, menu doesn't get rearranged)
function windowResized() {
if (canvasContainer != null)
resizeCanvas(canvasContainer.offsetWidth, canvasContainer.offsetHeight);
else
resizeCanvas(windowWidth, windowHeight);
buildMenu();
}
//canvas size dependent
function buildMenu() {
if (buttonGroup != null)
buttonGroup.removeSprites();
if (heatGroup != null)
heatGroup.removeSprites();
if (markerGroup != null)
markerGroup.removeSprites();
buttons = [];
buttonGroup = new Group();
heatGroup = new Group();
markerGroup = new Group();
var dy = round(height / (cubeLabels.length + 1));
var dx = round((MENU_W - BTN) / 2);
for (var i = 0; i < cubeLabels.length; i++) {
var l = cubeLabels[i];
var b = createSprite(dx, BTN / 2 + dy * i);
b.label = l;
b.cube = cubes[l];
b.setCollider('rectangle', BTN / 2, BTN / 2, BTN, BTN);
//b.debug = true;
//override function
b.draw = function() {
if (this.targetX != null) {
var dx = this.targetX - this.position.x;
if (dx < 1) {
this.position.x = this.targetX;
} else
this.position.x += dx / 4;
}
imageMode(CORNER);
if (this.cube == currentCube)
image(buttonSelected, 0, 0, BTN, BTN);
else if (this.mouseIsOver)
image(buttonRoll, 0, 0, BTN, BTN);
else
image(buttonSprite, 0, 0, BTN, BTN);
var s = tileGraphics[this.cube.buttonSprite];
image(s, 0, 0, BTN, BTN);
}
//override events
b.onMouseOver = function() {
if (this.visible)
onInterface = true;
};
b.onMouseOut = function() {
if (this.visible)
onInterface = false;
};
b.onMousePressed = function() {
if (this.visible && mouseWentDown(LEFT)) {
if (this.cube == currentCube)
currentCube = null;
else
selectCube(this.cube);
}
};
b.onMouseReleased = function() {};
//glitch and black activate later
if (l == "black") {
b.visible = false;
blackButton = b;
b.position.x = -BTN;
}
if (l == "glitch") {
b.visible = false;
glitchButton = b;
b.position.x = -BTN;
}
buttonGroup.add(b);
buttons.push(b);
} //buttons
//rect(width/2, height/2, BOX_W, BOX_H);
//ok button
okButton = createSprite(width / 2 - graphics.okBtn.width / 2 - 20, height / 2 + BOX_H / 5);
okButton.setCollider('rectangle', 0, 0, graphics.okBtn.width / 2, graphics.okBtn.height / 2);
//override functions
okButton.draw = function() {
if (this.mouseIsOver)
image(graphics.okBtn, 0, 0, graphics.okBtn.width / 2, graphics.okBtn.height / 2);
else
image(graphics.okBtnRoll, 0, 0, graphics.okBtn.width / 2, graphics.okBtn.height / 2);
};
okButton.onMouseReleased = function() {
if (restartDialog)
startGame();
};
//cancel button
cancelButton = createSprite(width / 2 + graphics.okBtn.width / 2 + 20, height / 2 + BOX_H / 5);
cancelButton.setCollider('rectangle', 0, 0, graphics.cancelBtn.width / 2, graphics.cancelBtn.height / 2);
//override functions
cancelButton.draw = function() {
if (this.mouseIsOver)
image(graphics.cancelBtn, 0, 0, graphics.cancelBtn.width / 2, graphics.cancelBtn.height / 2);
else
image(graphics.cancelBtnRoll, 0, 0, graphics.cancelBtnRoll.width / 2, graphics.cancelBtnRoll.height / 2);
};
cancelButton.onMouseReleased = function() {
if (restartDialog) {
restartDialog = false;
cancelButton.visible = false;
okButton.visible = false;
}
};
//restart button
var b = createSprite(width - MENU_W / 2 - RESTART_BTN / 2, 10);
b.setCollider('rectangle', RESTART_BTN / 2, RESTART_BTN / 2, RESTART_BTN, RESTART_BTN);
//b.debug = true;
//override functions
b.draw = function() {
imageMode(CORNER);
if (this.mouseIsOver)
image(graphics.restartRoll, 0, 0, RESTART_BTN, RESTART_BTN);
else
image(graphics.restart, 0, 0, RESTART_BTN, RESTART_BTN);
}
//override events
b.onMouseOver = function() {
if (this.visible) onInterface = true;
};
b.onMouseOut = function() {
if (this.visible) onInterface = false;
};
b.onMousePressed = function() {
if (this.visible)
restartDialog = true;
};
buttonGroup.add(b);
}
function restartGame() {
setupTitle();
sounds.forestLoop.setVolume(0);
sounds.cityLoop.setVolume(0);
sounds.desertLoop.setVolume(0);
}
function startGame() {
frameRate(FPS);
frames = 0;
gameState = "game";
restartDialog = false;
previousZoom = viewZoom = 0.5;
scrollCounter = createVector(0, 0);
turnCounter = 0;
lastMillis = 0;
zoomCounter = 0;
messages = [];
plasticCounter = 0;
plastiglomerates = 0;
nW = 0;
aW = 0;
seaLevelRise = 0;
//wait longer for first disaster
disasterCounter = random(DISASTER_FREQUENCY_MAX, DISASTER_FREQUENCY_MAX * 2);
//it will activate after certain events
lastDisaster = -1;
terrain = [];
cubeIndex = 0;
currentCube = null; //reference to an object
zoomCounter = 0;
resources = 0;
heatWaveCounter = 0;
wildFireCounter = 0;
floodCounter = 0;
floodViz = 0;
currentTile = null;
deltaTime = 0;
camera.position.x = 0;
camera.position.y = 0 + TILE_H * ((ROWS) / 2); //-sqrt(height);
//lil glitch
mouseWentDown(LEFT);
mouseWentDown(LEFT);
//shuffle plastigromerate so they don't follow the same sequence
//except for the first, that's always the same
var sprString = ecology.getRow(PLASTIC).getString("sprite");
var spr = sprString.split(",");
plasticSequence = [];
for (var i = 0; i < spr.length - 1; i++) {
plasticSequence[i] = i + 1;
}
shuffle(plasticSequence, true);
plasticSequence.unshift(0);
noiseSeed(random(0, 100));
//generate terrain
for (var r = 0; r < ROWS; r++) {
var row = [];
for (var c = 0; c < COLS; c++) {
var tile = createTile(r, c);
row.push(tile);
}
terrain.push(row);
}
//cross reference neighbors
for (var r = 0; r < ROWS; r++) {
for (var c = 0; c < COLS; c++) {
var tile = terrain[r][c];
tile.neighbors4 = [];
tile.neighbors8 = [];
var pR = (r > 0) ? r - 1 : null;
var pC = (c > 0) ? c - 1 : null;
var nR = (r < ROWS - 1) ? r + 1 : null;
var nC = (c < COLS - 1) ? c + 1 : null;
//North neighbor
if (pR !== null) {
tile.N = terrain[pR][c];
tile.neighbors4.push(terrain[pR][c]);
tile.neighbors8.push(terrain[pR][c]);
}
//NE neighbor
if (pR !== null && nC !== null) {
tile.NE = terrain[pR][nC];
tile.neighbors8.push(terrain[pR][nC]);
}
//East neighbor
if (nC !== null) {
tile.E = terrain[r][nC];
tile.neighbors4.push(terrain[r][nC]);
tile.neighbors8.push(terrain[r][nC]);
}
//SE neighbor
if (nR !== null && nC !== null) {
tile.SE = terrain[nR][nC];
tile.neighbors8.push(terrain[nR][nC]);
}
//South neighbor
if (nR !== null) {
tile.S = terrain[nR][c];
tile.neighbors4.push(terrain[nR][c]);
tile.neighbors8.push(terrain[nR][c])
}
//SW neighbor
if (nR !== null && pC !== null) {
tile.SW = terrain[nR][pC];
tile.neighbors8.push(terrain[nR][pC]);
}
//West neighbor
if (pC !== null) {
tile.W = terrain[r][pC];
tile.neighbors4.push(terrain[r][pC]);
tile.neighbors8.push(terrain[r][pC])
}
//NW neighbor
if (pR !== null && pC !== null) {
tile.NW = terrain[pR][pC];
tile.neighbors8.push(terrain[pR][pC]);
}
}
}
mousePosition = createVector(0, 0);
currentCube = null;
time = 0;
turns = 0;
interpolator = 0;
////////
buildMenu();
sounds.forestLoop.loop();
sounds.cityLoop.loop();
sounds.desertLoop.loop();
sounds.forestLoop.setVolume(0);
sounds.cityLoop.setVolume(0);
sounds.desertLoop.setVolume(0);
for (var i = 0; i < sounds.tones.length; i++) {
sounds.tones[i].setVolume(0.1);
}
bg = createGraphics(width + NOISE_SHAKE * 2, height + NOISE_SHAKE * 2);
bgDisaster = createGraphics(width, height);
overlay = createGraphics(width, height);
terrain[floor(ROWS / 2) - 1][floor(COLS / 2) - 1].visible = true;
if (glitchUnlocked) {
glitchButton.visible = true;
glitchButton.position.x = round((MENU_W - BTN) / 2);
}
if (blackUnlocked) {
blackButton.visible = true;
blackButton.position.x = round((MENU_W - BTN) / 2);
}
firstPlay = false;
camera.off();
sounds.opening.play();
background(240);
}
function setupTitle() {
camera.position.x = width / 2;
camera.position.y = height / 2;
camera.zoom = 1;
gameState = "title";
noiseCount = 0;
background(0);
frameRate(60);
sounds.theme.play();
}
//do it with a thresholded noise image and the negative mask
function drawTitle() {
var TITLE = 1.5;
var SPEED = 0.06;
noiseCount += SPEED; //millis doesn't work well at the beginning i guess
background(0);
if (noiseCount < TITLE - SPEED) {
var t = map(noiseCount, 0, TITLE, 1, 0);
//t = sin(t * PI * 0.5);
t = t * t * (3 - 2 * t);
//t = 1 - cos(t * PI * 0.5)
//t = t*t;
image(titleNoise, width / 2 - TITLE_W / 2, height / 2 - TITLE_H / 2, TITLE_W, TITLE_H);
filter(THRESHOLD, t);
image(titleMask, width / 2 - TITLE_W / 2, height / 2 - TITLE_H / 2, TITLE_W, TITLE_H);
} else {
fill(255);
rect(width / 2 - TITLE_W / 2 + 1, height / 2 - TITLE_H / 2 + 1, TITLE_W - 2, TITLE_H - 2);
image(titleMask, width / 2 - TITLE_W / 2, height / 2 - TITLE_H / 2, TITLE_W, TITLE_H);
if (noiseCount > sounds.theme.duration() + 1) {
textSize(MSG_SIZE);
textAlign(CENTER, BOTTOM);
textFont(messageFont);
fill(255);
text("Click to Continue", width / 2, height - MSG_MARGIN);
}
if (mouseIsPressed) {
counter = 0;
gameState = "tutorial";
//gameState = "game";
//startGame();
}
}
}
function drawCredits() {
background(0);
var w = graphics.credits.width * 0.5;
var h = graphics.credits.height * 0.5;
image(graphics.credits, width / 2 - w / 2, height / 2 - h / 2, w, h);
counter += deltaTime;