-
Notifications
You must be signed in to change notification settings - Fork 1
/
world.lua
315 lines (268 loc) · 7.27 KB
/
world.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
local portal = require('portal')
local region = require('region')
local switch = require('switch')
local enemy = require('enemy')
local counter = require('counter')
local timerpool = require('hug.timerpool')
local color = require('hug.color')
local lazy = require('hug.lazy')
local world = {}
local mt = { __index = world }
local dofile, setmetatable, pairs, ipairs = dofile, setmetatable, pairs, ipairs
local graphics = love.graphics
local setLineWidth, setColor = graphics.setLineWidth, graphics.setColor
local line = graphics.line
local mathex = require('hug.extensions.math')
local lerp = mathex.lerp
local function safeCallTrigger(tt, name, ...)
if (tt and tt[name]) then
return tt[name](...)
end
end
local worldnames = lazy.new(function()
local worldfiles = love.filesystem.getDirectoryItems('worlds')
local names = {}
for i = 1, #worldfiles do
local _, _, name = worldfiles[i]:find('([%a%d_]+)%.lua$')
if name then
names[#names + 1] = name
end
end
return names
end)
function world.getNames()
return worldnames:get()
end
function world.new(name, context)
local timerpool = timerpool.new()
local chunk = love.filesystem.load('worlds/' .. name .. '.lua')
local env = {timerpool = timerpool}
for k,v in pairs(_G) do
env[k] = v
end
setfenv(chunk, env)
local data = chunk()
-- Build a world object from the data
local instance = setmetatable({}, mt)
instance.timerpool = timerpool
instance.background = color.fromtable(data.background or { 0, 0, 0 })
instance.foreground = data.foreground and color.fromtable(data.foreground) or -instance.background
instance.name = name
instance.lines = data.lines
instance.portals = {}
instance.triggers = data.triggers
instance.regions = {}
instance.switches = {}
instance.enemies = {}
instance.projectiles = {}
instance.counters = {}
instance.linewidth = data.linewidth or 1
-- Create actual portal objects from the data
for i,p in ipairs(data.portals or {}) do
instance.portals[p.name or i] = portal.new(instance, p.name, p.x, p.destination, p.dx, instance.foreground * 0.8)
end
for i,r in ipairs(data.regions or {}) do
instance.regions[r.name or i] = region.new(r.name, r.x, r.w)
end
for i,s in ipairs(data.switches or {}) do
local k = s.name or i
instance.switches[k] = switch.new(instance, k, s.x, s.ud, s.gvar)
if s.gvar then
local status = context.getVar(s.gvar)
instance.switches[k].status = status or false
end
end
for i,e in ipairs(data.enemies or {}) do
instance.enemies[i] = enemy.new(instance, e.x, e.patrol, e.size, e.speed)
end
for i,c in ipairs(data.counters or {}) do
instance.counters[c.name or i] = counter.new(instance, c.name, c.x, c.min, c.max, nil, c.interactive, instance.foreground * 0.8)
end
return instance
end
function world:y(x)
-- make sure there is enough data to work with and return the closest part
-- of the world if x isn't within the bounds of the world
if #self.lines < 4 then
return x
elseif x < self.lines[1] then
return self.lines[2]
elseif x > self.lines[#self.lines - 1] then
return self.lines[#self.lines]
end
-- traverse lines
for i = 1, #self.lines - 3, 2 do
local x1 = self.lines[i]
local y1 = self.lines[i + 1]
local x2 = self.lines[i + 2]
local y2 = self.lines[i + 3]
if x >= x1 and x <= x2 then
-- determine how far into the line we are
local d = (x - x1) / (x2 - x1)
-- calculate the slope of the line
local m = (y1 - y2) / (x1 - x2)
-- calculate the base of the line
local b = lerp(y1, y2, d)
-- y = mx + b
return m * d + b
end
end
end
function world:left()
return self.lines[1]
end
function world:right()
return self.lines[#self.lines - 1]
end
function world:update(dt)
self.timerpool:update(dt)
for _,e in ipairs(self.enemies) do
e:update(dt)
end
for _,p in ipairs(self.projectiles) do
p:update(dt)
end
end
function world:draw()
setLineWidth(self.linewidth)
setColor(self:oppositeColor())
line(self.lines)
for _,p in pairs(self.portals) do
p:draw()
end
for _,c in pairs(self.counters) do
c:draw()
end
for _,s in pairs(self.switches) do
setColor(self:oppositeColor())
s:draw()
end
for _,e in ipairs(self.enemies) do
setColor(self:oppositeColor())
e:draw()
end
for _,p in ipairs(self.projectiles) do
p:draw()
end
end
function world:scriptUpdate(context, dt)
safeCallTrigger(self.triggers, 'onUpdate', context, dt)
end
function world:scriptDraw(context)
safeCallTrigger(self.triggers, 'onDraw', context)
end
function world:portalAt(x)
for _,p in pairs(self.portals) do
if p:contains(x) then
return p
end
end
end
function world:regionAt(x)
for _,r in pairs(self.regions) do
if r:contains(x) then
return r
end
end
end
function world:regionOverlaps(x, r)
for _,reg in pairs(self.regions) do
if reg:overlaps(x, r) then
return reg
end
end
end
function world:enemyAt(x)
for _,e in ipairs(self.enemies) do
if e:contains(x) then
return e
end
end
end
function world:counterAt(x, r)
for _,c in pairs(self.counters) do
if c:overlaps(x, r) then
return c
end
end
end
function world:enemyOverlaps(x, r)
for _,e in ipairs(self.enemies) do
if e:overlaps(x, r) then
return e
end
end
end
function world:activateAt(x, r, context)
local result = false
for _,s in pairs(self.switches) do
if s.status == false and s:overlaps(x, r) then
result = true
s.status = true
self:onSwitchChanged(context, s)
end
end
return result
end
function world:addPortal(name, x, destination, dx)
self.portals[name] = portal.new(self, name, x, destination, dx, self.foreground * 0.8)
end
function world:removePortal(name)
self.portals[name] = nil
end
function world:addRegion(name, x, w)
self.regions[name] = region.new(name, x, w)
end
function world:oppositeColor()
return self.foreground
end
function world:getSwitchStatus(name)
local s = self.switches[name]
if s then
return s.status
else
return false
end
end
function world:setSwitchStatus(name, status)
local s = self.switches[name]
if s then
s.status = status
end
end
function world:getCounterValue(name)
local c = self.counters[name]
if c ~= nil then
return c.value
end
end
function world:onEnter(context)
safeCallTrigger(self.triggers, 'onEnter', context)
end
function world:onEnterRegion(context, r)
safeCallTrigger(self.triggers, 'onEnterRegion', context, r)
end
-- A script should return false to disable standard portal processing
-- True means the portal operates as normal after the trigger returns
function world:onEnterPortal(context, p)
local r = safeCallTrigger(self.triggers, 'onEnterPortal', context, p)
if r ~= nil then
return r
else
return true
end
end
function world:onSwitchChanged(context, s)
-- update the global variable this switch is linked to
if s.gvar ~= nil then
context.setVar(s.gvar, s.status)
end
safeCallTrigger(self.triggers, 'onSwitchChanged', context, s)
end
function world:onPlayerDeath(context)
safeCallTrigger(self.triggers, 'onPlayerDeath', context)
end
function world:onCounterChanged(context, c)
safeCallTrigger(self.triggers, 'onCounterChanged', context, c)
end
return world