Compare commits

..

No commits in common. "b7bd164269f7e662940ceb870c2e5c4294e62b76" and "5cead3c28291a1785797ec0741342437535a0013" have entirely different histories.

4 changed files with 20 additions and 27 deletions

View File

@ -8,28 +8,35 @@ RIGHT = 1
--- @class Animation --- @class Animation
--- @field animationTable table<string, table> --- @field animationTable table<string, table>
--- @field animationGrid table
--- @field state "idle"|"run"|"hurt"|"attack" --- @field state "idle"|"run"|"hurt"|"attack"
--- @field side 1|-1 --- @field side 1|-1
local animation = {} local animation = {}
animation.__index = animation
local function new(id, spriteDir) local function new(id, spriteDir)
local anim = { local anim = {
animationTable = {}, animationTable = {}
animationGrid = {}
} }
local animationGrid = {}
-- n: name; i: image -- n: name; i: image
for n, i in pairs(spriteDir) do for n, i in pairs(spriteDir) do
local aGrid = anim8.newGrid(96, 64, i:getWidth(), i:getHeight()) local aGrid = anim8.newGrid(96, 64, i:getWidth(), i:getHeight())
local tiles = '1-' .. math.ceil(i:getWidth() / 96) local tiles = '1-' .. math.ceil(i:getWidth() / 96)
anim.animationGrid[n] = aGrid(tiles, 1) animationGrid[n] = aGrid(tiles, 1)
end end
anim.state = "idle" anim.state = "idle"
anim.side = RIGHT anim.side = RIGHT
anim.animationTable.idle = anim8.newAnimation(animationGrid["idle"], ANIMATION_SPEED)
anim.animationTable.run = anim8.newAnimation(animationGrid["run"], ANIMATION_SPEED)
anim.animationTable.attack = anim8.newAnimation(animationGrid["attack"], ANIMATION_SPEED, function()
anim.state = "idle"
end)
anim.animationTable.hurt = anim8.newAnimation(animationGrid["hurt"], ANIMATION_SPEED, function()
anim.state = "idle"
end)
return setmetatable(anim, animation) return setmetatable(anim, animation)
end end
@ -37,9 +44,8 @@ function animation:getState()
return self.state return self.state
end end
--- @param state CharacterState --- @param state State
function animation:setState(state, onLoop) function animation:setState(state)
self.animationTable[state] = anim8.newAnimation(self.animationGrid[state], ANIMATION_SPEED, onLoop)
self.state = state self.state = state
end end

View File

@ -1,11 +1,11 @@
local utils = require "lib.utils.utils" local utils = require "lib.utils.utils"
--- @alias CharacterState "idle"|"run"|"attack"|"hurt" --- @alias State "idle"|"run"|"attack"|"hurt"
--- @class Logic --- @class Logic
--- @field id Id --- @field id Id
--- @field mapLogic MapLogic --- @field mapLogic MapLogic
--- @field state CharacterState --- @field state State
local logic = {} local logic = {}
logic.__index = logic logic.__index = logic
@ -20,18 +20,9 @@ local function new(id, position, size)
}, logic) }, logic)
end end
--- @param state CharacterState
function logic:setState(state)
self.state = state
Tree.level.characters[self.id].graphics.animation:setState(state, (state ~= "idle" and state ~= "run") and function()
self:setState("idle")
end or nil)
end
--- @param path Deque --- @param path Deque
function logic:followPath(path) function logic:followPath(path)
if path:is_empty() then return end if path:is_empty() then return end
self:setState("run")
self.mapLogic.path = path; self.mapLogic.path = path;
---@type Vec3 ---@type Vec3
local nextCell = path:peek_front() local nextCell = path:peek_front()
@ -42,6 +33,7 @@ end
--- @param target Vec3 --- @param target Vec3
function logic:runTo(target) function logic:runTo(target)
self.mapLogic.t0 = love.timer.getTime() self.mapLogic.t0 = love.timer.getTime()
self.state = "run"
self.mapLogic.runTarget = target self.mapLogic.runTarget = target
local charPos = self.mapLogic.position local charPos = self.mapLogic.position
if target.x < charPos.x then if target.x < charPos.x then
@ -62,7 +54,7 @@ function logic:update(dt)
self:runTo(self.mapLogic.path:peek_front()) self:runTo(self.mapLogic.path:peek_front())
self.mapLogic.path:pop_front() self.mapLogic.path:pop_front()
else -- мы добежали до финальной цели else -- мы добежали до финальной цели
self:setState("idle") self.state = "idle"
self.mapLogic.runTarget = nil self.mapLogic.runTarget = nil
end end
else -- анимация перемещения не завершена else -- анимация перемещения не завершена

View File

@ -5,12 +5,6 @@
local spell = {} local spell = {}
spell.__index = spell spell.__index = spell
function spell:update(caster, dt) end
function spell:draw() end
function spell:cast(caster, target) end
local walk = setmetatable({ local walk = setmetatable({
--- @type Deque --- @type Deque
path = nil path = nil

View File

@ -17,11 +17,12 @@ function love.load()
c.logic.mapLogic.position = Vec3 { x, y } c.logic.mapLogic.position = Vec3 { x, y }
c.logic.mapLogic.displayedPosition = Vec3 { x, y } c.logic.mapLogic.displayedPosition = Vec3 { x, y }
c.spellbook = spellbook.of { spellbook.walk } c.spellbook = spellbook.of { spellbook.walk }
c.logic:setState("attack")
end end
end end
end end
-- PlayerFaction.characters = { Hero1, Hero2 }
love.window.setMode(1080, 720, { resizable = true, msaa = 4, vsync = true }) love.window.setMode(1080, 720, { resizable = true, msaa = 4, vsync = true })
end end