-
Notifications
You must be signed in to change notification settings - Fork 0
/
boomerang.lua
2626 lines (2394 loc) · 82.7 KB
/
boomerang.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
C0 = 0 -- прозрачный цвет
-- блок со сменой цвета палитры
ADDR = 0x3FC0
eADDR = 0x4000
function exportpal(pal)
for i=1,#pal do
poke4((eADDR*2)+64+#pal-i,tonumber(pal:sub(i,i),16))
end
end
function savePalette()
palette = ""
for i=0, 15 do
for j=0, 2 do
palette = palette..string.format("%02x",peek(ADDR+(i*3)+j))
end
end
end
function colorChange(pn)
-- pn: palette number
local id = 0 -- id цвета
poke(ADDR+(id*3)+2, peek(ADDR+(pn*3)+2)) -- red
poke(ADDR+(id*3)+1, peek(ADDR+(pn*3)+1)) -- green
poke(ADDR+(id*3), peek(ADDR+(pn*3))) -- blue
id = 8
poke(ADDR+(id*3)+2, peek(ADDR+(pn*3)+24+2)) -- red
poke(ADDR+(id*3)+1, peek(ADDR+(pn*3)+24+1)) -- green
poke(ADDR+(id*3), peek(ADDR+(pn*3)+24)) -- blue
savePalette()
exportpal(palette)
end
--
function negative_colorChange(pn)
-- pn: palette number
local id = 8 -- id цвета
poke(ADDR+(id*3)+2, peek(ADDR+(pn*3)+2)) -- red
poke(ADDR+(id*3)+1, peek(ADDR+(pn*3)+1)) -- green
poke(ADDR+(id*3), peek(ADDR+(pn*3))) -- blue
id = 0
poke(ADDR+(id*3)+2, peek(ADDR+(pn*3)+24+2)) -- red
poke(ADDR+(id*3)+1, peek(ADDR+(pn*3)+24+1)) -- green
poke(ADDR+(id*3), peek(ADDR+(pn*3)+24)) -- blue
savePalette()
exportpal(palette)
end
--
BOSS_PIXELS = {
-- hair
{x = 1, y = 1},
{x = 2, y = 2},
{x = 3, y = 3},
{x = 5, y = 1},
{x = 6, y = 2},
{x = 7, y = 3},
{x = 9, y = 1},
{x = 10, y = 2},
{x = 11, y = 3},
{x = 13, y = 1},
{x = 14, y = 2},
{x = 15, y = 3},
{x = 17, y = 1},
{x = 18, y = 2},
{x = 19, y = 3},
-- top head line
{x = 3, y = 4},
{x = 4, y = 4},
{x = 5, y = 4},
{x = 6, y = 4},
{x = 7, y = 4},
{x = 8, y = 4},
{x = 9, y = 4},
{x = 10, y = 4},
{x = 11, y = 4},
{x = 12, y = 4},
{x = 13, y = 4},
{x = 14, y = 4},
{x = 15, y = 4},
{x = 16, y = 4},
{x = 17, y = 4},
{x = 18, y = 4},
{x = 19, y = 4},
-- nose
{x = 12, y = 9},
{x = 13, y = 9},
{x = 12, y = 10},
{x = 13, y = 10},
{x = 13, y = 11},
-- beard
{x = 5, y = 8},
{x = 4, y = 9},
{x = 5, y = 9},
{x = 6, y = 9},
{x = 19, y = 9},
{x = 4, y = 10},
{x = 5, y = 10},
{x = 6, y = 10},
{x = 7, y = 10},
{x = 8, y = 10},
{x = 9, y = 10},
{x = 10, y = 10},
{x = 15, y = 10},
{x = 16, y = 10},
{x = 17, y = 10},
{x = 18, y = 10},
{x = 19, y = 10},
{x = 5, y = 11},
{x = 6, y = 11},
{x = 7, y = 11},
{x = 8, y = 11},
{x = 9, y = 11},
{x = 10, y = 11},
{x = 15, y = 11},
{x = 16, y = 11},
{x = 17, y = 11},
{x = 18, y = 11},
{x = 6, y = 12},
{x = 7, y = 12},
{x = 8, y = 12},
{x = 9, y = 12},
{x = 10, y = 12},
{x = 11, y = 12},
{x = 15, y = 12},
{x = 16, y = 12},
{x = 17, y = 12},
{x = 18, y = 12},
{x = 7, y = 13},
{x = 8, y = 13},
{x = 9, y = 13},
{x = 10, y = 13},
{x = 11, y = 13},
{x = 12, y = 13},
{x = 13, y = 13},
{x = 14, y = 13},
{x = 15, y = 13},
{x = 16, y = 13},
{x = 17, y = 13},
{x = 7, y = 14},
{x = 8, y = 14},
{x = 9, y = 14},
{x = 10, y = 14},
{x = 11, y = 14},
{x = 12, y = 14},
{x = 13, y = 14},
{x = 14, y = 14},
{x = 15, y = 14},
{x = 16, y = 14},
{x = 17, y = 14},
{x = 8, y = 15},
{x = 9, y = 15},
{x = 10, y = 15},
{x = 11, y = 15},
{x = 12, y = 15},
{x = 13, y = 15},
{x = 14, y = 15},
{x = 15, y = 15},
{x = 16, y = 15},
{x = 9, y = 16},
{x = 10, y = 16},
{x = 11, y = 16},
{x = 12, y = 16},
{x = 13, y = 16},
{x = 14, y = 16},
{x = 15, y = 16},
{x = 10, y = 17},
{x = 11, y = 17},
{x = 12, y = 17},
{x = 13, y = 17},
{x = 14, y = 17},
{x = 11, y = 18},
{x = 12, y = 18},
{x = 13, y = 18}
}
function table.shuffle(t)
res = {}
for _, e in ipairs(t) do
local i = math.random(1, #t)
while res[i] ~= nil do
i = math.random(1, #t)
end
res[i] = e
end
return res
end
--
function math.sign(x)
if x<0 then
return -1
end
if x>0 then
return 1
end
return 0
end
function make_angles()
a0 = Body:new(ANGLE:copy(), 0, 16*8)
a1 = Body:new(ANGLE:copy(), 29*8, 16*8)
a1.flip = 1
a2 = Body:new(ANGLE:copy(), 0, 0)
a2.flip = 2
a3 = Body:new(ANGLE:copy(), 29*8, 0)
a3.flip = 3
return {a0, a1, a2, a3}
end
function sq_distance(x1, y1, x2, y2)
-- возвращает квадрат расстояния между точками
return math.abs(x1 - x2)^2 + math.abs(y1 - y2)^2
end
function sq_point_ortsegment_distance(x, y, x1, y1, x2, y2)
-- отрезок ортогональный
if (x == x1 and x == x2) or (y == y1 and y == y2) then
return 0
end
if x1 <= x and x <= x2 then
return math.min(sq_distance(x, y, x1, y1), sq_distance(x, y, x2, y2), sq_distance(x, y, x, y1))
end
if y1 <= y and y <= y2 then
return math.min(sq_distance(x, y, x1, y1), sq_distance(x, y, x2, y2), sq_distance(x, y, x1, y))
end
return math.min(sq_distance(x, y, x1, y1), sq_distance(x, y, x2, y2))
end
function fence(x, left, right)
if x < left then
return left
end
if x > right then
return right
end
return x
end
function animation_generation(t)
-- #t -- делитель 60
res = {}
for _, pict in ipairs(t) do
for i=1, 60 // (#t) do
table.insert(res, pict)
end
end
return res
end
function plr_death_anim()
res = {}
for i=272, 278 do
for _=1, 8 do
table.insert(res, i)
end
end
for _=1, 4 do
table.insert(res, 279)
end
return res
end
function boss_opening_mouth_anim()
res = {}
for _, f in ipairs({384, 387, 390}) do
for i = 1, 10 do
table.insert(res, f)
end
end
return res
end
function boss_beard_anim()
return animation_generation({
{1, 2, 3, 4, 5},
{1, 2, 3, 4, 5},
{-1, 2, 3, 4, 5},
{-1, 2, 3, 4, 5},
{-1, -2, -3, 4, 5},
{-1, -2, -3, 4, 5},
{1, -2, -3, -4, -5},
{1, -2, -3, -4, -5},
{1, 2, 3, -4, -5},
{1, 2, 3, -4, -5}
})
end
function table.shallow_copy(t)
local t2 = {}
for k,v in pairs(t) do
t2[k] = v
end
return t2
end
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
function table.reverse(t)
res = {}
for i = #t, 1, -1 do
table.insert(res, t[i])
end
return res
end
-- Cartoon -- описание интерфейса
Cartoon = {}
function Cartoon:new()
obj = {
t = 0, limit = 100,
name = 'nil'
}
-- чистая магия!
setmetatable(obj, self)
self.__index = self; return obj
end
function Cartoon:update()
self.t = self.t + 1
end
function Cartoon:check()
return self.t >= self.limit
end
--
--
BornBossCartoon = table.shallow_copy(Cartoon)
function BornBossCartoon:new()
obj = {
t = 0, limit = 31,
name = 'BornBoss',
bomb = Bomb:new(28, 68, 28, 68),
boss = Boss:new(20, 60)
}
-- чистая магия!
setmetatable(obj, self)
self.__index = self; return obj
end
function BornBossCartoon:update()
if self.t <= self.limit - 2 then
self.t = self.t + 1
self.bomb:draw()
return
end
self.bomb:update()
if self.bomb.mode == 'vanish' then
self.boss:draw()
if self.bomb.sprite:animation_end() then
self.t = self.limit
end
end
end
function BornBossCartoon:get_boss()
return self.boss
end
--
--
DeathBossCartoon = table.shallow_copy(Cartoon)
function DeathBossCartoon:new(boss)
obj = {
t = 0, limit = 900,
name = 'DeathBoss',
boss = boss
}
-- obj['boss'].sprite = Boss.dying_a:copy()
-- чистая магия!
setmetatable(obj, self)
self.__index = self; return obj
end
function DeathBossCartoon:update()
if self.t == 0 then
self.boss.sprite = Boss.dying_a:copy()
end
self.t = self.t + 1
self.boss.sprite:next_frame()
self.boss:draw()
if self.t >= 300 then
if self.t % 3 == 0 then
self.boss.x = self.boss.x - 1
end
elseif self.t >= 100 then
rect(self.boss.x + 26, self.boss.y, 18*5 + 7, 9, 8)
print("Battle isn't over!", self.boss.x + 28, self.boss.y + 2, 0)
end
if self.boss.x <= -29 then
self.t = self.limit
end
end
--
--
FinalCartoon = table.shallow_copy(Cartoon)
function FinalCartoon:new(boss)
obj = {
t = 0, limit = 900 + 7*60,
name = 'Final',
boss = boss,
pixels = table.shuffle(BOSS_PIXELS), i = 0,
bullets = {}, fall_y = 6
}
-- obj['boss'].sprite = Boss.dying_a:copy()
-- чистая магия!
setmetatable(obj, self)
self.__index = self; return obj
end
function FinalCartoon:update()
if self.t == 0 then
self.boss.sprite = Sprite:new({192}, 3)
end
if self.t <= 300 or self.t >= 900 then
self.t = self.t + 1
end
if self.t >= 900 then
self:sunglasses_fall()
elseif self.t >= 300 then
self:decay()
elseif self.t >= 100 then
Boss.sunglasses:draw(self.boss.x + 3, self.boss.y + 6, 0)
self.boss.sprite:draw(self.boss.x, self.boss.y, 0)
self.boss:draw_health_bar()
rect(self.boss.x + 26, self.boss.y, 18*5 + 7, 9, 8)
print("Not bad, hatter", self.boss.x + 28, self.boss.y + 2, 0)
else
Boss.sunglasses:draw(self.boss.x + 3, self.boss.y + 6, 0)
self.boss.sprite:draw(self.boss.x, self.boss.y, 0)
self.boss:draw_health_bar()
end
end
function FinalCartoon:decay()
self.i = self.i + 1
if self.i > #self.pixels then
self.t = 900 -- marker
return
end
Boss.sunglasses:draw(self.boss.x + 3, self.boss.y + 6, 0)
self.boss.sprite:draw(self.boss.x, self.boss.y, 0)
self.boss:draw_health_bar()
for j = 1, self.i do
rect(self.boss.x + self.pixels[j].x, self.boss.y + self.pixels[j].y, 1, 1, 0)
end
table.insert(self.bullets,
ImprovedBullet:new(-1, PIXEL, self.boss.x + self.pixels[self.i].x, self.boss.y + self.pixels[self.i].y, self.boss.x + 10, self.boss.y + 9))
for _, b in ipairs(self.bullets) do
b:update()
if b.x <= 0 or b.x >= 240 or b.y <= 0 or b.y >= 136 then
table.remove(self.bullets, _)
end
end
end
function FinalCartoon:sunglasses_fall()
while #self.bullets ~= 0 do
for _, b in ipairs(self.bullets) do
b:update()
if b.x <= 0 or b.x >= 240 or b.y <= 0 or b.y >= 136 then
table.remove(self.bullets, _)
end
end
Boss.sunglasses:draw(self.boss.x + 3, self.boss.y + self.fall_y, 0)
return
end
if self.fall_y < 18 then
self.fall_y = self.fall_y + 0.2
end
Boss.sunglasses:draw(self.boss.x + 3, self.boss.y + self.fall_y, 0)
end
--
--
FirstCartoon = table.shallow_copy(Cartoon)
INF = 99999999
function FirstCartoon:new()
obj = {
t = 0, limit = 120*2,
name = 'First',
flag = false,
plr = PseudoPlayer:new(-10, 54),
-- plr = 1,
boomerang_status = 0
}
-- чистая магия!
setmetatable(obj, self)
self.__index = self; return obj
end
function FirstCartoon:update()
if self.t > 0 then
self.t = self.t + 1
if self.t <= 120 then
print('BOOMERANG', 90, 53, 8)
else
VCROC:draw(108, 54, 0)
print('Made by', 100, 76, 8)
print('V. Crocodile', 100, 84, 8)
end
return
end
-- trace(self.plr)
if self.plr.x >= 60 then
self.flag = true
self.plr:autothrow()
end
self.plr:update(self.flag)
-- if self.flag then
-- print('Press [z]', 77, 110, 8)
-- end
if self.plr.boomerang and self.boomerang_status == 0 then
self.boomerang_status = 1
elseif not self.plr.boomerang and self.boomerang_status == 1 then
self.t = 1
end
end
--
--
Hitbox = {}
function Hitbox:new(x1, y1, x2, y2)
obj = {
x1 = x1, y1 = y1,
x2 = x2, y2 = y2
}
-- чистая магия!
setmetatable(obj, self)
self.__index = self; return obj
end
function Hitbox:collide(hb)
if hb.x1 >= self.x2 or self.x1 >= hb.x2 or
hb.y1 >= self.y2 or self.y1 >= hb.y2 then
return false
end
return true
end
function Hitbox:set_xy(x, y)
dx = x - self.x1
dy = y - self.y1
self.x1 = x; self.y1 = y
self.x2 = self.x2 + dx
self.y2 = self.y2 + dy
end
--
-- Не имеет ничего общего с Hitbox кроме принципа работы
HitCircle = table.shallow_copy(Hitbox)
function HitCircle:new(x, y, d)
obj = {
x = x, y = y, -- left top pixel
d = d, -- diameter
hb = Hitbox:new(x, y, x+d, y+d) -- для упрощения расчетов
}
-- чистая магия!
setmetatable(obj, self)
self.__index = self; return obj
end
function HitCircle:collide(hb)
if not self.hb:collide(hb) then
return false
end
local d = self.d
-- is center in hb
if hb.x1 <= self.x + d/2 and self.x + d/2 <= hb.x2 and
hb.y1 <= self.y + d/2 and self.y + d/2 <= hb.y2 then
return true
end
-- is hb side collide circle
if sq_point_ortsegment_distance(self.x + d/2, self.y + d/2, hb.x1, hb.y1, hb.x1, hb.y2) <= d^2 / 4 then
return true
end
if sq_point_ortsegment_distance(self.x + d/2, self.y + d/2, hb.x2, hb.y1, hb.x2, hb.y2) <= d^2 / 4 then
return true
end
if sq_point_ortsegment_distance(self.x + d/2, self.y + d/2, hb.x1, hb.y1, hb.x2, hb.y1) <= d^2 / 4 then
return true
end
if sq_point_ortsegment_distance(self.x + d/2, self.y + d/2, hb.x1, hb.y2, hb.x2, hb.y2) <= d^2 / 4 then
return true
end
return false
end
function HitCircle:set_xy(x, y)
self.x = x
self.y = y
self.hb:set_xy(x, y)
end
--
--
Sprite = {}
function Sprite:new(animation, size)
obj = {
animation = animation,
frame = 1, -- номер кадра
size = size -- размер спрайта
}
-- чистая магия!
setmetatable(obj, self)
self.__index = self; return obj
end
function Sprite:next_frame()
self.frame = self.frame % #self.animation + 1
end
function Sprite:draw(x, y, flip)
-- print(self.animation[self.frame], 20, 20, 5)
-- print(self.frame, 40, 40, 3)
spr(self.animation[self.frame], x, y, C0, 1, flip, 0, self.size, self.size)
end
function Sprite:animation_end()
return self.frame == #self.animation
end
function Sprite:copy()
return Sprite:new(self.animation, self.size)
end
--
VCROC = Sprite:new({40}, 3)
-- базовый класс
Body = {}
function Body:new(sprite, x, y)
obj = {
sprite = sprite,
hitbox = 'nil',
flip = 0,
x = x, y = y,
hp = 0, born_flag = true
}
-- чистая магия!
setmetatable(obj, self)
self.__index = self; return obj
end
function Body:is_dead()
return (self.hp == 0)
end
function Body:take_damage(damage)
self.hp = fence(self.hp - damage, 0, self.hp)
end
function Body:draw()
self.sprite:draw(self.x, self.y, self.flip)
end
function Body:born_update()
self:draw()
-- trace('born')
if self.sprite:animation_end() then
self.born_flag = false
return false
end
self.sprite:next_frame()
return true
end
--
--
Player = table.shallow_copy(Body)
Player.stay_a = Sprite:new({257}, 1)
Player.run_a = Sprite:new(animation_generation({256, 257, 258, 259, 256, 257, 258, 259, 256, 257, 258, 259}), 1)
Player.death_a = Sprite:new(animation_generation(plr_death_anim()), 1)
Player.born_a = Sprite:new(table.reverse(animation_generation(plr_death_anim())), 1)
Player.hat_a = Sprite:new(animation_generation({279}), 1)
function Player:new(x, y)
obj = {
sprite = Player.born_a:copy(),
start_x = x, start_y = y,
x = x, y = y,
last_dx = 1, last_dy = 0,
dx = 0, dy = 0, v = 1,
flip = 0, -- направление при отрисовке спрайта
hitbox = Hitbox:new(x+2, y+1, x+5, y+7),
hp = 1,
born_flag = true,
boomerang = false
}
-- чистая магия!
setmetatable(obj, self)
self.__index = self; return obj
end
function Player:update()
if self:is_dead() then
self:death_update()
return
end
if self.born_flag then
if not self:born_update() then -- если рождение закончилось
self.sprite = Player.stay_a:copy()
end
return
end
flag = false
if math.abs(self.dx) + math.abs(self.dy) ~= 0 then -- is moving
flag = true
end
self.dx = 0; self.dy = 0
if btn(0) then
self.dy = self.dy - 1
end
if btn(1) then
self.dy = self.dy + 1
end
if btn(2) then
self.dx = self.dx - 1
end
if btn(3) then
self.dx = self.dx + 1
end
k = 1
if self.dx * self.dy ~= 0 then
k = 1 / math.sqrt(2)
end
if math.abs(self.dx) + math.abs(self.dy) ~= 0 then -- is moving
self.last_dx = self.dx; self.last_dy = self.dy
if not flag or #self.sprite.animation == 1 then
self.sprite = Player.run_a:copy()
end
else
self.sprite = Player.stay_a:copy()
end
if self.dx == -1 then
self.flip = 1
elseif self.dx == 1 then
self.flip = 0
end
self.sprite:next_frame()
self.x = fence(self.x + self.dx * self.v * k, 0, 240 - 8)
self.y = fence(self.y + self.dy * self.v * k, 0, 136 - 8)
self.hitbox:set_xy(self.x+2, self.y+1)
self:draw()
if btnp(4) and not self.boomerang then
self:shoot()
end
if self.boomerang then
self.boomerang:focus(self.x, self.y)
self.boomerang:update()
if self.boomerang.hitbox:collide(self.hitbox) and
self.boomerang.v < self.v then
self.boomerang = false
end
end
end
function Player:shoot()
self.boomerang = Boomerang:new(self.x, self.y, self.last_dx, self.last_dy)
end
function Player:death_update()
self.sprite:next_frame()
if self.sprite.frame == 60 then
self.sprite = Player.hat_a:copy()
end
self:draw()
end
function Player:death()
-- self.dead = true
self.sprite = Player.death_a:copy()
end
function Player:set_start_stats()
self.hp = 1
self.boomerang = false
self.x = self.start_x
self.y = self.start_y
self.sprite = Player.born_a:copy()
self.born_flag = true
self.hitbox = Hitbox:new(self.x+2, self.y+1, self.x+5, self.y+7)
end
--
--
PseudoPlayer = table.shallow_copy(Player)
function PseudoPlayer:update(cartflag)
if self:is_dead() then
self:death_update()
return
end
if self.born_flag then
if not self:born_update() then -- если рождение закончилось
self.sprite = Player.stay_a:copy()
end
return
end
flag = false
if math.abs(self.dx) + math.abs(self.dy) ~= 0 then -- is moving
flag = true
end
self.dx = 0; self.dy = 0
-- if not cartflag then
self.dx = self.dx + 0.7
-- end
k = 1
if self.dx * self.dy ~= 0 then
k = 1 / math.sqrt(2)
end
if math.abs(self.dx) + math.abs(self.dy) ~= 0 then -- is moving
self.last_dx = self.dx; self.last_dy = self.dy
if not flag then
self.sprite = Player.run_a:copy()
end
else
self.sprite = Player.stay_a:copy()
end
if self.dx == -1 then
self.flip = 1
elseif self.dx == 1 then
self.flip = 0
end
self.sprite:next_frame()
self.x = fence(self.x + self.dx * self.v * k, 0, 240 - 8)
self.y = fence(self.y + self.dy * self.v * k, 0, 136 - 8)
self.hitbox:set_xy(self.x+2, self.y+1)
self:draw()
-- if btnp(4) and not self.boomerang and cartflag then
-- self:shoot()
-- end
if self.boomerang then
self.boomerang:focus(self.x, self.y)
self.boomerang:update()
if self.boomerang.hitbox:collide(self.hitbox) and
self.boomerang.v < self.v then
self.boomerang = false
end
end
end
function PseudoPlayer:autothrow()
if not self.boomerang then
self:shoot()
end
end
--
--
Boomerang = table.shallow_copy(Body)
BOOMERANG_A = Sprite:new(animation_generation({264, 265, 266, 264, 265, 266, 264, 265, 266, 264, 265, 266}), 1)
function Boomerang:new(x, y, dx, dy)
obj = { -- dx, dy in [-1, 1]
sprite = BOOMERANG_A:copy(),
x = x, y = y,
dx = dx, dy = dy,
v = 1.8, k = 1,
px = 0, py = 0,
hitbox = Hitbox:new(x+2, y+2, x+5, y+5)
}
obj['flip'] = -fence(dx, -1, 0)
if obj['dx'] * obj['dy'] ~= 0 then
obj['k'] = 1 / math.sqrt(2)
end
obj['dv'] = obj['v'] / 90
-- чистая магия!
setmetatable(obj, self)
self.__index = self; return obj-- body
end
function Boomerang:update()
self.sprite:next_frame()
self.v = self.v - self.dv
if self.v < 0 then
self:reverse_update()
return
end
self.x = self.x + self.v * self.dx * self.k
self.y = self.y + self.v * self.dy * self.k
self.hitbox:set_xy(self.x + 2, self.y + 2)
self:draw()
end
function Boomerang:focus(x, y)
self.px = x; self.py = y
end
function Boomerang:reverse_update()
fx = self.px; fy = self.py
x = self.x; y = self.y
if fx == x then
fx = fx + 0.0000001
end
d = math.abs(fy - y) / math.abs(fx - x)
dx = self.v / math.sqrt(1 + d*d)
dy = dx * d
kx = 1; ky = 1
if fx < x then
kx = -1
end
if fy < y then
ky = -1
end
self.x = self.x - kx * dx
self.y = self.y - ky * dy
self.hitbox:set_xy(self.x + 2, self.y + 2)
self:draw()
end
--
--
Enemy = table.shallow_copy(Body)
Enemy.stay_a = Sprite:new(animation_generation({261}), 1)
Enemy.suffer_a = Sprite:new(animation_generation({262, 263, 262, 263, 262, 263, 262, 263, 262, 263, 262, 263}), 1)
Enemy.death_a = Sprite:new(animation_generation({288, 289, 290, 291, 292, 293}), 1)
Enemy.born_a = Sprite:new(table.reverse(animation_generation({288, 289, 290, 291, 292, 293})), 1)
function Enemy:new(x, y)
obj = {
sprite = Enemy.born_a:copy(),
x = x, y = y, hp = 24,
px = 0, py = 0, -- положение игрока
flip = math.random(0, 1), -- направление при отрисовке спрайта
charge = 0, -- заряд выстрела
hitbox = Hitbox:new(x+1, y+1, x+5, y+6),
born_flag = true, death_flag = true
}
-- чистая магия!
setmetatable(obj, self)
self.__index = self; return obj
end
function Enemy:update()
if self.born_flag then
if not self:born_update() then
self.sprite = Enemy.stay_a:copy()
end
return
end
if self:is_dead() and self.death_flag then
self.death_flag = false
self.sprite = Enemy.death_a:copy()