-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
165 lines (144 loc) · 5.06 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
local Raycaster = require 'raycasting'
local worldMap = {
{ 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,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1 },
{ 1,0,1,0,1,1,1,0,1,0,1,1,1,1,0,0,0,1,1,1,1,0,0,1 },
{ 1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1 },
{ 1,0,1,0,0,0,1,0,1,0,1,0,0,1,0,0,0,1,0,1,0,1,0,1 },
{ 1,0,1,0,0,0,1,0,1,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1 },
{ 1,0,2,1,1,0,1,0,1,0,1,0,0,1,1,0,1,1,1,1,1,1,1,1 },
{ 1,0,0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{ 1,0,0,0,0,0,1,0,1,0,1,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,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,0,1 },
{ 1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,1 },
{ 1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1 },
{ 1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,1,1,0,1,1,1 },
{ 1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1 },
{ 1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1 },
{ 1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,1,0,0,1 },
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1 },
{ 1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1 },
{ 1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1 },
{ 1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1 },
{ 1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1 },
{ 1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,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 },
}
local raycaster = Raycaster()
local wW = love.graphics.getWidth()
local wH = love.graphics.getHeight()
player = {}
player.pos = {}
player.dir = {}
player.plane = {}
player.pos.x = 10.5
player.pos.y = 10.5
player.dir.x = 0
player.dir.y = 0
player.plane.x = 0
player.plane.y = 0
player.plane.angle = 0
player.theta = math.pi / 2
camera = {}
camera.fov = 90
camera.skew = 0
camera.stretch = 0
var = 1
local gamma = 0
function love.load()
tilebrick = love.image.newImageData("textures/redbrick.png")
texture1 = {}
for i = 1,tilebrick:getHeight() do
texture1[i] = {}
for j = 1,tilebrick:getHeight() do
texture1[i][j] = {}
texture1[i][j].r, texture1[i][j].g, texture1[i][j].b = tilebrick:getPixel(i-1,j-1)
end
end
texture1.width = tilebrick:getWidth()
texture1.height = tilebrick:getHeight()
raycaster:init()
end
function love.update(dt)
if love.keyboard.isDown('kp+') then
var = var + 0.01
elseif love.keyboard.isDown('kp-') then
var = var - 0.01
end
--gamma = gamma + 0.001
--var = 1 + 0.7 * math.sin(gamma)
if love.keyboard.isDown('left') then
player.theta = player.theta + (90 * math.pi / 180) * dt
elseif love.keyboard.isDown('right') then
player.theta = player.theta - (90 * math.pi / 180) * dt
end
if love.keyboard.isDown('up', 'down') then
local newpos = {}
if love.keyboard.isDown('up') then
newpos.x = player.pos.x + 3 * math.cos(player.theta) * dt
newpos.y = player.pos.y + 3 * math.sin(player.theta) * dt
elseif love.keyboard.isDown('down') then
newpos.x = player.pos.x - 3 * math.cos(player.theta) * dt
newpos.y = player.pos.y - 3 * math.sin(player.theta) * dt
end
if worldMap[math.floor(newpos.x)][math.floor(newpos.y)] == 0 then
player.pos.x = newpos.x
player.pos.y = newpos.y
elseif worldMap[math.floor(player.pos.x)][math.floor(newpos.y)] == 0 then
player.pos.y = newpos.y
elseif worldMap[math.floor(newpos.x)][math.floor(player.pos.y)] == 0 then
player.pos.x = newpos.x
end
end
if love.keyboard.isDown('q') then
camera.skew = camera.skew + dt;
elseif love.keyboard.isDown('w') then
camera.skew = camera.skew - dt;
end
player.dir.x = math.cos(player.theta)
player.dir.y = math.sin(player.theta)
player.plane.x = var * math.sin(player.theta + camera.skew)
player.plane.y = var * -math.cos(player.theta + camera.skew)
raycaster:update(dt, worldMap)
end
function love.draw()
raycaster:draw()
-- Minimap scale factor and variable importing
local factor = 10
local pos = {}
local dir = {}
local plane = {}
pos.x = player.pos.y * factor
pos.y = player.pos.x * factor
dir.x = player.dir.y * factor
dir.y = player.dir.x * factor
plane.x = player.plane.y * factor
plane.y = player.plane.x * factor
-- Player location on the minimap
love.graphics.setColor(1,1,1,1)
love.graphics.line(pos.x, pos.y, pos.x + dir.x, pos.y + dir.y)
love.graphics.line(pos.x + dir.x, pos.y + dir.y, pos.x + dir.x + plane.x, pos.y + dir.y + plane.y)
love.graphics.line(pos.x + dir.x, pos.y + dir.y, pos.x + dir.x - plane.x, pos.y + dir.y - plane.y)
love.graphics.setColor(1,0,0,1)
love.graphics.points(pos.x, pos.y)
love.graphics.setColor(0,1,0,1)
love.graphics.points(pos.x + dir.x, pos.y + dir.y)
love.graphics.setColor(0,0,1,1)
love.graphics.points(pos.x + dir.x, pos.y + dir.y, pos.x + dir.x + plane.x, pos.y + dir.y + plane.y)
love.graphics.points(pos.x + dir.x, pos.y + dir.y, pos.x + dir.x - plane.x, pos.y + dir.y - plane.y)
-- Minimap tiles
for i = 1,#worldMap do
for j = 1,#worldMap[i] do
if worldMap[i][j] > 0 then
love.graphics.setColor(0,0,1,1)
love.graphics.polygon('line', j*factor, i*factor, (j+1)*factor, i*factor, (j+1)*factor, (i+1)*factor, j*factor, (i+1)*factor)
end
end
end
end
function love.keypressed(key)
if key == 'escape' then
love.event.quit()
end
end