-
Notifications
You must be signed in to change notification settings - Fork 2
/
assets.lua
executable file
·240 lines (197 loc) · 5.74 KB
/
assets.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
232
233
234
235
236
237
238
239
240
local function matchType(file, name)
if type(file) == "table" then file = file[1] end
if name then
for _, ext in ipairs(assets.types[name]) do
if file:match(ext .. "$") then
return true
end
end
return false
else
for name, _ in pairs(assets.types) do
if matchType(file, name) then
return assets["new" .. name:gsub("^.", string.upper)]
end
end
error("Unable to automatically match asset type to file '" .. file .. "'")
end
end
local assets = setmetatable({}, {
__index = function(self, key) return self.get(key) end,
__call = function(self, ...)
local args = {...}
local func
if #args == 1 and type(args[1]) == "table" then
for key, val in pairs(args[1]) do
func = matchType(val)
func(val, type(key) ~= "number" and key or nil)
end
else
for _, f in ipairs(args) do
func = matchType(f)
func(f)
end
end
end
})
assets.path = "assets"
assets.types = {}
function assets.get(key)
return assets.images[key] or assets.sfx[key] or assets.music[key] or assets.shaders[key] or assets.fonts[key]
end
function assets.newType(name, plural, path, types, loadFunc)
plural = plural or name .. "s"
path = path or plural
local function loadAsset(...)
local obj, objName, assign = loadFunc(assets, ...)
if assign ~= false then
assets[plural][objName] = obj
end
return obj, objName
end
local t = setmetatable({}, {
__call = function(self, ...)
local args = {...}
if #args == 1 and type(args[1]) == "table" then
for key, val in pairs(args[1]) do
loadAsset(val, type(key) ~= "number" and key or nil)
end
else
for _, f in ipairs(args) do
loadAsset(f)
end
end
end
})
assets[name] = t
assets[plural] = t
assets["new" .. name:gsub("^.", string.upper)] = loadAsset
assets[name .. "Path"] = path
if types then
assets.types[name] = type(types) == "string" and { types } or types
end
assets["all" .. plural:gsub("^.", string.upper)] = function(func)
local obj, objName
for _, f in ipairs(love.filesystem.getDirectoryItems(assets.getPath(nil, name))) do
obj = nil
if not assets.types[name] then
obj, objName = loadAsset(f)
elseif matchType(f, name) then
obj, objName = loadAsset(f)
end
if obj and func then
func(obj, objName)
end
end
end
end
-- isolates file names, and camelcases it where punctuation separates words
function assets.getName(path, multi)
if multi then
local stripEnd = path:match("([^/]+)([%-_]?%d+)%.[^%.]+$") -- strip ending number
if stripEnd then
path = stripEnd
else
multi = false -- use the matching below
end
end
if not multi then
path = path:match("([^/]+)%.[^%.]+$")
end
path = path:gsub("%p(%w)", string.upper)
return path
end
function assets.getPath(file, type)
return (assets.path and (assets.path .. "/") or "") .. assets[type .. "Path"] .. "/" .. (file or "")
end
assets.newType("image", nil, nil, { "png", "jpg", "jpeg", "tga", "bmp" },
function (self, file, name)
return love.graphics.newImage(self.getPath(file, "image")), name or self.getName(file)
end
)
assets.newType("sfx", "sfx", nil, { "ogg", "oga", "wav", "mp3" },
function (self, file, name, pool, volume, stream)
if type(name) == "boolean" then
stream = volume
volume = pool
pool = name
name = nil
elseif type(name) == "number" then
volume = name
stream = pool
pool = false
name = nil
end
if type(file) == "table" then
if not name then
name = self.getName(file[1], true)
end
for i, v in ipairs(file) do
file[i] = self.getPath(file[i], "sfx")
end
else
file = self.getPath(file, "sfx")
end
local sound
if pool then
sound = SoundPool:new(file, volume or 1, stream)
elseif Sound then
sound = Sound:new(file, volume or 1, stream)
else
sound = love.audio.newSource(file, stream and "stream" or "static")
if volume then sound:setVolume(volume) end
end
return sound, name or self.getName(file)
end
)
assets.newType("music", "music", nil, nil,
function (self, file, name, volume)
if type(name) == "number" then
volume = name
name = nil
end
local sound
if Sound then
sound = Sound:new(self.getPath(file, "music"), volume or 1, true)
else
sound = love.audio.newSource(file, "stream")
if volume then sound:setVolume(volume) end
end
return sound, name or self.getName(file)
end
)
assets.newType("shader", nil, nil, { "frag", "vert", "glsl", "shader" },
function (self, file, fileOrName, name)
local source = love.filesystem.read(self.getPath(file, "shader"))
local shader
if name then
local source2 = love.filesystem.read(self.getPath(fileOrName, "shader"))
shader = love.graphics.newShader(source, source2)
else
shader = love.graphics.newShader(source)
name = fileOrName
end
return shader, name or self.getName(file)
end
)
assets.newType("font", nil, nil, { "ttf", "otf" },
function (self, file, size, name)
-- support for assets() syntax
if type(file) == "table" then
size = file[2]
name = file[3]
file = file[1]
end
name = name or self.getName(file)
size = size or 12
if type(size) == "table" then
for i = 1, #size do self.newFont(file, size[i], name) end
return self.fonts[name], name, false
end
local font = love.graphics.newFont(self.getPath(file, "font"), size)
if not self.fonts[name] then self.fonts[name] = {} end
self.fonts[name][size] = font
return font, name, false
end
)
return assets