Advertisement
gr_eg

Exemple d'utilisation de la métaclasse Animation

May 12th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.89 KB | None | 0 0
  1. -- Récupération de la métaclasse
  2. local Animation = require("Animation")
  3.  
  4.  
  5.  
  6. --------------------- ATTRIBUTS DE CLASSE
  7.  
  8.  
  9. -- Chargement des images pour chaque animation
  10. local IMGS = {}
  11. IMGS.idle[1] = love.graphics.newImage("path/idle1.png")
  12. IMGS.idle[2] = love.graphics.newImage("path/idle2.png")
  13.  
  14. IMGS.attack[1] = love.graphics.newImage("path/attack1.png")
  15. IMGS.attack[2] = love.graphics.newImage("path/attack2.png")
  16. IMGS.attack[3] = love.graphics.newImage("path/attack3.png")
  17. IMGS.attack[4] = love.graphics.newImage("path/attack4.png")
  18.  
  19. IMGS.death[1] = love.graphics.newImage("path/death1.png")
  20. IMGS.death[2] = love.graphics.newImage("path/death2.png")
  21. IMGS.death[3] = love.graphics.newImage("path/death3.png")
  22. IMGS.death[4] = love.graphics.newImage("path/death4.png")
  23. IMGS.death[5] = love.graphics.newImage("path/death5.png")
  24.  
  25.  
  26. -- Création des Animations (se fait une seule fois dans tout le code)
  27. local ANIMS = {}
  28. ANIMS.idle = Animation.initClass(IMGS.idle, 0.4)
  29. ANIMS.attack = Animation.initClass(IMGS.attack, 0.8)
  30. ANIMS.death = Animation.initClass(IMGS.death, 2, false)
  31.  
  32.  
  33.  
  34.  
  35.  
  36. --------------------- ATTRIBUTS D'INSTANCE
  37.  
  38.  
  39. -- C'est lors de l'instanciation des objets qu'on initialise chaque animation.
  40. -- Ainsi chaque objet partage les mêmes animations (attributs de classes), mais avec des temps de parcours différents (attributs d'instances)
  41. perso.anims = {}
  42. perso.anims.idle = ANIMS.idle.new()
  43. perso.anims.attack = ANIMS.attack.new()
  44. perso.anims.death = ANIMS.death.new()
  45.  
  46.  
  47.  
  48. -- Lors de l'utilisation, il suffit d'utiliser l'état dominant (en faisant l'hypothèse que tu stockes l'état dans un attribut 'state')
  49. -- Lors d'une maj
  50. perso.img = perso.anims[perso.state].update(dt)
  51.  
  52. -- Ou lors d'un changement d'état
  53. perso.state = newState
  54. perso.img = perso.anims[perso.state].update(dt, true)
  55.  
  56.  
  57. -- Et donc lors de l'affichage
  58. love.graphics.draw(perso.img, perso.x, perso.y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement