-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle_forge.lua
2443 lines (2132 loc) · 85 KB
/
particle_forge.lua
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
-- title: Particle Forge
-- author: ArchaicVirus
-- desc: An interactive particle creator and editor
-- site: github.com/archaicvirus
-- license: prop
-- version: 0.1
-- script: lua
floor, ceil, rnd, abs, rad, deg, cos, sin, min, max = math.floor, math.ceil, math.random, math.abs, math.rad, math.deg, math.cos, math.sin, math.min, math.max
require("classes/vec2D")
require("classes/gui")
CURRENT_EMITTER_PREVIEW = 13
grid = vec2(14 * 8, 9)
GRID_WIDTH = 240 - grid.x
GRID_HEIGHT = 136 - grid.y
require("saves/particle_defs")
require("classes/particles")
Ship = {fg = {}, bg = {}}
pos = vec2(-8, -8)
LAST_FRAME_TIME = time()
selection = {}
SPRITE_PAGE = 0
SHOW_SPRITES = false
SHOW_GRID = true
SHOW_MAP = false
SHOW_DEBUG = false
SHOW_AXIS = true
SHOW_AXIS_ON_HOVER = false
SHOW_HOVER_TEXT = true
SHOW_PARTICLE_BOUNDS = false
AUTO_SCROLL_PARTICLE_SETTINGS = false
AUTO_SCROLL_SHOW_HOVER_BARS = false
UNUSED_BIT = false
LAYER = true
EMITTER_AUTO_SCROLL = true
EMITTER_SCROLL_POS = 0
AUTO_SCROLL_SPEED = 1
AUTO_SCROLL_WIDGET_HEIGHT = 9
PARTICLE_SETTING_SCROLL_POS = 0
rotation = 0
selected_sprite = {pos = vec2(0, 9), id = 0, w = 1, h = 1}
TICK = 0
CENTER_X = 120
CENTER_Y = 68
CENTER = vec2(CENTER_X, CCENTER_Y)
TILE_SIZE = 8
GRID_SIZE = 9
SPEED = 2
STATE = 'start'
EDIT_STATE = 'emitter'
LAST_STATE = tostring(STATE)
MOUSE = 502
--BUTTON ID DEFS---------------
BUTTON_CLOSE = 480
BUTTON_CLOSE_SMALL = 480
BUTTON_CLONE_SMALL = 481
BUTTON_ARROW_DOWN = 501
BUTTON_ARROW_DOWN_SMALL = 497
BUTTON_ARROW_UP = 500
BUTTON_ARROW_UP_SMALL = 496
BUTTON_ARROW_RIGHT = 498
BUTTON_ARROW_RIGHT_SMALL = 497
BUTTON_MENU = 505
BUTTON_MENU_SMALL = 504
BUTTON_EXPORT = 501
BUTTON_TEXT = 509
BUTTON_ADD = 508
BUTTON_MUL = 480
BUTTON_GRID = 503
BUTTON_AXIS = 499
BUTTON_DEBUG = 506
BUTTON_DEBUG_2 = 482
BUTTON_EDIT = 504
BUTTON_VISIBILITY = 482
BUTTON_INVISIBILITY = 483
------------------------------
LAST_HOVERED_MENU_ITEM = 1
TOTAL_PARTICLES_FG = 0
TOTAL_PARTICLES_BG = 0
TOTAL_ADDED_PARTICLES = 0
PARTICLE_NAME_MAX_WIDTH = 90
EMITTER_NAME_MAX_WIDTH = 175
CURRENT_EMITTER_SETTING = 1
CURRENT_PARTICLE = 1
CURRENT_PARTICLE_SETTING_PAGE = 1
CURRENT_PARTICLE_VALUE_EDIT = {}
PARTICLE_WIDGETS_PER_PAGE = 13
EMITTER_WIDGETS_PER_PAGE = 10
PARTICLE_SETTTING_WIDGET_HEIGHT = 9
EMITTER_POSITION = vec2(grid.x + (GRID_WIDTH/2), grid.y + (GRID_HEIGHT/2))
HOVER_TEXT = false
HOVER_TEXT_WRAP = 100
HOVER_TEXT_COLOR = 2
FRAME_ACCUMULATOR = 0
FPS_ACCUMULATOR = {}
LAST_FPS = 60
CURRENT_COLOR = 7
CURRENT_COLOR_2 = 13
GRID = 507
HIGHLIGHT = 503
--DEFAULT_NEW_PARTICLE = {sprite={id=0,w=1,h=1},vis=true,name='Default',1,1000,20,1,1,15,15,0,0,0,0,1,3.0,0,1,0,0.0,0,0,-15.0,0,0,1,0,1.0,0.05,0.0}
bb = {a = vec2(0, 0), b = vec2(0, 0), w = 0, h = 0}
bb_set = true
sspr = spr
chars = {
["A"] = {char = "A", large = 6, small = 4},
["B"] = {char = "B", large = 6, small = 4},
["C"] = {char = "C", large = 6, small = 4},
["D"] = {char = "D", large = 6, small = 4},
["E"] = {char = "E", large = 6, small = 4},
["F"] = {char = "F", large = 6, small = 4},
["G"] = {char = "G", large = 6, small = 4},
["H"] = {char = "H", large = 6, small = 4},
["I"] = {char = "I", large = 5, small = 4},
["J"] = {char = "J", large = 6, small = 4},
["K"] = {char = "K", large = 6, small = 4},
["L"] = {char = "L", large = 6, small = 4},
["M"] = {char = "M", large = 6, small = 4},
["N"] = {char = "N", large = 6, small = 4},
["O"] = {char = "O", large = 6, small = 4},
["P"] = {char = "P", large = 6, small = 4},
["Q"] = {char = "Q", large = 6, small = 4},
["R"] = {char = "R", large = 6, small = 4},
["S"] = {char = "S", large = 6, small = 4},
["T"] = {char = "T", large = 5, small = 4},
["U"] = {char = "U", large = 6, small = 4},
["V"] = {char = "V", large = 6, small = 4},
["W"] = {char = "W", large = 6, small = 4},
["X"] = {char = "X", large = 6, small = 4},
["Y"] = {char = "Y", large = 5, small = 4},
["Z"] = {char = "Z", large = 6, small = 4},
["a"] = {char = "a", large = 6, small = 4},
["b"] = {char = "b", large = 6, small = 4},
["c"] = {char = "c", large = 6, small = 4},
["d"] = {char = "d", large = 6, small = 4},
["e"] = {char = "e", large = 6, small = 4},
["f"] = {char = "f", large = 6, small = 4},
["g"] = {char = "g", large = 6, small = 4},
["h"] = {char = "h", large = 6, small = 4},
["i"] = {char = "i", large = 3, small = 2},
["j"] = {char = "j", large = 6, small = 4},
["k"] = {char = "k", large = 6, small = 4},
["l"] = {char = "l", large = 5, small = 4},
["m"] = {char = "m", large = 6, small = 4},
["n"] = {char = "n", large = 6, small = 4},
["o"] = {char = "o", large = 6, small = 4},
["p"] = {char = "p", large = 6, small = 4},
["q"] = {char = "q", large = 6, small = 4},
["r"] = {char = "r", large = 6, small = 4},
["s"] = {char = "s", large = 6, small = 4},
["t"] = {char = "t", large = 6, small = 4},
["u"] = {char = "u", large = 6, small = 4},
["v"] = {char = "v", large = 6, small = 4},
["w"] = {char = "w", large = 6, small = 4},
["x"] = {char = "x", large = 6, small = 4},
["y"] = {char = "y", large = 6, small = 4},
["z"] = {char = "z", large = 6, small = 4},
["0"] = {char = "0", large = 6, small = 4},
["1"] = {char = "1", large = 5, small = 4},
["2"] = {char = "2", large = 6, small = 4},
["3"] = {char = "3", large = 6, small = 4},
["4"] = {char = "4", large = 6, small = 4},
["5"] = {char = "5", large = 6, small = 4},
["6"] = {char = "6", large = 6, small = 4},
["7"] = {char = "7", large = 6, small = 4},
["8"] = {char = "8", large = 6, small = 4},
["9"] = {char = "9", large = 6, small = 4},
["!"] = {char = "!", large = 3, small = 2},
["@"] = {char = "@", large = 6, small = 4},
["#"] = {char = "#", large = 6, small = 4},
["$"] = {char = "$", large = 6, small = 4},
["%"] = {char = "%", large = 6, small = 4},
["^"] = {char = "^", large = 6, small = 4},
["&"] = {char = "&", large = 6, small = 4},
["*"] = {char = "*", large = 6, small = 4},
["("] = {char = "(", large = 3, small = 3},
[")"] = {char = ")", large = 3, small = 3},
["-"] = {char = "-", large = 4, small = 4},
["_"] = {char = "_", large = 5, small = 4},
["="] = {char = "=", large = 4, small = 4},
["+"] = {char = "+", large = 4, small = 4},
["{"] = {char = "{", large = 4, small = 4},
["}"] = {char = "}", large = 4, small = 4},
["["] = {char = "[", large = 3, small = 3},
["]"] = {char = "]", large = 3, small = 3},
[";"] = {char = ";", large = 3, small = 3},
[":"] = {char = ":", large = 3, small = 2},
["'"] = {char = "'", large = 3, small = 2},
[","] = {char = ",", large = 3, small = 3},
["<"] = {char = "<", large = 4, small = 4},
["."] = {char = ".", large = 3, small = 2},
[">"] = {char = ">", large = 4, small = 4},
["/"] = {char = "/", large = 6, small = 4},
["?"] = {char = "?", large = 5, small = 4},
["\""]= {char = "\"",large = 4, small = 4},
['`'] = {char = '`', large = 3, small = 3},
['~'] = {char = '~', large = 5, small = 4},
[' '] = {char = ' ', large = 4, small = 2},
}
KEYS = {
[ 1] = {'A', 'a'},
[ 2] = {'B', 'b'},
[ 3] = {'C', 'c'},
[ 4] = {'D', 'd'},
[ 5] = {'E', 'e'},
[ 6] = {'F', 'f'},
[ 7] = {'G', 'g'},
[ 8] = {'H', 'h'},
[ 9] = {'I', 'i'},
[10] = {'J', 'j'},
[11] = {'K', 'k'},
[12] = {'L', 'l'},
[13] = {'M', 'm'},
[14] = {'N', 'n'},
[15] = {'O', 'o'},
[16] = {'P', 'p'},
[17] = {'Q', 'q'},
[18] = {'R', 'r'},
[19] = {'S', 's'},
[20] = {'T', 't'},
[21] = {'U', 'u'},
[22] = {'V', 'v'},
[23] = {'W', 'w'},
[24] = {'X', 'x'},
[25] = {'Y', 'y'},
[26] = {'Z', 'z'},
[27] = {')', '0'},
[28] = {'@', '1'},
[29] = {'#', '2'},
[30] = {'$', '3'},
[31] = {'%', '4'},
[32] = {'^', '5'},
[33] = {'&', '6'},
[34] = {'*', '7'},
[35] = {'(', '8'},
[36] = {'(', '9'},
[37] = {'_', '-'},
[38] = {'+', '='},
[39] = {'{', '['},
[40] = {'}', ']'},
[41] = {'|', '\\'},
[42] = {':', ';'},
[43] = {'"', '\''},
[44] = {'~', '`'},
[45] = {'<', ','},
[46] = {'>', '.'},
[47] = {'?', '/'},
--[48] = {' '}, --SPACE
--[49] = {' '}, --TAB
--[50] = {'\n'}, --ENTER KEY
[79] = {'0'},
[80] = {'1'},
[81] = {'2'},
[82] = {'3'},
[83] = {'4'},
[84] = {'5'},
[85] = {'6'},
[86] = {'7'},
[87] = {'8'},
[88] = {'9'},
[89] = {'+'},
[90] = {'-'},
[91] = {'*'},
[92] = {'/'},
[93] = {''}, -- NUMPAD ENTER
[94] = {'.'},
}
palettes = {
bg = '1d1d20ba5d1d856d2844690c203810185050346d912424441414147530619d557d59300c611c10242c2c5d5d5d999595',
fg = '1d1d20f9801dfed83d80c71f5e7c16169c9c4c7dca20387d141414c74ebdf38baa8d4820b02e26474f528d8d89d6d6d2',
}
function loadPalette(palette, bank)
for i=0,15 do
local r=tonumber(string.sub(palette,i*6+1,i*6+2),16)
local g=tonumber(string.sub(palette,i*6+3,i*6+4),16)
local b=tonumber(string.sub(palette,i*6+5,i*6+6),16)
poke(0x3FC0+(i*3)+0,r)
poke(0x3FC0+(i*3)+1,g)
poke(0x3FC0+(i*3)+2,b)
end
end
function load_settings()
local settings = pmem(0)
SHOW_GRID = extract_bit(settings, 0)
SHOW_AXIS = extract_bit(settings, 1)
SHOW_AXIS_ON_HOVER = extract_bit(settings, 2)
SHOW_HOVER_TEXT = extract_bit(settings, 3)
AUTO_SCROLL_PARTICLE_SETTINGS = extract_bit(settings, 4)
AUTO_SCROLL_SHOW_HOVER_BARS = extract_bit(settings, 5)
EMITTER_AUTO_SCROLL = extract_bit(settings, 6)
UNUSED_BIT = extract_bit(settings, 7)
trace("Loaded settings!")
end
function save_settings()
local settings = 0
settings = set_bit(settings, 0, SHOW_GRID)
settings = set_bit(settings, 1, SHOW_AXIS)
settings = set_bit(settings, 2, SHOW_AXIS_ON_HOVER)
settings = set_bit(settings, 3, SHOW_HOVER_TEXT)
settings = set_bit(settings, 4, AUTO_SCROLL_PARTICLE_SETTINGS)
settings = set_bit(settings, 5, AUTO_SCROLL_SHOW_HOVER_BARS)
settings = set_bit(settings, 6, EMITTER_AUTO_SCROLL)
settings = set_bit(settings, 7, UNUSED_BIT)
pmem(0, settings)
trace("Saved settings!")
end
function extract_bit(num, bit)
return (num & (1 << bit)) ~= 0
end
function set_bit(num, bit, value)
if value then
return num | (1 << bit)
else
return num & ~(1 << bit)
end
end
function BOOT()
--load_settings()
end
vbank(0)
loadPalette(palettes.bg)
cls()
vbank(1)
loadPalette(palettes.fg)
load_settings()
function deep_copy(obj, seen)
if type(obj) ~= 'table' then return obj end
if seen and seen[obj] then return seen[obj] end
local s = seen or {}
local res = setmetatable({}, getmetatable(obj))
s[obj] = res
for k, v in pairs(obj) do res[deep_copy(k, s)] = deep_copy(v, s) end
return res
end
TEMP_EMITTER = {
name = 'TEMP EMITTER',
position = vec2(120, 68),
bounds = {x = 1, y = 8, w = 238, h = 136 - 18},
particle_systems = {
[1] = {
name = "PARTICLE_PREVIEW_SIMULATION",
position = EMITTER_POSITION,
--type = 'Shape',
kill = false,
visibility = true,
settings = unpack_particle_settings(DEFAULT_NEW_PARTICLE),
particles = {
fg = {},
bg = {},
},
},
},
}
EMITTER = {
name = 'DefaultEmitter_01',
position = EMITTER_POSITION,
particle_systems = {},
bounds = {x = grid.x + 1, y = grid.y + 1, w = GRID_WIDTH - 2, h = GRID_HEIGHT - 3},
}
EMITTER_DEFAULT = {
name = 'DefaultEmitter_01',
position = vec2(grid.x + (GRID_WIDTH/2), grid.y + (GRID_HEIGHT/2)),
bounds = {x = grid.x + 1, y = grid.y + 1, w = GRID_WIDTH - 2, h = GRID_HEIGHT - 1},
particle_systems = {
[1] = {
name = "Default",
position = CENTER,
kill = false,
visibility = true,
settings = unpack_particle_settings(DEFAULT_NEW_PARTICLE),
particles = {
fg = {},
bg = {},
},
},
}
}
TEXT_BUFFER = ''
PARTICLE_SYSTEM = {
name = 'DefaultParticle_01',
position = EMITTER_POSITION,
type = 'Shape',
particles = {
fg = {},
bg = {},
},
}
cursor = {
x = 8,
y = 8,
id = 352,
lx = 8,
ly = 8,
tx = 8,
ty = 8,
wx = 0,
wy = 0,
sx = 0,
sy = 0,
lsx = 0,
lsy = 0,
l = false,
ll = false,
released_left = false,
m = false,
lm = false,
r = false,
lr = false,
released_right = false,
prog = false,
rot = 0,
last_rotation = 0,
hold_time = 0,
type = 'pointer',
item = 'transport_belt',
drag = false,
panel_drag = false,
drag_dir = 0,
drag_loc = {x = 0, y = 0},
hand_item = {id = 0, count = 0},
drag_offset = {x = 0, y = 0},
item_stack = {id = 9, count = 100},
drag_loc2 = {x = 0, y = 0},
drag_offset2 = {x = 0, y = 0},
cooldown = 0
}
local _rect = rect
rect = function(x, y, w, h, color)
if type(x) == 'table' then
_rect(x.x, x.y, x.w, x.h, y)
else
_rect(x, y, w, h, color)
end
end
local _rectb = rectb
rectb = function(x, y, w, h, color)
if type(x) == 'table' then
_rectb(x.x, x.y, x.w, x.h, y)
else
_rectb(x, y, w, h, color)
end
end
--RETURNS EXACT PIXEL WIDTH OF A STRING, IN LARGE OR SMALL FONT
--CHEAPER & FASTER THEN PRINT()
function tw(text, size)
local output = 0
for i = 1, #text do
output = output + (size and chars[text:sub(i,i)].small or chars[text:sub(i,i)].large)
end
return output
end
--ALIAS
text_width = function(...) return tw(...) end
function update_cursor_state()
local x, y, l, m, r, sx, sy = mouse()
if not STATE == 'start' then
local _, wx, wy = get_world_cell(x, y)
cursor.wx, cursor.wy = wx, wy
end
local tx, ty = get_screen_cell(x, y)
--update hold state for left and right click
if l and cursor.l and not cursor.held_left and not cursor.r then
cursor.held_left = true
end
if r and cursor.r and not cursor.held_right and not cursor.l then
cursor.held_right = true
end
if cursor.held_left or cursor.held_right then
cursor.hold_time = cursor.hold_time + 1
end
if not l and cursor.held_left then
cursor.held_left = false
cursor.hold_time = 0
end
if not r and cursor.held_right then
cursor.held_right = false
cursor.hold_time = 0
end
cursor.ltx, cursor.lty = cursor.tx, cursor.ty
cursor.tx, cursor.ty, cursor.sx, cursor.sy = tx, ty, sx, sy
cursor.lx, cursor.ly, cursor.ll, cursor.lm, cursor.lr, cursor.lsx, cursor.lsy = cursor.x, cursor.y, cursor.l, cursor.m, cursor.r, cursor.sx, cursor.sy
cursor.x, cursor.y, cursor.l, cursor.m, cursor.r, cursor.sx, cursor.sy = x, y, l, m, r, sx, sy
if cursor.tx ~= cursor.ltx or cursor.ty ~= cursor.lty then
cursor.hold_time = 0
end
if cursor.cooldown > 0 then
cursor.cooldown = cursor.cooldown - 1
cursor.l, cursor.r, cursor.ll, cursor.lr, cursor.held_left, cursor.held_right, cursor.hold_time = false, false, false, false, false, false, 0
cursor.released_left = false
cursor.released_right = false
else
cursor.released_left = cursor.ll and not cursor.l
cursor.released_right = cursor.lr and not cursor.r
end
end
function get_screen_cell(mouse_x, mouse_y)
local cam_x, cam_y = CENTER_X - floor(pos.x), CENTER_Y - floor(pos.y)
local mx = floor(cam_x) % TILE_SIZE
local my = floor(cam_y) % TILE_SIZE
return mouse_x - ((mouse_x - mx) % TILE_SIZE), mouse_y - ((mouse_y - my) % TILE_SIZE)
end
function get_world_cell(mouse_x, mouse_y)
local cam_x = floor(pos.x - CENTER_X)
local cam_y = floor(pos.y - CENTER_Y)
local sub_tile_x = cam_x % TILE_SIZE
local sub_tile_y = cam_y % TILE_SIZE
local sx = floor((mouse_x + sub_tile_x) / TILE_SIZE)
local sy = floor((mouse_y + sub_tile_y) / TILE_SIZE)
local wx = floor(cam_x / TILE_SIZE) + sx + 1
local wy = floor(cam_y / TILE_SIZE) + sy + 1
return wx, wy
end
function get_subtile_position(point)
point.x = floor(point.x)
point.y = floor(point.y)
local cameraTopLeftX = floor(pos.x - CENTER_X) + floor(point.x)
local cameraTopLeftY = floor(pos.y - CENTER_Y) + floor(point.y)
local subTileX = cameraTopLeftX % TILE_SIZE + 1
local subTileY = cameraTopLeftY % TILE_SIZE + 1
local startX = floor(cameraTopLeftX / TILE_SIZE)
local startY = floor(cameraTopLeftY / TILE_SIZE)
return subTileX, subTileY
--local wx, wy = get_world_cell(floor(pos.x) - point.x, floor(pos.y) - point.y)
--local sub_x, sub_y = math.floor(116-local_x), math.floor(64-local_y)
end
function world_to_screen(world_x, world_y)
local screen_x = (world_x * TILE_SIZE) - (pos.x - CENTER_X)
local screen_y = (world_y * TILE_SIZE) - (pos.y - CENTER_Y)
return screen_x - TILE_SIZE, screen_y - TILE_SIZE
end
function screen_to_world(screen_x, screen_y)
local cam_x = pos.x - CENTER_X
local cam_y = pos.y - CENTER_Y
local sub_tile_x = cam_x % TILE_SIZE
local sub_tile_y = cam_y % TILE_SIZE
local sx = floor((screen_x + sub_tile_x) / TILE_SIZE)
local sy = floor((screen_y + sub_tile_y) / TILE_SIZE)
local wx = floor(cam_x / TILE_SIZE) + sx + 1
local wy = floor(cam_y / TILE_SIZE) + sy + 1
return wx, wy
end
function prints(txt, x, y, bg, fg, shadow_offset, small_font)
bg, fg = bg or 0, fg or 4
shadow_offset = shadow_offset or {x = 1, y = 0}
print(txt, x + shadow_offset.x, y + shadow_offset.y, bg, false, 1, small_font)
print(txt, x, y, fg, false, 1, small_font)
end
function hovered(_mouse, _box)
local mx, my, ax, by, bw, bh = _mouse.x, _mouse.y, _box.x, _box.y, _box.w, _box.h
return mx >= ax and mx < ax + bw and my >= by and my < by + bh
end
function lerp(a,b,mu)
return a*(1-mu)+b*mu
end
function pal(c0, c1)
if not c0 and not c1 then
for i = 0, 15 do
poke4(0x3FF0 * 2 + i, i)
end
elseif type(c0) == 'table' then
for i = 1, #c0, 2 do
poke4(0x3FF0*2 + c0[i], c0[i + 1])
end
else
poke4(0x3FF0*2 + c0, c1)
end
end
function clamp(val, min, max)
return math.max(min, math.min(val, max))
end
function swap(object, from_index, to_index)
object[from_index], object[to_index] = object[to_index], object[from_index]
end
function remap(n, a, b, c, d)
return c + (n - a) * (d - c) / (b - a)
end
function rndi(min, max)
-- Ensure that min and max are integers
if min == 0 and max == 0 then return 0 end
min, max = floor(min), floor(max)
-- Handle the case where min is greater than max
if min > max then
min, max = max, min
end
-- Generate and return the random integer
return math.random(min, max)
end
-- Function to generate a random float in the range [min, max]
function rndf(min, max)
if min == 0 and max == 0 then return 0 end
-- Handle the case where min is greater than max
if min > max then
min, max = max, min
end
-- Generate and return the random float
return min + math.random() * (max - min)
end
function rspr(id, x, y, colorkey, scaleX, scaleY, flip, rotate, tile_width, tile_height, pivot, skip)
colorkey = colorkey or -1
scaleX = scaleX or 1
scaleY = scaleY or 1
flip = flip or 0
rotate = rotate or 0
tile_width = tile_width or 1
tile_height = tile_height or 1
pivot = pivot or vec2(4, 4)
skip = skip or {false, false}
-- Draw a sprite using two textured triangles.
-- Apply affine transformations: scale, shear, rotate, flip
-- scale / flip
if flip % 2 == 1 then
scaleX = -scaleX
end
if flip >= 2 then
scaleY = -scaleY
end
local ox = tile_width * 8 // 2
local oy = tile_height * 8 // 2
local ox = ox * -scaleX
local oy = oy * -scaleY
-- shear / rotate
local shx1 = 0
local shy1 = 0
local shx2 = 0
local shy2 = 0
local shx1 = shx1 * -scaleX
local shy1 = shy1 * -scaleY
local shx2 = shx2 * -scaleX
local shy2 = shy2 * -scaleY
local rr = math.rad(rotate)
local sa = math.sin(rr)
local ca = math.cos(rr)
local function rot(x, y, px, py)
-- Translate point to origin (pivot)
x, y = x - px, y - py
-- Rotate
local rx, ry = x * ca - y * sa, x * sa + y * ca
-- Translate back
return rx + px, ry + py
end
local rx1, ry1 = rot(ox + shx1, oy + shy1, pivot.x, pivot.y)
local rx2, ry2 = rot((( tile_width * 8) * scaleX) + ox + shx1, oy + shy2, pivot.x, pivot.y)
local rx3, ry3 = rot(ox + shx2, ((tile_height * 8) * scaleY) + oy + shy1, pivot.x, pivot.y)
local rx4, ry4 = rot((( tile_width * 8) * scaleX) + ox + shx2, ((tile_height * 8) * scaleY) + oy + shy2, pivot.x, pivot.y)
local x1 = x + rx1 - pivot.x
local y1 = y + ry1 - pivot.y
local x2 = x + rx2 - pivot.x
local y2 = y + ry2 - pivot.y
local x3 = x + rx3 - pivot.x
local y3 = y + ry3 - pivot.y
local x4 = x + rx4 - pivot.x
local y4 = y + ry4 - pivot.y
-- UV coords
local u1 = (id % 16) * 8
local v1 = math.floor(id / 16) * 8
local u2 = u1 + tile_width * 8
local v2 = v1 + tile_height * 8
if not skip[1] then
ttri(x1, y1, x2, y2, x3, y3, u1, v1, u2, v1, u1, v2, 0, colorkey)
end
if not skip[2] then
ttri(x3, y3, x4, y4, x2, y2, u1, v2, u2, v2, u2, v1, 0, colorkey)
end
end
function get_subtile_position(point)
point.x = floor(point.x)
point.y = floor(point.y)
local cameraTopLeftX = floor(pos.x - 116) + floor(point.x)
local cameraTopLeftY = floor(pos.y - 64) + floor(point.y)
local subTileX = cameraTopLeftX % 8 + (cameraTopLeftX % 8 == 0 and 0 or 1)
local subTileY = cameraTopLeftY % 8 + (cameraTopLeftY % 8 == 0 and 0 or 1)
local startX = floor(cameraTopLeftX / 8)
local startY = floor(cameraTopLeftY / 8)
return subTileX, subTileY
--local wx, wy = get_world_cell(floor(pos.x) - point.x, floor(pos.y) - point.y)
--local sub_x, sub_y = math.floor(116-local_x), math.floor(64-local_y)
end
function BOOT()
--poke(0x3FF8, 8)
end
function update_sync()
local pages = {
[0] = {bank = 0, mask = 1},
[1] = {bank = 0, mask = 2},
[2] = {bank = 1, mask = 1},
[3] = {bank = 1, mask = 2},
[4] = {bank = 2, mask = 1},
[5] = {bank = 2, mask = 2},
[6] = {bank = 3, mask = 1},
[7] = {bank = 3, mask = 2},
[8] = {bank = 4, mask = 1},
[9] = {bank = 4, mask = 2},
[10] = {bank = 5, mask = 1},
[11] = {bank = 5, mask = 2},
[12] = {bank = 6, mask = 1},
[13] = {bank = 6, mask = 2},
[14] = {bank = 7, mask = 1},
[15] = {bank = 7, mask = 2},
}
sync(pages[SPRITE_PAGE].mask, pages[SPRITE_PAGE].bank, false)
end
function draw_sprite_window(x, y)
rect(x, y, 128, 128, 8)
spr(SPRITE_PAGE*256, x, y, 0, 1, 0, 0, 16, 16)
--rectb(x-1, y-1, 130, 130, 15)
end
function draw_sprite_grid(w, h, position, color)
--if not SHOW_GRID then return end
local sub_x, sub_y = position.x % 8, position.y % 8
local rows, cols = h//8 + 1, w//8 + 1
for row = 1, rows do
for col = 1, cols do
pal({15, color})
--spr(GRID, position.x + -sub_x + (col-1) * 8, position.y + -sub_y + (row - 1) * 8 - 4, 0)
spr(GRID, position.x + (col-1) * 8, position.y + (row - 1) * 8, 0)
end
end
pal()
-- if SHOW_AXIS then
-- local center_x, center_y = world_to_screen(0, 0)
-- line(center_x - 10000, center_y, center_x + 10000, center_y, 12)
-- line(center_x, center_y - 10000, center_x, center_y + 10000, 3)
-- end
end
function update_text_buffer(callback, max_size)
max_size = max_size or math.huge
-- local alpha = {
-- [ 1] = {'A', 'a'},
-- [ 2] = {'B', 'b'},
-- [ 3] = {'C', 'c'},
-- [ 4] = {'D', 'd'},
-- [ 5] = {'E', 'e'},
-- [ 6] = {'F', 'f'},
-- [ 7] = {'G', 'g'},
-- [ 8] = {'H', 'h'},
-- [ 9] = {'I', 'i'},
-- [10] = {'J', 'j'},
-- [11] = {'K', 'k'},
-- [12] = {'L', 'l'},
-- [13] = {'M', 'm'},
-- [14] = {'N', 'n'},
-- [15] = {'O', 'o'},
-- [16] = {'P', 'p'},
-- [17] = {'Q', 'q'},
-- [18] = {'R', 'r'},
-- [19] = {'S', 's'},
-- [20] = {'T', 't'},
-- [21] = {'U', 'u'},
-- [22] = {'V', 'v'},
-- [23] = {'W', 'w'},
-- [24] = {'X', 'x'},
-- [25] = {'Y', 'y'},
-- [26] = {'Z', 'z'},
-- [27] = {'0'},
-- [28] = {'1'},
-- [29] = {'2'},
-- [30] = {'3'},
-- [31] = {'4'},
-- [32] = {'5'},
-- [33] = {'6'},
-- [34] = {'7'},
-- [35] = {'8'},
-- [36] = {'9'},
-- [37] = {'_', '-'},
-- }
local alpha = KEYS
----------TYPE/APPEND BUFFER-------
for k, v in pairs(alpha) do
if keyp(k) and text_width(TEXT_BUFFER, true) < max_size then
local char = v[1]
if not key(64) and v[2] then
char = v[2]
end
TEXT_BUFFER = TEXT_BUFFER .. char
end
end
--------BACKSPACE/TRIM BUFFER------
if keyp(51, 30, 3) and #TEXT_BUFFER > 0 then
TEXT_BUFFER = TEXT_BUFFER:sub(1, #TEXT_BUFFER - 1)
end
if keyp(48) and text_width(TEXT_BUFFER, true) < max_size then
TEXT_BUFFER = TEXT_BUFFER .. "_"
end
if keyp(50) or keyp(93) then
callback()
end
end
function draw_text_buffer(pos, size, color, callback)
rect(pos.x, pos.y, size.w, size.h, color.bg)
rectb(pos.x, pos.y, size.w, size.h, color.fg)
rect(pos.x + text_width(TEXT_BUFFER, true) + 2, pos.y + 2, 1, 5, TICK % 60 > 30 and 15 or 0)
prints(TEXT_BUFFER, pos.x + 2, pos.y + 2, 8, 2, {x = 1, y = 0}, true)
prints("Press ENTER to accept", size.w/2 - text_width("Press ENTER to accept", true)/2, pos.y + 12, 8, TICK % 60 > 30 and 13 or 15, {x = 1, y = 0}, true)
end
function draw_main_menu(dt)
update_cursor_state()
vbank(0)
cls()
draw_sprite_grid(248, 144, vec2(), 13, true)
vbank(1)
cls()
HOVER_TEXT = ''
local items = {}
items[1] = {x = 120 - 25, y = 73, w = 50, h = 10}
items[2] = {x = 120 - 25, y = 89, w = 50, h = 10}
--items[3] = {x = 120 - (text_width("sETTINGS", true)/2), y = 93, w = text_width("sETTINGS", true) + 4, h = 10}
local offset = remap(sin(TICK/2%30/25), 0, 1, -5, 5)
local padding_left, padding_right = 10, 2
--offset = 1
--printo(offset, 1, 136 - 8, 0, 2, true)
for k, v in ipairs(items) do
if hovered(cursor, v) then
LAST_HOVERED_MENU_ITEM = k
end
end
local el = items[LAST_HOVERED_MENU_ITEM]
pal({1, 0, 15, 2, 4, 8})
spr(BUTTON_ARROW_UP_SMALL, el.x - padding_left - abs(offset) + 2, el.y - 1, 0, 1, 0, 2)
spr(BUTTON_ARROW_UP_SMALL, el.x + el.w + padding_right + abs(offset), el.y, 0, 1, 0, 0)
pal()
local new_particle_text = " New "
if ui.draw_text_button(120 - (text_width(new_particle_text, true)/2), 73, BUTTON_TEXT, text_width(new_particle_text, true) + 4, 8, 12, 8, 1, {text = new_particle_text, x = 2, y = 1, bg = 0, fg = 15, shadow = {x = 1, y = 0}}, false, true) then
--trace("clicked new")
STATE = 'edit'
set_emitter_main()
EMITTER = EMITTER_DEFAULT
EMITTER_POSITION = vec2(grid.x + (GRID_WIDTH/2), grid.y + (GRID_HEIGHT/2))
EDIT_STATE = 'emitter'
return
end
local load_preset_text = " Load "
if ui.draw_text_button(120 - (text_width(load_preset_text, true)/2), 89, BUTTON_TEXT, text_width(load_preset_text, true) + 4, 8, 7, 8, 5, {text = load_preset_text, x = 2, y = 1, bg = 8, fg = 15, shadow = {x = 1, y = 0}}, false, true) then
STATE = 'load'
set_emitter_preview()
--trace('clicked load preset button')
EMITTER_POSITION = vec2(120, 68)
return
end
-- if ui.draw_text_button(120 - (text_width("Settings", true)/2), 93, BUTTON_TEXT, text_width("Settings", true) + 4, 8, 6, 8, 5, {text = "Settings", x = 2, y = 1, bg = 8, fg = 15, shadow = {x = 1, y = 0}}, false, true) then
-- trace("clicked settings")
-- LAST_STATE = STATE
-- STATE = 'settings'
-- return
-- end
draw_header_overlay()
if ui.draw_button(240 - 6, 0, 0, BUTTON_MENU, 15, 8, 6, function()
HOVER_TEXT = "Editor Settings"
end, {x = -1, y = -1, w = 7, h = 7}) then
LAST_STATE = STATE
STATE = 'settings'
--trace("clicked editor settings button")
end
-- if HOVER_TEXT ~= '' then
-- printo(HOVER_TEXT, clamp(cursor.x + 8, 1, 239 - text_width(HOVER_TEXT, true)), clamp(cursor.y + 8, 1, 136 - 8), 8, 15, true)
-- end
draw_hover_text()
-- if ui.draw_text_button(120 - (text_width("RESUME", true)/2), 93, BUTTON_TEXT, text_width("RESUME", true) + 4, 8, 12, 8, 1, {text = "RESUME", x = 2, y = 1, bg = 8, fg = 2, shadow = {x = 1, y = 0}}, false, true) then
-- STATE = LAST_STATE
-- return
-- end
end
function draw_header_overlay()
rect(0, 0, 240, 8, 7)
local button_color = 12
local button_highlight = 2
local button_hover = 5
prints("Particle Forge", 120 - (text_width("Particle Forge", false)/2), 1, 8, 2, {x = 1, y = 1}, false)
end
function draw_particle_preview_window_simulation(x, y, w, h, bounds, dt)
local grid_hovered = hovered(cursor, {x = bounds.x, y = bounds.y, w = bounds.w, h = bounds.h})
local particle_emitter = TEMP_EMITTER
--update_cursor_state()
--update_particles(dt)
--UPDATE PARTICLES------------------------------------------
for k, v in ipairs(particle_emitter.particle_systems) do
--SORT THE PARTICLES BY AGE
-- table.sort(v.particles.fg, function(a, b)
-- return a.age > b.age
-- end)