-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscenery.lua
186 lines (152 loc) · 6.41 KB
/
scenery.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
local Scenery = {
__NAME = "Scenery";
__VERSION = "0.4";
__DESCRIPTION = "Scenery - A dead simple Love2D SceneManager";
__LICENSE = [[
MIT License
Copyright (c) 2024 Paltze and Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
}
-- Split file into name and extension
local split = function(inputstr, sep)
local t = {}
for res in string.gmatch(inputstr, "([^"..sep.."]+)") do table.insert(t, res) end
return t[1], t[#t]
end
-- Automatically load scenes from the given directory
local autoLoad = function(directory)
-- Get the files in the directory
local files = love.filesystem.getDirectoryItems(directory)
local scenes = {}
for _, value in ipairs(files) do
local file, ext = split(value, ".")
-- Require scene
if ext == file then
local info = love.filesystem.getInfo(directory .. "/" .. file)
-- Check if item is a directory
if info and (info.type == "directory" or info.type == "symlink") then
info = love.filesystem.getInfo(directory .. "/" .. file .. "/init.lua")
-- Check for the init file
if info and info.type == "file" then
scenes[file] = require(directory .. "." .. file)
end
end
elseif ext == "lua" and file ~= "conf" and file ~= "main" then
scenes[file] = require(directory .. "." .. file)
end
end
return scenes
end
-- Iterate over the passed tables
local manualLoad = function(config)
local scenes = {}
local currentScene
-- Loop through the parameters
for _, value in ipairs(config) do
-- Check if path is string
assert(type(value.path) == "string", "Given path not a string.")
-- Check if key is number or string
assert(type(value.key) == "number" or type(value.key) == "string", "Given key not a number or string.")
--Check for duplicate scene keys
assert(not scenes[value.key], "Duplicate scene keys provided")
scenes[value.key] = require(value.path)
-- Check if default scene present
if value.default then
assert(not currentScene, "More than one default scene defined")
currentScene = value.key
end
end
-- If no default scene, set first scene as default
if not currentScene then
currentScene = config[1].key
end
return scenes, currentScene
end
local checkScenePresent = function(scene, sceneTable)
local present = false
for index, _ in pairs(sceneTable) do
if index == scene then
present = true
end
end
return present
end
-- The base Scenery Class
Scenery.__index = Scenery
function Scenery.init(...)
-- Set metatable to create a class
local this = setmetatable({}, Scenery)
-- Get all the parameters
local config = { ... }
-- Get scenes
if config[1] == nil then
error("No default scene supplied", 2)
elseif type(config[1]) == "table" then
this.scenes, this.currentscene = manualLoad(config)
elseif type(config[1]) =="string" then
this.scenes = autoLoad(config[2] or "scenes")
assert(checkScenePresent(config[1], this.scenes), "No scene '" .. config[1] .. "' present")
this.currentscene = config[1]
else
error("Unknown token '" .. config[1] .. "'", 2)
end
-- This function is available for all scene.
function this.setScene(key, data)
assert(this.scenes[key], "No such scene '" .. key .. "'")
this.currentscene = key
if this.scenes[this.currentscene].load then
this.scenes[this.currentscene]:load(data)
end
end
for _, value in pairs(this.scenes) do
value["setScene"] = this.setScene
end
-- All the callbacks available in Love 11.4 as described on https://love2d.org/wiki/Category:Callbacks
local loveCallbacks = { "load", "draw", "update" } -- Except these three.
for k in pairs(love.handlers) do
table.insert(loveCallbacks, k)
end
-- Loop through the callbacks creating a function with same name on the base class
for _, value in ipairs(loveCallbacks) do
this[value] = function(self, ...)
assert(type(self.scenes[self.currentscene]) == "table", "Scene '" .. self.currentscene .. "' not a valid scene.")
-- Check if the function exists on the class
if self.scenes[self.currentscene][value] then
return self.scenes[self.currentscene][value](self.scenes[self.currentscene], ...)
end
end
end
-- Inject callbacks into a table. Examples:
-- scenery:hook(love)
-- scenery:hook(love, { 'load', 'update', 'draw' })
function this.hook(self, t, keys)
assert(type(t) == "table", "Given param is not a table")
local registry = {}
keys = keys or loveCallbacks
for _, f in pairs(keys) do
registry[f] = t[f] or function() end
t[f] = function(...)
registry[f](...)
return self[f](self, ...)
end
end
end
return this
end
-- Return the initialising function
return Scenery.init