-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhud.lua
387 lines (358 loc) · 13.3 KB
/
hud.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
hud = {}
hud.panels = {}
hud.buttons = {}
hud.inventorySlots = {}
table.insert(hud.inventorySlots, {type = 'head', x = 34, y = 10, w = 15, h = 15})
table.insert(hud.inventorySlots, {type = 'weapon', x = 12, y = 32, w = 15, h = 15})
table.insert(hud.inventorySlots, {type = 'chest', x = 34, y = 32, w = 15, h = 15})
table.insert(hud.inventorySlots, {type = 'secondary', x = 56, y = 32, w = 15, h = 15})
table.insert(hud.inventorySlots, {type = 'accessory', x = 12, y = 53, w = 15, h = 15})
table.insert(hud.inventorySlots, {type = 'legs', x = 34, y = 53, w = 15, h = 15})
table.insert(hud.inventorySlots, {type = 'accessory', x = 56, y = 53, w = 15, h = 15})
for j=1, 2 do
for i=1, 4 do
table.insert(hud.inventorySlots, {
x = 7 + (i-1)*18,
y = 79 + (j-1)*18,
w = 15,
h = 15
})
end
end
function hud.addPanel(t)
local defaults = {
openPos = {x=0, y=0},
closedPos = {x=0, y=0},
img = gfx.hud.panels.chat,
open = true,
}
for k, v in pairs(defaults) do
if t[k] == nil then t[k] = v end
end
if t.timer == nil then t.timer = t.open and 0 or 1 end
if t.open then
t.x = t.openPos.x
t.y = t.openPos.y
else
t.x = t.closedPos.x
t.y = t.closedPos.y
end
if not t.update then
t.update = function(self, dt)
if self.open then
self.timer = lume.clamp(self.timer - 3*dt, 0, 1)
else
self.timer = lume.clamp(self.timer + 3*dt, 0, 1)
end
local t = ease.inOutCubic(self.timer)
self.x = lume.lerp(self.openPos.x, self.closedPos.x, t)
self.y = lume.lerp(self.openPos.y, self.closedPos.y, t)
end
end
table.insert(hud.panels, t)
return t
end
function hud.addButton(t)
local defaults = {
x = 0, y = 0,
img = gfx.ui.buttons.down
-- panel, action, draw nil
}
for k, v in pairs(defaults) do
if t[k] == nil then t[k] = v end
end
if t.panel then
t.panelOffset = {
x = t.x - t.panel.x,
y = t.y - t.panel.y
}
if not t.update then
t.update = function(self, dt)
self.x = self.panel.x + self.panelOffset.x
self.y = self.panel.y + self.panelOffset.y
end
end
end
table.insert(hud.buttons, t)
return t
end
function hud.load()
hud.mapPanel = hud.addPanel{
img = gfx.hud.panels.map, openPos={x=392, y=13}, closedPos={x=473, y=13}
}
hud.mapButton = hud.addButton{
img=gfx.ui.buttons.right, x=388, y=28, panel=hud.mapPanel,
action = function(self)
self.panel.open = not self.panel.open
self.img = self.panel.open and gfx.ui.buttons.right or gfx.ui.buttons.left
end
}
hud.chatPanel = hud.addPanel{
img=gfx.hud.panels.chat, openPos={x=6, y=136}, closedPos={x=6, y=270}
}
hud.addButton{
img=gfx.hud.buttons.chat, x=16, y=235,
update = function(self, dt)
local t = ease.inOutCubic(hud.chatPanel.timer)
self.y = lume.lerp(132, 235, t)
end,
action = function(self)
hud.chatPanel.open = not hud.chatPanel.open
if not hud.chatPanel.open then
chat.active = false
end
end,
draw = function(self, mx, my)
love.graphics.setColor(1, 1, 1)
local t = gameTime - chat.lastMsgTime
if t < 1 then
local c = (math.cos(t*2*math.pi)+3)/4
love.graphics.setColor(c, c, 1)
end
if mx > self.x and mx < self.x + self.img:getWidth() and my > self.y and my < self.y + self.img:getHeight() then
cursor.cursor = cursor.hand
local r, g, b = love.graphics.getColor()
love.graphics.setColor(r*0.8, g*0.8, b*0.8)
end
love.graphics.draw(self.img, lume.round(self.x), lume.round(self.y))
end
}
hud.addButton{
img=gfx.hud.buttons.chatField, x=11, y=253, panel=hud.chatPanel, id='chatField',
action = function(self)
chat.active = true
end
}
hud.chatPanel.open = false -- attaching chatField while open
hud.chatPanel.timer = 1
hud.statsPanel = hud.addPanel{
img=gfx.hud.panels.stats, openPos={x=160, y=170}, closedPos={x=160, y=241}, open=false
}
hud.addButton{
img=gfx.hud.buttons.stats, x=175, y=236, panel=hud.statsPanel,
action = function(self)
self.panel.open = not self.panel.open
end
}
hud.addButton{
img=gfx.hud.buttons.backpack, x=287, y=236, panel=hud.statsPanel
}
hud.inventoryPanel = hud.addPanel{
img=gfx.hud.panels.inventory, openPos={x=378, y=144}, closedPos={x=473, y=144}
}
hud.inventoryButton = hud.addButton{
img=gfx.ui.buttons.right, x=374, y=178, panel=hud.inventoryPanel,
action = function(self)
self.panel.open = not self.panel.open
self.img = self.panel.open and gfx.ui.buttons.right or gfx.ui.buttons.left
end
}
end
function hud.update(dt)
for _, v in pairs(hud.panels) do
if v.update then v.update(v, dt) end
end
for _, v in pairs(hud.buttons) do
if v.update then v.update(v, dt) end
end
end
function hud.mousepressed(x, y, btn, isTouch, presses)
local mx, my = window2game(x, y)
mx, my = lume.round(mx), lume.round(my)
-- deactivate chat if click outside field
local chatFieldPressed = false
for _, v in pairs(hud.buttons) do
if mx > v.x and mx < v.x + v.img:getWidth() and my > v.y and my < v.y + v.img:getHeight() then
if v.action then v.action(v) end
uiMouseDown = true
if v.id == 'chatField' then chatFieldPressed = true end
end
end
if chat.active and not chatFieldPressed then
chat.active = false
end
end
function hud.mousereleased(x, y, btn, isTouch, presses)
local mx, my = window2game(x, y)
mx, my = lume.round(mx), lume.round(my)
end
function hud.keypressed(k, scancode, isrepeat)
if scancode == 'tab' then
hud.inventoryPanel.open = not hud.inventoryPanel.open
hud.inventoryButton.img = hud.inventoryPanel.open and gfx.ui.buttons.right or gfx.ui.buttons.left
end
if k == 'm' then
hud.mapPanel.open = not hud.mapPanel.open
hud.mapButton.img = hud.mapPanel.open and gfx.ui.buttons.right or gfx.ui.buttons.left
elseif k == 'l' then
hud.statsPanel.open = not hud.statsPanel.open
end
end
function hud.draw()
local _canvas = love.graphics.getCanvas()
local _shader = love.graphics.getShader()
local mx, my = window2game(love.mouse.getPosition())
mx, my = lume.round(mx), lume.round(my)
local p = playerController.player
love.graphics.setColor(1, 1, 1)
love.graphics.draw(gfx.hud.frame, 0, 0)
-- hp/mana
love.graphics.setShader(shaders.lifemana)
shaders.lifemana:send('hp', p.hp/p.hpMax)
shaders.lifemana:send('mana', 1)
love.graphics.rectangle('fill', 11, 18, 131, 25)
love.graphics.setShader(_shader)
clientRealm.world:drawMap('mini')
-- panels/buttons
love.graphics.setColor(1, 1, 1)
for _, v in pairs(hud.panels) do
love.graphics.draw(v.img, lume.round(v.x), lume.round(v.y))
end
for _, v in pairs(hud.buttons) do
if v.draw then
v.draw(v, mx, my)
else
love.graphics.setColor(1, 1, 1)
if mx > v.x and mx < v.x + v.img:getWidth() and my > v.y and my < v.y + v.img:getHeight() then
cursor.cursor = cursor.hand
love.graphics.setColor(0.8, 0.8, 0.8)
end
love.graphics.draw(v.img, lume.round(v.x), lume.round(v.y))
end
end
-- level bar
local l = p.xp2level(p.xp)
local x, y = hud.statsPanel.x, hud.statsPanel.y
love.graphics.setColor(221/255, 217/255, 0)
local t = l - math.floor(l)
love.graphics.rectangle('fill', 191, lume.round(y + 20), t*99, 3)
love.graphics.setColor(1, 1, 1)
local font = fonts.c17
love.graphics.setFont(font)
local level = tostring(math.floor(l))
text.print(level, lume.round(240 - font:getWidth(level)/2), lume.round(y))
-- stats
local font = fonts.stats
love.graphics.setFont(font)
local stats_x, stats_y = 202, 227
local stats_dx, stats_dy = 20, 11
for j, row in ipairs{'base', 'arm', 'total'} do
local c = ({
{255/255, 175/255, 48/255},
{255/255, 84/255, 252/255},
{48/255, 255/255, 241/255}
})[j]
love.graphics.setColor(c)
for i, col in ipairs{'vit', 'atk', 'spd', 'wis', 'def', 'reg'} do
local sx = stats_x + stats_dx*(i-1)
local sy = stats_y + stats_dy*(j-1)
sx = x + (sx - hud.statsPanel.openPos.x)
sy = y + (sy - hud.statsPanel.openPos.y)
local txt = tostring(p.stats[col][row])
text.print(txt, lume.round(sx - font:getWidth(txt)/2), lume.round(sy - font:getHeight()/2))
end
end
love.graphics.setCanvas(canvases.tempGame)
love.graphics.clear()
local hpHovered = mx > 39 and mx < 141 and my > 23 and my < 31
local txt = string.format('%i / %i', p.hp, p.hpMax)
love.graphics.setColor(0, 0, 0)
text.print(txt, lume.round(90 - font:getWidth(txt)/2 - 1), lume.round(27 - font:getHeight()/2 + 1))
love.graphics.setColor(1, 1, 1)
text.print(txt, lume.round(90 - font:getWidth(txt)/2), lume.round(27 - font:getHeight()/2))
love.graphics.setCanvas(_canvas)
if hpHovered then
love.graphics.setColor(1, 1, 1, 0.8)
else
love.graphics.setColor(1, 1, 1, 0.4)
end
love.graphics.draw(canvases.tempGame, 0, 0)
love.graphics.setColor(1, 1, 1)
love.graphics.setFont(fonts.c17)
text.print(p.name, 44, 5)
-- inventory items
love.graphics.push()
local panel = hud.inventoryPanel
love.graphics.translate(panel.x, panel.y)
local pmx = mx - lume.round(panel.x)
local pmy = my - lume.round(panel.y)
for slotId, slot in ipairs(hud.inventorySlots) do
local item = items.client.getItem(p.inventory.items[slotId])
if pmx >= slot.x and pmx <= slot.x + slot.w
and pmy >= slot.y and pmy <= slot.y + slot.h and panel.open 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 == 'inventory' 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()
-- item info
local item = playerController.hoveredItem
if item then
love.graphics.setColor(1, 1, 1)
love.graphics.setShader(shaders.panel)
local w = 104
local h = 91
local x = lume.clamp(mx - w, 0, gsx - w)
local y = lume.clamp(my - h, 0, gsy - h)
shaders.panel:send('box', {x, y, w, h})
love.graphics.rectangle('fill', x, y, w, h)
love.graphics.setShader(_shader)
love.graphics.push()
love.graphics.translate(x, y)
love.graphics.draw(gfx.ui.itemInfo.base, 0, 0)
love.graphics.setColor(200/255, 88/255, 9/255)
love.graphics.draw(gfx.ui.itemInfo.tierColor, 5, 6)
love.graphics.setColor(1, 1, 1)
love.graphics.draw(gfx.items[item.imageId], 7, 8)
love.graphics.draw(gfx.ui.icons.atk, 14, 29)
love.graphics.draw(gfx.ui.icons.wis, 57, 29)
love.graphics.draw(gfx.ui.icons.spd, 14, 51)
love.graphics.draw(gfx.ui.icons.reg, 57, 51)
local font = fonts.stats
love.graphics.setFont(font)
love.graphics.setColor(193/255, 193/255, 193/255)
text.print('"TOOLTIP WOULD GO\nHERE"', 26, 9)
love.graphics.setColor(1, 1, 1)
love.graphics.draw(gfx.ui.itemInfo.specialIcon, 9, 74)
text.print('25% CHANCE TO BURN', 16, 74)
local playerWeapon = items.client.getItem(p.inventory.items[2])
if isSword[item.imageId] and item.atk then
if playerWeapon and playerWeapon.atk then
if item.atk < playerWeapon.atk then
love.graphics.setColor(1, 0, 0)
elseif item.atk > playerWeapon.atk then
love.graphics.setColor(0, 1, 0)
end
end
text.print(item.atk, 35, 34)
else
text.print('100', 35, 34)
end
love.graphics.setColor(1, 1, 1)
text.print('100', 78, 34)
text.print('100', 35, 56)
text.print('100', 78, 56)
love.graphics.pop()
end
-- held item
local heldItem = playerController.heldItem
if heldItem.itemId then
local item = items.client.getItem(heldItem.itemId)
if item then
cursor.cursor = cursor.hand
love.graphics.setColor(1, 1, 1)
love.graphics.draw(gfx.items[item.imageId], mx + heldItem.offset.x, my + heldItem.offset.y)
end
end
end