-
Notifications
You must be signed in to change notification settings - Fork 8
/
Example - Growing Slime Monster in mine map.monkey
314 lines (298 loc) · 8.2 KB
/
Example - Growing Slime Monster in mine map.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
Import mojo
Global mapwidth:Int=200
Global mapheight:Int=100
'
' This is a growing slime entity.
'
'
Class growslime
Field map:Int[][]
Field w:Float,h:Float
Field tw:Float,th:Float
Field openx:Stack<Int>
Field openy:Stack<Int>
Field slimetile:Int=10
Field slimestartx:Int,slimestarty:Int
Method New()
w = mymaptest.w * 2
h = mymaptest.h * 2
tw = 640 / w
th = 480 / h
map = New Int[w][]
For Local i:Int=0 Until w
map[i] = New Int[h]
Next
'copy the map from the game into this map
For Local y:Int=0 Until mymaptest.h
For Local x:Int=0 Until mymaptest.w
For Local y2:Int=0 Until 2
For Local x2:Int=0 Until 2
map[(x*2)+x2][(y*2)+y2] = mymaptest.map[x][y]
Next
Next
Next
Next
'create the active slime list
openx = New Stack<Int>
openy = New Stack<Int>
findslimestartpos()
openx.Push(slimestartx)
openy.Push(slimestarty)
map[slimestartx][slimestarty]=slimetile
End Method
Method findslimestartpos()
For Local y:Int=h-1 To 0 Step -1
For Local x:Int=0 Until w
If map[x][y] = 1 Then
slimestartx = x
slimestarty = y
Return
End If
Next
Next
End Method
Method update(speed:String)
Local freq:Int
If speed = "slow" Then freq = 120 Else freq = 20
' Expand Slime
For Local i:Int=0 Until openx.Length
If Rnd(freq) > 2 Then Continue
Local x2:Int=openx.Get(i)
Local y2:Int=openy.Get(i)
'bottom bleft or bright first
Local r:Int=Rnd(0,6)
If r=0 And y2+1<h And map[x2][y2+1] = 1 Then addslime(x2,y2+1) ; Continue
If r=1 And x2-1 >=0 And y2+1 <h And map[x2-1][y2+1] = 1 Then addslime(x2-1,y2+1) ; Continue
If r=2 And y2+1<h And map[x2+1][y2+1] = 1 Then addslime(x2+1,y2+1) ; Continue
' left Or right Then
r = Rnd(0,2)
If r=0 And x2-1>=0 And map[x2-1][y2] = 1 Then addslime(x2-1,y2) ; Continue
If r=1 And x2+1<w And map[x2+1][y2] = 1 Then addslime(x2+1,y2) ; Continue
' up lup and rup
r = Rnd(0,23)
If r=0 And y2-1>=0 And map[x2][y2-1] = 1 Then addslime(x2,y2-1);Continue
If r=1 And x2-1>=0 And y2-1>=0 And map[x2-1][y2-1] = 1 Then addslime(x2-1,y2-1);Continue
If r=2 And x2+1<w And y2-1>=0 And map[x2+1][y2-1] = 1 Then addslime(x2+1,y2-1);Continue
Next
' Remove Obsolete slime
For Local i:Int=0 Until openx.Length
Local cnt:Int=0
For Local y:Int=-1 To 1
For Local x:Int=-1 To 1
Local x2:Int=openx.Get(i)+x
Local y2:Int=openy.Get(i)+y
If x2<0 Or y2<0 Or x2>=w Or y2>=h Then
cnt+=1
Continue
End If
If map[x2][y2] = slimetile Then cnt+=1
Next
Next
If cnt=9 Then
openx.Remove(i)
openy.Remove(i)
End If
Next
End Method
Method addslime(sx:Int,sy:Int)
openx.Push(sx)
openy.Push(sy)
map[sx][sy] = slimetile
End Method
Method update_vine(speed:String)
Local freq:Int
If speed = "slow" Then freq = 200 Else freq = 20
' Expand Slime
For Local i:Int=0 Until openx.Length
For Local y:Int=-1 To 1
For Local x:Int=-1 To 1
If Rnd(freq)>2 Then Continue
Local x2:Int=openx.Get(i)+x
Local y2:Int=openy.Get(i)+y
If x2<0 Or y2<0 Or x2>=w Or y2>=h Then Continue
If map[x2][y2] = 1 Then
openx.Push(x2)
openy.Push(y2)
map[x2][y2] = slimetile
End If
Next
Next
Next
' Remove Obsolete slime
For Local i:Int=0 Until openx.Length
Local cnt:Int=0
For Local y:Int=-1 To 1
For Local x:Int=-1 To 1
Local x2:Int=openx.Get(i)+x
Local y2:Int=openy.Get(i)+y
If x2<0 Or y2<0 Or x2>=w Or y2>=h Then
cnt+=1
Continue
End If
If map[x2][y2] = slimetile Then cnt+=1
Next
Next
If cnt=9 Then
openx.Remove(i)
openy.Remove(i)
End If
Next
End Method
Method draw()
' Draw the solid slimes ()
For Local y:Float=0 Until h
For Local x:Float=0 Until w
Local x1:Float=x*tw
Local y1:Float=y*th
If map[x][y] = slimetile
SetColor 20,200,10
DrawOval x1,y1,tw+1,th+1
End If
Next
Next
End Method
End Class
Class maptest
Field tw:Float,th:Float
Field w:Int,h:Int
'bottom x and y contain the coords of the next
'shaft to be created. center of room last pass
Field bottomy:Int
Field bottomx:Int
Field map:Int[][]
Method New(w:Int,h:Int)
Self.w = w
Self.h = h
tw = DeviceWidth()/w
th = DeviceHeight()/h
map = New Int[w][]
For Local i=0 Until w
map[i] = New Int[h]
Next
drawmaprect(0,0,w-1,15)
For Local i=0 Until h
map[1][i] = 0
map[w-2][i] = 0
Next
' x,y,number of tunnels>>
makemine(w/2,15,Rnd(1,3))
makemine(bottomx,bottomy,Rnd(1,3))
If bottomy<(Float(mapheight)/2)
makemine(bottomx,bottomy,Rnd(1,3))
End If
End Method
Method makemine(x:Int,y:Int,depth:Int)
Local vy:Int=y
For Local mydepth=0 Until depth
Local d1:Int=Rnd(8,16)'depth
tunneldown(x,y,d1)
y+=d1
Local d2:Int=Rnd(1,4)'direction
If d2=1 Then
sidetunnel(x,y,"left")
End If
If d2=2 Then
sidetunnel(x,y,"right")
End If
If d2=3 Then
sidetunnel(x,y,"left")
sidetunnel(x,y,"right")
End If
Next
'For Local y1=vy Until bottomy+2
' map[x][y1] = 2
' Next
End Method
Method sidetunnel(x:Int,y:Int,d:String)
If d="left"
Local width:Int=Rnd(5,15)
drawmaprect(x-width+2,y,width,3)
Local roomw:Int=Rnd(5,15)
drawmaprect(x-width+2-roomw,y-1,roomw,5)
For Local x1=0 Until roomw/3
map[(x-width+2-roomw)+x1][y+4] = 3
Next
bottomx = x-width-(roomw/2)
bottomy = y
End If
If d="right"
Local width:Int=Rnd(5,15)
drawmaprect(x-1,y,width,3)
Local roomw:Int=Rnd(5,15)
drawmaprect(x+width,y-1,roomw,5)
For Local x1=roomw Until roomw/1.5 Step -1
map[(x+width)+x1][y+4] = 3
Next
bottomx = x+width+(roomw/2)
bottomy = y
End If
End Method
Method tunneldown(x:Int,y:Int,d:Int)
drawmaprect(x-2,y,4,d)
End Method
Method drawmaprect(x:Int,y:Int,w:Int,h:Int)
For Local y1=y To y+h
For Local x1=x To x+w
map[x1][y1] = 1
Next
Next
End Method
Method draw()
For Local y=0 Until h
For Local x=0 Until w
Local x1:Float=DeviceWidth()/Float(mapwidth)*Float(x)
Local y1:Float=DeviceHeight/Float(mapheight)*Float(y)
If map[x][y] = 1
SetColor 255,255,255
DrawRect x1,y1,tw+1,th+1
End If
If map[x][y] = 3
SetColor 200,200,10
DrawOval x1,y1,tw+1,th+1
End If
Next
Next
End Method
End Class
' -----------------------------------------------------------------------------------------------
Global mymaptest:maptest
Global mygrowslime:growslime
Class MyGame Extends App
Field nmap:Int=0
Method OnCreate()
Local date := GetDate()
Seed = date[5]
SetUpdateRate(60)
restartgame
End Method
Method OnUpdate()
nmap+=1
If KeyDown(KEY_SPACE)=True Or nmap>3500
restartgame
nmap=0
End If
mygrowslime.update("fast")
End Method
Method OnRender()
Cls 0,0,0
mymaptest.draw()
mygrowslime.draw()
SetColor 255,255,0
DrawText "MonkeyX - Growing Slime in the Mines Example",20,0
End Method
End Class
Function Main()
New MyGame()
End Function
Function restartgame()
mymaptest = New maptest(mapwidth,mapheight)
mygrowslime = New growslime()
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