-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportals.lua
80 lines (69 loc) · 2.02 KB
/
portals.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
portals = {
server = {
container = {}
},
client = {}
}
function portals.server.spawn(data)
local defaults = {
x = 0, y = 0,
-- life defaults to nil (permanent)
}
for k, v in pairs(defaults) do
if data[k] == nil then data[k] = v end
end
data.id = lume.uuid()
data.spawnTime = gameTime
portals.server.container[data.id] = data
local state = {
id = data.id,
x = data.x, y = data.y,
life = data.life
}
server.currentState.portals[data.id] = state
server.added.portals[data.id] = state
end
function portals.server.destroy(id)
portals.server.container[id] = nil
server.currentState.portals[id] = nil
server.removed.portals[id] = id
end
function portals.server.reset()
for k, v in pairs(portals.server.container) do
portals.server.destroy(k)
end
end
function portals.server.update(dt)
for k, v in pairs(portals.server.container) do
if gameTime - v.spawnTime > v.life then
portals.server.destroy(k)
end
end
end
function portals.client.keypressed(k, scancode, isrepeat)
if scancode == 'e' and not isrepeat then
-- todo: closest like lootBags
local px, py = playerController.player.body:getPosition()
for k, v in pairs(client.currentState.portals) do
if (px-v.x)^2+(py-v.y)^2 < 24^2 then
client.usePortal{id=v.id}
end
end
end
end
function portals.client.draw()
for _, v in pairs(client.currentState.portals) do
scene.add{
draw = function()
love.graphics.setColor(0.2, 0.2, 0.8)
local px, py = playerController.player.body:getPosition()
if (px-v.x)^2+(py-v.y)^2 < 24^2 then
love.graphics.setColor(0.5, 0.5, 0.9)
end
local w, h = 16, 24
love.graphics.rectangle('fill', lume.round(v.x - w/2), lume.round(v.y - h), w, h)
end,
y = v.y
}
end
end