-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathactor.lua
More file actions
116 lines (96 loc) · 2.95 KB
/
actor.lua
File metadata and controls
116 lines (96 loc) · 2.95 KB
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
--[[
The Actor Class for animX
Author: Neer
]]
local Actor={
animations, --list of all the animations in the actor
current, --the current animation (by it's name)
p_onAnimSwitch, --handler called when an animation is switched [internal]
}
--an internal function which just sets the default values
function Actor:init()
self.animations={}
self.p_onAnimSwitch=function() end
end
function Actor:switch(newState)
local prevState=self.current
if prevState==newState then return self end
--This prevents a recursive situation!!
if self:isActive() then self:stopAnimation() end
self.current=newState
self:p_onAnimSwitch(prevState)
self:startAnimation()
return self
end
--Checks if the current animation of the actor is stopped
function Actor:isActive()
if not self:getCurrentAnimation() then return false end
return self:getCurrentAnimation():isActive()
end
--Stops the actor's animation
function Actor:stopAnimation()
if self:getCurrentAnimation() then
self:getCurrentAnimation():stop()
end
return self
end
--Starts the animation for the actor
function Actor:startAnimation()
if self:getCurrentAnimation() then
self:getCurrentAnimation():start()
end
return self
end
--These functions is defined in the main file
Actor.addAnimation=nil
Actor.exportToXML=nil
--Not to be confused with p_onAnimSwitch!
function Actor:onSwitch(fn)
self.p_onAnimSwitch=fn
return self
end
--Returns the given or current animation if given arg is nil
function Actor:getAnimation(name)
return self.animations[name or self.current]
end
--Returns the current animation that's being rendered
function Actor:getCurrentAnimation()
return self:getAnimation(self.current)
end
--Gets a table of all the states
function Actor:getStates()
local t={}
for i in pairs(self.animations) do
t[#t+1]=i
end
return t
end
--Loops all the animations in the actor
function Actor:loopAll(...)
for i in pairs(self.animations) do
self.animations[i]:loop(...)
end
return self
end
--Returns the name of the current animation
function Actor:getName() return self.current end
--Returns the size of the current animation
function Actor:getDimensions() return self:getCurrentAnimation():getDimensions() end
function Actor:getWidth() return self:getCurrentAnimation():getWidth() end
function Actor:getHeight() return self:getCurrentAnimation():getHeight() end
--Sets the style of the image
function Actor:setStyle(val) self:getCurrentAnimation():setStyle(val) return self end
--Flips the current animation
function Actor:flip(...) self:getCurrentAnimation():flip(...) return self end
function Actor:flipX(...) self:getCurrentAnimation():flipX(...) return self end
function Actor:flipY(...) self:getCurrentAnimation():flipY(...) return self end
function Actor:render(...)
self:getCurrentAnimation():render(...)
end
--==Aliases==--
Actor.getCurrentAnim=Actor.getCurrentAnimation
Actor.getState=Actor.getName
Actor.changeAnim=Actor.switch
Actor.set=Actor.switch
Actor.draw=Actor.render
return Actor