Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- metaclass = {}
- function metaclass.initClass(frames, duration, isLooping)
- local cls = {__type = "animation"}
- cls.frames = frames
- cls.nbFrames = #frames
- cls.duration = duration
- cls.speed = cls.nbFrames / cls.duration -- frames / seconde
- cls.isLooping = (isLooping ~= false)
- -- FONCTION D'INSTANCIATION
- function cls.new()
- local self = setmetatable({}, {__index = cls})
- cls:reset()
- return self
- end
- -- LES METHODES
- function cls:reset()
- self.isAnim = true
- self.elapsedTime = 0
- self.index = 1
- self.frame = cls.frames[self.index]
- end
- function cls:update(dt, reset)
- if reset == true then
- self:reset()
- else
- self.elapsedTime = self.elapsedTime + dt
- if self.elapsedTime > cls.duration then
- self.isAnim = cls.isLooping
- end
- if self.isAnim == true then
- self.index = math.floor(cls.speed * self.elapsedTime) + 1
- self.frame = cls.frames[self.index]
- end
- end
- return self.frame
- end
- return cls
- end
- return metaclass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement