-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.lua
238 lines (204 loc) · 6.58 KB
/
main.lua
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
-----------------------------------------------------------------------------------
--------Raycasting code written by Ben White (scutheotaku), 2012 ------------------
--------Based on code by Lode Vandevenne (http://lodev.org/cgtutor/raycasting.html)
-----------------------------------------------------------------------------------
-------
------
-----
----
---
--
--setup map
map={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1},
{1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1},
{1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,0,0,0,0,5,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
function love.load()
posX = 22
posY = 12
dirX = -1
dirY = 0
planeX = 0
planeY = 0.66
w = 512
h = 384
love.graphics.setMode(w, h, false, true, 0)
drawScreenLineStart = {}
drawScreenLineEnd = {}
drawScreenLineColor = {}
end
function love.update(dt)
for x = 0, w, 1 do
local cameraX = 2 * x / w - 1
local rayPosX = posX
local rayPosY = posY
local rayDirX = dirX + planeX * cameraX
local rayDirY = dirY + planeY * cameraX
local mapX = math.floor(rayPosX)
local mapY = math.floor(rayPosY)
local sideDistX
local sideDistY
local deltaDistX = math.sqrt(1 + (rayDirY ^ 2) / (rayDirX ^ 2))
local deltaDistY = math.sqrt(1 + (rayDirX ^ 2) / (rayDirY ^ 2))
local perWallDist
local stepX
local stepY
local hit = 0
local side = 0
if (rayDirX < 0) then
stepX = -1
sideDistX = (rayPosX - mapX) * deltaDistX
else
stepX = 1
sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX
end
if (rayDirY < 0) then
stepY = -1
sideDistY = (rayPosY - mapY) * deltaDistY
else
stepY = 1
sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY
end
while (hit == 0) do
if (sideDistX < sideDistY) then
sideDistX = sideDistX + deltaDistX
mapX = mapX + stepX
side = 0
else
sideDistY = sideDistY + deltaDistY
mapY = mapY + stepY
side = 1
end
if (map[mapX][mapY] > 0) then
hit = 1
end
end
if (side == 0)then
perpWallDist = math.abs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX)
else
perpWallDist = math.abs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY)
end
lineHeight = math.abs(math.floor(h / perpWallDist))
drawStart = -lineHeight / 2 + h / 2
if (drawStart < 0) then drawStart = 0 end
drawEnd = lineHeight / 2 + h / 2
if (drawEnd >= h) then drawEnd = h - 1 end
if (map[mapX][mapY] == 1) then
if (side == 1) then
drawScreenLineColor[x] = {127,0,0,255}
else
drawScreenLineColor[x] = {255,0,0,255}
end
elseif (map[mapX][mapY] == 2) then
if (side == 1) then
drawScreenLineColor[x] = {0,127,0,255}
else
drawScreenLineColor[x] = {0,255,0,255}
end
elseif (map[mapX][mapY] == 3) then
if (side == 1) then
drawScreenLineColor[x] = {0,0,127,255}
else
drawScreenLineColor[x] = {0,0,255,255}
end
elseif (map[mapX][mapY] == 4) then
if (side == 1) then
drawScreenLineColor[x] = {127,127,127,255}
else
drawScreenLineColor[x] = {255,255,255,255}
end
else
if (side == 1) then
drawScreenLineColor[x] = {127,127,0,255}
else
drawScreenLineColor[x] = {255,255,0,255}
end
end
drawScreenLineStart[x] = drawStart
drawScreenLineEnd[x] = drawEnd
end
moveSpeed = dt * 5.0
rotSpeed = dt * 3.0
strafeSpeed = dt * 5.0
if love.keyboard.isDown("w") then
if (map[math.floor(posX + dirX * moveSpeed)][math.floor(posY)] == 0) then
posX = posX + dirX * moveSpeed
end
if (map[math.floor(posX)][math.floor(posY + dirY * moveSpeed)] == 0) then
posY = posY + dirY * moveSpeed
end
end
if love.keyboard.isDown("s") then
if (map[math.floor(posX - dirX * moveSpeed)][math.floor(posY)] == 0) then
posX = posX - dirX * moveSpeed
end
if (map[math.floor(posX)][math.floor(posY - dirY * moveSpeed)] == 0) then
posY = posY - dirY * moveSpeed
end
end
if love.keyboard.isDown("d") then
if (map[math.floor(posX + planeX * moveSpeed)][math.floor(posY)] == 0) then
posX = posX + planeX * strafeSpeed
end
if (map[math.floor(posX)][math.floor(posY + planeY * moveSpeed)] == 0) then
posY = posY + planeY * strafeSpeed
end
end
if love.keyboard.isDown("a") then
if (map[math.floor(posX - planeX * moveSpeed)][math.floor(posY)] == 0) then
posX = posX - planeX * strafeSpeed
end
if (map[math.floor(posX)][math.floor(posY - planeY * moveSpeed)] == 0) then
posY = posY - planeY * strafeSpeed
end
end
if love.keyboard.isDown("right") then
oldDirX = dirX
dirX = dirX * math.cos(-rotSpeed) - dirY * math.sin(-rotSpeed)
dirY = oldDirX * math.sin(-rotSpeed) + dirY * math.cos(-rotSpeed)
oldPlaneX = planeX
planeX = planeX * math.cos(-rotSpeed) - planeY * math.sin(-rotSpeed)
planeY = oldPlaneX * math.sin(-rotSpeed) + planeY * math.cos(-rotSpeed)
end
if love.keyboard.isDown("left") then
oldDirX = dirX
dirX = dirX * math.cos(rotSpeed) - dirY * math.sin(rotSpeed)
dirY = oldDirX * math.sin(rotSpeed) + dirY * math.cos(rotSpeed)
oldPlaneX = planeX
planeX = planeX * math.cos(rotSpeed) - planeY * math.sin(rotSpeed)
planeY = oldPlaneX * math.sin(rotSpeed) + planeY * math.cos(rotSpeed)
end
end
function love.draw()
for x = 0, w, 1 do
love.graphics.setColor(drawScreenLineColor[x])
love.graphics.line(x, drawScreenLineStart[x], x, drawScreenLineEnd[x])
end
love.graphics.setColor(255, 12, 12)
love.graphics.print("FPS: "..tostring(love.timer.getFPS( )), 10, 10, 0, 3)
end
function love.keypressed(key, unicode)
if key == " " then love.graphics.toggleFullscreen() end
end