From 05d3e8ace8408eee6a513d1c951f7e6bddb9552d Mon Sep 17 00:00:00 2001 From: VisualGMQ <2142587070@qq.com> Date: Tue, 2 Aug 2022 21:58:27 +0800 Subject: [PATCH] finish animation --- game/animation.lua | 14 ++++++++------ game/content.lua | 20 ++++++++++++++++++++ game/main.lua | 30 +++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/game/animation.lua b/game/animation.lua index 91cbf05..51864aa 100644 --- a/game/animation.lua +++ b/game/animation.lua @@ -10,8 +10,9 @@ local _M = {} ---@param tilesheet Texture ---@param frames table -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 diff --git a/game/content.lua b/game/content.lua index 8ff38eb..c8cd263 100644 --- a/game/content.lua +++ b/game/content.lua @@ -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 \ No newline at end of file diff --git a/game/main.lua b/game/main.lua index 8b907df..353a578 100644 --- a/game/main.lua +++ b/game/main.lua @@ -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()