-
Notifications
You must be signed in to change notification settings - Fork 8
/
2d Platformer RPG 05.monkey2
4350 lines (4095 loc) · 114 KB
/
2d Platformer RPG 05.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
' short list
' add - hp damage effect (added - test)
' add - power bar (added)
' add - town shop
' add - player inventory (first draft - collected items get added)
' add - towns people
' Bug - flying/walking monster attacking player can go through laser wall (first fix - more testing)
' Bug walking monster standing still
' check laser wall ontop of eggs situation
#Import "<std>"
#Import "<mojo>"
Using std..
Using mojo..
Global developmode:Bool=False
Global theversion:String="Version 8-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 maxwalkingmonsters: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
Global maxplayeritems:Int=10
'
' The town buildings
'
Class building
Field image:Image
Field icanvas:Canvas
Field px:Int,py:Int
Field totalwidth:Int
Field blockhouse:Int=1
Field blockdoor:Int=2
' For collision (enter/flee in home/shop)
Field doorx:Int,doory:Int
Field doorwidth:Int,doorheight:Int
Field blocksmallwindow:Int=3
Field blockwidewindow:Int=4
Field blockcrateleft:Int=5
Field blockcrateright:Int=6
Field blockiceboxleft:Int=7
Field blockiceboxright:Int=7
Field blocktoiletleft:Int=8
Field blocktoiletright:Int=9
Field blockfrontcrate:Int=10
Field blockrooftop:Int=11
Field blockchimney:Int=12
Field blockshopsign:Int=13
Field houselayer:Int[]=New Int[3] 'base blocks
Field rooftoplayer:Int[]=New Int[3]
Field chimneylayer:Int[]=New Int[3]
Field doorlayer:Int[] = New Int[3]
Field windowlayer:Int[] = New Int[3] '011010'
Field housesidelayer:Int[] = New Int[2]
Field shopsignlayer:Int[] = New Int[3]
Field frontlayer:Int[] = New Int[6]
Field bw:Float,bh:Float
Method New(x:Int,y:Int,w:Int,bw:Int,bh:Int,isshop:Bool)
Self.bw = bw
Self.bh = bh
image = New Image(bw*(w+2),bh*2)
image.Handle=New Vec2f( 0,0 )
icanvas = New Canvas(image)
px = x
py = y
totalwidth = w
makehouse(w,isshop)
Local opx:Int=px
Local opy:Int=py
px=bw
py=bh
bufferdraw(icanvas)
px = opx
py = opy-bh
icanvas.Flush()
End Method
Method makehouse(w:Int,isshop:Bool)
' Make the base house blocks
For Local i:Int=0 Until w
houselayer[i] = blockhouse
Next
' Create the items at the side of the houses
If Rnd(10)<5
' add to which side(s)
Local sides:String="left"
If Rnd(10)<3 Then sides="right"
If Rnd(10)<3 Then sides="both"
' Add items to side(s)
If sides="left" Or sides="both" Then
housesidelayer[0] = blocktoiletleft
If Rnd(10)<3 Then
housesidelayer[0] = blockcrateleft
End If
If Rnd(10)<3 Then
housesidelayer[0] = blockiceboxleft
End If
Endif
If sides="right" Or sides="both" Then
housesidelayer[1] = blocktoiletright
If Rnd(10)<3 Then
housesidelayer[1] = blockcrateright
End If
If Rnd(10)<3 Then
housesidelayer[1] = blockiceboxright
End If
Endif
End If
'Create the crates at the front of the house
For Local i:Int= 0 Until (w*2)-2
If Rnd(10)<2 Then
frontlayer[i] = blockfrontcrate
End If
Next
'Create windows
Select w
Case 2
windowlayer[0] = blocksmallwindow
Case 3
windowlayer[0] = blockwidewindow
End Select
' create door
Select w
Case 1
doorlayer[0] = blockdoor
If isshop Then shopsignlayer[0] = blockshopsign
Case 2
doorlayer[1] = blockdoor
If isshop Then shopsignlayer[1] = blockshopsign
Case 3
doorlayer[2] = blockdoor
If isshop Then shopsignlayer[2] = blockshopsign
End Select
' rooftop
Select w
Case 1
rooftoplayer[0] = blockrooftop
Case 2
rooftoplayer[0] = blockrooftop
rooftoplayer[1] = blockrooftop
Case 3
rooftoplayer[0] = blockrooftop
rooftoplayer[1] = blockrooftop
rooftoplayer[2] = blockrooftop
End Select
' chimney
Select w
Case 1
chimneylayer[0] = blockchimney
Case 2
chimneylayer[Rnd(0,2)] = blockchimney
Case 3
chimneylayer[Rnd(0,3)] = blockchimney
End Select
End Method
Method bufferdraw(canvas:Canvas)
canvas.Clear(New Color(0,0,0,0))
' Draw the house blocks
For Local i:Int=0 Until 3
If houselayer[i] = blockhouse Then
canvas.Color = New Color((1.0/255.0)*150,(1.0/255.0)*140,(1.0/255.0)*150)
canvas.DrawRect(px+(i*bw),py,bw+1,bh)
canvas.Color = New Color((1.0/255.0)*200,(1.0/255.0)*200,(1.0/255.0)*200)
canvas.DrawRect( px+(i*bw),py,bw,bh)
'SetColor 230,230,230
'shadow top
canvas.Color = New Color((1.0/255.0)*60,(1.0/255.0)*60,(1.0/255.0)*60)
canvas.DrawRect( px+(i*bw),py,bw,bh/15)
'shadow bottom
canvas.Color = New Color((1.0/255.0)*150,(1.0/255.0)*150,(1.0/255.0)*150)
canvas.DrawRect( px+(i*bw),py+bh/1.1,bw,bh/8)
'highlight left
If i=0
canvas.Color = New Color((1.0/255.0)*220,(1.0/255.0)*220,(1.0/255.0)*220)
canvas.DrawRect( px+(i*bw),py,1,bh/3)
Endif
End If
Next
' Draw the rooftop
For Local i:Int=0 Until 3
If rooftoplayer[i] = blockrooftop Then
'SetColor 200,100,100
canvas.Color = New Color((1.0/255.0)*170,(1.0/255.0)*70,(1.0/255.0)*60)
canvas.DrawRect( px+(i*bw),py-(bh/1.5),bw,bh-(bh/3))
' Bottom shade
canvas.Color = New Color((1.0/255.0)*130,(1.0/255.0)*50,(1.0/255.0)*30)
'SetColor 200,100,100
canvas.DrawRect( px+(i*bw),py-(bh/8),bw,bh/8)
' top shade
canvas.Color = New Color((1.0/255.0)*190,(1.0/255.0)*90,(1.0/255.0)*80)
canvas.DrawRect( px+(i*bw),py-(bh/1.5),bw,1)
' top shade
If i=0
'horizontal
canvas.Color = New Color((1.0/255.0)*220,(1.0/255.0)*120,(1.0/255.0)*110)
canvas.DrawRect( px+(i*bw),py-(bh/1.5),bw/2,1)
'vertical
canvas.Color = New Color((1.0/255.0)*200,(1.0/255.0)*100,(1.0/255.0)*100)
canvas.DrawRect( px+(i*bw),py-(bh/1.5),1,bh/3)
End If
End If
Next
' Draw the chimney
For Local i:Int=0 Until 3
If chimneylayer[i] = blockchimney Then
canvas.Color = New Color((1.0/255.0)*100,(1.0/255.0)*100,(1.0/255.0)*100)
canvas.DrawRect( px+(i*bw)+(bw/4),py-(bh/1.2),bw/2.5,bh/4)
'chimney highlight
canvas.Color = New Color((1.0/255.0)*140,(1.0/255.0)*130,(1.0/255.0)*120)
canvas.DrawRect( px+(i*bw)+(bw/4),py-(bh/1.2),bw/6,1)
End If
Next
'Draw the windows
For Local i:Int=0 Until 3
If windowlayer[i] = blocksmallwindow
canvas.Color = New Color((1.0/255.0)*0,(1.0/255.0)*100,(1.0/255.0)*200)
canvas.DrawRect( px+(i*bw)+(bw/3),py+(bh/5),bw-(bw/3),bh-(bh/2.5))
' light bottom
canvas.Color = New Color((1.0/255.0)*0,(1.0/255.0)*115,(1.0/255.0)*210)
canvas.DrawRect( px+(i*bw)+(bw/3),py+(bh/2),bw-(bw/3),(bh/3.3))
' dark bottom
canvas.Color = New Color((1.0/255.0)*180,(1.0/255.0)*125,(1.0/255.0)*20)
canvas.DrawRect( px+(i*bw)+(bw/3),py+(bh*.7),bw-(bw/3),(bh/8.3))
End If
If windowlayer[i] = blockwidewindow
canvas.Color = New Color((1.0/255.0)*0,(1.0/255.0)*100,(1.0/255.0)*200)
canvas.DrawRect( px+(i*bw)+(bw/3),py+(bh/5),(bw*2)-(bw/3),bh-(bh/2.5))
'light bottom
canvas.Color = New Color((1.0/255.0)*0,(1.0/255.0)*115,(1.0/255.0)*210)
canvas.DrawRect( px+(i*bw)+(bw/3),py+(bh/2),(bw*2)-(bw/3),(bh/3.3))
'dark bottom
canvas.Color = New Color((1.0/255.0)*180,(1.0/255.0)*125,(1.0/255.0)*20)
canvas.DrawRect( px+(i*bw)+(bw/3),py+(bh*.7),(bw*2)-(bw/3),(bh/8.3))
End If
Next
' Draw the door
For Local i:Int=0 Until 3
If doorlayer[i] = blockdoor
canvas.Color = New Color((1.0/255.0)*100,(1.0/255.0)*50,(1.0/255.0)*50)
If shopsignlayer[i] = blockshopsign
canvas.Color = New Color((1.0/255.0)*250,(1.0/255.0)*200,(1.0/255.0)*50)
End If
canvas.DrawRect( px+(i*bw)+(bw/5),py+(bh/5),bw-(bw/2),bh-(bh/4))
' doorknob
canvas.Color = New Color((1.0/255.0)*200,(1.0/255.0)*210,(1.0/255.0)*210)
canvas.DrawRect( px+(i*bw)+(bw/2),py+(bh/1.7),(bw/9),(bh/9))
'numberplate
canvas.Color = New Color((1.0/255.0)*200,(1.0/255.0)*250,(1.0/255.0)*250)
canvas.DrawRect( px+(i*bw)+(bw/1.3),py+(bh/3),(bw/9),(bh/9))
canvas.Color = New Color((1.0/255.0)*10,(1.0/255.0)*50,(1.0/255.0)*50)
canvas.DrawRect( px+(i*bw)+(bw/1.25),py+(bh/2.8),(bw/18),(bh/14))
End If
Next
' Draw the sides
If housesidelayer[0] = blocktoiletleft Then drawtoilet(canvas,px,py,bw,bh,"left")
If housesidelayer[1] = blocktoiletright Then drawtoilet(canvas,px,py,bw,bh,"right")
If housesidelayer[0] = blockcrateleft Then drawsidecrate(canvas,px,py,bw,bh,"left")
If housesidelayer[1] = blockcrateright Then drawsidecrate(canvas,px,py,bw,bh,"right")
If housesidelayer[0] = blockiceboxleft Then drawsideicebox(canvas,px,py,bw,bh,"left")
If housesidelayer[1] = blockiceboxright Then drawsideicebox(canvas,px,py,bw,bh,"right")
'Draw the crates at the front of the house
For Local i:Int=0 Until (totalwidth*2)-2
If frontlayer[i] = blockfrontcrate
canvas.Color = New Color((1.0/255.0)*100,(1.0/255.0)*50,(1.0/255.0)*50)
canvas.DrawRect( px+((bw/2)*i),py+bh/1.2,bw/4,bh/6.4)
End If
Next
'Draw the shop sign
For Local i:Int=0 Until totalwidth
If shopsignlayer[i] = blockshopsign
canvas.Color = New Color((1.0/255.0)*255,(1.0/255.0)*40,(1.0/255.0)*30)
Local x:Int=px+(bw*i)-bw/8
Local y:Int=py-bh/5
canvas.DrawRect( x,y,bw*1.2,bh/3)
canvas.Color = New Color((1.0/255.0)*255,(1.0/255.0)*255,(1.0/255.0)*255)
'DrawText "Shop X",x+5,y+5
canvas.DrawRect( x+bh/10,y+bh/12,4,4)
End If
Next
End Method
Method drawtoilet(canvas:Canvas,x:Int,y:Int,w:Int,h:Int,side:String)
If side = "left"
Local ltx:Float=x-w/2
Local lty:Float=y+(h/4)
Local rtx:Float=ltx+(w/2)
Local rty:Float=lty
Local lbx:Float=ltx
Local lby:Float=lty+(h-h/4)
Local rbx:Float=ltx+(w/2)
Local rby:Float=lby
Local toil:Float[] = New Float[8]
toil[0] = ltx
toil[1] = lty-(h/6)
toil[2] = rtx
toil[3] = rty
toil[4] = rbx
toil[5] = rby
toil[6] = lbx
toil[7] = lby
canvas.Color = New Color( (1.0/255.0)*150,(1.0/255.0)*50,(1.0/255.0)*50 )
canvas.DrawPoly(toil)
'canvas.DrawRect( x-w/2,y+10,w/2,h-10
Elseif side="right"
Local ltx:Float=(x)+(totalwidth*w)
Local lty:Float=y+(h/4)
Local rtx:Float=ltx+(w/2)
Local rty:Float=lty
Local rbx:Float=ltx+(w/2)
Local rby:Float=rty+(h-h/4)
Local lbx:Float=ltx
Local lby:Float=rby
canvas.Color = New Color((1.0/255.0)*150,(1.0/255.0)*50,(1.0/255.0)*50)
Local box:Float[] = New Float[8]
box[0] = ltx
box[1] = lty
box[2] = rtx
box[3] = rty-(h/6)
box[4] = rbx
box[5] = rby
box[6] = lbx
box[7] = lby
canvas.DrawPoly(box)
' SetColor 100,50,50
' canvas.DrawRect( (x)+totalwidth*w,y+10,w/2,h-10
End If
End Method
Method drawsidecrate(canvas:Canvas,x:Int,y:Int,w:Int,h:Int,side:String)
If side = "left"
' pipe
canvas.Color = New Color((1.0/255.0)*120,(1.0/255.0)*120,(1.0/255.0)*120)
canvas.DrawRect( x-w/8,y,w/8,h)
'barrel
canvas.Color = New Color((1.0/255.0)*100,(1.0/255.0)*50,(1.0/255.0)*50)
canvas.DrawRect( x-w/3,y+(h/1.5),w/3,h-(h/1.5))
Elseif side="right"
'pipe
canvas.Color = New Color((1.0/255.0)*120,(1.0/255.0)*120,(1.0/255.0)*120)
canvas.DrawRect( (x)+totalwidth*w,y,w/8,h)
'barrel
canvas.Color = New Color((1.0/255.0)*100,(1.0/255.0)*50,(1.0/255.0)*50)
canvas.DrawRect( (x)+totalwidth*w,y+(h/1.5),w/3,h-(h/1.5))
End If
End Method
Method drawsideicebox(canvas:Canvas,x:Int,y:Int,w:Int,h:Int,side:String)
If side = "left"
Local ltx:Float=x-w/2
Local lty:Float=y+(h/1.5)
Local rtx:Float=ltx+(w/2)
Local rty:Float=lty
Local lbx:Float=ltx
Local lby:Float=lty+(h-h/1.5)
Local rbx:Float=ltx+(w/2)
Local rby:Float=lby
canvas.Color = New Color((1.0/255.0)*200,(1.0/255.0)*200,(1.0/255.0)*200)
Local box:Float[] = New Float[8]
box[0] = ltx
box[1] = lty+(h/6)
box[2] = rtx
box[3] = rty
box[4] = rbx
box[5] = rby
box[6] = lbx
box[7] = lby
'canvas.DrawRect( ltx,lty,w/2,h-(h/1.5)
canvas.DrawPoly(box)
Elseif side="right"
Local ltx:Float=(x)+(totalwidth*w)
Local lty:Float=y+(h/1.5)
Local rtx:Float=ltx+(w/2)
Local rty:Float=lty
Local rbx:Float=ltx+(w/2)
Local rby:Float=rty+(h-h/1.5)
Local lbx:Float=ltx
Local lby:Float=rby
canvas.Color = New Color((1.0/255.0)*200,(1.0/255.0)*200,(1.0/255.0)*200)
Local box:Float[] = New Float[8]
box[0] = ltx
box[1] = lty
box[2] = rtx
box[3] = rty+h/6
box[4] = rbx
box[5] = rby
box[6] = lbx
box[7] = lby
canvas.DrawPoly(box)
'canvas.DrawRect( (x)+totalwidth*w,y+(h/1.5),w/2,h-(h/1.5)
End If
End Method
Method draw(canvas:Canvas,x:Int,y:Int)
canvas.Color = Color.White
canvas.DrawImage(image,x,y)
End Method
End Class
'
' The player inventory
''
Class inventory
Field px:Int,py:Int
Field pw:Int,ph:Int
Method New()
pw = 450
ph = 240
px = (screenwidth/2)-(pw/2)
py = (screenheight/2)-(ph/2)
End Method
Method update()
If Keyboard.KeyReleased(Key.I) Or Keyboard.KeyReleased(Key.Escape)
gamestate = "play"
Return
End If
End Method
Method draw(canvas:Canvas)
canvas.Clear(Color.Black)
canvas.OutlineMode=OutlineMode.Solid
canvas.OutlineColor = Color.White
canvas.OutlineWidth = 1
'Draw the main window
canvas.Color = Color.Grey
canvas.DrawRect(px,py,pw,ph)
canvas.OutlineMode=OutlineMode.None
'Draw the items and their count
Local cnt:Int=0
For Local y:Int=0 Until 15
For Local x:Int=0 Until 2
If cnt>=myplayer.playeritemnames.Length Then Continue
Local x2:Int=(x*(pw/2))+px
Local y2:Int=y*20+py
canvas.Color = Color.White
canvas.DrawText(myplayer.playeritem[cnt,1],x2,y2)
canvas.DrawText(myplayer.playeritemnames[myplayer.playeritem[cnt,0]],x2+48,y2)
cnt+=1
Next
Next
canvas.DrawText("Press I or escape to return to game",px+(pw/2),py+ph-20,.5,.5)
End method
End Class
'
' Drop a number from a x,y position
'
Class numberfall
Field px:Float,py:Float
Field number:Int
Field col:Color
Field mx:Float
Field my:Float
Field time:Int ' if 0 then remove
Field deleteme:Bool
Method New(x:Int,y:Int,number:Int,col:Color)
Self.px = x
Self.py = y
Self.number = number
Self.col = col
Self.time = 80
mx = Rnd(-1,1)
my = -2
End Method
Method update()
my += .17
px += mx
py += my
time-=1
If time<=0 Then deleteme = True
End Method
End Class
'
' These are the walking monsters that
' hatch from the eggs. They do not lay eggs
' themselfs but guard the other nearby eggs.
'
Class walkingmonster
Field px:float,py:Float 'pixel position (0-width)
Field sx:Float,sy:Float 'movement speed
Field x:Int,y:Int 'tile x and y position
Field w:Float,h:Float
Field hp:Int 'hitpoints
Field hpmax:Int
Field deleteme:Bool
Field state:String
Field substate:String
Field jx:Float
Field jy:Float
Field oldpy:Float 'old py coordinate
Method New(x:Int,y:Int)
Self.x = x
Self.y = y
Self.w = tilewidth
Self.h = tileheight
px = x*w
py = y*h
hp = Rnd(10,30)
hpmax = hp
'set the movement speed
sx = Rnd(0.3,3)
' sy = sx
state="hatched"
End Method
Method update()
' If laserwait>0 Then laserwait-=1
' Damage to player
If developmode = False
If distance(myplayer.px+(myplayer.pw/2),myplayer.py+(myplayer.ph/2),px+(w/2),py+(h/2)) < tilewidth
myplayer.hp -= 2
mynumberfall.Add(New numberfall(myplayer.px+(myplayer.pw/2),myplayer.py,2,Color.Red))
If myplayer.hp < 0 Then gamestate = "select"
End If
End If
If state<>"attack"
'If Rnd(10)<1 Print Millisecs() + "sx : " + sx
If px < x*w Then px += sx
If px > x*w Then px -= sx
' If py < y*h Then py += sy
' If py > y*h Then py -= sy
If laserwall() Then changedirection()
If distance(px,py,x*w,y*h) > 8 Then Return
Else
If substate = "jump"
If mymap.mapcollide(px+jx,py,w,h) = False
px += jx
Else
'substate = "finishjump"
End If
If mymap.mapcollide(px,py+jy,w,h-1) = False
py += jy
Else
x = myplayer.px / tilewidth
y = myplayer.py / tileheight
substate = "finishjump"
Return
End If
jy += .1
x = px / tilewidth
y = py / tileheight
End If
End If
Select state
Case "hatched"
state="roam"
substate="left"
Case "attack"
Select substate
Case "setjump"
If myplayer.py < py Then substate="finishjump"
If myplayer.py > py+3 Then substate="finishjump"
Local d:Int=distance(myplayer.px,myplayer.py,px,py)
If px < myplayer.px Then jx = d/35 Else jx = -d/35
If distance(px,py,myplayer.px,myplayer.py) > 50 Then jy = -2
substate = "jump"
Return
Case "jump"
'
Local cnt:Int=0
For Local ppy:Int=0 Until 40
If mymap.mapcollide(px+(jx*10),py+ppy,w,h) = False Then cnt+=1
Next
If cnt>30 Then jx=-jx
Case "finishjump"
state = "roam"
x = px / tilewidth
y = py / tileheight
If Rnd(2)< 1 Then substate = "left" Else substate="right"
End Select
Case "roam"
If canattackplayer() Then state="attack" ; substate="setjump"
Select substate
Case "left"
x-=1
'If Rnd(50) < 1 And mymap.mapfinal[x-1,y] = mymap.tileempty And mymap.mapfinal[x-1,y+1] <> mymap.tileempty Then substate="right"
'If Rnd(50) < 1 And cannotgohere(x+1,y) = False And cannotgohere(x+1,y+1)=True Then substate="right"
If cannotgohere(x-1,y) = True Then substate = "right"
If cannotgohere(x-1,y+1) = False Then substate = "right"
'If mymap.mapfinal[x-1,y] <> mymap.tileempty Then substate="right"
'If mymap.mapfinal[x-1,y+1] = mymap.tileempty Then substate="right"
If x<3 Then substate="right"
Case "right"
x+=1
'If Rnd(50) < 1 And cannotgohere(x-1,y) = False And cannotgohere(x-1,y+1)=True Then substate="left"
If cannotgohere(x+1,y) = True Then substate="left"
If cannotgohere(x+1,y+1) = False Then substate="left"
'If Rnd(50) < 1 And mymap.mapfinal[x+1,y] = mymap.tileempty And mymap.mapfinal[x+1,y+1] <> mymap.tileempty Then substate="left"
'If mymap.mapfinal[x+1,y] <> mymap.tileempty Then substate="left"
'If mymap.mapfinal[x+1,y+1] = mymap.tileempty Then substate="left"
If x>mapwidth-3 Then substate="left"
End Select
'gorandleftorright()
End Select
End Method
Method canattackplayer:Bool()
If distance(px,py,myplayer.px,myplayer.py) > 150 Then Return False
If myplayer.px < px
For Local xx:Int=px/tilewidth Until px/tilewidth-3 Step -1
If mymap.mapfinal[xx,y+1] <> mymap.tilesolid Then Return False
Next
Else
For Local xx:Int=px/tilewidth Until px/tilewidth+3
If mymap.mapfinal[xx,y+1] <> mymap.tilesolid Then Return False
Next
End if
Local angle:Float = getangle(px,py,myplayer.px,myplayer.py)
Local clearpath:Bool=False
Local mx:Float = Cos(angle)
Local my:Float = Sin(angle)
Local monx:Float = px
Local mony:Float = py
For Local i:Int=0 Until 200
monx += mx
mony += my
If distance(monx,mony,myplayer.px,myplayer.py) < 20 Then Return True
If mymap.mapcollide(monx,mony,w/2,h/2) = True Then Return False
If collidelaserwall(monx,mony,w,h) Then Return False
Next
Return True
End Method
' for the walking monster Check if tile on map is blocked
Method cannotgohere:Bool(x:Int,y:Int)
If mymap.mapfinal[x,y] = mymap.tilesolid Then Return True
If mymap.mapfinal[x,y] = mymap.tilemineable Then Return True
If mymap.mapfinal[x,y] = mymap.tileturret Then Return True
Return False
End Method
Method laserwall:Bool()
For Local i:=Eachin mylaserwall
If rectsoverlap((px-w)+(Rnd(-w,w)),py-h,w*2,h*2,i.tx-5,i.ty,10,i.by-i.ty)
Return True
End If
Next
Return False
End Method
Method collidelaserwall:Bool(x:Int,y:Int,w1:Int,h1:Int)
For Local i:=Eachin mylaserwall
If rectsoverlap((x-w1)+(Rnd(-w1,w1)),y-h1,w1*2,h1*2,i.tx-5,i.ty,10,i.by-i.ty)
Return True
End If
Next
Return False
End Method
Method changedirection()
state = "roam"
If substate = "left" Then
substate="right"
x += 2
Else
substate="left"
x -= 2
End If
End Method
Method draw(canvas:Canvas)
Local x1:Float=screenwidth/Float(mapwidth)*Float(x)
Local y1:Float=screenheight/Float(mapheight)*Float(y)
canvas.Color = Color.White
'SetColor 255,255,255
canvas.DrawRect(x1,y1,3+2,3+2)
canvas.Color = Color.Red
'SetColor 255,0,0
canvas.DrawRect(x1+1,y1+1,3,3)
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
Function rectsoverlap:Bool(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int)
If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then Return False
If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then Return False
Return True
End Function
End Class
Class gradienttile
Field image:Image[] = New Image[17]
Field icanvas:Canvas[] = New Canvas[17]
Field width:Int,height:Int
Field tile:Color[][][]
Method New(w:Int,h:Int)
Self.width = w
Self.height = h
tile = New Color[width][][]
For Local i:Int = 0 Until width
tile[i] = New Color[height][]
For Local z:Int=0 Until height
tile[i][z] = New Color[17]
Next
Next
For Local i:Int=0 Until 17
image[i]=New Image( tilewidth,tileheight)',TextureFlags.FilterMipmap)'|TextureFlags.Dynamic )
image[i].Handle=New Vec2f( 0,0 )
icanvas[i]=New Canvas( image[i] )
Next
End Method
Method generate()
Local mc:Color
Local r:Float=1,g:Float=1,b:Float=1
For Local n:Int=0 Until 17
For Local y:Int=0 Until height
mc = New Color(r,g,b)
tile[0][y][n] = mc
r-=1/(17*Float(height))
g-=1/(17*Float(height))
b-=.3/(17*Float(height))
Next
Next
End Method
Method create()
For Local i:=0 Until 17
draw(icanvas[i],i,0,0,1,1)
icanvas[i].Flush()
Next
End Method
' Draw the tile at x and y position and tw=size
Method draw(canvas:Canvas,thetile:Int,sx:Int,sy:Int,tw:Int,th:Int)
Local x:Int
Local y:Int
canvas.Clear(Color.Black)
For y=0 Until height
'For x=0 Until width
Local t:Color
t = tile[x][y][thetile]
Local x2:Int=0
Local y2:Int=y*th
y2+=sy
canvas.Color = t
canvas.DrawRect(x2,y2,width,th)
'Next
Next
End Method
End Class
'
' This is the tile class.
' Here a stone tile is created
' using the generate method
Class tile
Field image:Image[,] = New Image[5,5]
Field icanvas:Canvas[,] = New Canvas[5,5]
Field width:Int,height:Int
Field map:Int[][]
Field a:Int[][]
Field tile:Int[][][]
Method New(w:Int,h:Int)
Self.width = w
Self.height = h
map = New Int[width][]
For Local i:Int=0 Until width
map[i] = New Int[height]
Next
tile = New Int[width][][]
For Local i:Int = 0 Until width
tile[i] = New Int[height][]
For Local z:Int=0 Until height
tile[i][z] = New Int[10]
Next
Next
For Local ii:Int=0 Until 3
For Local i:Int=0 Until 4
image[i,ii]=New Image( tilewidth,tileheight)',TextureFlags.FilterMipmap)'|TextureFlags.Dynamic )
image[i,ii].Handle=New Vec2f( 0,0 )
icanvas[i,ii]=New Canvas( image[i,ii] )
Next
Next
End Method
Method create()
Local col:Color[]=New Color[10]
col[0] = New Color(0,0,0)
col[1] = Color.Grey
col[2] = Color.Brown
col[3] = Color.Blue
col[4] = Color.Gold
col[5] = Color.SeaGreen
For Local ii:Int=0 Until 3
For Local i:=0 Until 4
draw(icanvas[i,ii],i,0,0,1,1,col[ii])
icanvas[i,ii].Flush()
Next
Next
End Method
Method generate(spacing:Int)
' Put a number of stone points on the map
' try to keep a distance between them
Local numpoints:Int=Rnd(width/5,width/2)
For Local i:Int=0 Until numpoints
Local x:Int=Rnd(width)
Local y:Int=Rnd(height)
Local exitloop:Bool=False
While exitloop=False
exitloop = True
If disttootherstone(x,y,i) < spacing
x=Rnd(width)
y=Rnd(height)
exitloop=False
End If
Wend
map[x][y] = i
Next
'
growstone(spacing)
copyintotile(0)
shadeedges(0)
'
For Local i:Int=1 Until 5
preparecenter(spacing)
growstone(spacing)
copyintotile(i)
shadeedges(i)
Next
End Method
Method copyintotile(num:Int)
For Local y:Int=0 Until height
For Local x:Int=0 Until width
tile[x][y][num] = map[x][y]
Next
Next
End Method
Method preparecenter(spacing:Int)
'
' Erase the center and create a new center
'
For Local y:Int=2 Until height-2
For Local x:Int=2 Until width-2
map[x][y] = 0
Next
Next
' Create a few new points
Local numpoints:Int=Rnd(1,4)
For Local i:Int=40 Until 40+numpoints
'map[Rnd(9,width-9)][Rnd(9,height-9)] = i
Local x:Float=Rnd(9,width-9)
Local y:Float=Rnd(9,height-9)
Local angle:Int=Rnd(360)
Local d:Int=Rnd(3,(width+height)/5)
For Local ii:Int=0 Until d
x+=Cos(angle)*1
y+=Sin(angle)*1
map[x][y] = i
For Local iii:Int=0 Until 2
map[x+Rnd(-2,2)][y+Rnd(-2,2)] = i
Next
If disttootherstone(x,y,i) < spacing Then Exit
Next
Next
'
End Method
Method growstone(spacing:Int)
' Grow the stone points
For Local i:Int=0 Until (width*height)*10
Local x:Int=Rnd(width)
Local y:Int=Rnd(height)
If map[x][y] > 0
If disttootherstone(x,y,map[x][y]) < spacing Then Continue
For Local y2:Int=y-1 To y+1
For Local x2:Int=x-1 To x+1
If x2<0 Or y2<0 Or x2>=width Or y2>=height Then Continue
If Rnd(5)<2 Then
If map[x2][y2] = 0
If x2 = 0 Then map[width-1][y2] = map[x][y]
If x2 = width-1 Then map[0][y2] = map[x][y]
If y2 = 0 Then map[x2][height-1] = map[x][y]
If y2 = height-1 Then map[x2][0] = map[x][y]
map[x2][y2] = map[x][y]
End If
End If
Next
Next
End If
Next
End Method
' Add ligther and darker pixels ontop of the stones
' value 1 to <100 is each seperate stone
' value 200 is light color 100 is dark color
Method shadeedges(mytile:Int)
For Local y:Int=0 Until height
For Local x:Int=0 Until width
If tile[x][y][mytile] > 0 And tile[x][y][mytile] <> 200
If x-1 >=0 And tile[x-1][y][mytile] = 0
For Local x2:Int=x+2 To x+4
If Rnd(2)<1
If x2>=0 And x2<width And tile[x2][y][mytile] >0 Then tile[x2][y][mytile] = 100
End If
Next
End If
If x-1 >=0 And y-1>=0 And tile[x-1][y-1][mytile] = 0
tile[x][y][mytile] = 100
End If
If x+1 < width And tile[x+1][y][mytile] = 0
For Local x2:Int=x-4 To x+2
If Rnd(2)<1
If x2>=0 And x2<width And tile[x2][y][mytile] >0 Then tile[x2][y][mytile] = 200
End If
Next
End If
End If
Next
Next
End Method
' Returns the shortest distance to any other stone part then
' the currentstore
Method disttootherstone:Int(sx:Int,sy:Int,currentstone:Int)
Local shortest:Int=9999
For Local y:Int=0 Until height
For Local x:Int=0 Until width
If map[x][y] <> 0 And map[x][y] <> currentstone
Local d:Int=distance(sx,sy,x,y)
If d<shortest Then shortest = d
End If
Next
Next
Return shortest
End Method
' Draw the tile at x and y position and tw=size
Method draw(canvas:Canvas,thetile:Int,sx:Int,sy:Int,tw:Int,th:Int,col:Color)
Local x:Int
Local y:Int
canvas.Clear(Color.Black)
For y=0 Until height
For x=0 Until width
Local t:Int
t = tile[x][y][thetile]
Local x2:Int=x*tw
Local y2:Int=y*th
x2+=sx
y2+=sy
If t >= 1 Then 'grey base color
canvas.Color = New Color(.4,.4,.4).Blend(col,.5)
End If
If t=100 Then canvas.Color = New Color(.2,.2,.2).Blend(col,.5) 'dark shade color
If t=200 Then canvas.Color = New Color(.7,.7,.7).Blend(col,.5)'light shade color
If t>0 ' draw a rect (part of the stone)
canvas.DrawRect(x2,y2,tw,th)
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
End Class
Class laserwall
' bottom x and y and top x and y
Field bx:Float,by:Float
Field tx:Float,ty:Float
Field deleteme:Bool