-
Notifications
You must be signed in to change notification settings - Fork 8
/
2d Platformer RPG 04.monkey2
2572 lines (2450 loc) · 64.3 KB
/
2d Platformer RPG 04.monkey2
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
#Import "<std>"
#Import "<mojo>"
Using std..
Using mojo..
Global developmode:Bool=False
Global theversion:String="Version 5-11-2017"
Global gamestate:String="select"
Global egghatchspeed:Float = 0.1 'how fast eggs hatch
Global egglayingfreq:Float = 0.1 ' lay lots of eggs 1 lay less eggs 0 '0 to 1
Global startingeggfreq:Float = 0.2 '0 to 1 0 is none 1 is full
Global maxflyingmonsters:Int=30
Global mapwidth:Int=320
Global mapheight:Int=240
Global screenwidth:Int=800
Global screenheight:Int=600
Global tilewidth:Int=20
Global tileheight:Int=20
'
' The tentacle for the growing slime monster
'
Class tentacle
Field basex:Float,basey:Float
Field targetx:Float,targety:Float
Field topx:Float,topy:Float
Field angle:Float
Field mx:Float,my:Float
Field speed:Float = 14
Field deleteme:Bool=False
Field state:String = "outgoing"
Field grabbed:Bool=False
Field grabbedx:Int
Field grabbedy:Int
Method New(basex:Float,basey:Float,targetx:Float,targety:Float)
Self.basex = basex
Self.basey = basey
Self.topx = basex
Self.topy = basey
Self.targetx = targetx
Self.targety = targety
angle = getangle(basex,basey,targetx,targety)
mx = Cos(angle)
my = Sin(angle)
End Method
Method update()
If speed > 1 Then speed -= .1
For Local movement:Int=0 Until speed
topx += mx
topy += my
If state = "outgoing"
'check collision with flyingmonster
For Local i:=Eachin myflyingmonster
Local x:Int=i.px+(i.w/2)
Local y:Int=i.py+(i.h/2)
If distance(topx,topy,x,y) < 10
topx = i.px
topy = i.py
angle = getangle(topx,topy,basex,basey)
mx = Cos(angle)
my = Sin(angle)
grabbed = True
state = "ingoing"
i.deleteme = True
Exit
End If
Next
'check collision with player
Local x:Int=myplayer.px+(myplayer.pw/2)
Local y:Int=myplayer.py+(myplayer.ph/2)
If developmode = False And distance(x,y,topx,topy) < 20
mx = -mx
my = -my
state = "ingoing"
myplayer.hp -= 10
If myplayer.hp < 0 Then
gamestate = "select"
Return
End if
End If
End if
If state="outgoing" And grabbed=False And distance(topx,topy,targetx,targety) < 10 Then
state = "ingoing"
mx = -mx
my = -my
Return
End If
If state = "ingoing"
If distance(topx,topy,basex,basey) < 10 Then
deleteme = True
Return
End If
End If
Next
End Method
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
Function getangle:float(x1:Int,y1:Int,x2:Int,y2:Int)
Return ATan2(y2-y1, x2-x1)
End Function
End Class
'
' Items that move towards the player and into his inventory
' by mining,
'
Class item
Field px:Float,py:Float
Field w:Int,h:Int
Field deleteme:Bool
Field angle:Float
Field mx:Float,my:Float
Field speed:Float
Field kind:String 'coal,metal,gold,rock
Field removetime:Int=2000
Field time:Int
Method New(x:Int,y:Int,kind:String)
Self.px = x
Self.py = y
Self.kind = kind
End Method
Method update()
' if to long in game then remove
time+=1
If time>removetime Then deleteme=True
' get player center position
Local pcx:Int=myplayer.px + (myplayer.pw/2)
Local pcy:Int=myplayer.py + (myplayer.ph/2)
'if distance closest then add to player inventory
If distance(pcx,pcy,px,py) < 10 Then
' code to add to player inventory here...
deleteme = True
End If
' gravity
If mymap.mapcollide(px,py+Ceil(my)+2,w,h) = False Then
If my<3 Then my+=.1
Else
my=0
End if
' if in range then move to player
If distance(pcx,pcy,px,py) < 150 Then
angle = getangle(px,py,pcx,pcy)
mx = Cos(angle)
my = Sin(angle)
Else
mx=0
End if
' floor collision
If mymap.mapcollide(px,py+1,w,h) = True Then
my=-1
End If
' ceiling collision
If mymap.mapcollide(px,py-1,w,h) = True Then
my=1
End If
' left side collision
If mymap.mapcollide(px-1,py,w,h) = True Then
mx=1
End if
' right side collision
If mymap.mapcollide(px+1,py,w,h) = True Then
mx=-1
End If
' move the item
px += mx
py += my
End Method
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
Function getangle:float(x1:Int,y1:Int,x2:Int,y2:Int)
Return ATan2(y2-y1, x2-x1)
End Function
End Class
'fragmentation things
Class frag
Field px:Float,py:Float 'pixel x and y
Field owner:String
Field angle:Float
Field w:Int,h:Int
Field deleteme:Bool
Field countdown:Int
Field mx:Float
Field my:Float
Field fragspeed:Int
Method New(x:Int,y:Int,owner:String)
Self.owner = owner
Self.px = x
Self.py = y
Self.w = 3
Self.h = 3
fragspeed = Rnd(4,7)
countdown = 100+Rnd(300)
Self.angle = Rnd(TwoPi)
mx = Cos(angle)
my = Sin(angle)
End Method
Method update()
For Local ps:Int=0 Until fragspeed
countdown-=1
If countdown < 0 Then deleteme = True ; Return
'bouncy vertical
If mymap.mapcollide(px,py+2,1,h) Then
my = -my+Rnd(-.1,.1)
Local cnt:Int=0
While mymap.mapcollide(px,py+2,1,h)
py+=my
cnt+=1
If cnt>100 Then Exit
Wend
mx*=.8
my*=.8
If my<0 And my>-0.2 Then my=-0.2
If my>0 And my<.2 Then my=.2
If mx<0 And mx>-0.2 Then mx=-0.2
If mx>0 And mx<.2 Then mx=.2
End If
'bounce horizontally
If mymap.mapcollide(px-w,py,w*2,1) Then
mx = -mx+Rnd(-.1,.1)
Local cnt:Int=0
While mymap.mapcollide(px-w,py,w*2,1)
px+=mx
cnt+=1
If cnt>100 Then Exit
Wend
mx*=.8
my*=.8
If my<0 And my>-0.2 Then my=-0.2
If my>0 And my<.2 Then my=.2
If mx<0 And mx>-0.2 Then mx=-0.2
If mx>0 And mx<.2 Then mx=.2
End If
px += mx
py += my
my+=.005
' frag collision with flying monsters
For Local i:=Eachin myflyingmonster
If distance(i.x*tilewidth+tilewidth/2,i.y*tileheight+tileheight/2,px,py) < 10
i.hp -= 5
If i.hp<=0
i.deleteme = True
myitem.Add(New item(i.px,i.py,"gold"))
End If
deleteme = True
Return
End If
Next
' frag collision with the eggs
If eggcollide(px-1,py-1,w+2,h+2)
deleteme = True
'myitem.Add(New item(px,py,"gold"))
Return
End if
Next
End Method
Method eggcollide:Bool(x:Int,y:Int,w:Int,h:Int)
Local lefttopx:Int =((x)/tilewidth)
Local lefttopy:Int =((y)/tileheight)
Local righttopx:Int =((x+w)/tilewidth)
Local righttopy:Int =((y)/tileheight)
Local leftbottomx:Int =((x)/tilewidth)
Local leftbottomy:Int =((y+h)/tileheight)
Local rightbottomx:Int =((x+w)/tilewidth)
Local rightbottomy:Int =((y+h)/tileheight)
Local x2:Int=-1,y2:Int=-1
If mymap.mapfinal[lefttopx,lefttopy] = mymap.tileegg Then x2=lefttopx;y2=lefttopy
If mymap.mapfinal[righttopx,righttopy] = mymap.tileegg Then x2=righttopx;y2=righttopy
If mymap.mapfinal[leftbottomx,leftbottomy] = mymap.tileegg Then x2=leftbottomx;y2=leftbottomy
If mymap.mapfinal[rightbottomx,rightbottomy] = mymap.tileegg Then x2=rightbottomx;y2=rightbottomy
If x2<>-1
If Rnd(7)<1 Then
mymap.mapfinal[x2,y2] = mymap.tileempty
myitem.Add(New item(x2*tilewidth,y2*tileheight,"gold"))
mymap.updateimage(mymap.mapcanvas)
End If
Return True
End If
Return False
End Method
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
End Class
Class grenade
Field px:Float,py:Float 'pixel x and y
Field slimed:Bool=False
Field angle:Float
Field w:Int,h:Int
Field deleteme:Bool
Field countdown:Int
Field mx:Float
Field my:Float
Method New(x:Int,y:Int,facing:String)
Self.px = x
Self.py = y
Self.w = 6
Self.h = 6
countdown = 500
Select facing
Case "left"
angle=Pi+.3
Case "right"
angle=0-.3
Case "up"
'Print Pi*1.5
angle=Pi*1.55+Rnd(-.2,.2)
Case "down"
angle = Pi/2+Rnd(-.2,.2)
End Select
Self.angle = angle
mx = Cos(angle)
my = Sin(angle)
End Method
Method update()
For Local bulletspeed:Int=0 Until 4
' Life of frags
countdown-=1
If countdown < 0 Then
deleteme = True
Local numfrags:Int=Rnd(6,20)
For Local i:Int=0 Until numfrags
myfrag.AddLast(New frag(px,py,"player"))
next
Return
End If
If slimed=False Then
If slimecollide(px-w,py-h,w*2.5,h*2.5) Then slimed = True ; Continue
'bounc ceiling
If mapcollide(px,py-2,1,h) Then
'slimed=slimecollide(px,py+2,1,h)
'If slimed = True Then Return
my = -my+Rnd(-.1,.1)
Local cnt:Int=0
While mapcollide(px,py-2,1,h)
py+=my
cnt+=1
If cnt>100 Then Exit
Wend
mx*=.8
my*=.8
If my<0 And my>-0.2 Then my=-0.2
If my>0 And my<.2 Then my=.2
If mx<0 And mx>-0.2 Then mx=-0.2
If mx>0 And mx<.2 Then mx=.2
End If
'bouncy vertical
If mapcollide(px,py+2,1,h) Then
'slimed=slimecollide(px,py+2,1,h)
'If slimed = True Then Return
my = -my+Rnd(-.1,.1)
Local cnt:Int=0
While mapcollide(px,py+2,1,h)
py+=my
cnt+=1
If cnt>100 Then Exit
Wend
mx*=.8
my*=.8
If my<0 And my>-0.2 Then my=-0.2
If my>0 And my<.2 Then my=.2
If mx<0 And mx>-0.2 Then mx=-0.2
If mx>0 And mx<.2 Then mx=.2
End If
'bounce horizontally
If mapcollide(px-w,py,w*2,1) Then
'slimed=mapcollide(px-w,py,w*2,1)
'If slimed Then Return
mx = -mx+Rnd(-.1,.1)
Local cnt:Int=0
While mapcollide(px-w,py,w*2,1)
px+=mx
cnt+=1
If cnt>100 Then Exit
Wend
mx*=.8
my*=.8
If my<0 And my>-0.2 Then my=-0.2
If my>0 And my<.2 Then my=.2
If mx<0 And mx>-0.2 Then mx=-0.2
If mx>0 And mx<.2 Then mx=.2
End If
' update the frag location
px += mx
py += my
my+=.005
End if
Next
End Method
Method mapcollide:Bool(x:Int,y:Int,w:Int,h:Int)
Local mmw:Int=mymap.mmw
Local mmh:Int=mymap.mmh
Local lefttopx:Int =((x)/tilewidth)
Local lefttopy:Int =((y)/tileheight)
Local righttopx:Int =((x+w)/tilewidth)
Local righttopy:Int =((y)/tileheight)
Local leftbottomx:Int =((x)/tilewidth)
Local leftbottomy:Int =((y+h)/tileheight)
Local rightbottomx:Int =((x+w)/tilewidth)
Local rightbottomy:Int =((y+h)/tileheight)
If lefttopx < 0 Or lefttopx >= mmw Then Return True
If lefttopy < 0 Or lefttopy >= mmh Then Return True
If righttopx < 0 Or righttopx >= mmw Then Return True
If righttopy < 0 Or righttopy >= mmh Then Return True
If leftbottomx < 0 Or leftbottomx >= mmw Then Return True
If leftbottomy < 0 Or leftbottomy >= mmh Then Return True
If rightbottomx < 0 Or rightbottomx >= mmw Then Return True
If rightbottomy < 0 Or rightbottomy >= mmh Then Return True
If mymap.mapfinal[lefttopx,lefttopy] <> mymap.tileempty Then Return True
If mymap.mapfinal[righttopx,righttopy] <> mymap.tileempty Then Return True
If mymap.mapfinal[leftbottomx,leftbottomy] <> mymap.tileempty Then Return True
If mymap.mapfinal[rightbottomx,rightbottomy] <> mymap.tileempty Then Return True
If mygrowslime.map[lefttopx*2,lefttopy*2] = mymap.tileslime Then Return True
If mygrowslime.map[righttopx*2,righttopy*2] = mymap.tileslime Then Return True
If mygrowslime.map[leftbottomx*2,leftbottomy*2] = mymap.tileslime Then Return True
If mygrowslime.map[rightbottomx*2,rightbottomy*2] = mymap.tileslime Then Return True
Return False
End Method
Method slimecollide:Bool(x:Int,y:Int,w:Int,h:Int)
Local mmw:Int=mymap.mmw
Local mmh:Int=mymap.mmh
Local lefttopx:Int =((x)/tilewidth)
Local lefttopy:Int =((y)/tileheight)
Local righttopx:Int =((x+w)/tilewidth)
Local righttopy:Int =((y)/tileheight)
Local leftbottomx:Int =((x)/tilewidth)
Local leftbottomy:Int =((y+h)/tileheight)
Local rightbottomx:Int =((x+w)/tilewidth)
Local rightbottomy:Int =((y+h)/tileheight)
' If lefttopx < 0 Or lefttopx >= mmw Then Return True
' If lefttopy < 0 Or lefttopy >= mmh Then Return True
' If righttopx < 0 Or righttopx >= mmw Then Return True
' If righttopy < 0 Or righttopy >= mmh Then Return True
' If leftbottomx < 0 Or leftbottomx >= mmw Then Return True
' If leftbottomy < 0 Or leftbottomy >= mmh Then Return True
' If rightbottomx < 0 Or rightbottomx >= mmw Then Return True
' If rightbottomy < 0 Or rightbottomy >= mmh Then Return True
'
If mygrowslime.map[lefttopx*2,lefttopy*2] = mymap.tileslime Then Return True
If mygrowslime.map[righttopx*2,righttopy*2] = mymap.tileslime Then Return True
If mygrowslime.map[leftbottomx*2,leftbottomy*2] = mymap.tileslime Then Return True
If mygrowslime.map[rightbottomx*2,rightbottomy*2] = mymap.tileslime Then Return True
Return False
End Method
Function getangle:float(x1:Int,y1:Int,x2:Int,y2:Int)
Return ATan2(y2-y1, x2-x1)
End Function
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
End Class
Class bullet
Field px:Float,py:Float
Field mx:Float,my:Float
Field angle:Float
Field w:Int,h:Int
Field deleteme:Bool
Field countdown:Int
Field owner:String
Method New(x:Int,y:Int,angle:Float,owner:String,kind:String)
Self.owner = owner
Self.px = x
Self.py = y
Self.w = 2
Self.h = 2
mx = 1
my = 1
If kind="shotgun"
w = 2
h = 2
mx = Rnd(.9,1)
my = Rnd(.9,1)
End If
countdown = 500
Self.angle = angle
End Method
Method update()
For Local bulletspeed:Int=0 Until 5
countdown-=1
If countdown < 0 Then deleteme = True ; Return
If mymap.mapcollide(px,py,w,h) Then deleteme = True
' Collision with bullet and flying monster
For Local i:=Eachin myflyingmonster
If distance(px,py,i.x*tilewidth,i.y*tileheight) < tilewidth Then
i.hp -= 1
deleteme = True
If i.hp <= 0
i.deleteme = True
myitem.Add(New item(px,py,"gold"))
Return
End if
Return
End If
Next
' Collision with bullet and egg
If eggcollide(px,py,w,h) Then
deleteme = True
myitem.Add(New item(px,py,"gold"))
Return
End If
px += Cos(angle)*mx
py += Sin(angle)*my
Next
End Method
Method eggcollide:Bool(x:Int,y:Int,w:Int,h:Int)
Local lefttopx:Int =((x)/tilewidth)
Local lefttopy:Int =((y)/tileheight)
Local righttopx:Int =((x+w)/tilewidth)
Local righttopy:Int =((y)/tileheight)
Local leftbottomx:Int =((x)/tilewidth)
Local leftbottomy:Int =((y+h)/tileheight)
Local rightbottomx:Int =((x+w)/tilewidth)
Local rightbottomy:Int =((y+h)/tileheight)
Local x2:Int=-1,y2:Int=-1
If mymap.mapfinal[lefttopx,lefttopy] = mymap.tileegg Then x2=lefttopx;y2=lefttopy
If mymap.mapfinal[righttopx,righttopy] = mymap.tileegg Then x2=righttopx;y2=righttopy
If mymap.mapfinal[leftbottomx,leftbottomy] = mymap.tileegg Then x2=leftbottomx;y2=leftbottomy
If mymap.mapfinal[rightbottomx,rightbottomy] = mymap.tileegg Then x2=rightbottomx;y2=rightbottomy
If x2<>-1
If Rnd(7)<1 Then
mymap.mapfinal[x2,y2] = mymap.tileempty
mymap.updateimage(mymap.mapcanvas)
End If
Return True
End If
Return False
End Method
Function getangle:float(x1:Int,y1:Int,x2:Int,y2:Int)
Return ATan2(y2-y1, x2-x1)
End Function
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
End Class
'uses myflyingmonster
Class turret
Field deleteme:Bool
Field mapx:Int,mapy:Int
Field tw:Int,th:Int 'tilewidht and height
Field owner:String="town"
Method New()
findstartposition()
tw = tilewidth
th = tileheight
End Method
Method update()
If Rnd(20)<10 Then Return
For Local i:=Eachin myflyingmonster
If distance(mapx,mapy,i.x,i.y) > 15 Then Continue
'if distance is close by
If clearshot(mapx*tw,mapy*th,getangle(mapx*tw,mapy*th,i.x*tw,i.y*th)+Rnd(-.2,.2))
mybullet.AddLast(New bullet(mapx*tw,mapy*th,getangle(mapx*tw,mapy*th,i.x*tw,i.y*th)+Rnd(-.2,.2),"town","turret"))
Endif
Next
End Method
' Check if we are not recklessly shooting into a solid tile
Method clearshot:Bool(x:Float,y:Float,angle:Float)
For Local i:Int=0 Until 100
x+=Cos(angle)
y+=Sin(angle)
If mymap.mapcollide(x,y,3,3) Then Return False
Next
Return True
End Method
Method findstartposition()
' position on the left side
For Local y:Int=2 Until mymap.mmh-2
For Local x:Int=2 Until mymap.mmw-2
If mymap.mapfinal[x,y] = mymap.tileempty
If mymap.mapfinal[x+1,y] = mymap.tileempty
If mymap.mapfinal[x+1,y+1] = mymap.tileempty
If mymap.mapfinal[x,y+1] = mymap.tilesolid
If mymap.mapfinal[x-1,y] = mymap.tileempty
mymap.mapfinal[x-1,y] = mymap.tileturret
mapx = x - 1
mapy = y
Return
Elseif mymap.mapfinal[x+5,y] = mymap.tileempty
mymap.mapfinal[x+5,y] = mymap.tileturret
mapx = x + 5
mapy = y
Return
Else ' no place for turret
deleteme = True
Return
End if
End If
End If
End If
End If
Next
Next
End Method
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
Function getangle:float(x1:Int,y1:Int,x2:Int,y2:Int)
Return ATan2(y2-y1, x2-x1)
End Function
End Class
Class growslime
Field map:Int[,]
Field w:Float,h:Float
Field tw:Float,th:Float
Field openx:Stack<Int>
Field openy:Stack<Int>
Field slimetile:Int=10
Field slimestartx:Int,slimestarty:Int
Method New()
w = mymap.mmw * 2
h = mymap.mmh * 2
tw = mymap.tw * 2
th = mymap.th * 2+2
map = New Int[w,h]
'copy the map from the game into this map
For Local y:Int=0 Until mymap.mmh
For Local x:Int=0 Until mymap.mmw
For Local y2:Int=0 Until 2
For Local x2:Int=0 Until 2
map[(x*2)+x2,(y*2)+y2] = mymap.mapfinal[x,y]
Next
Next
Next
Next
'create the active slime list
openx = New Stack<Int>
openy = New Stack<Int>
findslimestartpos()
openx.Push(slimestartx)
openy.Push(slimestarty)
map[slimestartx,slimestarty]=slimetile
End Method
Method findslimestartpos()
For Local y:Int=h-1 To 0 Step -1
For Local x:Int=0 Until w
If map[x,y] = 1 Then
slimestartx = x
slimestarty = y
Return
End If
Next
Next
End Method
Method update(speed:String)
Local freq:Int
If speed = "slow" Then freq = 120 Else freq = 20
' Tentacle check
' Every now and then check if active slime part is in range
' of player/enemy and then check if path is clear and if so
' then initiate tentacle.
' Grab player
For Local i:Int=0 Until openx.Length
Local x1:Int = openx.Get(i) * (myplayer.tw/2)
Local y1:Int = openy.Get(i) * (myplayer.th/2)
Local pcx:Int=myplayer.px+(myplayer.pw/2)
Local pcy:Int=myplayer.py+(myplayer.ph/2)
' grab monster
If Rnd(300) < 2
For Local i2:=Eachin myflyingmonster
Local mcx:Int=i2.px+(i2.w/2)
Local mcy:Int=i2.py+(i2.h/2)
If distance(mcx,mcy,x1,y1) < 200
Local an:Float = getangle(x1,y1,mcx,mcy)
Local mx:Float = Cos(an)
Local my:Float = Sin(an)
Local x2:Float = x1
Local y2:Float = y1
For Local i3:Int=0 Until 200
x2 += mx
y2 += my
If mymap.mapcollide(x2,y2,i2.w,i2.h) = True Then
Exit
End If
If distance(x2,y2,mcx,mcy) < 50 Then
mytentacle.Add(New tentacle(x1,y1,mcx,mcy))
Exit
End If
Next
Exit
End If
Next
End if
'grab player
If Rnd(300)<2 And distance(pcx,pcy,x1,y1) < 200
Local an:Float = getangle(x1,y1,pcx,pcy)
Local mx:Float = Cos(an)
Local my:Float = Sin(an)
Local x2:Float = x1
Local y2:Float = y1
For Local i2:Int=0 Until 200
x2 += mx
y2 += my
If mymap.mapcollide(x2,y2,myplayer.pw,myplayer.ph) = True Then
Exit
End If
If distance(x2,y2,pcx,pcy) < 50 Then
mytentacle.Add(New tentacle(x1,y1,pcx,pcy))
Exit
End If
Next
Exit
End if
Next
' Expand Slime
For Local i:Int=0 Until openx.Length
If Rnd(freq) > 2 Then Continue
Local x2:Int=openx.Get(i)
Local y2:Int=openy.Get(i)
'bottom bleft or bright first
Local r:Int=Rnd(0,6)
If r=0 And y2+1<h And map[x2,y2+1] = 1 Then addslime(x2,y2+1) ; Continue
If r=1 And x2-1 >=0 And y2+1 <h And map[x2-1,y2+1] = 1 Then addslime(x2-1,y2+1) ; Continue
If r=2 And y2+1<h And map[x2+1,y2+1] = 1 Then addslime(x2+1,y2+1) ; Continue
' left Or right Then
r = Rnd(0,2)
If r=0 And x2-1>=0 And map[x2-1,y2] = 1 Then addslime(x2-1,y2) ; Continue
If r=1 And x2+1<w And map[x2+1,y2] = 1 Then addslime(x2+1,y2) ; Continue
' up lup and rup
r = Rnd(0,23)
If r=0 And y2-1>=0 And map[x2,y2-1] = 1 Then addslime(x2,y2-1);Continue
If r=1 And x2-1>=0 And y2-1>=0 And map[x2-1,y2-1] = 1 Then addslime(x2-1,y2-1);Continue
If r=2 And x2+1<w And y2-1>=0 And map[x2+1,y2-1] = 1 Then addslime(x2+1,y2-1);Continue
Next
Local tx:Stack<Int> = New Stack<Int>
Local ty:Stack<Int> = New Stack<Int>
' Remove Obsolete slime
For Local i:Int=0 Until openx.Length
Local cnt:Int=0
For Local y:Int=-1 To 1
For Local x:Int=-1 To 1
Local x2:Int=openx.Get(i)+x
Local y2:Int=openy.Get(i)+y
If x2<0 Or y2<0 Or x2>=w Or y2>=h Then
cnt+=1
Continue
End If
If map[x2,y2] = 10 Then cnt+=1
if map[x2,y2] = 0 Then cnt+=1
'if map[x2,y2] = 1 Then cnt+=1
'If map[x2,y2] <> 1 Then cnt+=1
Next
Next
If cnt=9 Then
'openx.Remove(i)
'openy.Remove(i)
Else
tx.Push(openx.Get(i))
ty.Push(openy.Get(i))
End If
Next
openx = New Stack<Int>
openy = New Stack<Int>
For Local i:Int=0 Until tx.Length
openx.Push(tx.Get(i))
openy.Push(ty.Get(i))
Next
End Method
Method addslime(sx:Int,sy:Int)
openx.Push(sx)
openy.Push(sy)
map[sx,sy] = slimetile
End Method
Method drawmap(canvas:Canvas)
' Draw the solid slimes ()
For Local y:Int=0 Until h
For Local x:Int=0 Until w
Local x1:Float=x*mymap.tw/2
Local y1:Float=y*mymap.th/2
If map[x,y] = slimetile
canvas.Color = Color.Green
canvas.DrawRect(x1,y1,mymap.tw/2,mymap.th/2)
End If
Next
Next
End Method
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
Function getangle:float(x1:Int,y1:Int,x2:Int,y2:Int)
Return ATan2(y2-y1, x2-x1)
End Function
End Class
Class menuselect
Field image:Image
Field icanvas:Canvas
Field m:map
Field index:Int=0
Method New()
image=New Image(800,600)
image.Handle=New Vec2f( 0,0 )
icanvas=New Canvas( image )
icanvas.Clear(Color.Black)
Local x:Int=50
Local y:Int=100
Local w:Int=150
Local h:Int=150
Local sw:Int=150
Local sh:Int=150
SeedRnd(1)
m = New map(sw,sh,w,h)
drawmap(icanvas,x,y,w,h)
x = 200
y = 100
w = 250
h = 250
sw = 250
sh = 250
m = New map(sw,sh,w,h)
drawmap(icanvas,x,y,w,h)
x = 450
y = 100
w = 300
h = 300
sw = 300
sh = 300
m = New map(sw,sh,w,h)
drawmap(icanvas,x,y,w,h)
icanvas.Flush()
End Method
Method update()
If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()
If Keyboard.KeyReleased(Key.Left) Then index-=1
If Keyboard.KeyReleased(Key.Right) Then index+=1
If index<0 Then index = 0
If index>2 Then index = 2
If Keyboard.KeyReleased(Key.Enter) Then
Select index
Case 0
mapwidth = 150
mapheight = 150
Case 1
mapwidth = 250
mapheight = 250
Case 2
mapwidth = 300
mapheight = 300
End Select
resetmap(screenwidth,screenheight)
gamestate="play"
End If
End Method
Method draw(canvas:Canvas)
canvas.DrawImage(image,0,0)
canvas.DrawText("Use Cursor left and Right to select Enter to Play",0,0)
canvas.PushMatrix()
canvas.Scale(2,2)
canvas.DrawText("Select Difficulty",400/2,50/2,.5,.5)
canvas.PopMatrix()
canvas.Color = Color.Yellow
Local x:int=50
Local y:Int=100
Local w:Int=150
Local h:int=150
If index = 0 Then
mydrawrect(canvas,x,y,w,h)
mydrawrect(canvas,x+1,y+1,w-2,h-2)
End If
x = 200
y = 100
w = 250
h = 250
If index = 1 Then
mydrawrect(canvas,x,y,w,h)
mydrawrect(canvas,x+1,y+1,w-2,h-2)
End If
x = 450
y = 100
w = 300
h = 300
If index = 2 Then
mydrawrect(canvas,x,y,w,h)
mydrawrect(canvas,x+1,y+1,w-2,h-2)
End If
End Method
Method drawmap(canvas:Canvas,x:Int,y:Int,w:Int,h:Int)
For Local y1:Int=0 Until h
For Local x1:Int=0 Until w
Select m.mapfinal[x1,y1]
Case 1
canvas.Color = New Color(1,1,1)
canvas.DrawRect(x1+x,y1+y,1,1)
End Select
Next
Next
End Method
Method mydrawrect(canvas:Canvas,x:Int,y:Int,w:Int,h:Int)
canvas.DrawLine(x,y,x+w,y)
canvas.DrawLine(x+w,y,x+w,y+h)
canvas.DrawLine(x,y,x,y+h)
canvas.DrawLine(x,y+h,x+w,y+h)
End Method
End Class
Class player
Field hp:Int=100
Field mineitem:String[] = New String[]("gold","coal","metal","rock")
Field regularmode:Bool=True
Field jump:Bool=False
Field incy:Float=0
Field movespeed:Float
Field px:Float,py:Float
Field pmx:Float,pmy:Float
Field ptx:Float,pty:Float
Field mox:Int,moy:Float
Field pw:Int,ph:Int
Field mcx:Int,mcy:Int,mpx:Int,mpy:Int ' scroll coordinates
Field maptileswidth:Int,maptilesheight:Int
Field tw:Float=tilewidth
Field th:float=tileheight
Field facing:String="right"
Field gtkd:Bool=False 'grenade thrown key down
Field sfkd:Bool=False 'shogun fire key down
Field minedelay:Int
Method New()
movespeed = 1
px = tilewidth*20
py = tileheight*14
pw = tilewidth
ph = tileheight
maptileswidth = screenwidth / tw
maptilesheight = screenheight / th
mcx=0
mcy=0
End Method
Method updateplayercontrols()
'
pmx = px-(mcx*tw)
pmy = py-(mcy*th)
ptx = px/tw
pty = py/th
If playerladdercollision(px,py)
jump=False
laddermode()
movespeed = 1
Else
For Local i:=0 Until 2
playergravity()
Next
End If
movespeed +=.1
If movespeed > 4 Then movespeed = 4
' If no movement left and right up and down then slow down
' movement
If Keyboard.KeyDown(Key.Right) = False
If Keyboard.KeyDown(Key.Left) = False
If Keyboard.KeyDown(Key.Up) = False
If Keyboard.KeyDown(Key.Down) = False
movespeed = 1
End If
End If