feat: add damage system

Fixes: #90
This commit is contained in:
Kazhnuz 2021-04-19 18:04:29 +02:00
parent 36e30be245
commit e7878c1efa
9 changed files with 157 additions and 5 deletions

View file

@ -70,7 +70,7 @@ return {
forceAction = nil, forceAction = nil,
level=-8, level=-8,
height=0, height=0,
speedFactor = 0.66, speedFactor = 0.5,
canJump = false, canJump = false,
} }
}, },
@ -79,7 +79,7 @@ return {
forceAction = nil, forceAction = nil,
level=0, level=0,
height=8, height=8,
speedFactor = 0.66, speedFactor = 0.5,
canJump = false, canJump = false,
} }
} }

View file

@ -93,6 +93,17 @@ function CharacterManager:heal(name)
self.list[name]:heal() self.list[name]:heal()
end end
function CharacterManager:sendDamageFromMap(name, damageRatio)
local character = self.list[name]
if (character.hp > 0) then
local newHP = math.floor(character.hp - (character.stats.hpmax * damageRatio))
if (not game.difficulty:get("hazardMakesKo")) then
newHP = math.max(1, newHP)
end
self.list[name]:setHP(newHP, false)
end
end
function CharacterManager:addToTeam(name) function CharacterManager:addToTeam(name)
self:heal(name) self:heal(name)
if (#self.team < 4) then if (#self.team < 4) then

View file

@ -8,6 +8,7 @@ function PlayerHealth:initHealth()
self.hpbar = ComplexHPBar(HPBAR_SIZE) self.hpbar = ComplexHPBar(HPBAR_SIZE)
self.hpbar:setColorForeground(248/255, 160/255, 0, 1) self.hpbar:setColorForeground(248/255, 160/255, 0, 1)
self.hpbar:setColorBackground(112/255, 0, 0) self.hpbar:setColorBackground(112/255, 0, 0)
self.fallDamage = 0
end end
function PlayerHealth:drawHealth(x, y) function PlayerHealth:drawHealth(x, y)
@ -20,4 +21,16 @@ function PlayerHealth:drawHealth(x, y)
end end
end end
function PlayerHealth:takeDamage(damage)
local damage = damage or 10
damage = damage / 100
if (game.difficulty:get("allDamage")) then
for _, name in ipairs(game.characters.team) do
game.characters:sendDamageFromMap(name, damage)
end
else
game.characters:sendDamageFromMap(game.characters.team[game.characters.active], damage)
end
end
return PlayerHealth return PlayerHealth

View file

@ -81,6 +81,7 @@ function Player:checkGround()
if (self.z <= RESPAWN_LIMIT) then if (self.z <= RESPAWN_LIMIT) then
self.x = self.lastPos.x self.x = self.lastPos.x
self.y = self.lastPos.y self.y = self.lastPos.y
self:takeDamage(self.fallDamage)
end end
end end
end end

View file

@ -129,8 +129,10 @@ function PlayerInteractions:setTerrainData(data)
if (self.onGround) then if (self.onGround) then
self.speedFactor = data.speedFactor or 1 self.speedFactor = data.speedFactor or 1
self.canJump = data.canJump ~= false self.canJump = data.canJump ~= false
self.canAct = data.canAct ~= false
self.forceAction = data.forceAction self.forceAction = data.forceAction
end end
self.fallDamage = data.fallDamage or 0
self.terrain.data = data self.terrain.data = data
end end

View file

@ -16,10 +16,21 @@ function Team:initTeam()
end end
function Team:updateActiveCharacter() function Team:updateActiveCharacter()
local everybodyIsKo = true
for id, name in ipairs(game.characters.team) do
if (game.characters.list[name].hp > 0) then
everybodyIsKo = false
break;
end
end
if (everybodyIsKo) then
self.scene:gameover()
else
if ((self.active.hp == 0) and not game.difficulty:get("playerKoChar")) then if ((self.active.hp == 0) and not game.difficulty:get("playerKoChar")) then
self:switchActiveCharacter() self:switchActiveCharacter()
end end
end end
end
function Team:getCurrentCharType() function Team:getCurrentCharType()
return self.active.data.class return self.active.data.class

View file

@ -139,6 +139,15 @@ function OverWorld:pause()
screens.mainmenu.pause(self) screens.mainmenu.pause(self)
end end
function OverWorld:gameover()
self.tweens:newTween(0,0.2, {backGroundOpacity=0.75}, "inQuad")
self.tweens:newTween(0,0.3, {borderPosition=30}, "inOutQuad")
self.tweens:newTween(0, 0.3, {emblemPosition=500}, "inOutQuad")
self.tweens:newTween(0, 0.3, {ringBorder=-16}, "inOutQuad")
self.world.isActive = false
screens.gameover(self)
end
function OverWorld:restored() function OverWorld:restored()
self.world:restoreActions() self.world:restoreActions()
end end

View file

@ -0,0 +1,104 @@
local BaseScreen = require "scenes.overworld.screens.parent"
local GameOver = BaseScreen:extend()
local TweenManager = require "game.modules.tweenmanager"
local gui = require "game.modules.gui"
local tw, th = 128, 32
local ConfirmDialog = require "game.modules.confirmdialog"
local defTransitions = require "core.modules.transitions"
local radTransitions = require "game.modules.transitions"
local ConfirmDialog = require "game.modules.confirmdialog"
function GameOver:new(scene)
GameOver.super.new(self, scene, "")
self.assets = scene.assets
self.turnSystem = scene.turns
self:setVariables()
self.continueBox = gui.newTextBox("assets/gui/dialogbox.png", tw, th)
self.tweens = TweenManager(self)
self:prepareAnimation()
end
function GameOver:setVariables()
-- Vignette Opacity
self.vignetteOpacity = 0
-- Battle FInished Label
self.labelOpacity = 0
local width, height = core.screen:getDimensions()
self.labelX = width/2
-- Infobox
self.tbSize = 0.6
self.tbOpacity = 0
self.continue = false
self.continueValue = -1
end
function GameOver:prepareAnimation()
-- Label
self.tweens:newTween(0, 0.6, {labelOpacity=1}, 'inExpo')
self.tweens:newTween(0.9, 0.4, {labelX=4}, 'inExpo')
self.tweens:newSwitch(1.8, {"continue"})
end
function GameOver:update(dt)
self.tweens:update(dt)
if (self.continue) then
self.continue = false
local confirm = ConfirmDialog(self.scene, "Do you want to return to title ? \nYou can also reload your latest save.",
function() self:returnToTitle() end, "Return to title",
function() self:loadLastSave() end, "Reload last save")
confirm.darken = false
end
end
function GameOver:draw()
local width, height = core.screen:getDimensions()
self:drawLabel(width/2, 48, self.labelX, self.labelOpacity)
end
function GameOver:drawVignette(width, height)
love.graphics.setColor(0, 0, 0, self.vignetteOpacity)
love.graphics.rectangle("fill", 0, 0, width, height)
end
function GameOver:drawLabel(x, y, x2, opacity)
love.graphics.setColor(1, 1, 1, opacity)
self.assets.fonts["SA2font"]:print("GAME", x - x2, y, "right")
self.assets.fonts["SA2font"]:print("OVER", x + x2, y, "left")
end
function GameOver:drawContinueBox(x, y, continueValue, size, opacity)
love.graphics.setColor(1, 1, 1, opacity)
love.graphics.draw(self.continueBox, x, y, 0, size, size, tw/2, th/2)
local text = "continue"
if (continueValue == 1) then
text = "quit"
end
self.assets.images["arrow"]:draw(x - continueValue*64, y, math.rad(-90) * continueValue, continueValue, 1, 13, 13)
self.assets.fonts["SA2font"]:print(text, x, y-13, "center")
end
function GameOver:returnToTitle()
core.screen:startTransition(defTransitions.default, defTransitions.circle, function() scenes.title(true) end, 424/2, 240/2)
end
function GameOver:loadLastSave()
self.scene.tweens:newTween(0, 0.3, {borderPosition=0}, "inOutQuad")
core.screen:startTransition(defTransitions.default, defTransitions.default, function() game:reload() scenes.overworld() end, 424/2, 240/2)
end
return GameOver

View file

@ -1,3 +1,4 @@
return { return {
mainmenu = require "scenes.overworld.screens.mainmenu" mainmenu = require "scenes.overworld.screens.mainmenu",
gameover = require "scenes.overworld.screens.gameover"
} }