finish animation

This commit is contained in:
VisualGMQ 2022-08-02 21:58:27 +08:00
parent a39ad6b75e
commit 05d3e8ace8
3 changed files with 57 additions and 7 deletions

View File

@ -10,8 +10,9 @@ local _M = {}
---@param tilesheet Texture
---@param frames table<Frame>
function _M.CreateAnimation(tilesheet, frames)
local o = {tilesheet = tilesheet, frames = frames, index = 1, counter = 0, isPlaying = false}
---@param onEndCallback function
function _M.CreateAnimation(tilesheet, frames, onEndCallback)
local o = {tilesheet = tilesheet, frames = frames, index = 1, counter = 0, isPlaying = false, onEndCallback = onEndCallback}
setmetatable(o, {__index = _M})
return o
end
@ -47,7 +48,7 @@ end
---@param self Animation
function _M.Rewind(self)
self.index = 0
self.index = 1
self.counter = 0
end
@ -63,14 +64,15 @@ function _M.Update(self)
end
if self.index > #self.frames then
self:Pause()
if self.onEndCallback then
self.onEndCallback(self)
end
return
end
self.counter = self.counter + hazel.Time.GetElapseTime()
---@type Frame
local curFrame = self.frames[self.index]
if not curFrame then
return
end
if self.counter >= curFrame.time then
self.counter = self.counter - curFrame.time
self.index = self.index + 1

View File

@ -37,4 +37,24 @@ _M.MonsterBirthNum = 0
_M.MonsterBirthCountDown = 0
_M.Animations = {
---@type Animation
PlayerWalkDown = nil,
---@type Animation
PlayerWalkUp = nil,
---@type Animation
PlayerWalkRight = nil,
---@type Animation
PlayerWalkLeft = nil,
---@type Animation
EnemyWalkDown = nil,
---@type Animation
EnemyWalkUp = nil,
---@type Animation
EnemyWalkRight = nil,
---@type Animation
EnemyWalkLeft = nil,
}
return _M

View File

@ -4,6 +4,7 @@ local constants = require "constants"
local content = require "content"
local vmath = require "vmath"
local timer = require "timer"
local animation = require "animation"
local function drawCurosr()
hazel.Renderer.SetDrawColor(1, 0, 0, 1)
@ -171,6 +172,11 @@ local function initGame()
content.PlayerEntity = ECS.CreatePlayer(hazel.CreatePos(constants.TileSize * 16, constants.TileSize * 13))
content.MonsterBirthNum = constants.MonsterBirthInitNum
content.GameState = content.GameStateEnum.WaitStart
---@param v Animation
for _, v in pairs(content.Animations) do
v:Rewind()
v:Stop()
end
end
local function generateMonster()
@ -208,6 +214,19 @@ end
---@type Timer
local showLicenseTimer = nil
---@return Animation
---@param row number
---@param time number
local function createAnimation(row, time)
local frame = {}
for i = 0, 2 do
table.insert(frame, {row = row, col = i, time = time})
end
return animation.CreateAnimation(content.Tilesheet, frame, function(self)
self:Rewind()
end)
end
function GameStart()
hazel.SetWindowIcon("resources/icon.png")
content.Texture = hazel.LoadTexture("resources/tilesheet.png")
@ -220,9 +239,18 @@ function GameStart()
content.GameState = content.GameStateEnum.ShowLogo
content.Animations.PlayerWalkDown = createAnimation(0, 0.1)
content.Animations.PlayerWalkUp = createAnimation(1, 0.1)
content.Animations.PlayerWalkRight = createAnimation(2, 0.1)
content.Animations.PlayerWalkLeft = createAnimation(3, 0.1)
content.Animations.EnemyWalkDown = createAnimation(5, 0.1)
content.Animations.EnemyWalkUp = createAnimation(6, 0.1)
content.Animations.EnemyWalkRight = createAnimation(7, 0.1)
content.Animations.EnemyWalkLeft = createAnimation(8, 0.1)
showLicenseTimer = timer.CreateTimer(constants.ShowLicenseTime, 1, function()
content.GameState = content.GameStateEnum.WaitStart
showLicenseTimer = nil
end)
hazel.HideCursor()