bzr branch
http://66.246.75.32/bzr/zoeplat
1
by Josh C
zoetrope 1.3.1 |
1 |
-- Section: Globals |
2 |
||
3 |
-- Variable: the |
|
4 |
-- This is a repository table for the current app, view, keys, and mouse. |
|
5 |
-- You can use this for other objects that are useful to track. This should |
|
6 |
-- be considered read-only; references here are for convenience and changing |
|
7 |
-- things here will have no effect. |
|
8 |
the = {} |
|
9 |
||
10 |
-- Constant: STRICT |
|
11 |
-- If set to true, then Zoetrope will do some extra checking for obvious |
|
12 |
-- errors at the expense of some performance. Some of these checks will |
|
13 |
-- throw errors, while others will simply print a warning to the console. |
|
14 |
-- If you are going to use this, make sure to do so before your project's |
|
15 |
-- require 'zoetrope' statement. |
|
16 |
||
17 |
-- Constant: DEBUG |
|
18 |
-- If set to true, Zoetrope's debug console will be enabled. If you are |
|
19 |
-- going to use this, make sure to do so before your project's require |
|
20 |
-- 'zoetrope' statement. |
|
21 |
||
22 |
-- Constant: NEARLY_ZERO |
|
23 |
-- Any number less than this is considered 0 by Zoetrope. |
|
24 |
NEARLY_ZERO = 0.0001 |
|
25 |
||
26 |
-- Constant: UP |
|
27 |
-- Directional constant corresponding to up. |
|
28 |
UP = 'up' |
|
29 |
||
30 |
-- Constant: DOWN |
|
31 |
-- Directional constant corresponding to down. |
|
32 |
DOWN = 'down' |
|
33 |
||
34 |
-- Constant: LEFT |
|
35 |
-- Directional constant corresponding to left. |
|
36 |
LEFT = 'left' |
|
37 |
||
38 |
-- Constant: RIGHT |
|
39 |
-- Directional constant corresponding to right. |
|
40 |
RIGHT = 'right' |
|
41 |
||
42 |
||
43 |
-- Function: trim |
|
44 |
-- trim() implementation for strings via http://lua-users.org/wiki/stringtrim |
|
45 |
-- |
|
46 |
-- Arguments: |
|
47 |
-- source - source string |
|
48 |
-- |
|
49 |
-- Returns: |
|
50 |
-- string trimmed of leading and trailing whitespace |
|
51 |
||
52 |
function trim (source) |
|
53 |
return source:gsub("^%s*(.-)%s*$", "%1") |
|
54 |
end |
|
55 |
||
56 |
-- Function: split |
|
57 |
-- split() implementation for strings via http://lua-users.org/wiki/splitjoin |
|
58 |
-- |
|
59 |
-- Arguments: |
|
60 |
-- source - source string |
|
61 |
-- pattern - Lua pattern to split on, see http://www.lua.org/pil/20.1.html |
|
62 |
-- |
|
63 |
-- Returns: |
|
64 |
-- table of split strings |
|
65 |
||
66 |
function split (source, pattern) |
|
67 |
assert(type(source) == 'string', 'source must be a string') |
|
68 |
assert(type(pattern) == 'string', 'pattern must be a string') |
|
69 |
||
70 |
local result = {} |
|
71 |
local searchStart = 1 |
|
72 |
local splitStart, splitEnd = string.find(source, pattern, searchStart) |
|
73 |
||
74 |
while splitStart do |
|
75 |
table.insert(result, string.sub(source, searchStart, splitStart - 1)) |
|
76 |
searchStart = splitEnd + 1 |
|
77 |
splitStart, splitEnd = string.find(source, pattern, searchStart) |
|
78 |
end |
|
79 |
||
80 |
table.insert(result, string.sub(source, searchStart)) |
|
81 |
return result |
|
82 |
end |
|
83 |
||
84 |
-- Function: tovalue |
|
85 |
-- Coerces, if possible, a string to a boolean or number value. |
|
86 |
-- If the string cannot be coerced to either of these types, this |
|
87 |
-- returns the same string passed. |
|
88 |
-- |
|
89 |
-- Arguments: |
|
90 |
-- source - string to coerce |
|
91 |
-- |
|
92 |
-- Returns: |
|
93 |
-- number, boolean, or string |
|
94 |
||
95 |
function tovalue (source) |
|
96 |
if source == 'true' then return true end |
|
97 |
if source == 'false' then return false end |
|
98 |
||
99 |
return tonumber(source) or source |
|
100 |
end |
|
101 |
||
102 |
-- Function: sound |
|
103 |
-- Loads a sound but does not play it. It's important to use the hint property |
|
104 |
-- appropriately; if set incorrectly, it can cause sound playback to stutter or lag. |
|
105 |
-- |
|
106 |
-- Arguments: |
|
107 |
-- path - string pathname to sound |
|
108 |
-- hint - either 'short' or 'long', depending on length of sound; default 'short' |
|
109 |
-- |
|
110 |
-- Returns: |
|
111 |
-- LOVE sound source, see https://love2d.org/wiki/Source |
|
112 |
||
113 |
function sound (path, hint) |
|
114 |
return love.audio.newSource(Cached:sound(path, hint or 'short')) |
|
115 |
end |
|
116 |
||
117 |
-- Function: playSound |
|
118 |
-- Plays a sound once. This is the easiest way to play a sound. It's important |
|
119 |
-- to use the hint property appropriately; if set incorrectly, it can cause sound |
|
120 |
-- playback to stutter or lag. |
|
121 |
-- |
|
122 |
-- Arguments: |
|
123 |
-- path - string pathname to sound |
|
124 |
-- volume - volume to play at, from 0 to 1; default 1 |
|
125 |
-- hint - either 'short' or 'long', depending on length of sound; default 'short' |
|
126 |
-- |
|
127 |
-- Returns: |
|
128 |
-- LOVE sound source, see https://love2d.org/wiki/Source |
|
129 |
||
130 |
function playSound (path, volume, hint) |
|
131 |
volume = volume or 1 |
|
132 |
local source = sound(path, hint) |
|
133 |
source:setVolume(volume) |
|
134 |
source:play() |
|
135 |
return source |
|
136 |
end |
|
137 |
||
138 |
-- Function: searchTable |
|
139 |
-- Returns the index of a value in a table. If the value |
|
140 |
-- does not exist in the table, this returns nil. |
|
141 |
-- |
|
142 |
-- Arguments: |
|
143 |
-- table - table to search |
|
144 |
-- search - value to search for |
|
145 |
-- |
|
146 |
-- Returns: |
|
147 |
-- integer index or nil |
|
148 |
||
149 |
function searchTable (table, search) |
|
150 |
for i, value in ipairs(table) do |
|
151 |
if value == search then return i end |
|
152 |
end |
|
153 |
||
154 |
return nil |
|
155 |
end |
|
156 |
||
157 |
-- Function: copyTable |
|
158 |
-- Returns a superficial copy of a table. If it contains a |
|
159 |
-- reference to another table in one of its properties, that |
|
160 |
-- reference will be copied shallowly. |
|
161 |
-- |
|
162 |
-- Arguments: |
|
163 |
-- source - source table |
|
164 |
-- |
|
165 |
-- Returns: |
|
166 |
-- new table |
|
167 |
||
168 |
function copyTable (source) |
|
169 |
assert(type(source) == 'table', "asked to copy a non-table") |
|
170 |
||
171 |
local result = {} |
|
172 |
setmetatable(result, getmetatable(source)) |
|
173 |
||
174 |
for key, value in pairs(source) do |
|
175 |
result[key] = value |
|
176 |
end |
|
177 |
||
178 |
return result |
|
179 |
end |
|
180 |
||
181 |
-- Function: shuffleTable |
|
182 |
-- Shuffles the contents of a table in place. |
|
183 |
-- via http://www.gammon.com.au/forum/?id=9908 |
|
184 |
-- |
|
185 |
-- Arguments: |
|
186 |
-- table - table to shuffle |
|
187 |
-- |
|
188 |
-- Returns: |
|
189 |
-- table passed |
|
190 |
||
191 |
function shuffleTable (table) |
|
192 |
local n = #table |
|
193 |
||
194 |
while n >= 2 do |
|
195 |
-- n is now the last pertinent index |
|
196 |
local k = math.random(n) -- 1 <= k <= n |
|
197 |
-- Quick swap |
|
198 |
table[n], table[k] = table[k], table[n] |
|
199 |
n = n - 1 |
|
200 |
end |
|
201 |
||
202 |
return table |
|
203 |
end |
|
204 |
||
205 |
-- Function: dump |
|
206 |
-- Returns a string representation of a variable in a way |
|
207 |
-- that can be reconstituted via loadstring(). Yes, this |
|
208 |
-- is basically a serialization function, but that's so much |
|
209 |
-- to type :) This ignores any userdata, functions, or circular |
|
210 |
-- references. |
|
211 |
-- via http://www.lua.org/pil/12.1.2.html |
|
212 |
-- |
|
213 |
-- Arguments: |
|
214 |
-- source - variable to describe |
|
215 |
-- ignore - a table of references to ignore (to avoid cycles), |
|
216 |
-- defaults to empty table. This uses the references as keys, |
|
217 |
-- *not* as values, to speed up lookup. |
|
218 |
-- |
|
219 |
-- Returns: |
|
220 |
-- string description |
|
221 |
||
222 |
function dump (source, ignore) |
|
223 |
ignore = ignore or { source = true } |
|
224 |
local sourceType = type(source) |
|
225 |
||
226 |
if sourceType == 'table' then |
|
227 |
local result = '{ ' |
|
228 |
||
229 |
for key, value in pairs(source) do |
|
230 |
if not ignore[value] then |
|
231 |
local dumpValue = dump(value, ignore) |
|
232 |
||
233 |
if dumpValue ~= '' then |
|
234 |
result = result .. '["' .. key .. '"] = ' .. dumpValue .. ', ' |
|
235 |
end |
|
236 |
||
237 |
if type(value) == 'table' then |
|
238 |
ignore[value] = true |
|
239 |
end |
|
240 |
end |
|
241 |
end |
|
242 |
||
243 |
if result ~= '{ ' then |
|
244 |
return string.sub(result, 1, -3) .. ' }' |
|
245 |
else |
|
246 |
return '{}' |
|
247 |
end |
|
248 |
elseif sourceType == 'string' then |
|
249 |
return string.format('%q', source) |
|
250 |
elseif sourceType ~= 'userdata' and sourceType ~= 'function' then |
|
251 |
return tostring(source) |
|
252 |
else |
|
253 |
return '' |
|
254 |
end |
|
255 |
end |
|
256 |
||
257 |
bind = function (...) |
|
258 |
return Cached:bind(...) |
|
259 |
end |