forked from LIKO-12/LIKO-12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.lua
174 lines (154 loc) · 3.43 KB
/
runtime.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
local lume = require("libraries.lume")
local r = {}
function r:newGlobals(spritesheet,tilemap)
local GLOB = {
assert=assert,
error=error,
ipairs=ipairs,
pairs=pairs,
ripairs = lume.ripairs,
next=next,
pcall=pcall,
select=select,
tonumber=tonumber,
tostring=tostring,
type=type,
unpack=unpack,
_VERSION=_VERSION,
xpcall=xpcall,
string={
byte=string.byte,
char=string.char,
find=string.find,
format=string.format,
gmatch=string.gmatch,
gsub=string.gsub,
len=string.len,
lower=string.lower,
match=string.match,
rep=string.rep,
reverse=string.reverse,
sub=string.sub,
upper=string.upper
},
table={
insert=table.insert,
maxn=table.maxn,
remove=table.remove,
sort=table.sort
},
math={
abs=math.abs,
acos=math.acos,
asin=math.asin,
atan=math.atan,
atan2=math.atan2,
ceil=math.ceil,
cos=math.cos,
cosh=math.cosh,
deg=math.deg,
exp=math.exp,
floor=math.floor,
fmod=math.fmod,
frexp=math.frexp,
huge=math.huge,
ldexp=math.ldexp,
log=math.log,
log10=math.log10,
max=math.max,
min=math.min,
modf=math.modf,
pi=math.pi,
pow=math.pow,
rad=math.rad,
random=math.random,
randomseed=math.randomseed,
sin=math.sin,
sinh=math.sinh,
sqrt=math.sqrt,
tan=math.tan,
tanh=math.tanh,
}
}
local newAPI = api.newAPI(true,spritesheet,tilemap)
for k,v in pairs(newAPI) do
GLOB[k] = v
end
GLOB._G=GLOB --Mirror Mirror
return GLOB
end
local function tr(fnc,...)
if not fnc then return end
local ok, err = pcall(fnc,...)
if not ok then
r.onerr(err)
r.cg = {}
end
end
function r:compile(code, G, spritesheet)
local G = G or r:newGlobals(spritesheet)
local chunk, err = loadstring(code or "")
if err and not chunk then -- maybe it's an expression, not a statement
chunk, err = loadstring("return " .. code)
if err and not chunk then
return false, err
end
end
setfenv(chunk,G)
self.cg = G
return chunk
end
function r:loadGame(code,spritesheet,tilemap,onerr,...)
--[[local success, err = self:compile(code, false, spritesheet, false)
if not success then return false, err end
self.onerr = onerr or error
return true]] -- The loadGame will have it's own loading code because r:compile made by technomancy is so so buggy..
local G = self:newGlobals(spritesheet,tilemap)
local cart, err = loadstring(code or "")
if not cart then return false, err end
setfenv(cart,G)
local ok, err = pcall(cart,...)
if not ok then return err end
self.cg = G
self.onerr = onerr or error
require("editor.console").G = G
return self
end
function r:startGame()
api.clear(1)
tr(self.cg._init)
end
function r:_init()
self.cg = {}
end
function r:_update(...)
tr(self.cg._update,...)
end
function r:_mpress(...)
tr(self.cg._mpress,...)
end
function r:_mmove(...)
tr(self.cg._mmove,...)
end
function r:_mrelease(...)
tr(self.cg._mrelease,...)
end
function r:_tpress(...)
tr(self.cg._tpress,...)
end
function r:_tmove(...)
tr(self.cg._tmove,...)
end
function r:_trelease(...)
tr(self.cg._trelease,...)
end
function r:_kpress(...)
tr(self.cg._kpress,...)
end
function r:_krelease(...)
tr(self.cg._krelease,...)
end
function r:_tinput(...)
tr(self.cg._tinput,...)
end
return r