1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
--[[
Animation - Sprite sheet animation system for ReiLua-Enhanced
Features:
- Grid-based sprite sheet animation
- Multiple animation tracks per spritesheet
- Configurable FPS and looping
- Callbacks on animation complete
- Pause/resume/reset functionality
Usage:
local Animation = require("lib.animation")
-- Create animation from sprite sheet
local playerAnim = Animation.new(
playerTexture, -- RL.LoadTexture("player.png")
32, 32, -- Frame width, height
{
idle = {frames = {1, 2, 3, 4}, fps = 8, loop = true},
walk = {frames = {5, 6, 7, 8, 9, 10}, fps = 12, loop = true},
jump = {frames = {11, 12, 13}, fps = 10, loop = false}
}
)
playerAnim:play("idle")
-- In update:
playerAnim:update(dt)
-- In draw:
playerAnim:draw(x, y)
]]
local Animation = {}
Animation.__index = Animation
-- Create new animation from sprite sheet
function Animation.new(texture, frameWidth, frameHeight, animations)
local self = setmetatable({}, Animation)
self.texture = texture
self.frameWidth = frameWidth
self.frameHeight = frameHeight
-- Calculate grid dimensions
local texSize = RL.GetTextureSize(texture)
self.columns = math.floor(texSize[1] / frameWidth)
self.rows = math.floor(texSize[2] / frameHeight)
-- Animation tracks
self.animations = animations or {}
-- Current state
self.currentAnim = nil
self.currentFrame = 1
self.frameTimer = 0
self.playing = false
self.paused = false
self.onComplete = nil
-- Default tint and scale
self.tint = RL.WHITE
self.flipX = false
self.flipY = false
self.rotation = 0
self.scale = {1, 1}
self.origin = {frameWidth / 2, frameHeight / 2}
return self
end
-- Add animation track
function Animation:addAnimation(name, frames, fps, loop)
self.animations[name] = {
frames = frames,
fps = fps or 10,
loop = loop ~= false -- Default true
}
end
-- Play animation
function Animation:play(name, onComplete)
if not self.animations[name] then
print("Warning: Animation '" .. name .. "' not found")
return
end
-- Don't restart if already playing
if self.currentAnim == name and self.playing then
return
end
self.currentAnim = name
self.currentFrame = 1
self.frameTimer = 0
self.playing = true
self.paused = false
self.onComplete = onComplete
end
-- Stop animation
function Animation:stop()
self.playing = false
self.currentFrame = 1
self.frameTimer = 0
end
-- Pause animation
function Animation:pause()
self.paused = true
end
-- Resume animation
function Animation:resume()
self.paused = false
end
-- Reset to first frame
function Animation:reset()
self.currentFrame = 1
self.frameTimer = 0
end
-- Update animation
function Animation:update(dt)
if not self.playing or self.paused or not self.currentAnim then
return
end
local anim = self.animations[self.currentAnim]
if not anim then return end
self.frameTimer = self.frameTimer + dt
local frameDuration = 1.0 / anim.fps
-- Advance frames
while self.frameTimer >= frameDuration do
self.frameTimer = self.frameTimer - frameDuration
self.currentFrame = self.currentFrame + 1
-- Check if animation completed
if self.currentFrame > #anim.frames then
if anim.loop then
self.currentFrame = 1
else
self.currentFrame = #anim.frames
self.playing = false
-- Call completion callback
if self.onComplete then
self.onComplete()
self.onComplete = nil
end
end
end
end
end
-- Get source rectangle for current frame
function Animation:getFrameRect()
if not self.currentAnim then
return {0, 0, self.frameWidth, self.frameHeight}
end
local anim = self.animations[self.currentAnim]
if not anim then
return {0, 0, self.frameWidth, self.frameHeight}
end
-- Get frame index from animation
local frameIndex = anim.frames[self.currentFrame] or 1
-- Convert to grid position (0-indexed for calculation)
local gridX = (frameIndex - 1) % self.columns
local gridY = math.floor((frameIndex - 1) / self.columns)
-- Calculate source rectangle
local x = gridX * self.frameWidth
local y = gridY * self.frameHeight
local w = self.frameWidth
local h = self.frameHeight
-- Apply flip
if self.flipX then
w = -w
end
if self.flipY then
h = -h
end
return {x, y, w, h}
end
-- Draw animation at position
function Animation:draw(x, y, rotation, scale, origin, tint)
if not self.texture then return end
local source = self:getFrameRect()
local dest = {
x,
y,
math.abs(source[3]) * (scale and scale[1] or self.scale[1]),
math.abs(source[4]) * (scale and scale[2] or self.scale[2])
}
RL.DrawTexturePro(
self.texture,
source,
dest,
origin or self.origin,
rotation or self.rotation,
tint or self.tint
)
end
-- Draw animation with simple position
function Animation:drawSimple(x, y)
if not self.texture then return end
local source = self:getFrameRect()
RL.DrawTextureRec(self.texture, source, {x, y}, self.tint)
end
-- Set tint color
function Animation:setTint(color)
self.tint = color
end
-- Set flip
function Animation:setFlip(flipX, flipY)
self.flipX = flipX
self.flipY = flipY
end
-- Set scale
function Animation:setScale(sx, sy)
self.scale = {sx, sy or sx}
end
-- Set origin (rotation/scale point)
function Animation:setOrigin(ox, oy)
self.origin = {ox, oy}
end
-- Check if animation is playing
function Animation:isPlaying(name)
if name then
return self.currentAnim == name and self.playing
end
return self.playing
end
-- Get current animation name
function Animation:getCurrentAnimation()
return self.currentAnim
end
-- Get current frame number
function Animation:getCurrentFrame()
return self.currentFrame
end
return Animation
|