-
Notifications
You must be signed in to change notification settings - Fork 0
/
realm.lua
210 lines (191 loc) · 7.97 KB
/
realm.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
realm = {
server = {},
client = {}
}
local defaults = {server={}, client={}}
for _, sc in ipairs{'server', 'client'} do
defaults[sc].id = function() return lume.uuid() end
defaults[sc].name = function() return 'realm' end
for _, v in ipairs{'entities', 'projectiles', 'slimeBalls', 'lootBags', 'portals'} do
defaults[sc][v] = function() return {} end
end
for _, v in ipairs{{'physics', physics}, {'world', world}} do
defaults[sc][v[1]] = function() return v[2][sc]:new() end
end
end
function realm.server:new(o)
o = o or {}
for k, v in pairs(defaults.server) do
if o[k] == nil then o[k] = v() end
end
setmetatable(o, self)
self.__index = self
return o
end
function realm.server:load()
self.physics:load()
end
function realm.server:update(dt)
self.physics:update(dt)
for _, bag in pairs(self.lootBags) do
if bag.life and gameTime - bag.spawnTime > bag.life then
bag:destroy()
end
end
end
function realm.server:destroy()
self.world:destroy()
for _, v in pairs(self.lootBags) do
v:destroy()
end
end
function realm.client:new(o)
o = o or {}
for k, v in pairs(defaults.client) do
if o[k] == nil then o[k] = v() end
end
setmetatable(o, self)
self.__index = self
return o
end
function realm.client:load()
self.physics:load()
end
function realm.client:update(dt)
self.physics:update(dt)
self.world:update(dt)
end
function realm.client:destroy()
self.world:destroy()
for _, v in pairs(self.lootBags) do
v:destroy()
end
end
function realm.client:draw()
local mx, my = window2game(love.mouse.getPosition())
mx, my = lume.round(mx), lume.round(my)
local wmx, wmy = camera:screen2world(mx, my)
self.world:draw()
for _, bag in pairs(self.lootBags) do
scene.add{
draw = function()
local tint = 1
local outlineColor = {0, 0, 0, 1}
if bag.life and client.serverTime - bag.spawnTime > bag.life - 3 then
local t = (client.serverTime - bag.spawnTime - bag.life + 3)/3
tint = math.cos(t*5*math.pi)/4 + 1/4 + (1-t)/2
tint = tint/2 + 1/2
outlineColor = {0.8, 0, 0, 1}
end
local closestBag = playerController.closestBag
if bag.id == closestBag.id and closestBag.dist < playerController.interactRange then
outlineColor = {0.8, 0.8, 0.8, 1}
end
love.graphics.setColor(tint, tint, tint)
love.graphics.push()
love.graphics.translate(lume.round(bag.x), lume.round(bag.y))
local img = gfx.items[bag.type]
local _shader = love.graphics.getShader()
love.graphics.setShader(shaders.outline)
shaders.outline:send('stepSize', {1/img:getWidth(), 1/img:getHeight()})
shaders.outline:send('outlineColor', outlineColor)
love.graphics.draw(img, 0, 0, 0, 1, 1, lume.round(img:getWidth()/2), img:getHeight())
love.graphics.setShader(_shader)
if bag.id == closestBag.id and closestBag.open then
local img = gfx.ui.bag
love.graphics.push()
love.graphics.translate(-lume.round(img:getWidth()/2), -img:getHeight() - 20)
love.graphics.setColor(1, 1, 1)
love.graphics.draw(img, 0, 0)
local bmx = wmx - (lume.round(bag.x) - lume.round(img:getWidth()/2))
local bmy = wmy - (lume.round(bag.y) - img:getHeight() - 20)
for slotId, slot in ipairs(lootBagSlots) do
local item = items.client.getItem(bag.items[slotId])
if bmx >= slot.x and bmx <= slot.x + slot.w
and bmy >= slot.y and bmy <= slot.y + slot.h then
if item then
cursor.cursor = cursor.hand
playerController.hoveredItem = item
end
love.graphics.setColor(1, 1, 1, 0.4)
love.graphics.rectangle('fill', slot.x, slot.y, slot.w, slot.h)
end
local heldItem = playerController.heldItem
if not (heldItem.bagId == bag.id and heldItem.slotId == slotId) then
if item then
love.graphics.setColor(1, 1, 1)
love.graphics.draw(gfx.items[item.imageId], slot.x, slot.y)
end
end
end
love.graphics.pop()
end
love.graphics.pop()
end,
y = bag.y
}
end
local cqb = playerController.closestQuestBlock
if cqb.id and cqb.open then
local qb = client.currentState.entities[cqb.id]
if qb then
scene.add{
draw = function()
local img = gfx.ui.quest
love.graphics.push()
love.graphics.translate(
lume.round(qb.x + 8) - lume.round(img:getWidth()/2),
lume.round(qb.y + 8) - img:getHeight() - 20)
love.graphics.setColor(1, 1, 1)
love.graphics.draw(img, 0, 0)
local bmx = wmx - (lume.round(qb.x + 8) - lume.round(img:getWidth()/2))
local bmy = wmy - (lume.round(qb.y + 8) - img:getHeight() - 20)
for slotId, slot in ipairs(questBlockSlots) do
local exists = true
local item = items.client.getItem(quests.current.heldItems[slotId])
if not item then
exists = false
if slotId <= 4 then
item = items.client.getItem(quests.current.cost[slotId])
else
item = items.client.getItem(quests.current.reward[slotId - 4])
end
end
if slotId >= 5 then
local allExist = true
for i, item in ipairs(quests.current.cost) do
if not quests.current.heldItems[i] then
allExist = false
break
end
end
exists = allExist
end
if bmx >= slot.x and bmx <= slot.x + slot.w
and bmy >= slot.y and bmy <= slot.y + slot.h then
if item then
cursor.cursor = cursor.hand
playerController.hoveredItem = item
end
love.graphics.setColor(1, 1, 1, 0.4)
love.graphics.rectangle('fill', slot.x, slot.y, slot.w, slot.h)
end
local heldItem = playerController.heldItem
if not (heldItem.bagId == 'quest' and heldItem.slotId == slotId) then
if item then
if exists then
love.graphics.setColor(1, 1, 1)
else
love.graphics.setColor(1, 1, 1, 0.4)
end
love.graphics.draw(gfx.items[item.imageId], slot.x, slot.y)
end
end
end
love.graphics.pop()
end,
y = qb.y
}
end
end
end