-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.lua
173 lines (122 loc) · 3.81 KB
/
map.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
map = {}
map.nodes = {}
function map.load(map_name)
map.sprite = {
day = love.graphics.newImage("sprites/"..map_name.."Day.png"),
night = love.graphics.newImage("sprites/"..map_name.."Night.png"),
bgday = love.graphics.newImage("sprites/"..map_name.."BgDay.png"),
bgnight = love.graphics.newImage("sprites/"..map_name.."BgNight.png"),
}
map.mask = love.image.newImageData("sprites/"..map_name.."Mask.png")
map.x = 127
map.y = 127
map.loadnodes()
end
function map.draw()
if cycle.state == "day" then
love.graphics.draw(map.sprite.day)
else
love.graphics.draw(map.sprite.night)
end
-- quadstart = 0
-- mappos = 0
-- line = screen.size.y
-- if cycle.time > cycle.daylen then
-- if cycle.time < (cycle.daylen + cycle.transition) then
-- quadstart = (cycle.time - cycle.daylen) * screen.size.y
-- mappos = quadstart
-- elseif cycle.time > (cycle.length - cycle.transition) then
-- line = (cycle.time - cycle.length - cycle.transition) * screen.size.y
-- else
-- mappos = screen.size.y
-- end
-- end
-- quad = love.graphics.newQuad(0, quadstart, screen.size.x, line, map.sprite.day:getDimensions())
-- love.graphics.draw(map.sprite.night)
-- love.graphics.draw(map.sprite.day, quad, 0, mappos)
-- love.graphics.setColor(map.gradient:getPixel(0, line))
-- love.graphics.draw(map.sprite[0])
-- love.graphics.setColor(map.gradient:getPixel(1, line))
-- love.graphics.draw(map.sprite[1])
-- love.graphics.setColor(map.gradient:getPixel(2, line))
-- love.graphics.draw(map.sprite[2])
-- love.graphics.setColor(map.gradient:getPixel(3, line))
-- love.graphics.draw(map.sprite[3])
end
-- function map.drawframe()
-- love.graphics.draw(map.frame)
-- end
-- function map.drawgrid()
-- if time % 2 < 1 then
-- love.graphics.draw(map.grid)
-- end
-- end
function map.get(x, y)
if x < 1 or x > map.x or y < 1 or y > map.y then -- выход за границы карты
return nil
end
r, g, b, a = map.mask:getPixel(x, y)
if r > 0 and g == 0 and b == 0 then
return "wall"
elseif r > 0 and g > 0 and b == 0 then
return "door"
elseif r == 0 and g > 0 and b > 0 then
return "building"
elseif r == 0 and g > 0 and b == 0 then
return "forest"
elseif r == 0 and g == 0 and b > 0 then
return "water"
else
return "ground"
end
end
-- Создать массив ячеек
function map.loadnodes()
local surface
for y = 0, map.y do
for x = 0, map.x do
table.insert(map.nodes, {x = x, y = y})
end
end
end
-- Получить ссылку на ячейку
function map.getnode(x, y)
for i, node in map.nodes do
if node.x == x and node.y == y then
return node
end
end
end
-- Принадлежит ли клетка с координатами x, y сетке
function map.inbounds(x, y)
if x < 0 or x > map.x then
return false
end
if y < 0 or y > map.y then
return false
end
return true
end
-- Проверка клетки с координатами x, y на пригодность для прохода
function map.reachable(x, y)
local surface = map.get(x, y)
if surface == "wall" or surface == "building" or surface == "door" or surface == "water" then
return false
end
return true
end
-- Сетка проходимых и непроходимых клеток
function map.getgrid()
local grid = {}
for y = 0, map.y do
grid[y] = {}
for x = 0, map.x do
if map.reachable(x, y) then
grid[y][x] = 0
else
grid[y][x] = 1
end
end
end
return grid
end