-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathluapill.lua
227 lines (182 loc) · 5.08 KB
/
luapill.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
--[[
The zlib/libpng License Copyright (c) 2025 Kyrre Havik
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
This notice may not be removed or altered from any source distribution.
]]
require "math"
local luapill = {}
local TILES = {}
local MAP = {}
local TILESCALE = 1
local FOLDER = nil
local SORT_FOLDER = false
local CAMERA = {
x = love.graphics.getWidth() / 2,
y = 0
}
local TILE_WIDTH_HALF = nil
local TILE_HEIGHT_HALF = nil
local function drawTile(tile, level, x, y)
y = y - (tile:getHeight() - 83) - (level * TILE_HEIGHT_HALF) -- magic number
love.graphics.draw(tile,
x * TILESCALE, y * TILESCALE, -- cords
0, -- rotation
TILESCALE, TILESCALE -- scale
)
end
function luapill:draw(extras)
love.graphics.push()
love.graphics.translate(CAMERA.x, CAMERA.y)
for y = 1, MAP_SIZE do
for x = 1, MAP_SIZE do
local screen = luapill:mapToScreen({ x = x, y = y })
local tiles = MAP[y][x]
if extras[y] and extras[y][x] then
local extra = extras[y][x]
local tmp = MAP[y][x]
tiles = {}
local drawn = false
for level, tile in pairs(tmp) do
if level == extra.level then
drawTile(extra.image, level, screen.x, screen.y)
drawn = true
else
drawTile(TILES[tile], level, screen.x, screen.y)
end
end
if not drawn then
drawTile(extra.image, extra.level, screen.x + 75, screen.y - 50)
end
else
if tiles then
for level, tileID in pairs(tiles) do
if tileID then
local tile = TILES[tileID]
drawTile(tile, level, screen.x, screen.y)
end
end
end
end
end
end
love.graphics.pop()
end
function luapill:mapToScreen(map)
local screen = {}
screen.x = (map.x - map.y) * TILE_WIDTH_HALF
screen.y = (map.x + map.y) * TILE_HEIGHT_HALF
return screen
end
function luapill:screenToMap(screen)
local map = {}
screen.x = (screen.x - CAMERA.x) / TILESCALE
screen.y = (screen.y - CAMERA.y) / TILESCALE
map.x = math.floor(math.floor(screen.x / TILE_WIDTH_HALF + screen.y / TILE_HEIGHT_HALF) / 2) + 1
map.y = math.floor(math.floor(screen.y / TILE_HEIGHT_HALF - (screen.x / TILE_WIDTH_HALF)) / 2) + 2
return map
end
function luapill:getMouseAsMap()
return luapill:screenToMap({ x = love.mouse.getX(), y = love.mouse.getY() })
end
function luapill:zoomMap(scale)
TILESCALE = scale
if TILESCALE < .2 then
TILESCALE = .2
elseif TILESCALE > 2 then
TILESCALE = 2
end
end
local function initTiles()
local files = love.filesystem.getDirectoryItems(FOLDER)
for _, file in ipairs(files) do
table.insert(TILES, love.graphics.newImage(FOLDER .. "/" .. file))
end
if SORT_FOLDER then
table.sort(TILES)
end
end
local function initMap(size, tile)
for y = 1, size do
MAP[y] = {}
for x = 1, size do
MAP[y][x] = { tile }
end
end
end
function luapill:saveMap(path)
local output = "X;Y,TILE;LEVEL"
for y = 1, MAP_SIZE do
for x = 1, MAP_SIZE do
local tiles = MAP[y][x]
for level, tile in pairs(tiles) do
output = string.format("%s\n%d;%d;%d;%d",
output,
x, y,
tile,
level)
end
end
end
if not path then
path = "default_" .. love.timer.getTime() .. ".luapill"
end
return love.filesystem.write(path, output)
end
function luapill:loadMap(path)
initMap(MAP_SIZE, nil)
for line in io.lines(path) do
local x, y, tile, level = line.match(line, "(%d+);(%d+);(%d+);(%d+)")
x = tonumber(x)
y = tonumber(y)
tile = tonumber(tile)
level = tonumber(level)
if x then -- skips header line
MAP[y][x][level] = tile
end
end
end
function luapill:getTile(index)
return TILES[index]
end
function luapill:getTileCount()
return #TILES
end
function luapill:getScale()
return TILESCALE
end
function luapill:getMap()
return MAP
end
function luapill:moveCamera(x, y)
CAMERA.y = CAMERA.y + y
CAMERA.x = CAMERA.x + x
end
function luapill:placeTile(tile)
if not MAP[tile.y] or not MAP[tile.y][tile.x] then
return
end
MAP[tile.y][tile.x][tile.level] = tile.index
end
function luapill:deleteTile(tile)
if not MAP[tile.y] or not MAP[tile.y][tile.x] then
return
end
MAP[tile.y][tile.x][tile.level] = nil
end
function luapill:setup(config)
TILE_WIDTH_HALF = config.tilewidth / 2
TILE_HEIGHT_HALF = config.tileheight / 2
FOLDER = config.folder
SORT_FOLDER = config.sortFolder or false
MAP_SIZE = config.mapSize or 32
initTiles()
if config.loadMap then
luapill:loadMap(config.loadMap)
else
initMap(MAP_SIZE, config.defaultTile or 1)
end
end
return luapill