Skip to content

Commit

Permalink
damage numbers, hp & damage affected by stats
Browse files Browse the repository at this point in the history
  • Loading branch information
parameterized committed Feb 2, 2019
1 parent 0733a45 commit 47fc63a
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 13 deletions.
5 changes: 5 additions & 0 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ function client.connect(ip, port)
healPlayer = function(self, data)
local p = playerController.player
p.hp = math.min(p.hp + data.hp, p.hpMax)
end,
damageText = function(self, data)
damageText.add(data)
end
}
for k, v in pairs(bitserRPCs) do
Expand Down Expand Up @@ -119,6 +122,7 @@ function client.connect(ip, port)
clientRealm:destroy()
slimeBalls.reset()
items.client.reset()
damageText.reset()
collectgarbage()
end
client.currentState = client.newState()
Expand Down Expand Up @@ -222,6 +226,7 @@ function client.update(dt)
playerController.update(dt)
entities.client.update(dt)
slimeBalls.update(dt)
damageText.update(dt)
end
end

Expand Down
38 changes: 38 additions & 0 deletions damageText.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

damageText = {
container = {}
}

function damageText.reset()
damageText.container = {}
end

-- t = {v=_, x=_, y=_}
function damageText.add(t)
if t.v == nil then t.v = 0 end
if t.x == nil then t.x = 0 end
if t.y == nil then t.y = 0 end
t.v = tostring(t.v)
t.timer = 1
table.insert(damageText.container, t)
end

function damageText.update(dt)
for k, v in pairs(damageText.container) do
v.timer = v.timer - dt*0.5
if v.timer < 0 then
damageText.container[k] = nil
end
end
end

function damageText.draw()
local font = fonts.stats
love.graphics.setFont(font)
for _, v in pairs(damageText.container) do
local t = ease.outCubic(1 - v.timer)
local y = v.y - t*8
love.graphics.setColor(1, 0, 0, 1 - t)
love.graphics.print(v.v, lume.round(v.x - font:getWidth(v.v)/2), lume.round(y - font:getHeight()/2))
end
end
8 changes: 6 additions & 2 deletions entityDefs/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ for _, sc in ipairs{'server', 'client'} do
player[sc].newStats = function()
local t = {
vit = {base = 100, arm = 14},
atk = {base = 80, arm = 25},
atk = {base = 10, arm = 5},
spd = {base = 50, arm = 9},
wis = {base = 100, arm = 8},
def = {base = 20, arm = 25},
Expand Down Expand Up @@ -122,6 +122,10 @@ for _, sc in ipairs{'server', 'client'} do
end
end

local _hpMax = self.hpMax
self.hpMax = self.stats.vit.total
self.hp = self.hp + (self.hpMax - _hpMax)

self.x, self.y = self.body:getPosition()
self.xv, self.yv = self.body:getLinearVelocity()
self.base.update(self, dt)
Expand Down Expand Up @@ -246,7 +250,7 @@ function player.client:swing()
local playerDamage = 5
local attackItem = items.client.getItem(self.inventory.items[2])
if attackItem and attackItem.atk then
playerDamage = attackItem.atk
playerDamage = attackItem.atk + self.stats.atk.total
end
if self.isLocalPlayer then
client.spawnProjectile{
Expand Down
Binary file modified gfx/fonts/stat_font.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 26 additions & 9 deletions hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,18 @@ function hud.keypressed(k, scancode, isrepeat)
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
local p = playerController.player
love.graphics.setShader(shaders.lifemana)
shaders.lifemana:send('hp', p.hp/p.hpMax)
shaders.lifemana:send('mana', 1)
Expand Down Expand Up @@ -350,7 +352,7 @@ function hud.draw()
end

-- level bar
local l = playerController.player.xp2level(playerController.player.xp)
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)
Expand All @@ -363,7 +365,7 @@ function hud.draw()
text.print(level, lume.round(240 - font:getWidth(level)/2), lume.round(y))

-- stats
font = fonts.stats
local font = fonts.stats
love.graphics.setFont(font)
local stats_x, stats_y = 202, 227
local stats_dx, stats_dy = 20, 11
Expand All @@ -379,15 +381,30 @@ function hud.draw()
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(playerController.player.stats[col][row])
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)
love.graphics.setFont(fonts.c17)
text.print(playerController.player.name, 44, 5)
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()
Expand All @@ -396,7 +413,7 @@ function hud.draw()
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(playerController.player.inventory.items[slotId])
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
Expand Down Expand Up @@ -433,7 +450,7 @@ function hud.draw()
love.graphics.draw(gfx.ui.itemInfo, 0, 0)
font = fonts.stats
love.graphics.setFont(font)
local playerWeapon = items.client.getItem(playerController.player.inventory.items[2])
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
Expand All @@ -454,7 +471,7 @@ function hud.draw()
if heldItem.bagId then
local bag = client.currentState.lootBags[heldItem.bagId]
if heldItem.bagId == 'inventory' then
bag = playerController.player.inventory
bag = p.inventory
end
if bag then
local item = items.client.getItem(bag.items[heldItem.slotId])
Expand Down
2 changes: 1 addition & 1 deletion loadassets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ fonts = {

c13 = love.graphics.newImageFont('gfx/fonts/small_font.png', ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`\'*#=[]"|~@$^_{}<>'),
c17 = love.graphics.newImageFont('gfx/fonts/big_font.png', ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`\'*#=[]"|~@$^_{}<>'),
stats = love.graphics.newImageFont('gfx/fonts/stat_font.png', ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
stats = love.graphics.newImageFont('gfx/fonts/stat_font.png', ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/')
}
-- todo: test
-- love.graphics.newFont([filename, ] size, "mono")
Expand Down
2 changes: 2 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require 'playerController'
require 'items'
require 'lootBag'
require 'portals'
require 'damageText'
require 'realm'
require 'chat'

Expand Down Expand Up @@ -208,6 +209,7 @@ function love.draw()
portals.client.draw()

scene.draw()
damageText.draw()
prof.pop('draw scene')

prof.push('draw debug')
Expand Down
3 changes: 3 additions & 0 deletions physics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ function physics.server:load()
local swing = projectiles.server.container[uda.id]
local enemy = udb
if swing and enemy and not enemy.hitBy[swing.id] then
server.nutServer:sendRPC('damageText', bitser.dumps{
v=swing.damage, x=enemy.x, y=enemy.y-26})
enemy.hitBy[swing.id] = true
enemy:damage(swing.damage, swing.playerId)
swing.pierce = swing.pierce - 1
Expand Down Expand Up @@ -119,6 +121,7 @@ function physics.client:load()
local udb = fixb:getUserData() or {}
if uda.type == 'slimeBall' and udb.id == playerController.player.id then
self:postUpdatePush(function()
damageText.add{v=uda.damage, x=udb.x, y=udb.y-24}
udb:damage(uda.damage)
slimeBalls.destroy(uda.id)
end)
Expand Down
2 changes: 1 addition & 1 deletion server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ function server.addXP(playerId, xp)
local l = entities.server.defs.player.xp2level(p.xp)
if math.floor(_l) ~= math.floor(l) then -- level increased
p.stats.vit.base = math.floor(l*10 + 100)
p.stats.atk.base = math.floor(l*8 + 80)
p.stats.atk.base = math.floor(l*2 + 10)
p.stats.spd.base = math.floor(l*5 + 50)
p.stats.wis.base = math.floor(l*8 + 100)
p.stats.def.base = math.floor(l*4 + 20)
Expand Down
1 change: 1 addition & 0 deletions slimeBalls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ end

function slimeBalls.update(dt)
for k, v in pairs(slimeBalls.container) do
v.x, v.y = v.body:getPosition()
if gameTime - v.spawnTime > v.life then
slimeBalls.destroy(k)
end
Expand Down

0 comments on commit 47fc63a

Please sign in to comment.