From a39ad6b75ee7283c44ffa399b93dbf6663970169 Mon Sep 17 00:00:00 2001 From: VisualGMQ <2142587070@qq.com> Date: Tue, 2 Aug 2022 19:29:44 +0800 Subject: [PATCH] add animation --- game/animation.lua | 80 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 game/animation.lua diff --git a/game/animation.lua b/game/animation.lua new file mode 100644 index 0000000..91cbf05 --- /dev/null +++ b/game/animation.lua @@ -0,0 +1,80 @@ +local hazel = require "hazel" + +---@class Animation +local _M = {} + +---@class Frame +---@field row number +---@field col number +---@field time number + +---@param tilesheet Texture +---@param frames table +function _M.CreateAnimation(tilesheet, frames) + local o = {tilesheet = tilesheet, frames = frames, index = 1, counter = 0, isPlaying = false} + setmetatable(o, {__index = _M}) + return o +end + +---@param self Animation +function _M.Play(self) + self.isPlaying = true +end + +---@param self Animation +function _M.Stop(self) + self.isPlaying = false + self.index = 1 + self.counter = 0 +end + +---@param self Animation +function _M.Pause(self) + self.isPlaying = false +end + +function _M.GetCurFrame(self) + if self.index > #self.frames then + return self.frames[#self.frames] + else + return self.frames[self.index] + end +end + +function _M.GetTilesheet(self) + return self.tilesheet +end + +---@param self Animation +function _M.Rewind(self) + self.index = 0 + self.counter = 0 +end + +---@param self Animation +function _M.IsPlaying(self) + return self.isPlaying +end + +---@param self Animation +function _M.Update(self) + if not self.frames or not self:IsPlaying() then + return + end + if self.index > #self.frames then + self:Pause() + 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 + end +end + +return _M \ No newline at end of file