-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGenerator - Dungeon-cave random rect.monkey
More file actions
167 lines (157 loc) · 3.85 KB
/
Generator - Dungeon-cave random rect.monkey
File metadata and controls
167 lines (157 loc) · 3.85 KB
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
Import mojo
Class tilemap
Field tilew:Int
Field tileh:Int
Field width:Int
Field height:Int
Field map:Int[][]
Method New(width:Int,height:Int,tilew:Int,tileh:Int)
Self.width=width
Self.height=height
Self.tilew=tilew
Self.tileh=tileh
map = New Int[width][]
For Local i=0 Until width
map[i] = New Int[height]
Next
makemap
settiles
End Method
' Here we create the walls
Method settiles()
'create walls
For Local y=1 Until height-1
For Local x=1 Until width-1
Local t:Int=map[x][y]
If t=0 And map[x+1][y] = 1 Then map[x][y] = 2
If t=0 And map[x][y+1] = 1 Then map[x][y] = 2
If t=0 And map[x-1][y] = 1 Then map[x][y] = 2
If t=0 And map[x][y-1] = 1 Then map[x][y] = 2
Next
Next
' get corners
For Local y=1 Until height-1
For Local x=1 Until width-1
Local t:Int=map[x][y]
If t=0 And map[x+1][y] = 2 And map[x][y+1] = 2 Then map[x][y] = 3
If t=0 And map[x-1][y] = 2 And map[x][y+1] = 2 Then map[x][y] = 3
If t=0 And map[x][y-1] = 2 And map[x+1][y] = 2 Then map[x][y] = 3
If t=0 And map[x-1][y] = 2 And map[x][y-1] = 2 Then map[x][y] = 3
Next
Next
'set corners to value 2
For Local y=1 Until height-1
For Local x=1 Until width-1
If map[x][y] = 3 Then map[x][y] = 2
Next
Next
End Method
Method makemap()
'here we make random rectangles and see if they
'are placed next to another rectangle.
'This will grow a random level...
Local w1:Int=Rnd(1,4)
Local h1:Int=Rnd(1,4)
Local x1:Int=width/2
Local y1:Int=height/2
drawmaprect(x1,y1,w1,h1)
Local maxrooms:Int=(width*height)/30
Local numrooms:Int=0
Local cnt:Int=0
While numrooms < maxrooms And cnt<15000
cnt+=1
Local done:Bool=False
w1=Rnd(1,4)
h1=Rnd(1,4)
x1=Rnd(3,width-5)
y1=Rnd(3,height-5)
'check right
If map[x1][y1] = 1 And map[x1+1][y1] = 0 And map[x1+1][y1-2] = 0 And map[x1+1][y1+2] = 0
drawmaprect(x1,y1,w1,h1)
numrooms+=1
done=True
End If
'check bottom
If done=False And map[x1][y1] = 1 And map[x1][y1+1] = 0 And map[x1-2][y1+1] = 0 And map[x1+2][y1+1] = 0
drawmaprect(x1,y1,w1,h1)
numrooms+=1
done=True
End If
'check top
If done=False And y1+h1<height-1
If map[x1][y1+h1+1] = 1 And map[x1][y1+h1] = 0 And map[x1-2][y1+h1] = 0 And map[x1+2][y1+h1] = 0
drawmaprect(x1,y1,w1,h1)
numrooms+=1
done=True
End If
End If
'check left
If done=False And x1+w1<width-1
If map[x1+w1+1][y1] = 1 And map[x1+w1][y1] = 0 And map[x1+w1][y1-2] = 0 And map[x1+w1][y1+2] = 0
drawmaprect(x1,y1,w1,h1)
numrooms+=1
End If
End If
Wend
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
If x1>-1 And y1>-1 And x1<width And y1<height
map[x1][y1] = 1
End If
Next
Next
End Method
Method draw()
SetColor 255,255,255
For Local y=0 Until height
For Local x=0 Until width
Local t:Int=map[x][y]
Select t
Case 1 'wall
SetColor 100,100,100
DrawRect x*tilew,y*tileh,tilew,tileh
Case 2 'wall
SetColor 200,200,200
DrawRect x*tilew,y*tileh,tilew,tileh
End Select
Next
Next
End Method
End Class
Global m:tilemap
Class MyGame Extends App
Method OnCreate()
Seed = GetDate[5]
SetUpdateRate(60)
createrandommap()
End Method
Method OnUpdate()
If KeyHit(KEY_SPACE) Or MouseHit(MOUSE_LEFT)
createrandommap()
End If
End Method
Method OnRender()
Cls 0,0,0
m.draw
SetColor 255,255,255
DrawText "Press Space or Tab to create new level",0,0
End Method
End Class
Function createrandommap()
Local r:Int=Rnd(0,3)
Local w:Int,h:Int,tw:Int,th:Int
Select r
Case 0
w=20;h=15;tw=32;th=32
Case 1
w=20*2;h=15*2;tw=16;th=16
Case 2
w=20*4;h=15*4;tw=8;th=8
End Select
m = New tilemap(w,h,tw,th)
End Function
Function Main()
New MyGame()
End Function