-
Notifications
You must be signed in to change notification settings - Fork 8
/
Example - Snake Style Water simulation.monkey
259 lines (238 loc) · 6.62 KB
/
Example - Snake Style Water simulation.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
'snake(style) 2d water
Import mojo
Global mapwidth:Int=200
Global mapheight:Int=100
Class watertest
Field map:Int[][]
Field w:Float,h:Float
Field tw:Float,th:Float
Field flowx:Int,flowy:Int
Field dir:String="down"
Method New()
Self.w = mymaptest.w*4
Self.h = mymaptest.h*4
Self.tw = 640/w
Self.th = 480/h
map = New Int[w][]
For Local i=0 Until w
map[i] = New Int[h]
Next
flowx = 10
flowy = 10
For Local y:Int=0 Until mymaptest.h
For Local x:Int=0 Until mymaptest.w
If mymaptest.map[x][y] = 1
For Local y2:Int=0 Until 4
For Local x2:Int=0 Until 4
map[x*4+x2][y*4+y2] = 1
Next
Next
End If
Next
Next
End Method
Method update()
Local a:Bool=False
dir = "new"
If map[flowx][flowy+1] = 1 Then dir="down" ; a=True
If a=False And map[flowx+1][flowy] = 1 Then dir="right"; a=True
If a=False And map[flowx-1][flowy] = 1 Then dir="left" ; a = True
If a=False And map[flowx][flowy-1] = 1 Then dir="up" ; a = True
If dir="left" And map[flowx-2][flowy] = 10 Then map[flowx-2][flowy] = 1
If dir="right" And map[flowx+2][flowy] = 10 Then map[flowx+2][flowy] = 1
map[flowx][flowy]=10
If dir="left"
If map[flowx-1][flowy-1]=10 Then map[flowx-1][flowy-1]=1
Elseif dir="right"
If map[flowx+1][flowy-1]=10 Then map[flowx+1][flowy-1]=1
End If
Select dir
Case "up"
flowy-=1
Case "left"
flowx-=1
Case "right"
flowx+=1
Case "down"
flowy+=1
End Select
If dir="new" Or Rnd(60*20) < 2 Then ' if the snake got stuck then
' find a new spot
dir = "done"
For Local y:Int=h-1 Until 1 Step -1
For Local x:Int=0 Until w
If map[x][y] = 1 And map[x][y+1] = 10 Then
flowx = x
flowy = y
Return
End If
Next
Next
End If
End Method
Method draw()
For Local y:Int=0 Until h
For Local x:Int=0 Until w
Local x1:Float=x*tw
Local y1:Float=y*th
If map[x][y] = 10
SetColor 0,0,255
DrawRect 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 mywatertest:watertest
Class MyGame Extends App
Field nmap:Int=0
Method OnCreate()
Local date := GetDate()
Seed = date[5]
SetUpdateRate(60)
restartgame
End Method
Method OnUpdate()
For Local i:Int=0 Until 6
mywatertest.update()
Next
If levelfilled() Then restartgame
End Method
Method OnRender()
Cls 0,0,0
mymaptest.draw()
mywatertest.draw()
SetColor 255,255,0
DrawText "MonkeyX - 2d Mining map and snake style water.",20,0
End Method
End Class
Function Main()
New MyGame()
End Function
Function levelfilled:Bool()
If Rnd(120)>2 Then Return False
Local cnt:Int=0
For Local y:Int=10 Until 60
For Local x:Int= 0 Until mywatertest.w
If mywatertest.map[x][y] = 10 Then cnt+=1
If cnt>100 Then Return True
Next
Next
Return False
End Function
Function restartgame()
mymaptest = New maptest(mapwidth,mapheight)
mywatertest = New watertest()
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