-
Notifications
You must be signed in to change notification settings - Fork 8
/
Generator - Level - animated 1.monkey
210 lines (196 loc) · 4.95 KB
/
Generator - Level - animated 1.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
Import mojo
Class theline
Field x1:Int,y1:Int,x2:Int,y2:Int
Method New(x1:Int,y1:Int,x2:Int,y2:Int)
Self.x1 = x1
Self.y1 = y1
Self.x2 = x2
Self.y2 = y2
End Method
End Class
Class therect
Field x:Int,y:Int,w:Int,h:Int
Field move:Bool=False
Method New(x:Int,y:Int,w:Int,h:Int)
Self.x = x
Self.y = y
Self.w = w
Self.h = h
End Method
End Class
Class thelevel
Field myl:List<theline> = New List<theline>
Field myr:List<therect> = New List<therect>
Field mapwidth:Int
Field mapheight:Int
Field tilewidth:Int
Field tileheight:Int
Field numrects:Int
Field phase:Int=1 'animate throught the steps
Field timer:Int=0
Field map:Int[][]
Method New(mapwidth:Int,mapheight:Int,numrects:Int)
Self.mapwidth = mapwidth
Self.mapheight = mapheight
Self.tilewidth = DeviceWidth()/mapwidth
Self.tileheight = DeviceHeight()/mapheight
Self.numrects = numrects
map = New Int[mapwidth][]
For Local i = 0 Until mapwidth
map[i] = New Int[mapheight]
Next
For Local i=0 Until numrects
myr.AddLast(New therect(mapwidth/2+Rnd(-10,10),mapheight/2+Rnd(-10,10),Rnd(5,15),Rnd(5,15)))
Next
End Method
Method update()
If phase = 1 'phase 1- <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
For Local i:=Eachin myr
i.move=False
Next
Local nothingdone:Bool=True
For Local i:=Eachin myr
For Local ii:=Eachin myr
If i<>ii
If i.move=False
If rectsoverlap(i.x,i.y,i.w,i.h,ii.x,ii.y,ii.w,ii.h)
If i.x <= ii.x Then i.x -= 1
If i.x >= ii.x Then i.x += 1
If i.y <= ii.y Then i.y -= 1
If i.y >= ii.y Then i.y += 1
i.move = True
nothingdone = False
End If
Endif
Endif
Next
Next
' if done moving
If nothingdone = True Then phase = 2
End If
If phase = 2 ' phase 2 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
myl.Clear()
For Local i:=Eachin myr
For Local ii:=Eachin myr
If i<>ii
If rectsoverlap(i.x-3,i.y-3,i.w+6,i.h+6,ii.x-3,ii.y-3,ii.w+6,ii.h+6)
myl.AddLast(New theline(i.x+i.w/2,i.y+i.h/2,ii.x+ii.w/2,ii.y+ii.h/2))
End If
End If
Next
Next
phase = 3
timer = 0
End If
If phase = 3 ' phase 3 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
timer = timer + 1
If timer > 120
phase = 4
End If
End If
If phase = 4 'phase 4 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
For Local i:=Eachin myl
For Local y=-1 To 1
For Local x=-1 To 1
bline(i.x1+x,i.y1+y,i.x2+x,i.y2+y)
Next
Next
Next
phase = 5
timer = 0
End If
If phase = 5 'phase 5 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
timer += 1
If timer>120 Then phase = 6
End If
End Method
Method draw()
'draw the rooms
SetColor 255,255,255
For Local i:= Eachin myr
DrawRect i.x*tilewidth,i.y*tileheight,i.w*tilewidth,i.h*tileheight
Next
'draw the corridors
SetColor 255,255,255
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
If map[x][y] = 1
DrawRect x*tilewidth,y*tileheight,tilewidth,tileheight
Endif
Next
Next
If phase < 4
' draw the connections
SetColor 255,0,0
For Local i:=Eachin myl
DrawLine i.x1*tilewidth,i.y1*tileheight,i.x2*tilewidth,i.y2*tileheight
Next
Endif
End Method
Method 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 Method
'bresenham line method
Method bline:Void(x1:Int,y1:Int,x2:Int,y2:Int)
Local dx:Int, dy:Int, sx:Int, sy:Int, e:Int
dx = Abs(x2 - x1)
sx = -1
If x1 < x2 Then sx = 1
dy = Abs(y2 - y1)
sy = -1
If y1 < y2 Then sy = 1
If dx < dy Then
e = dx / 2
Else
e = dy / 2
End If
Local exitloop:Bool=False
While exitloop = False
If x1>0 And y1>0 And x1<mapwidth And y1<mapheight
map[x1][y1] = 1
Endif
If x1 = x2
If y1 = y2
exitloop = True
End If
End If
If dx > dy Then
x1 += sx ; e -= dy
If e < 0 Then e += dx ; y1 += sy
Else
y1 += sy ; e -= dx
If e < 0 Then e += dy ; x1 += sx
Endif
Wend
End Method
End Class
Global mylevel:thelevel
Class MyGame Extends App
Method OnCreate()
SetUpdateRate(60)
Seed = GetDate[5]
Local w:Int=Rnd(50,150)
Local h:Int=w
Local c:Int=w/6
mylevel = New thelevel(w,h,c)
End Method
Method OnUpdate()
mylevel.update
If mylevel.phase = 6
Local w:Int=Rnd(50,150)
Local h:Int=w
Local c:Int=w/6
mylevel = New thelevel(w,h,c)
End If
End Method
Method OnRender()
Cls 0,0,0
SetColor 255,255,255
mylevel.draw
End Method
End Class
Function Main()
New MyGame()
End Function