-
Notifications
You must be signed in to change notification settings - Fork 8
/
CivClone 0.3.monkey2
2756 lines (2593 loc) · 73.4 KB
/
CivClone 0.3.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..
'Texture quality
Global texturequality:String="Low" 'High , Medium and Low
' Here is how many tiles there are drawn on the screen.
' Currently tested from 16x16 up to 32x32
Global mystartmapwidth:Int=20
Global mystartmapheight:Int=20
Global blinkspeed:Int=5 ' lower is faster
Global turn:Int=1
Global activeunitmovesleft:Float=1
Global gamehasmovesleft:Bool=True
Global cityscreenopen:Bool=False
Global currentcityx:Int
Global currentcityy:Int
Global currentcityfarms:Int
Global currentcitymines:Int
Global currentcitybarracks:Bool
Global currentcitywalls:Bool
Global currentcitysize:Int
Global currentcityname:String
Global currentcityfood:Int
Global currentcityresources:Int
Global currentcityproduction:String
Global currentcityproductiontime:Int
Global currentcityharbor:Bool=False
Global currentcitycoastal:Bool=False
'This variable is increased in the main loop
'if a key is pressed and this value is > 0 then this
'variable is set to 0 again.
Global keydelay:Int=0
Global mousedelay:Int=0
Class gamemessage
'This class contains the messages
Class message
Field tekst:String
Method New(tekst:String)
Self.tekst = tekst
End Method
End Class
'store the messages in a stack
Field mymessage:Stack<message>
Field Width:Int,Height:Int
Field time:Int 'count up to timeover means new message
Field timeover:Int=60'message dissapear time
Field currentmessage:String 'this contains the current message
Method New(Width:Int,Height:Int)
Self.Width = Width
Self.Height = Height
'create a message stack
mymessage = New Stack<message>
End Method
'Here we add a message to the system
Method pushmessage(tekst:String)
mymessage.Push(New message(tekst))
' time=timeover-5
End Method
'this method updates the messages
'removing old messages
Method update()
If mymessage.Length > 5 Then timeover = 20 Else timeover = 60
If mymessage.Length > 20 Then timeover = 5
time+=1
If time > timeover
time = 0
currentmessage = ""
If mymessage.Length
currentmessage = mymessage.Top.tekst
mymessage.Pop()
End If
End If
End Method
'Draw the messages to the canvas
Method drawmessage(canvas:Canvas)
If currentmessage = "" Then Return
Local mx:Float=Width/2-200
Local my:Float=10
canvas.PushMatrix()
canvas.Scale(1.2,1.2)
canvas.Color = Color.White
canvas.DrawRect(mx/1.2-2,my/1.2-2,400+4,20+4)
canvas.Color = Color.Grey
canvas.DrawRect(mx/1.2,my/1.2,400,20)
canvas.Color = Color.White
canvas.DrawText(currentmessage,(Width/2)/1.2,my/1.2+2,.5,0)
canvas.PopMatrix()
End Method
End Class
Class greybackground
Field greyimage:Image
Field greycanvas:Canvas
Field mapwidth:Int
Field mapheight:Int
Field tilewidth:Float
Field tileheight:Float
Field mapdepth:Int
'Field mapr:Int[][]
'Field mapg:Int[][]
'Field mapb:Int[][]
Field mapr:Int[,]
Field mapg:Int[,]
Field mapb:Int[,]
Method New(Width:float,Height:float,mapwidth:Float,mapheight:Float)
greyimage = New Image(Width,Height)
greycanvas = New Canvas(greyimage)
Self.mapwidth = mapwidth
Self.mapheight = mapheight
tilewidth = Width/Float(mapwidth)
tileheight = Height/Float(mapheight)
mapr = New Int[mapwidth,mapheight]
mapg = New Int[mapwidth,mapheight]
mapb = New Int[mapwidth,mapheight]
'mapr = New Int[mapwidth][]
'mapg = New Int[mapwidth][]
'mapb = New Int[mapwidth][]
'For Local i:= 0 Until mapwidth
' mapr[i] = New Int[mapheight]
' mapg[i] = New Int[mapheight]
' mapb[i] = New Int[mapheight]
' Next
addrectslayer()
brusheffect2()
smooth()
createimage(greycanvas)
End Method
Method brusheffect2()
For Local i:=0 Until (mapwidth*mapheight)
Local x1:Float=Rnd(0,mapwidth)
Local y1:Float=Rnd(0,mapheight)
Local angle:Int=Rnd(-180,180)
Local dist:Int=Rnd(3,5)
For Local iii:=0 Until 20
For Local ii:=0 Until dist
Local x4:Float=x1+Rnd(-5,5)
Local y4:Float=y1+Rnd(-5,5)
Local x2:Float=x4+Cos(angle)*1
Local y2:Float=y4+Sin(angle)*1
Local x3:Float=x4+Cos(angle)*2
Local y3:Float=y4+Sin(angle)*2
If x2>-1 And y2>-1 And x2<mapwidth And y2<mapheight
If x3>-1 And y3>-1 And x3<mapwidth And y3<mapheight
mapr[x3,y3] = mapr[x2,y2]
mapg[x3,y3] = mapg[x2,y2]
mapb[x3,y3] = mapb[x2,y2]
End If
End If
Next
Next
Next
End Method
Method smooth()
For Local i:=0 Until (mapwidth*mapheight)
Local x:Int=Rnd(0,mapwidth-1)
Local y:Int=Rnd(0,mapheight-1)
Local col1r:Int=mapr[x+1,y]
Local col1g:Int=mapg[x+1,y]
Local col1b:Int=mapb[x+1,y]
Local col2r:Int=mapr[x+1,y+1]
Local col2g:Int=mapg[x+1,y+1]
Local col2b:Int=mapb[x+1,y+1]
Local col3r:Int=mapr[x,y+1]
Local col3g:Int=mapg[x,y+1]
Local col3b:Int=mapb[x,y+1]
Local col4r:Int=(col1r+col2r+col3r)/3
Local col4g:Int=(col1g+col2g+col3g)/3
Local col4b:Int=(col1b+col2b+col3b)/3
mapr[x,y] = col4r
mapg[x,y] = col4g
mapb[x,y] = col4b
Next
End Method
Method addrectslayer()
For Local i:=0 Until (mapwidth*mapheight)
Local w:Int=Rnd(3,15)
Local h:Int=Rnd(3,15)
Local x:Int=Rnd(-3,mapwidth)
Local y:Int=Rnd(-3,mapheight)
Local colm:Int=Rnd(50)
Local colr:Int=205+colm
Local colg:Int=205+colm
Local colb:Int=205+colm
mapdrawrect(x,y,w,h,colr,colg,colb)
Next
End Method
Method mapdrawrect(x:Int,y:Int,w:Int,h:Int,colr:Int,colg:Int,colb:Int)
For Local y1:=y Until y+h
For Local x1:=x Until x+w
If x1>-1 And y1>-1 And x1<mapwidth And y1<mapheight
mapr[x1,y1] = colr
mapg[x1,y1] = colg
mapb[x1,y1] = colb
End If
Next
Next
End Method
Method draw(canvas:Canvas,x:Int=0,y:Int=0)
canvas.DrawImage(greyimage,0,0)
End Method
Method createimage(canvas:Canvas)
For Local y:=0 Until mapheight
For Local x:=0 Until mapwidth
Local colr:Float=mapr[x,y]
Local colg:Float=mapg[x,y]
Local colb:Float=mapb[x,y]
canvas.Color = New Color(colr/255,colg/255,colb/255)
canvas.DrawRect(x*tilewidth,y*tileheight,tilewidth+1,tileheight+1)
Next
Next
canvas.Flush()
End Method
Method distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Method
End Class
Class unituserinterface
Field Width:Int,Height:Int
Field arrowimage:Image
Field arrowcanvas:Canvas
Field ix:Int,iy:Int
Field undockx:Int,undocky:Int
Field undockw:Int,undockh:Int
Field dockx:Int,docky:Int
Field dockw:Int,dockh:Int
Field docked:Bool=false
Method New(Width:Int,Height:Int)
Self.Width = Width
Self.Height = Height
dockside("Left")
arrowimage = New Image(Width/10,Height/10)
arrowcanvas = New Canvas(arrowimage)
arrowimage.Handle = New Vec2f( .5,.5 )
makearrow(arrowcanvas)
End Method
Method update()
' the dock undock buttons controls
Local mx:Int,my:Int
If docked = False
If Mouse.ButtonReleased(MouseButton.Left) Or Touch.FingerReleased(0)
If Mouse.ButtonReleased(MouseButton.Left)
mx = Mouse.X
my = Mouse.Y
End If
If Touch.FingerReleased(0)
mx = Touch.FingerX(0)
my = Touch.FingerY(0)
End If
If rectsoverlap(mx,my,1,1,dockx,docky,dockw,dockh)
docked = True
mousedelay = 0
Return
End If
End If
End If
If docked = True
If Mouse.ButtonReleased(MouseButton.Left) Or Touch.FingerReleased(0)
If Mouse.ButtonReleased(MouseButton.Left)
mx = Mouse.X
my = Mouse.Y
End If
If Touch.FingerReleased(0)
mx = Touch.FingerX(0)
my = Touch.FingerY(0)
End If
If rectsoverlap(mx,my,1,1,undockx,undocky,undockw,undockh)
docked = False
mousedelay = 0
Return
End If
End If
End If
'unit movement controls (the arrows)
If docked = False
If Mouse.ButtonReleased(MouseButton.Left)
Local x:Int=ix-Width/20
Local y:Int=iy-Height/20
Local dx:Int,dy:Int
'find current active unit x and y position
For Local i:=Eachin myunit
If i.active = True
dx=i.x
dy=i.y
Exit
End If
Next
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x,y,Width/10,Height/10)
'Print "Up"+Millisecs()
myunitmethod.moveactiveunitto(dx,dy-1)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x,y+100,Width/10,Height/10)
'Print "Down"+Millisecs()
myunitmethod.moveactiveunitto(dx,dy+1)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x-50,y+50,Width/10,Height/10)
'Print "Left"+Millisecs()
myunitmethod.moveactiveunitto(dx-1,dy)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+50,y+50,Width/10,Height/10)
'Print "Right"+Millisecs()
myunitmethod.moveactiveunitto(dx+1,dy)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x-45,y,Width/10,Height/10)
'Print "LeftUp"+Millisecs()
myunitmethod.moveactiveunitto(dx-1,dy-1)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x-45,y+100,Width/10,Height/10)
'Print "LeftDown"+Millisecs()
myunitmethod.moveactiveunitto(dx-1,dy+1)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+45,y,Width/10,Height/10)
'Print "RightUp"+Millisecs()
myunitmethod.moveactiveunitto(dx+1,dy-1)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+45,y+100,Width/10,Height/10)
'Print "RightDown"+Millisecs()
myunitmethod.moveactiveunitto(dx+1,dy+1)
redrawgame()
mousedelay = 0
End If
End If 'end if rectsoverlap
End If 'docked = false
If docked = False
' The unit commands. Taken directly from the controls class
' update there means update here (lazy)
If Mouse.ButtonReleased(MouseButton.Left)
Local x:Int=ix
Local y:Int=iy
'drawunitbutton(canvas,x+100,y,"R")
'drawunitbutton(canvas,x+100,y+32,"B")
'drawunitbutton(canvas,x+100,y+64,"F")
'drawunitbutton(canvas,x+100,y+96,"S")
'drawunitbutton(canvas,x-16,y+34,"E")
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+100,y,32,32)
'Print "Build Road"
If myunitmethod.activeunitissetler() = False Then Return
myunitmethod.buildroadatactiveunitpos()
myworld.updatedrawroads(myworld.roadcanvas)
myunitmethod.activateamovableunit()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+100,y+32,32,32)
'Print "Build City"
If myunitmethod.activeunitissetler() = False Then Return
If myunitmethod.iscityatactiveunitpos() = True Then Return
myunitmethod.buildroadatactiveunitpos(False)
Local x:Int,y:Int
'get the active unit x and y coordinates
For Local i:=Eachin myunit
If i.active = True
x = i.x
y = i.y
i.deleteme = True
Exit
End If
Next
mycity.Add(New city(x,y))
myworld.updatedrawroads(myworld.roadcanvas)
myunitmethod.activateamovableunit()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+100,y+64,32,32)
'Print "Fortify"
myunitmethod.unitactivefortify()
myunitmethod.activateamovableunit()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+100,y+96,32,32)
'Print "Skip turn"
myunitmethod.activeunitskipturn()
myunitmethod.activateamovableunit()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x-16,y+34,32,32)
'Print "End Turn"
'Update the buildlist of each city
For Local i:=Eachin mycity
i.turnend()
Next
'restore the moves of each unit
For Local i:=Eachin myunit
i.movesleft = i.originalmoves
i.active = False
Next
' set the game movement tip
gamehasmovesleft = True
'increase turn
turn+=1
' find a moveable unit
myunitmethod.activateamovableunit()
' set mouse delay so as not to double feaure click
mousedelay = 0
End If
End If
End If 'end if docked is false
End Method
Method dockside(side:String)
Select side
Case "Right"
ix = Width-150
iy = Height-200
undockx = ix + 100
undocky = iy + 160
undockw = 32
undockh = 20
dockx = ix + 100
docky = iy - 32
dockw = 32
dockh = 20
Case "Left"
ix = 150
iy = Height-200
undockx = ix - 140
undocky = iy + 160
undockw = 32
undockh = 20
dockx = ix - 140
docky = iy - 32
dockw = 32
dockh = 20
End Select
End Method
Method draw(canvas:Canvas)
If docked = False Then
Local x:Int=ix
Local y:Int=iy
'draw the unit commands buttons.
drawunitbutton(canvas,x+100,y,"R")
drawunitbutton(canvas,x+100,y+32,"B")
drawunitbutton(canvas,x+100,y+64,"F")
drawunitbutton(canvas,x+100,y+96,"S")
drawunitbutton(canvas,x-16,y+34,"E")
'draw the arrows
drawarrow(canvas,x,y,"Up")
drawarrow(canvas,x-50,y+50,"Left")
drawarrow(canvas,x+50,y+50,"Right")
drawarrow(canvas,x,y+100,"Down")
drawarrow(canvas,x-35,y,"LeftUp")
drawarrow(canvas,x-35,y+100,"LeftDown")
drawarrow(canvas,x+35,y,"RightUp")
drawarrow(canvas,x+35,y+100,"RightDown")
drawarrow2(canvas,x,y,"Up")
drawarrow2(canvas,x-50,y+50,"Left")
drawarrow2(canvas,x+50,y+50,"Right")
drawarrow2(canvas,x,y+100,"Down")
drawarrow2(canvas,x-45,y,"LeftUp")
drawarrow2(canvas,x-45,y+100,"LeftDown")
drawarrow2(canvas,x+45,y,"RightUp")
drawarrow2(canvas,x+45,y+100,"RightDown")
'draw the dock
canvas.Color = Color.White
canvas.DrawRect(dockx,docky,dockw,dockh)
canvas.Color = Color.Black
canvas.DrawLine(dockx,docky,dockx+dockw/2,docky+dockh)
canvas.DrawLine(dockx+dockw,docky,dockx+dockw/2,docky+dockh)
End If
' if we are docked
If docked = True
'draw the dock
canvas.Color = Color.White
canvas.DrawRect(undockx,undocky,undockw,undockh)
canvas.Color = Color.Black
canvas.DrawLine(undockx,undocky+undockh/2,undockx+undockw/2,undocky)
canvas.DrawLine(undockx+undockw,undocky+undockh/2,undockx+undockw/2,undocky)
End If
End Method
Method drawunitbutton(canvas:Canvas,x:Int,y:Int,t:String)
canvas.Color = Color.Red
canvas.DrawRect(x,y,32,32)
canvas.Color = Color.Black
canvas.DrawRect(x+2,y+2,32-4,32-4)
canvas.Color = Color.White
canvas.DrawText(t,x+16,y+16,.5,.5)
End Method
Method drawarrow(canvas:Canvas,x:Int,y:Int,d:String)
Local rotation:Float=0
Select d
Case "RightUp"
rotation=-Pi/1.4
Case "RightDown"
rotation=Pi/1.4
Case "LeftDown"
rotation=Pi/3.3
Case "LeftUp"
rotation=-Pi/3.3
Case "Up"
rotation=-Pi/2
Case "Down"
rotation=Pi/2
Case "Left"
rotation=0
Case "Right"
rotation=-Pi
End Select
canvas.Color = Color.White
canvas.DrawCircle(x,y,Width/20)
canvas.DrawImage(arrowimage,x,y,rotation)
end Method
Method drawarrow2(canvas:Canvas,x:Int,y:Int,d:String)
Local rotation:Float=0
Select d
Case "RightUp"
rotation=-Pi/1.4
Case "RightDown"
rotation=Pi/1.4
Case "LeftDown"
rotation=Pi/3.3
Case "LeftUp"
rotation=-Pi/3.3
Case "Up"
rotation=-Pi/2
Case "Down"
rotation=Pi/2
Case "Left"
rotation=0
Case "Right"
rotation=-Pi
End Select
canvas.Color = Color.Red
canvas.DrawImage(arrowimage,x,y,rotation)
end Method
Method makearrow(canvas:Canvas)
canvas.Clear(New Color(0,0,0,0))
Local pol:= New Float[14]
Local w:Float = Width/10
Local h:Float = Height/10
pol[0] = 0
pol[1] = h/2
pol[2] = w/3
pol[3] = h
pol[4] = w/3
pol[5] = h/1.5
pol[6] = w
pol[7] = h/1.5
pol[8] = w
pol[9] = h/3
pol[10] = w/3
pol[11] = 0+h/3
pol[12] = w/3
pol[13] = 0
canvas.Color = Color.Grey
canvas.DrawPoly(pol)
canvas.Flush()
End Method
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 citycontrols
'Here we update the production window
Method productionupdate()
If mousedelay < 20 Then Return
' if opressed left mouse in production box
If Mouse.ButtonReleased(MouseButton.Left) = True
If mycityscreen.unitprodscreen = False
Local x1:Int=mycityscreen.prodx
Local y1:Int=mycityscreen.prody
Local w1:Int=mycityscreen.prodw
Local h1:Int=mycityscreen.prodh
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x1,y1,w1,h1)
'if pressed on unit production info box
mycityscreen.unitprodscreen = True
mousedelay=0
End If
End If
End If
'if right mouse on production box then erase production
If Mouse.ButtonReleased(MouseButton.Right) = True
If mycityscreen.unitprodscreen = False
Local x1:Int=mycityscreen.prodx
Local y1:Int=mycityscreen.prody
Local w1:Int=mycityscreen.prodw
Local h1:Int=mycityscreen.prodh
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x1,y1,w1,h1)
'erase city production list
For Local i:=Eachin mycity
If i.x = currentcityx And i.y=currentcityy
i.myproduction = New Stack<city.production>
mycityscreen.updateproduction()
mousedelay=0
End If
Next
End If
End If
End If
If Mouse.ButtonReleased(MouseButton.Left) = True
If mycityscreen.unitprodscreen = True 'if unitprodscreen = true draw unit production screen
Local x1:Int=mycityscreen.prodsx
Local y1:Int=mycityscreen.prodsy
Local w1:Int=mycityscreen.prodsw
Local h1:Int=mycityscreen.prodsh
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x1,y1,w1,h1)
'If pressed inside new production list
If mycityscreen.mybuildlist.Length
Local myselt:Bool=False
Local myseltname:String=""
Local y:Int=20
For Local i:=Eachin mycityscreen.mybuildlist
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x1,y1+y,w1,20)
'Print i.name - here we have selected a production
myselt = True
myseltname = i.name
End If
y+=20
Next
If myselt = True 'if we have selected a unit to be build
mousedelay=0
mycityscreen.myproduction.Push(New cityscreen.production(myseltname,4))
For Local i:=Eachin mycity
If i.x = currentcityx And i.y = currentcityy
i.myproduction.Push(New city.production(myseltname))
End If
Next
mycityscreen.unitprodscreen = False
End If
End If
End If
End If
End If
End Method
'Here we select a unit from the garrison
Method garrisonupdate()
If mousedelay<20 Then Return
If Mouse.ButtonReleased(MouseButton.Left)
Local garx:Int=mycityscreen.garx
Local gary:Int=mycityscreen.gary
Local garw:Int=mycityscreen.garw
Local garh:Int=mycityscreen.garh
If rectsoverlap(Mouse.X,Mouse.Y,1,1,garx,gary,garw,garh)
If mycityscreen.mygarrison
Local y:Int=20
For Local i:=Eachin mycityscreen.mygarrison
If rectsoverlap(Mouse.X,Mouse.Y,1,1,garx,gary+y,garw,20)
'Here we have selected a unit from the garrison
i.fortify = False
myunitmethod.unitfortify(i.id,False)
If i.movesleft > 0.3
cityscreenopen = False
keydelay = 0
mousedelay = 0
myunitmethod.activateamovableunit()
mousedelay=0
Return
End If
End If
y+=20
Next
End If
End If
End If
End Method
'Here we exit the city screen
Method controls()
productionupdate()
If mycityscreen.unitprodscreen = False Then
garrisonupdate()
End If
'if press on bottom screen in cityscreen window then exit back to game
If Mouse.ButtonReleased(MouseButton.Left)
If rectsoverlap(Mouse.X,Mouse.Y,1,1,150,mycityscreen.Height-32,mycityscreen.Width-300,32)
mycityscreen.unitprodscreen = False
cityscreenopen = False
keydelay = 0
mousedelay = 0
End If
End If
' if press esacpe or space then exit city screen
If Keyboard.KeyReleased(Key.Escape) Or Keyboard.KeyReleased(Key.Space)
mycityscreen.unitprodscreen = False
cityscreenopen = False
keydelay = 0
mousedelay = 0
End If
End Method
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 cityscreen
Class garrison
Field name:String
Field fortify:Bool
Field movesleft:Float
Field id:Int
Method New(id:Int,name:String)
Self.id = id
Self.name = name
movesleft = myunitmethod.unitmovesleft(id)
fortify = myunitmethod.unitisfortified(id)
End Method
End Class
Field mygarrison:Stack<garrison>
' What is the city building
Class production
Field name:String="Settler"
Field turns:Int=4
Method New(name:String,turns:Int)
Self.name=name
Self.turns = turns
End Method
End Class
Field myproduction:Stack<production>
'What can the city produce
Class buildlist
Field name:String
Method New(name:String)
Self.name = name
End Method
End Class
Field mybuildlist:Stack<buildlist>
Field Width:Int,Height:Int
'the variables for the map of the city
Field tw:Float
Field th:Float
Field citymapx:Int,citymapy:Int
Field citymapw:Int,citymaph:Int
'variables for the production info
Field prodx:Int
Field prody:Int
Field prodw:Int
Field prodh:Int
Field unitprodscreen:Bool=False
Field prodsx:Int,prodsy:Int,prodsw:int,prodsh:Int
'variables for the units garrision
Field garx:Int,gary:Int,garw:Int,garh:Int
'variables for the city info
Field cityix:Int,cityiy:int,cityiw:int,cityih:Int
Method New(Width:Int,Height:Int)
Self.Width = Width
Self.Height = Height
tw = myworld.tw
th = myworld.th
' fill the city map variables
citymapw = tw*5
citymaph = th*5
citymapx = Width/2-citymapw/2
citymapy = Height/2-citymaph/2
' fill the city production info variables
prodx = Width/1.5
prody = Height/1.5
prodw = Width/3.3
prodh = Height/3.3
prodsw = 150
prodsh = 200
prodsx = Width/2-prodsw/2
prodsy = Height/2-prodsh/2
updatecitybuildlist()
myproduction = New Stack<production>
'fill the city garrison info variables
garx = 10
gary = 10
garw = 100
garh = 100
' fill the city info variables
cityix = 10
cityiy = Height/2
cityiw = 220
cityih = 200
End Method
Method updatecitybuildlist()
mybuildlist = New Stack<buildlist>
If currentcitycoastal And currentcitysize > 1 Then mybuildlist.Push(New buildlist("Sea Unit"))
If currentcitysize > 1 Then mybuildlist.Push(New buildlist("Settlers"))
If currentcitywalls = False Then mybuildlist.Push(New buildlist("City Walls"))
If currentcitymines < 22 Then mybuildlist.Push(New buildlist("Mine"))
If currentcityfarms < 22 Then mybuildlist.Push(New buildlist("Farm"))
If currentcitybarracks = False Then mybuildlist.Push(New buildlist("Barracks"))
If currentcitysize < 22 Then mybuildlist.Push(New buildlist("Expand City"))
End Method
Method draw(canvas:Canvas)
canvas.Color = Color.Black
canvas.DrawRect(0,0,Width,Height)
canvas.Color = Color.White
canvas.Color = New Color(.7,.7,1)
canvas.DrawImage(mygreybackground.greyimage,0,0)
canvas.Color = Color.White
drawcitymap(canvas)
drawproduction(canvas)
drawgarrison(canvas)
drawcityinfo(canvas)
canvas.Color = Color.White
canvas.DrawText("Press Space to Exit",Width/2,Height-20,.5,.5)
End Method
Method updateproduction()
myproduction = New Stack<production>
For Local i:=Eachin mycity
If i.x = currentcityx And i.y = currentcityy
If i.myproduction.Length
For Local i2:= Eachin i.myproduction
myproduction.Push(New production(i2.name,i2.turns))
Next
End If
End If
Next
End Method
Method updategarrison()
mygarrison = New Stack<garrison>
For Local i:=Eachin myunit
If i.x = currentcityx And i.y = currentcityy
mygarrison.Push(New garrison(i.id,i.name))
End If
Next
End Method
Method drawcityinfo(canvas:Canvas)
' Draw the textured window
Local rec := New Recti<Int>
rec.X = 0
rec.Y = 0
rec.Size = New Vec2i(Width,Height)
canvas.Scissor = rec
canvas.Color = Color.White
canvas.DrawRect(cityix-2,cityiy-2,cityiw+4,cityih+4)
rec = New Recti<Int>
rec.X = cityix
rec.Y = cityiy
rec.Size = New Vec2i(cityiw,cityih)
canvas.Scissor = rec
canvas.Color = New Color(.5,.5,.5)
canvas.DrawImage(mygreybackground.greyimage,0,0)
rec = New Recti<Int>
rec.X = 0
rec.Y = 0
rec.Size = New Vec2i(Width,Height)
canvas.Scissor = rec
' fill with text
canvas.Color = Color.White
' Table with text
canvas.DrawText("Size:",cityix+10,cityiy+20*1)
canvas.DrawText("Farms:",cityix+10,cityiy+20*2)
canvas.DrawText("Food:",cityix+10,cityiy+20*3)
canvas.DrawText("Mines:",cityix+10,cityiy+20*4)
canvas.DrawText("Resources:",cityix+10,cityiy+20*5)
canvas.DrawText("Barracks:",cityix+10,cityiy+20*6)
canvas.DrawText("Walls:",cityix+10,cityiy+20*7)
' Table with numbers
Local foo:Int
foo = currentcityfarms-currentcitysize
Local min:Int = currentcitymines - currentcitysize
If currentcitybarracks =True Then min-=1
If currentcitywalls=True Then min-=1
canvas.DrawText(currentcitysize,cityix+100,cityiy+20*1)
canvas.DrawText(currentcityfarms,cityix+100,cityiy+20*2)
canvas.DrawText(currentcityfood,cityix+100,cityiy+20*3)
canvas.DrawText(currentcitymines,cityix+100,cityiy+20*4)
canvas.DrawText(currentcityresources,cityix+100,cityiy+20*5)
Local s:String
If currentcitybarracks = True Then s = "Yes" else s="No"
canvas.DrawText(s,cityix+100,cityiy+20*6)
If currentcitywalls = True Then s = "Yes" else s="No"
canvas.DrawText(s,cityix+100,cityiy+20*7)
' The numbers behind the food and resources info labels
If foo>= 0 Then
canvas.Color=Color.Green
canvas.DrawText("+"+foo,cityix+130,cityiy+20*3)
Else
canvas.Color = Color.Red
canvas.DrawText(foo,cityix+130,cityiy+20*3)
End If
If min>= 0 Then
canvas.Color=Color.Green
canvas.DrawText("+"+min,cityix+130,cityiy+20*5)
Else
canvas.Color = Color.Red
canvas.DrawText(min,cityix+130,cityiy+20*5)
End If
End Method
Method drawgarrison(canvas:Canvas)
Local rec := New Recti<Int>
rec.X = 0
rec.Y = 0
rec.Size = New Vec2i(Width,Height)
canvas.Scissor = rec
canvas.Color = Color.White
canvas.DrawRect(garx-2,gary-2,garw+4,garh+4)
rec = New Recti<Int>
rec.X = garx
rec.Y = gary
rec.Size = New Vec2i(garw,garh)
canvas.Scissor = rec
canvas.Color = New Color(.5,.5,.5)
canvas.DrawImage(mygreybackground.greyimage,0,0)
rec = New Recti<Int>
rec.X = 0
rec.Y = 0
rec.Size = New Vec2i(Width,Height)
canvas.Scissor = rec
canvas.Color = Color.White
canvas.DrawText("Garrison",garx,gary)
' canvas.Color = Color.Black
' canvas.DrawRect(garx,gary,garw,garh)
If mygarrison.Length>0
canvas.Color = Color.Black
Local y:Int=gary+20
For Local i:=Eachin mygarrison
Local a:String=""
If i.fortify Then
a+="F-"
End If
a+=i.name
canvas.DrawText(a,garx,y)
y+=20
If y>garh Then Exit
Next
End If
End Method
Method drawproduction(canvas:Canvas)
'white outline
canvas.Color = Color.White
canvas.DrawRect(prodx-2,prody-2,prodw+4,prodh+4)
' textured inside
Local rec := New Recti<Int>
rec.X = prodx
rec.Y = prody
rec.Size = New Vec2i(prodw,prodh)
canvas.Scissor = rec
canvas.Color = New Color(.5,.5,.5)
canvas.DrawImage(mygreybackground.greyimage,0,0)
' the production text
rec = New Recti<Int>
rec.X = 0
rec.Y = 0
rec.Size = New Vec2i(Width,Height)
canvas.Scissor = rec
' canvas.Color = Color.Black
' canvas.DrawRect(prodx,prody,prodw,prodh)
canvas.Color = Color.Black
If myproduction.Length > 0
canvas.DrawText("Current Production :",prodx+10,prody+10)
canvas.DrawText(myproduction.Top.name,prodx+10,prody+30)
canvas.DrawText(myproduction.Top.turns+" Turns Left",prodx+10,prody+50)
End If
canvas.DrawText("Click to add",prodx+10,prody+70)