-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGenerator - Tileable Various textured rock pattern.monkey
257 lines (239 loc) · 5.92 KB
/
Generator - Tileable Various textured rock pattern.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
Import mojo
'
' This is the tile class.
' Here a stone tile is created
' using the generate method
Class tile
Field width:Int,height:Int
Field map:Int[][]
Field a:Int[][]
Field tile:Int[][][]
Method New(w:Int,h:Int)
Self.width = w
Self.height = h
map = New Int[width][]
For Local i:Int=0 Until width
map[i] = New Int[height]
Next
tile = New Int[width][][]
For Local i:Int = 0 Until width
tile[i] = New Int[height][]
For Local z:Int=0 Until height
tile[i][z] = New Int[10]
Next
Next
End Method
Method generate(spacing:Int)
' Put a number of stone points on the map
' try to keep a distance between them
Local numpoints:Int=Rnd(width/5,width/2)
For Local i:Int=0 Until numpoints
Local x:Int=Rnd(width)
Local y:Int=Rnd(height)
Local exitloop:Bool=False
While exitloop=False
exitloop = True
If disttootherstone(x,y,i) < spacing
x=Rnd(width)
y=Rnd(height)
exitloop=False
End If
Wend
map[x][y] = i
Next
'
growstone(spacing)
copyintotile(0)
shadeedges(0)
'
For Local i:Int=1 Until 5
preparecenter(spacing)
growstone(spacing)
copyintotile(i)
shadeedges(i)
Next
End Method
Method copyintotile(num:Int)
For Local y:Int=0 Until height
For Local x:Int=0 Until width
tile[x][y][num] = map[x][y]
Next
Next
End Method
Method preparecenter(spacing:Int)
'
' Erase the center and create a new center
'
For Local y:Int=2 Until height-2
For Local x:Int=2 Until width-2
map[x][y] = 0
Next
Next
' Create a few new points
Local numpoints:Int=Rnd(1,4)
For Local i:Int=40 Until 40+numpoints
'map[Rnd(9,width-9)][Rnd(9,height-9)] = i
Local x:Float=Rnd(9,width-9)
Local y:Float=Rnd(9,height-9)
Local angle:Int=Rnd(360)
Local d:Int=Rnd(3,(width+height)/5)
For Local ii:Int=0 Until d
x+=Cos(angle)*1
y+=Sin(angle)*1
map[x][y] = i
For Local iii:Int=0 Until 2
map[x+Rnd(-2,2)][y+Rnd(-2,2)] = i
Next
If disttootherstone(x,y,i) < spacing Then Exit
Next
Next
'
End Method
Method growstone(spacing:Int)
' Grow the stone points
For Local i:Int=0 Until (width*height)*10
Local x:Int=Rnd(width)
Local y:Int=Rnd(height)
If map[x][y] > 0
If disttootherstone(x,y,map[x][y]) < spacing Then Continue
For Local y2:Int=y-1 To y+1
For Local x2:Int=x-1 To x+1
If x2<0 Or y2<0 Or x2>=width Or y2>=height Then Continue
If Rnd(5)<2 Then
If map[x2][y2] = 0
If x2 = 0 Then map[width-1][y2] = map[x][y]
If x2 = width-1 Then map[0][y2] = map[x][y]
If y2 = 0 Then map[x2][height-1] = map[x][y]
If y2 = height-1 Then map[x2][0] = map[x][y]
map[x2][y2] = map[x][y]
End If
End If
Next
Next
End If
Next
End Method
' Add ligther and darker pixels ontop of the stones
' value 1 to <100 is each seperate stone
' value 200 is light color 100 is dark color
Method shadeedges(mytile:Int)
For Local y:Int=0 Until height
For Local x:Int=0 Until width
If tile[x][y][mytile] > 0 And tile[x][y][mytile] <> 200
If x-1 >=0 And tile[x-1][y][mytile] = 0
For Local x2:Int=x+2 To x+4
If Rnd(2)<1
If x2>=0 And x2<width And tile[x2][y][mytile] >0 Then tile[x2][y][mytile] = 100
End If
Next
End If
If x-1 >=0 And y-1>=0 And tile[x-1][y-1][mytile] = 0
tile[x][y][mytile] = 100
End If
If x+1 < width And tile[x+1][y][mytile] = 0
For Local x2:Int=x-4 To x+2
If Rnd(2)<1
If x2>=0 And x2<width And tile[x2][y][mytile] >0 Then tile[x2][y][mytile] = 200
End If
Next
End If
End If
Next
Next
End Method
' Returns the shortest distance to any other stone part then
' the currentstore
Method disttootherstone:Int(sx:Int,sy:Int,currentstone:Int)
Local shortest:Int=9999
For Local y:Int=0 Until height
For Local x:Int=0 Until width
If map[x][y] <> 0 And map[x][y] <> currentstone
Local d:Int=distance(sx,sy,x,y)
If d<shortest Then shortest = d
End If
Next
Next
Return shortest
End Method
' Draw the tile at x and y position and tw=size
Method draw(thetile:Int,sx:Int,sy:Int,tw:Int,th:Int)
Local x:Int
Local y:Int
For y=0 Until height
For x=0 Until width
Local t:Int
t = tile[x][y][thetile]
Local x2:Int=x*tw
Local y2:Int=y*th
x2+=sx
y2+=sy
If t >= 1 Then 'grey base color
SetColor 100,100,100
End If
If t=100 Then SetColor 40,40,40 'dark shade color
If t=200 Then SetColor 140,140,140 'light shade color
If t>0 ' draw a rect (part of the stone)
DrawRect x2,y2,tw,th
End If
Next
Next
End Method
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
End Class
Class MyGame Extends App
Field mytile:tile
Field cnt:Int=0
Field map:Int[][]
Field view:Int
Method OnCreate()
Seed = GetDate[4]*GetDate[5]
SetUpdateRate(1)
mytile = New tile(32,32)
mytile.generate(6)
map = New Int[30][]
For Local i:Int=0 Until 30
map[i] = New Int[30]
Next
For Local y:Int=0 Until 30
For Local x:Int=0 Until 30
map[x][y] = Rnd(0,5)
Next
Next
End Method
Method OnUpdate()
cnt+=1
If KeyHit(KEY_SPACE) Or cnt>3
If Rnd(10)<5 Then view = 0 Else view = 1
cnt = 0
mytile = New tile(32,32) 'reset/create new tile
Local spacing:Int=Rnd(4,10) ' set random spacing
If Rnd(3)<1 Then spacing=4
mytile.generate(spacing) ' generate the tile
End If
End Method
Method OnRender()
Cls 0,0,0
If view=0
For Local y:Int=0 Until 20
For Local x:Int=0 Until 20
Local x2:Int=x*32
Local y2:Int=y*32
mytile.draw(map[x][y],x2,y2,1,1)
Next
Next
Else
For Local y:Int=0 Until 10
For Local x:Int=0 Until 10
Local x2:Int=x*32*2
Local y2:Int=y*32*2
mytile.draw(map[x][y],x2,y2,2,2)
Next
Next
End If
End Method
End Class
Function Main()
New MyGame()
End Function