Advertisement
gr_eg

La classe Animation

May 12th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.09 KB | None | 0 0
  1. metaclass = {}
  2.  
  3.  
  4.  
  5. function metaclass.initClass(frames, duration, isLooping)
  6.   local cls = {__type = "animation"}
  7.  
  8.   cls.frames = frames
  9.   cls.nbFrames = #frames
  10.   cls.duration = duration
  11.   cls.speed = cls.nbFrames / cls.duration -- frames / seconde
  12.  
  13.   cls.isLooping = (isLooping ~= false)
  14.  
  15.  
  16.  
  17.   -- FONCTION D'INSTANCIATION
  18.   function cls.new()
  19.     local self = setmetatable({}, {__index = cls})
  20.  
  21.     cls:reset()
  22.  
  23.     return self
  24.   end
  25.    
  26.  
  27.  
  28.   -- LES METHODES
  29.   function cls:reset()
  30.     self.isAnim = true
  31.     self.elapsedTime = 0
  32.     self.index = 1
  33.     self.frame = cls.frames[self.index]
  34.   end
  35.  
  36.  
  37.   function cls:update(dt, reset)
  38.     if reset == true then
  39.       self:reset()
  40.    
  41.     else
  42.       self.elapsedTime = self.elapsedTime + dt
  43.       if self.elapsedTime > cls.duration then
  44.         self.isAnim = cls.isLooping
  45.       end
  46.  
  47.       if self.isAnim == true then
  48.         self.index = math.floor(cls.speed * self.elapsedTime) + 1
  49.         self.frame = cls.frames[self.index]
  50.       end
  51.     end
  52.    
  53.     return self.frame
  54.   end
  55.  
  56.  
  57.   return cls
  58. end
  59.  
  60.  
  61. return metaclass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement