-
Notifications
You must be signed in to change notification settings - Fork 8
/
Generator - Simple 2d Procedural Houses.monkey
423 lines (397 loc) · 10.2 KB
/
Generator - Simple 2d Procedural Houses.monkey
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
Import mojo
Class building
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]
Method New(x:Int,y:Int,w:Int,isshop:Bool)
px = x
py = y
totalwidth = w
makehouse(w,isshop)
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 draw(w:Int,h:Int)
Local bw:Int=w
Local bh:Int=h
' Draw the house blocks
For Local i:Int=0 Until 3
If houselayer[i] = blockhouse Then
SetColor 150,140,150
DrawRect px+(i*bw),py,bw+1,bh
SetColor 200,200,200
DrawRect px+(i*bw),py,bw,bh
'SetColor 230,230,230
'shadow top
SetColor 60,60,60
DrawRect px+(i*bw),py,bw,bh/15
'shadow bottom
SetColor 150,150,150
DrawRect px+(i*bw),py+bh/1.1,bw,bh/8
'highlight left
If i=0
SetColor 220,220,220
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
SetColor 170,70,60
DrawRect px+(i*bw),py-(bh/1.5),bw,bh-(bh/3)
' Bottom shade
SetColor 130,50,30
'SetColor 200,100,100
DrawRect px+(i*bw),py-(bh/8),bw,bh/8
' top shade
SetColor 190,90,80
DrawRect px+(i*bw),py-(bh/1.5),bw,1
' top shade
If i=0
'horizontal
SetColor 220,120,110
DrawRect px+(i*bw),py-(bh/1.5),bw/2,1
'vertical
SetColor 200,100,100
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
SetColor 100,100,100
DrawRect px+(i*bw)+(bw/4),py-(bh/1.2),bw/2.5,bh/4
'chimney highlight
SetColor 140,130,120
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
SetColor 0,100,200
DrawRect px+(i*bw)+(bw/3),py+(bh/5),bw-(bw/3),bh-(bh/2.5)
' light bottom
SetColor 0,115,210
DrawRect px+(i*bw)+(bw/3),py+(bh/2),bw-(bw/3),(bh/3.3)
' dark bottom
SetColor 180,125,20
DrawRect px+(i*bw)+(bw/3),py+(bh*.7),bw-(bw/3),(bh/8.3)
End If
If windowlayer[i] = blockwidewindow
SetColor 0,100,200
DrawRect px+(i*bw)+(bw/3),py+(bh/5),(bw*2)-(bw/3),bh-(bh/2.5)
'light bottom
SetColor 0,115,210
DrawRect px+(i*bw)+(bw/3),py+(bh/2),(bw*2)-(bw/3),(bh/3.3)
'dark bottom
SetColor 180,125,20
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
SetColor 100,50,50
If shopsignlayer[i] = blockshopsign
SetColor 250,200,50
End If
DrawRect px+(i*bw)+(bw/5),py+(bh/5),bw-(bw/2),bh-(bh/4)
' doorknob
SetColor 200,210,210
DrawRect px+(i*bw)+(bw/2),py+(bh/1.7),(bw/9),(bh/9)
'numberplate
SetColor 200,250,250
DrawRect px+(i*bw)+(bw/1.3),py+(bh/3),(bw/9),(bh/9)
SetColor 10,50,50
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(px,py,bw,bh,"left")
If housesidelayer[1] = blocktoiletright Then drawtoilet(px,py,bw,bh,"right")
If housesidelayer[0] = blockcrateleft Then drawsidecrate(px,py,bw,bh,"left")
If housesidelayer[1] = blockcrateright Then drawsidecrate(px,py,bw,bh,"right")
If housesidelayer[0] = blockiceboxleft Then drawsideicebox(px,py,bw,bh,"left")
If housesidelayer[1] = blockiceboxright Then drawsideicebox(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
SetColor 100,50,50
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
SetColor 255,40,30
Local x:Int=px+(bw*i)-bw/8
Local y:Int=py-bh/5
DrawRect x,y,bw*1.2,bh/3
SetColor 255,255,255
'DrawText "Shop X",x+5,y+5
DrawRect x+bh/10,y+bh/12,4,4
End If
Next
End Method
Method drawtoilet(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[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
SetColor 150,50,50
DrawPoly(toil)
'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
SetColor 150,50,50
Local box: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
DrawPoly(box)
' SetColor 100,50,50
' DrawRect (x)+totalwidth*w,y+10,w/2,h-10
End If
End Method
Method drawsidecrate(x:Int,y:Int,w:Int,h:Int,side:String)
If side = "left"
' pipe
SetColor 120,120,120
DrawRect x-w/8,y,w/8,h
'barrel
SetColor 100,50,50
DrawRect x-w/3,y+(h/1.5),w/3,h-(h/1.5)
Elseif side="right"
'pipe
SetColor 120,120,120
DrawRect (x)+totalwidth*w,y,w/8,h
'barrel
SetColor 100,50,50
DrawRect (x)+totalwidth*w,y+(h/1.5),w/3,h-(h/1.5)
End If
End Method
Method drawsideicebox(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
SetColor 200,200,200
Local box: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
'DrawRect ltx,lty,w/2,h-(h/1.5)
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
SetColor 200,200,200
Local box: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
DrawPoly(box)
'DrawRect (x)+totalwidth*w,y+(h/1.5),w/2,h-(h/1.5)
End If
End Method
End Class
Class MyGame Extends App
Field mybuilding:List<building>
Field time:Int=Millisecs()
Field hw:Int=48,hh:Int=64
Method OnCreate()
Seed = GetDate[4]*GetDate[5]
SetUpdateRate(2)
makehouses()
End Method
Method OnUpdate()
If KeyHit(KEY_SPACE) Or Millisecs() > time
time=Millisecs()+2000
makehouses
End If
End Method
Method OnRender()
Cls 0,0,0
SetColor 50,155,255
DrawRect 0,0,DeviceWidth,150+hh
SetColor 50,125,235
DrawRect 0,100,DeviceWidth,(150+hh)-100
SetColor 5,250,5
DrawRect 0,150+hh,DeviceWidth,DeviceHeight-(150+hh)
SetColor 125,250,125
DrawRect 0,150+hh,DeviceWidth,2
For Local i:=Eachin mybuilding
i.draw(hw,hh)
Next
End Method
Method makehouses()
mybuilding = New List<building>
hw = Rnd(20,50)
hh = hw
Local st:Int=hw*4
Local x:Int=0
While x<DeviceWidth
Local z1:Bool
If Rnd(5)<1 Then z1 = True
Local w:Int = Rnd(1,4)
st = hw*(w+2)
' mybuilding = New building(x,150,Rnd(1,4),z1)
mybuilding.AddLast(New building(x,150,w,z1))
x+=st
Wend
End Method
End Class
Function Main()
New MyGame()
End Function