-
Notifications
You must be signed in to change notification settings - Fork 8
/
Generator - 2d Maps using lines.monkey
131 lines (125 loc) · 3.23 KB
/
Generator - 2d Maps using lines.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
' Create a map with lines.
' It works by randomly selecting a point on the map
' and the drawing into a random direction. We stop
' drawing if we are close to a previously drawn line.
'
Import mojo
Class map
Field screenwidth:Int
Field screenheight:Int
Field mapwidth:Int
Field mapheight:Int
Field tilewidth:Float
Field tileheight:Float
Field map:Int[][]
Field spacing:Int
Method New(sw:Int,sh:Int,mw:Int,mh:Int,spacing:Int)
Self.screenwidth = sw
Self.screenheight = sh
Self.mapwidth = mw
Self.mapheight = mh
Self.tilewidth = Float(sw) / Float(mw)
Self.tileheight = Float(sh) / Float(mh)
map = New Int[mapwidth][]
For Local i:Int=0 Until mapwidth
map[i] = New Int[mapheight]
Next
Self.spacing = spacing
createmap()
End Method
Method createmap()
' Loop a number of times
For Local i:Int=0 Until (mapwidth+mapheight)
' Get a random x and y spot on the map
Local x:Float=Rnd(mapwidth)
Local y:Float=Rnd(mapheight)
' Chose an angle
Local angle:Int=Rnd(360)
' We will draw d into angle it's direction
Local d:Int=Rnd(3,mapwidth/3)
Local xitloop:Bool=False
' We change the angle and distance 3 times
For Local iii:Int=0 Until 3
' Loop the distance
For Local ii:Int=0 Until d
' If spot taken with 1 or out of screen
' then exit the loop
If maptaken(x-spacing,y-spacing,spacing*2,spacing*2) Then xitloop=True ; Exit
' Put value 2 into the map
map[x][y] = 2
' Next x and y position
x+=Cos(angle)*1
y+=Sin(angle)*1
Next
' Exit the loop(spot taken)
If xitloop=True Then Exit
' Change angle and distance
angle+=Rnd(-90,90)
d=Rnd(3,mapwidth/3)
Next
' Turn all new drawn 2 value's into
' value of 1
For Local y:Int=0 Until mapheight
For Local x:Int=0 Until mapwidth
If map[x][y] = 2 Then map[x][y] = 1
Next
Next
Next
End Method
' See if the area selected is outside the
' screen or if the map value is 1
Method maptaken:Bool(x:Int,y:Int,w:Int,h:Int)
For Local y2:Int=y Until y+h
For Local x2:Int=x Until x+w
If x2<0 Or x2>=mapwidth Or y2<0 Or y2>=mapheight Then Return True
If map[x2][y2] = 1 Then Return True
Next
Next
Return False
End Method
' Draw the map
Method draw()
For Local y:Int=0 Until mapheight
For Local x:Int=0 Until mapwidth
If map[x][y] = 0 Then Continue
SetColor 255,255,255
Local x2:Int=x*tilewidth
Local y2:Int=y*tileheight
DrawRect x2,y2,tilewidth+1,tileheight+1
Next
Next
End Method
End Class
Class MyGame Extends App
Field mymap:map
Field cnt:Int=0
Method OnCreate()
Seed = GetDate[4] * GetDate[5]
SetUpdateRate(1)
createrandommap()
End Method
Method OnUpdate()
cnt+=1
If KeyHit(KEY_SPACE) Or cnt > 1
cnt=0
createrandommap()
End If
End Method
Method OnRender()
Cls 0,0,0
' Draw the map
mymap.draw()
SetColor 255,255,255
Local mw:Int=mymap.mapwidth
Local mh:Int=mymap.mapheight
Local sp:Int=mymap.spacing
DrawText "Width : "+mw+" Height : "+mh+" Spacing : "+sp,0,0
End Method
Method createrandommap()
Local size:Int=Rnd(20,200)
mymap = New map(DeviceWidth,DeviceHeight,size,size,Rnd(2,6))
End Method
End Class
Function Main()
New MyGame()
End Function