forked from TodePond/CellPond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththe-one-true-todey-file-of-cellpond.js
9613 lines (7990 loc) · 265 KB
/
the-one-true-todey-file-of-cellpond.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
/*
ribbit
░██████╗██╗░░░██╗██████╗░██████╗░███████╗░█████╗░██╗░░░░░██╗░██████╗████████╗
██╔════╝██║░░░██║██╔══██╗██╔══██╗██╔════╝██╔══██╗██║░░░░░██║██╔════╝╚══██╔══╝
╚█████╗░██║░░░██║██████╔╝██████╔╝█████╗░░███████║██║░░░░░██║╚█████╗░░░░██║░░░
░╚═══██╗██║░░░██║██╔══██╗██╔══██╗██╔══╝░░██╔══██║██║░░░░░██║░╚═══██╗░░░██║░░░
██████╔╝╚██████╔╝██║░░██║██║░░██║███████╗██║░░██║███████╗██║██████╔╝░░░██║░░░
╚═════╝░░╚═════╝░╚═╝░░╚═╝╚═╝░░╚═╝╚══════╝╚═╝░░╚═╝╚══════╝╚═╝╚═════╝░░░░╚═╝░░░
░█████╗░██╗░░░██╗████████╗░█████╗░███╗░░░███╗░█████╗░████████╗██╗░██████╗███╗░░░███╗
██╔══██╗██║░░░██║╚══██╔══╝██╔══██╗████╗░████║██╔══██╗╚══██╔══╝██║██╔════╝████╗░████║
███████║██║░░░██║░░░██║░░░██║░░██║██╔████╔██║███████║░░░██║░░░██║╚█████╗░██╔████╔██║
██╔══██║██║░░░██║░░░██║░░░██║░░██║██║╚██╔╝██║██╔══██║░░░██║░░░██║░╚═══██╗██║╚██╔╝██║
██║░░██║╚██████╔╝░░░██║░░░╚█████╔╝██║░╚═╝░██║██║░░██║░░░██║░░░██║██████╔╝██║░╚═╝░██║
╚═╝░░╚═╝░╚═════╝░░░░╚═╝░░░░╚════╝░╚═╝░░░░░╚═╝╚═╝░░╚═╝░░░╚═╝░░░╚═╝╚═════╝░╚═╝░░░░░╚═╝
Welcome traveller!
Welcome to the SOURCE of the CellPond.
If you venture further, may tode be with you.
What you are about to discover...
... is a single javascript file ...
... of gargantuan size ...
... over 8000 lines ...
... globally scoped ...
>>> There is no room for fear here! <<<
Be brave.
Trust no comments.
Trust no names.
A simple seed ... grown into a mountain ...
CellPond is a performance ...
and by reading this you JOIN THE RITUAL ...
=============================================================
... many months later
the source of CellPond calls me back
and it calls you too!
=============================================================
//------//
// FAQs //
//------//
Q:
A: it's a secret
*/
var middleClicked = false
document.addEventListener('mousedown', function(event) {
if (event.button === 1) {
middleClicked = true
}
})
const urlParams = new URLSearchParams(window.location.search)
const NO_SECRET_MODE = urlParams.has("nosecret")
const NO_FOOLS_MODE = urlParams.has("nofools")
const UNLOCK_MODE = urlParams.has("unlock")
const SCALE = urlParams.get("scale") ?? 1
const DPR = urlParams.get("dpr") ?? devicePixelRatio
print('DPR:', DPR)
if (NO_SECRET_MODE) {
localStorage.setItem("secretHasAlreadyBeenRevealed", "true")
}
const secretHasAlreadyBeenRevealed = localStorage.getItem("secretHasAlreadyBeenRevealed")
//========//
// COLOUR //
//========//
const TODEPOND_COLOURS = [
Colour.Green.splash,
Colour.Red.splash,
Colour.Blue.splash,
Colour.Yellow.splash,
Colour.Orange.splash,
Colour.Pink.splash,
Colour.Rose.splash,
Colour.Cyan.splash,
Colour.Purple.splash,
Colour.Black.splash,
Colour.Grey.splash,
Colour.Silver.splash,
Colour.White.splash,
]
const TODEPOND_RAINBOW_COLOURS = TODEPOND_COLOURS.slice(0, -4)
const getRGB = (splash) => {
const gb = splash % 100
let b = gb % 10
let g = gb - b
let r = splash - gb
return [r, g, b]
}
const clamp = (number, min, max) => {
if (number < min) return min
if (number > max) return max
return number
}
const wrap = (number, min, max) => {
const length = (max - min)+1
if (number < min) return wrap(number + length, min, max)
if (number > max) return wrap(number - length, min, max)
return number
}
let brushColourCycleIndex = 0
const brushColourCycle = [
999,
Colour.Green.splash,
Colour.Blue.splash,
Colour.Red.splash,
Colour.Yellow.splash,
Colour.Black.splash,
Colour.Rose.splash,
Colour.Cyan.splash,
Colour.Orange.splash,
Colour.Purple.splash,
Colour.Pink.splash,
Colour.Grey.splash,
Colour.Silver.splash,
]
//======//
// CELL //
//======//
const makeCell = ({x=0, y=0, width=1, height=1, colour=112} = {}) => {
const left = x
const right = x+width
const top = y
const bottom = y+height
const size = width * height
const centerX = left + width/2
const centerY = top + height/2
const sections = []
const lastDraw = undefined
//const lastDrawCount = 1
const lastDrawRepeat = 0
const cell = {x, y, width, height, colour, left, right, top, bottom, centerX, centerY, sections, size, lastDraw, lastDrawRepeat}
return cell
}
let edgeMode = 0
const pickCell = (x, y) => {
/*if (edgeMode === 0) {
if (x >= 1) return undefined
if (y >= 1) return undefined
if (x < 0) return undefined
if (y < 0) return undefined
} else if (edgeMode === 1) {
while (x >= 1) x -= 1
while (y >= 1) y -= 1
while (x < 0) x += 1
while (y < 0) y += 1
}*/
if (x >= 1) return undefined
if (y >= 1) return undefined
if (x < 0) return undefined
if (y < 0) return undefined
const gridX = Math.floor(x * GRID_SIZE)
const gridY = Math.floor(y * GRID_SIZE)
const sectionId = gridX*GRID_SIZE + gridY
const section = state.grid[sectionId]
if (section === undefined) return undefined
let i = 1
const size = section.size
const values = section.values()
for (const cell of values) {
if (i === size) return cell
i++
if (cell.left > x) continue
if (cell.top > y) continue
if (cell.right <= x) continue
if (cell.bottom <= y) continue
return cell
}
return undefined
}
const pickNeighbour = (cell, dx, dy) => {
const centerX = cell.left + cell.width/2
const centerY = cell.top + cell.height/2
const x = centerX + dx*cell.width
const y = centerY + dy*cell.height
const neighbour = pickCell(x, y)
return neighbour
}
const pickRandomCell = () => {
const x = Random.Uint32 / 4294967295
const y = Random.Uint32 / 4294967295
const cell = pickCell(x, y)
return cell
}
const pickRandomVisibleCell = () => {
if (!state.view.visible) return undefined
if (state.view.fullyVisible) return pickRandomCell()
const x = state.region.left + (Random.Uint32 / 4294967295) * state.region.width
const y = state.region.top + (Random.Uint32 / 4294967295) * state.region.height
const cell = pickCell(x, y)
return cell
}
//=======//
// STATE //
//=======//
const state = {
grid: [],
cellCount: 0,
ticker: () => {},
time: 0,
maxTime: 9999999,
/*speed: {
count: 100,
dynamic: false,
aer: 2.0,
redraw: 300.0,
redrawRepeatScore: 1.0,
redrawRepeatPenalty: 0.0,
},*/
speed: {
count: 4096 * 0.4,
dynamic: false,
//aer: 1.0,
redraw: 2.5,
redrawRepeatScore: 0.5,
redrawRepeatPenalty: 0.0,
},
image: {
data: undefined,
size: undefined,
baseSize: undefined,
},
view: {
height: undefined,
width: undefined,
iheight: undefined,
iwidth: undefined,
left: undefined,
right: undefined,
top: undefined,
bottom: undefined,
visible: true,
fullyVisible: true,
},
region: {
left: 0.0,
right: 1.0,
top: 0.0,
bottom: 1.0,
width: 1.0,
height: 1.0,
},
camera: {
x: 0,
y: 0,
dx: 0,
dy: 0,
dxTarget: 0,
dyTarget: 0,
dsControl: 1,
dsTargetSpeed: 0.05,
underScale: 0.9,
scale: 0.9,
mscale: 1.0,
dmscale: 0.002,
mscaleTarget: 1.0,
mscaleTargetControl: 0.001,
mscaleTargetSpeed: 0.05,
},
brush: {
colour: Colour.Purple.splash,
colour: Colour.Rose.splash,
colour: Colour.Yellow.splash,
colour: Colour.Grey.splash,
colour: Colour.Green.splash,
colour: 999,
size: 3,
},
cursor: {
previous: {
x: undefined,
y: undefined,
},
},
dragon: {
behaves: [],
}
}
let WORLD_SIZE = undefined
let WORLD_CELL_COUNT = undefined
let WORLD_DIMENSION = undefined
let WORLD_CELL_SIZE = undefined
const setWorldSize = (size) => {
WORLD_SIZE = size
WORLD_CELL_COUNT = 2 ** (WORLD_SIZE*2)
WORLD_DIMENSION = 2 ** WORLD_SIZE
WORLD_CELL_SIZE = 1 / WORLD_DIMENSION
}
setWorldSize(6)
const addCell = (cell) => {
cacheCell(cell)
state.cellCount++
}
const deleteCell = (cell) => {
uncacheCell(cell)
cell.isDeleted = true
state.cellCount--
}
const getCells = () => {
const cells = new Set()
for (const section of state.grid) {
for (const cell of section.values()) {
cells.add(cell)
}
}
return cells
}
//======//
// GRID //
//======//
// The grid is basically the screen cut up into smaller sections
// It helps to speed up cell lookup because it gives us a smaller area to search through
// Note: Cells can be in multiple sections if they are big enough :)
// NOTE: GRID_SIZE MUST BE BIG ENOUGH SO THAT SECTIONS ARE SMALLER OR EQUAL TO WORLD CELLS
const GRID_SIZE = 128
for (let x = 0; x < GRID_SIZE; x++) {
for (let y = 0; y < GRID_SIZE; y++) {
const section = new Set()
state.grid.push(section)
section.left = x / GRID_SIZE
section.top = y / GRID_SIZE
section.right = section.left + 1/GRID_SIZE
section.bottom = section.top + 1/GRID_SIZE
section.isSection = true
}
}
const cacheCell = (cell) => {
const left = Math.floor(cell.left * GRID_SIZE)
const top = Math.floor(cell.top * GRID_SIZE)
const right = Math.ceil(cell.right * GRID_SIZE)
const bottom = Math.ceil(cell.bottom * GRID_SIZE)
for (let x = left; x < right; x++) {
for (let y = top; y < bottom; y++) {
const id = x*GRID_SIZE + y
if (state.grid[id] === undefined) {
continue
}
state.grid[id].add(cell)
cell.sections.push(state.grid[id])
}
}
}
const uncacheCell = (cell) => {
for (const section of cell.sections) {
section.delete(cell)
}
}
//=======//
// SETUP //
//=======//
// Setup World
const world = makeCell({colour: WORLD_SIZE * 111})
addCell(world)
on.load(() => {
// Setup Show
const show = Show.start({paused: false, scale: DPR})
const {context, canvas} = show
canvas.style["position"] = "absolute"
//===============//
// IMAGE + SIZES //
//===============//
const updateImageSize = () => {
state.image.baseSize = Math.min(canvas.width, canvas.height)
state.image.size = state.image.baseSize * state.camera.scale
state.image.left = state.camera.x * state.camera.scale
state.image.top = state.camera.y * state.camera.scale
state.image.right = state.image.left + state.image.size
state.image.bottom = state.image.top + state.image.size
state.view.left = clamp(state.image.left, 0, canvas.width)
state.view.top = clamp(state.image.top, 0, canvas.height)
state.view.right = clamp(state.image.right, 0, canvas.width)
state.view.bottom = clamp(state.image.bottom, 0, canvas.height)
state.view.width = state.view.right - state.view.left
state.view.height = state.view.bottom - state.view.top
state.view.visible = state.view.width > 0 && state.view.height > 0
state.view.fullyVisible = state.view.left === state.image.left && state.view.right === state.image.right && state.view.top === state.image.top && state.view.bottom === state.image.bottom
state.view.iwidth = Math.ceil(state.view.width)
state.view.iheight = Math.ceil(state.view.height)
state.region.left = (state.view.left - state.image.left) / state.image.size
state.region.right = 1.0 + (state.view.right - state.image.right) / state.image.size
state.region.top = (state.view.top - state.image.top) / state.image.size
state.region.bottom = 1.0 + (state.view.bottom - state.image.bottom) / state.image.size
state.region.width = state.region.right - state.region.left
state.region.height = state.region.bottom - state.region.top
//state.image.data = context.getImageData(0, 0, state.image.size.iwidth, state.image.size.iheight)
drawQueueNeedsReset = true
}
const updateImageData = () => {
state.image.data = context.getImageData(0, 0, canvas.width, canvas.height)
}
// Setup ImageData
context.fillStyle = Colour.Void
context.fillRect(0, 0, canvas.width, canvas.height)
updateImageSize()
updateImageData()
state.camera.x += (canvas.width - state.image.size) / 2
state.camera.y += (canvas.height - state.image.size) / 2
//======//
// DRAW //
//======//
show.resize = () => {
context.fillStyle = Colour.Void
context.fillRect(0, 0, canvas.width, canvas.height)
updateImageSize()
updateImageData()
}
const stampScale = (scale) => {
//context.fillStyle = Colour.Void
//context.fillRect(0, 0, canvas.width, canvas.height)
//context.drawImage(canvas, 0, 0, canvas.width * scale, canvas.height * scale)
/*if (scale < 1.0) {
const growthX = canvas.width - canvas.width * scale
const growthY = canvas.height - canvas.height * scale
//context.fillRect(canvas.width - growthX, 0, growthX, canvas.height)
//context.fillRect(0, canvas.height - growthY, canvas.width, growthY)
}*/
updateImageSize()
}
const drawCells = () => {
const cells = getCells()
for (const cell of cells.values()) {
setCellColour(cell, cell.colour)
}
}
const drawCell = (cell, override) => {
return setCellColour(cell, cell.colour, override)
}
const isSectionVisible = (section) => {
if (section.right <= state.region.left) return false
if (section.left >= state.region.right) return false
if (section.bottom <= state.region.top) return false
if (section.top >= state.region.bottom) return false
return true
}
const isCellVisible = (cell) => {
if (cell.right <= state.region.left) return false
if (cell.left >= state.region.right) return false
if (cell.bottom <= state.region.top) return false
if (cell.top >= state.region.bottom) return false
return true
}
const queueCellDraw = (cell, colour) => {
cell.colour = colour
if (!isCellVisible(cell)) return 0
drawQueuePriority.add(cell)
drawQueue.delete(cell)
return 0.01
}
const setCellColour = (cell, colour, override = false) => {
if (cell.isDeleted) return 0
cell.colour = colour
if (!isCellVisible(cell)) return 0
/*
if (!override && cell.lastDraw === state.time) {
cell.lastDrawRepeat += state.speed.redrawRepeatPenalty
return state.speed.redrawRepeatScore * cell.lastDrawRepeat
}
*/
const size = state.image.size
const imageWidth = canvas.width
const panX = state.camera.x * state.camera.scale
const panY = state.camera.y * state.camera.scale
// Position
let left = Math.round(size * cell.left + panX)
if (left > canvas.width) return 0
if (left < 0) left = 0
let top = Math.round(size * cell.top + panY)
if (top > canvas.height) return 0
if (top < 0) top = 0
let right = Math.round(size * cell.right + panX)
if (right < 0) return 0
if (right > canvas.width) right = canvas.width
let bottom = Math.round(size * cell.bottom + panY)
if (bottom < 0) return 0
if (bottom > canvas.height) bottom = canvas.height
// Colour
const splash = Colour.splash(cell.colour)
let red = splash[0]
let green = splash[1]
let blue = splash[2]
/*if (!NO_FOOLS_MODE) {
const average = Math.round((red + green + blue) / 3)
red = average
green = average
blue = average
}*/
// Draw
const iy = imageWidth * 4
const width = right-left
const ix = 4
const sx = width * ix
//let pixelCount = 0
let id = (top*imageWidth + left) * 4
const data = state.image.data.data
let borderRed = Colour.Void.red
let borderGreen = Colour.Void.green
let borderBlue = Colour.Void.blue
if (!gridMode || width <= 3 || bottom-top <= 3) {
/*
borderRed = Colour.Void.red
borderGreen = Colour.Void.green
borderBlue = Colour.Void.blue
*/
borderRed = red
borderGreen = green
borderBlue = blue
for (let y = top; y < bottom; y++) {
for (let x = left; x < right; x++) {
data[id] = red
data[id+1] = green
data[id+2] = blue
id += 4
//pixelCount++
}
id += iy
id -= sx
}
return 1
}
const left1 = left + 1
const right_1 = right - 1
const top1 = top + 1
const bottom_1 = bottom - 1
// DRAW TOP ROW
data[id] = borderRed
data[id+1] = borderGreen
data[id+2] = borderBlue
id += 4
for (let x = left1; x < right_1; x++) {
data[id] = borderRed
data[id+1] = borderGreen
data[id+2] = borderBlue
id += 4
//pixelCount++
}
data[id] = borderRed
data[id+1] = borderGreen
data[id+2] = borderBlue
id += 4
id -= sx
id += iy
// DRAW MIDDLE ROWS
for (let y = top1; y < bottom_1; y++) {
data[id] = borderRed
data[id+1] = borderGreen
data[id+2] = borderBlue
id += 4
for (let x = left1; x < right_1; x++) {
data[id] = red
data[id+1] = green
data[id+2] = blue
id += 4
//pixelCount++
}
data[id] = borderRed
data[id+1] = borderGreen
data[id+2] = borderBlue
id += 4
id -= sx
id += iy
}
// DRAW BOTTOM ROW
data[id] = borderRed
data[id+1] = borderGreen
data[id+2] = borderBlue
id += 4
for (let x = left1; x < right_1; x++) {
data[id] = borderRed
data[id+1] = borderGreen
data[id+2] = borderBlue
id += 4
//pixelCount++
}
data[id] = borderRed
data[id+1] = borderGreen
data[id+2] = borderBlue
/*
cell.lastDraw = state.time
//cell.lastDrawCount = pixelCount
cell.lastDrawRepeat = 1
*/
return 1
}
//========//
// CURSOR //
//========//
const updateCursor = () => {
updateBrush()
updatePan()
const [x, y] = Mouse.position
state.cursor.previous.x = x
state.cursor.previous.y = y
}
let pencilled = false
const updateBrush = () => {
if (!state.worldBuilt) return
if (!Mouse.Middle) {
pencilled = false
}
if (state.colourTode.hand.state !== HAND.BRUSHING && state.colourTode.hand.state !== HAND.PENCILLING) return
if (Mouse.Middle && !pencilled) {
const [x, y] = Mouse.position
brush(...getCursorView(x, y), {single: true})
pencilled = true
}
if (!Mouse.Left) return
let [x, y] = getCursorView(...Mouse.position)
if (x === undefined || y === undefined) {
return
}
let [px, py] = getCursorView(state.cursor.previous.x, state.cursor.previous.y)
const size = state.brush.size * WORLD_CELL_SIZE
const dx = x - px
const dy = y - py
const sx = Math.sign(dx)
const sy = Math.sign(dy)
const ax = Math.abs(dx)
const ay = Math.abs(dy)
const biggest = Math.max(ax, ay)
let ix = 0
let iy = 0
if (ax === biggest) {
iy = (WORLD_CELL_SIZE * sy) * (ay / ax)
ix = WORLD_CELL_SIZE * sx
} else {
ix = (WORLD_CELL_SIZE * sx) * (ax / ay)
iy = WORLD_CELL_SIZE * sy
}
const points = new Set()
const length = biggest / WORLD_CELL_SIZE
if (dx === 0 && dy === 0) {
for (let dx = -size/2; dx <= size/2; dx += WORLD_CELL_SIZE) {
for (let dy = -size/2; dy <= size/2; dy += WORLD_CELL_SIZE) {
points.add([x + dx, y + dy])
}
}
}
else for (let i = 0; i <= length; i++) {
const X = px + ix * i
const Y = py + iy * i
for (let dx = -size/2; dx <= size/2; dx += WORLD_CELL_SIZE) {
for (let dy = -size/2; dy <= size/2; dy += WORLD_CELL_SIZE) {
points.add([X + dx, Y + dy])
}
}
}
for (const point of points.values()) {
brush(point[0], point[1])
}
}
const getCursorView = (x, y) => {
x -= state.camera.x * state.camera.scale / DPR
y -= state.camera.y * state.camera.scale / DPR
x /= state.image.size
y /= state.image.size
x *= DPR
y *= DPR
return [x, y]
}
const brush = (x, y, {single = false} = {}) => {
let cell = pickCell(x, y)
if (cell === undefined) return
if (!single && (cell.width !== WORLD_CELL_SIZE || cell.height != WORLD_CELL_SIZE)) {
const worldCells = getWorldCellsSet(x, y)
if (worldCells !== undefined) {
const merged = mergeCells([...worldCells])
cell = merged
}
}
if (typeof state.brush.colour === "number") {
cell.colour = state.brush.colour
drawCell(cell)
return
}
let children = []
if (state.brush.colour.left[0].content.isDiagram) {
children = splitCellToDiagram(cell, state.brush.colour.left[0].content)
} else {
children = splitCellToDiagram(cell, state.brush.colour)
}
for (const child of children) {
drawCell(child)
}
}
const getWorldCellsSet = (x, y) => {
const sections = getSectionsOfWorldCell(x, y)
const worldCells = getWorldCellsFromSections(sections, x, y)
return worldCells
}
const getSectionsOfWorldCell = (x, y) => {
const snappedX = Math.floor(x*WORLD_DIMENSION) / WORLD_DIMENSION
const snappedY = Math.floor(y*WORLD_DIMENSION) / WORLD_DIMENSION
const sectionSizeScale = GRID_SIZE / WORLD_DIMENSION
const sections = new Set()
for (let wx = 0; wx < sectionSizeScale; wx++) {
const gridX = Math.floor((snappedX + wx * WORLD_CELL_SIZE / sectionSizeScale) * GRID_SIZE)
for (let wy = 0; wy < sectionSizeScale; wy++) {
const gridY = Math.floor((snappedY + wy * WORLD_CELL_SIZE / sectionSizeScale) * GRID_SIZE)
const sectionId = gridX*GRID_SIZE + gridY
const section = state.grid[sectionId]
sections.add(section)
}
}
return sections
}
const getWorldCellsFromSections = (sections, x, y) => {
/*const wleft = x
const wtop = y
const wright = x + 1/256
const wbottom = y + 1/256*/
const worldCells = new Set()
// Check if any cells in these sections overlap with an outer section
for (const section of sections.values()) {
/*if (section.left >= wleft && section.right <= wright && section.top >= wtop && section.bottom <= wbottom) {
worldCells.add(section)
continue
}*/
for (const cell of section.values()) {
if (worldCells.has(cell)) continue
for (const cellSection of cell.sections) {
if (!sections.has(cellSection)) return undefined
}
worldCells.add(cell)
}
}
return worldCells
}
let dropperStartX = undefined
let dropperStartY = undefined
let dropperStartT = undefined
state.brush.hoverColour = Colour.Void
const updatePan = () => {
const [x, y] = Mouse.position
if (hand.state === HAND.BRUSH || hand.state === HAND.BRUSHING || hand.state === HAND.PENCILLING) {
const cell = pickCell(...getCursorView(x, y))
if (cell !== undefined) state.brush.hoverColour = cell.colour
} else {
const atom = getAtom(x / CT_SCALE, y / CT_SCALE)
if (atom !== undefined) {
if (atom.isSquare || atom === squareTool) {
state.brush.hoverColour = atom.value
if (atom.joinExpanded) {
const clon = cloneDragonArray(atom.value)
clon.joins = []
state.brush.hoverColour = clon
}
} else if (atom.isTallRectangle) {
// TODO: what colour should rectangles set the brush?
} else if (atom.isPaddle) {
state.brush.hoverColour = atom.getColour(atom)
} else if (atom.isSlot) {
state.brush.hoverColour = atom.parent.getColour(atom.parent)
} else {
state.brush.hoverColour = atom.colour.splash
}
} else {
state.brush.hoverColour = Colour.Void
}
}
if (!Mouse.Right) {
if (dropperStartX !== undefined) {
const dropperDistance = Math.hypot(x - dropperStartX, y - dropperStartY)
const dropperTime = Date.now() - dropperStartT
if (dropperTime < 100 || dropperDistance <= 0) {
if (state.brush.hoverColour === Colour.Void) {
brushColourCycleIndex++
if (brushColourCycleIndex >= brushColourCycle.length) {
brushColourCycleIndex = 0
}
setBrushColour(brushColourCycle[brushColourCycleIndex])
}
else {
setBrushColour(state.brush.hoverColour)
}
squareTool.toolbarNeedsColourUpdate = true
}
drawQueueNeedsReset = true
}
dropperStartX = undefined
dropperStartY = undefined
return
}
drawQueueNeedsReset = true
if (dropperStartX === undefined) {
dropperStartX = x
dropperStartY = y
dropperStartT = Date.now()
dropperMovement = 0