-
Notifications
You must be signed in to change notification settings - Fork 8
/
2d Platformer RPG 06.monkey2
4710 lines (4439 loc) · 124 KB
/
2d Platformer RPG 06.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 - town shop
' add - player inventory (first draft - collected items get added)
' add - towns people(initiated)
' 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 10-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
'
' This is the code for the people in the town.
'
Class townperson
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,1)
' sy = sx
state="roam"
If Rnd(10)<2 Then substate="left" Else substate="stand"
End Method
Method update()
' If laserwait>0 Then laserwait-=1
If px < x*w Then px += sx
If px > x*w Then px -= sx
If distance(px,py,x*w,y*h) > 8 Then Return
Select state
Case "roam"
Select substate
Case "left"
If x<3 Then substate="right" ; Return
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"
Case "right"
If x>mapwidth-3 Then substate="left" ; Return
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"
End Select
randaction()
End Select
End Method
Method randaction()
If substate = "left" And Rnd(600)<2 Then changedirection() ; Return
If substate = "right" And Rnd(600)<2 Then changedirection(); Return
If substate = "stand" And Rnd(600)<2 Then
If Rnd(2) < 1 Then substate = "left" Else substate="right"
Return
End If
If Rnd(100) < 2 Then substate = "stand"; Return
End Method
' 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 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
'
' Tree sprite generator
'
Class tree
Field image:Image
Field icanvas:Canvas
Field px:Int,py:Int
Field pw:Float,ph:Float
Field mapone:Color[][]
Field nocolor:Color = New Color(0,0,0,0)
Field outlcolor:Color = New Color(.01,0.1,0.1)
Field treecolor1:Color = New Color(Rnd(.5,.8),Rnd(.8,1),0)
Field treecolor2:Color = New Color(Rnd(.3,.7),Rnd(.7,.78),0)
Field treecolor3:Color = New Color(Rnd(.25,.5),Rnd(.5,.68),0)
Field treecolor4:Color = New Color(Rnd(.1,.20),Rnd(.2,.45),0)
Field treecolor1r:Color = New Color(Rnd(.1,.5),0,0)
Field basecolor1:Color = New Color(.5,.24,0)
Field basecolor2:Color = New Color(.8,.4,0)
Field basecolor3:Color = New Color(.9,.45,.0)
Method New(x:Int,y:Int,w:Int,h:Int)
If Rnd(3)<2 Then
treecolor1 = treecolor1.Blend(treecolor1r,.1)
treecolor2 = treecolor1.Blend(treecolor1r,.2)
treecolor3 = treecolor1.Blend(treecolor1r,.3)
treecolor4 = treecolor1.Blend(treecolor1r,.4)
End If
px = x
py = y
pw = w
ph = h
mapone = New Color[w][]
For Local i:Int=0 Until w
mapone[i] = New Color[h]
Next
maketree()
image = New Image(pw,ph)',TextureFlags.Dynamic)
icanvas = New Canvas(image)
icanvas.Clear(New Color(0,0,0,0))
icanvas.Flush()
Local opx:Int=x
Local opy:Int=y
Self.px = 0
Self.py = 0
draw(icanvas)
icanvas.Flush()
Self.px = opx
Self.py = opy
End Method
Method maketree()
Local mx:Float=0.05
Local my:Float=.1
Local y:Float=1
Local x:Float=pw/2+1
Local base:Float=0
Local bounce:Float=.1
Local col:Color
Local num:Float=2
Local stap:Float=Rnd(0.001,0.005)
Local stap2:Float=Rnd(0.01,0.2)
Local stap3:Float=Rnd(0.5,1.5)
' Place two black pixels at the top of the tree
mapone[x-1][0] = outlcolor
mapone[x-2][0] = outlcolor
' create the tree
While (y+5)<=(ph-(ph/20))
y+=my
x+=mx
' stay inside the image
If x>=pw Then x=pw-2
If x<=0 Then x=0
' change color of the tree depending
' on the current y location
If y<ph/1.4 Then col = treecolor4
If y<ph/1.6 Then col = treecolor3
If y<ph/1.9 Then col = treecolor2
If y<ph/4 Then col = treecolor1
' fill the current line
filltoleft(x,y,pw-x,col)
' black pixel to the left and right
mapone[x][y] = outlcolor
mapone[pw-x][y] = outlcolor
' next step in the tree shape
mx-=stap
If y<ph/1.45 Then
If mx<0
If x < ((pw/2)+num) Then mx=bounce ; bounce+=stap2 ; num+=stap3
End If
Else
If mx<0
If x<((pw/2)+num) Then bounce=.1 ; mx=bounce ; num-=stap3
Endif
End If
Wend
' Make sure the bottom of the tree is also drawn
filltoleft(x,y,pw-x,outlcolor)
' Make the tree trunk
maketreebase()
End Method
'
' Fill from x to tox on y line using col(or)
' We go from right to left and fill the line with
' a number. (tree inside color)
'
Method filltoleft(x:Int,y:Int,tox:Int,col:Color)
Local ls:Int=(pw/2)
Local len1:Int=(x-ls)/2
Local len2:Int=(x-ls)/1.7
For Local x2:Int=x To tox Step -1
mapone[x2][y] = col
If col = treecolor2 Then
If Rnd(4) < 1 And distance(x2,0,tox,0) < len1 And y<ph/2 Then mapone[x2][y] = treecolor1
If Rnd(4) < 1 And distance(x2,0,x,0)< len1 And y<ph/2 Then mapone[x2][y] = treecolor1
If Rnd(2)<1.3 And distance(x2,0,ls-len1,0) < 2 And y<ph/2 Then mapone[x2][y] = treecolor1
If Rnd(2)<1.3 And distance(x2,0,ls+len1,0) < 2 And y<ph/2 Then mapone[x2][y] = treecolor1
End If
If col=treecolor1
If Rnd(2) < 1 And y>5 And distance(x2,0,ls,0) < 3 Then mapone[x2][y] = treecolor2
End If
If col=treecolor3
If Rnd(2)<1.3 And distance(x2,0,ls,0) < len2 And y<ph/1.8 Then mapone[x2][y] = treecolor2
End If
If col=treecolor4
If Rnd(2)<1.3 And distance(x2,0,ls,0) < len2 And y<ph/1.45 Then mapone[x2][y] = treecolor3
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
Method maketreebase()
' treebase
For Local y:Int=ph-(ph/5) Until ph
For Local x:Int=(pw/2)-(pw/8) Until (pw/2)+(pw/8)
If x<0 Or y<0 Or x>=pw Or y>= ph Then Continue
mapone[x][y] = basecolor1
If x=(pw/2)-(pw/8) Then mapone[x][y] = outlcolor
If x=(pw/2)+(pw/8)-1 Then mapone[x][y]= outlcolor
If y=ph-1 Then mapone[x][y]=outlcolor
Next
Next
For Local y:Int=ph-(ph/5) Until ph-(ph/5)
For Local x:Int=(pw/2)-(pw/8) Until (pw/2)+(pw/8)
If x<0 Or y<0 Or x>=pw Or y>= ph Then Continue
mapone[x][y] =outlcolor
Next
Next
' tree base center lighting
For Local y:Int=ph-(ph/7) Until ph-1
For Local x:Int=(pw/2)-(pw/30) Until (pw/2)+(pw/30)
If x<0 Or y<0 Or x>=pw Or y>= ph Then Continue
mapone[x][y] = basecolor3
Next
Next
For Local y:Int=ph-(ph/7) Until ph-1
For Local x:Int=(pw/2) Until (pw/2)+(pw/30)
If x<0 Or y<0 Or x>=pw Or y>= ph Then Continue
mapone[x][y] = basecolor2
Next
Next
'Remove two black pixels from the bottom of the treebase
mapone[(pw/2)-(pw/8)][ph-1] = nocolor
mapone[(pw/2)+(pw/8)-1][ph-1] = nocolor
End Method
Method draw(canvas:Canvas)
'canvas.Clear(New Color(0,0,0,0))
For Local y:Int=0 Until ph
For Local x:Int=0 Until pw
If mapone[x][y] = nocolor Then Continue
canvas.Color = mapone[x][y]
canvas.DrawRect(px+(x*1),py+(y*1),1,1)
Next
Next
End Method
End Class
'
' 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]