-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
92 lines (80 loc) · 2.15 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
--Bresenham's field of view demonstration
--by Spellweaver (spellsweaver@gmail.com)
fov = require ('FoV')
function wheremouse() --returns x,y of the cell mouse is pointed at
local cellx = math.ceil(love.mouse.getX()/cell_width)
cellx=math.max(cellx,0)
local celly = math.ceil(love.mouse.getY()/cell_height)
celly=math.max(celly,0)
return {x=cellx,y=celly}
end
function love.load()
cell_width,cell_height=20,20
love.window.setMode (50*cell_width,50*cell_height,{fullscreen=false,vsync=true,resizable=false,borderless=false})
love.window.setTitle ("Field of View demonstration")
map={}
for i=1,50 do
map[i]={}
for j=1,50 do
map[i][j]=false
end
end
vision={}
for i=1,50 do
vision[i]={}
end
viewdist=5
circularview=true
seeall=false
end
function love.draw()
for i=1,50 do
for j=1,50 do
if seeall or vision[i][j] then
if map[i][j] then love.graphics.setColor(0,0,0)
else love.graphics.setColor(1,1,1)
end
else
love.graphics.setColor(0.25,0.25,0.25)
end
love.graphics.rectangle('fill',cell_width*(i-1),cell_height*(j-1),cell_width,cell_height)
end
end
end
function rescan()
if not seeall and wheremouse().x>0 and wheremouse().x<=50 and wheremouse().y>0 and wheremouse().y<=50 then
vision=FoVtable(map,wheremouse().x,wheremouse().y,viewdist,circularview)
end
end
function love.mousepressed( x, y, button )
if button==1 then
map[wheremouse().x][wheremouse().y] = not map[wheremouse().x][wheremouse().y]
rescan()
end
end
function love.wheelmoved(x,y)
viewdist=math.max(math.min(viewdist+y,50),0)
rescan()
end
function love.update(dt)
if wheremouse().x~=mousex0 or wheremouse().y~=mousey0 then
mousex0,mousey0=wheremouse().x,wheremouse().y
rescan()
end
end
function love.keypressed(key)
if key=='f' or key=='F' or key=='а' or key=='А' then
circularview = not circularview
rescan()
elseif key=='c' or key=='C' or key=='с' or key=='С' then
circularview=not circularview
elseif key==' ' then
seeall=not seeall
elseif key=='+' or key=='kp+' or key=='=' then
viewdist=math.min(viewdist+1,50)
rescan()
elseif key=='-' or key=='kp-' then
viewdist=math.max(viewdist-1,0)
rescan()
end
end