-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMonkey-X - 2d Ray Casting on small map - code example.monkey
132 lines (120 loc) · 3.68 KB
/
Monkey-X - 2d Ray Casting on small map - code example.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
Import mojo
Global mapwidth:Int=10
Global mapheight:Int=10
Global tilewidth:Int=640/mapwidth
Global tileheight:Int=480/mapheight
Global map:Int[][] = [ [1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,1,1,1,1],
[1,0,0,1,0,0,0,0,0,1],
[1,0,0,1,0,0,0,0,0,1],
[1,1,1,1,0,0,0,0,0,1],
[1,0,0,0,0,0,0,1,0,1],
[1,0,0,0,0,0,0,1,0,1],
[1,1,1,1,1,1,1,1,1,1] ]
Global hlmap:Int[mapwidth][]
Class players
Field x:Float,y:Float
Field angle:Int=100
Method New(x:Float,y:Float)
Self.x = x
Self.y = y
End Method
Method update()
angle-=1
If KeyDown(KEY_LEFT) Then angle+=3
If KeyDown(KEY_RIGHT) Then angle-=3
If angle<-180 Then angle = 180
If angle>180 Then angle = -180
'clear the higlight map
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
hlmap[x][y] = 0
Next
Next
'get the angle minus 45 degrees
Local a=angle-45
Local cnt:Int=0
' keep inside angle ragne
If a<0 Then a=a-360
'loop 90 degrees
For Local i=0 Until 90
Local aa=a+i
If aa>360 Then aa=aa+360
' ray cast 320 pixels distance
For Local d=0 Until 320 Step 8
Local x1:Float=((x+tilewidth/2)+(Sin(aa)*d))
Local y1:Float=((y+tileheight/2)+(Cos(aa)*d))
x1/=tilewidth
y1/=tileheight
' if inside map
If x1>-1 And y1>-1 And x1<mapwidth And y1<mapheight
' if wall then exit
If map[y1][x1] = 1 Then
hlmap[x1][y1] = 1
Exit
End If
' set ground
hlmap[x1][y1] = 2
End If
Next
cnt+=1
Next
End Method
Method draw()
SetColor 0,0,255
DrawOval x,y,tilewidth,tileheight
Local x1:Int=x+tilewidth/2
Local y1:Int=y+tileheight/2
DrawLine x1,y1,x1+(Sin(angle)*tilewidth),y1+(Cos(angle)*tileheight)
End Method
End Class
Global p:List<players> = New List<players>
Class MyGame Extends App
Method OnCreate()
SetUpdateRate(60)
For Local i = 0 Until mapwidth
hlmap[i] = New Int[mapheight]
Next
p.AddLast(New players(5*tilewidth,5*tileheight))
End Method
Method OnUpdate()
For Local i:=Eachin p
i.update
Next
End Method
Method OnRender()
Cls 0,0,0
SetColor 255,255,255
drawmap
For Local i:=Eachin p
i.draw
Next
SetColor 255,255,255
DrawText "Ray Casting example.",0,0
End Method
End Class
Function drawmap:Void()
SetColor 200,200,200
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
If map[y][x] = 1
SetColor 200,200,200
DrawRect x*tilewidth,y*tileheight,tilewidth,tileheight
End If
' draw the highlight map (1 =wall 2 = floor
If hlmap[x][y] = 1
SetColor 255,255,255
DrawRect x*tilewidth,y*tileheight,tilewidth,tileheight
End If
If hlmap[x][y] = 2
SetColor 55,55,55
DrawRect x*tilewidth,y*tileheight,tilewidth,tileheight
End If
Next
Next
End Function
Function Main()
New MyGame()
End Function