PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/graphics.lua

https://github.com/zorggn/Lionblaster
Lua | 121 lines | 83 code | 11 blank | 27 comment | 1 complexity | d28860a33f94a01d6bd7e384a9f400e5 MD5 | raw file
  1. -- the entity's graphics data
  2. -- could probably tear this apart more, but for now, this will suffice
  3. -- and yes, this can only draw images (and image regions) for now, no meshes.
  4. local static = {}
  5. -- creates initial hierarchy structure for entity
  6. static.init = function(self)
  7. -- this always exists as a fallback, and is always overwritten when a set happens, and the tables differ.
  8. current = {
  9. -- dimensions
  10. w = 1,
  11. h = 1,
  12. -- offsets (translation)
  13. ox = 0,
  14. oy = 0,
  15. -- orientation (rotation) and centre offsets
  16. phi = 0,
  17. cx = 0,
  18. cy = 0,
  19. -- scale
  20. sx = 1.0,
  21. sy = 1.0,
  22. -- flip
  23. fv = false,
  24. fh = false,
  25. fd = false,
  26. -- image or atlas/tilemap, with an optional quad
  27. --image = nil
  28. -- quad = nil
  29. -- 3D rotation, not implemented
  30. --[[
  31. r = {
  32. -- horiz., vert., diag. flip; z-axis rotation; x, y shearing; x, y mindpoint scale multiplier
  33. hf,vf,df,phi,kx,ky,kw,kh = false,false,false,0.0,0.0,0.0,1.0,1.0
  34. }
  35. --]]
  36. }
  37. -- these are the separate graphical states for an entity
  38. state = {}
  39. -- string:number pairs for easier handling
  40. stateEnums = {['default'] = 1}
  41. -- which graphics state we are in
  42. currentState = 1
  43. state[1] = {
  44. -- graphics data - state fallback
  45. graphics = current, -- if we edit a state's graphics, we want the fallback to be the last setting, so if it isn't do graphics.current = g.
  46. -- animation data
  47. animation = {
  48. frameCount = 1, -- how many frames are in this set; 1 makes it static, so framedelay, and loop properities are moot in that case.
  49. currentFrame = 1, -- which frame are we on
  50. frameDelay = 0.0, -- how long, in seconds, should a frame stay in place (granularity up to the defined frameRate, not the tickRate)
  51. biDirectional = false, -- if true, do the animation in reverse as well.
  52. isLooping = true, -- if false, it one-shots through the frames
  53. loopStart = 1,
  54. loopEnd = 1,
  55. resetOnSwitch = true, -- if true, it will reset currentFrame to 1, else it will be left alone.
  56. },
  57. -- the frames of a state
  58. frame = {},
  59. frame[1] = {
  60. graphics = current,
  61. frameDelay = 0.0,
  62. }
  63. }
  64. end
  65. -- switch the graphics state
  66. static.switchState = function(self,state,frame)
  67. local state = state
  68. if type(state) ~= 'number' then
  69. state = self.stateEnums[state]
  70. assert(state,"Error: no graphics state found with that name.")
  71. end
  72. assert(self.state[state].animation.frameCount >= frame,"Error: specified frame doesn't exist in the target animation")
  73. self.currentState = state
  74. self.state[state].animation.currentFrame = frame
  75. end
  76. -- toString method
  77. static.toString = function(self)
  78. local t = {}
  79. table.insert(t,'hierarchy:\n')
  80. table.insert(t,'parent: ')
  81. table.insert(t,self.parent or 'nil')
  82. table.insert(t,'\n')
  83. table.insert(t,'children:')
  84. if #self.children then
  85. table.insert(t,' nil')
  86. else
  87. table.insert(t,'\n')
  88. for i,v in ipairs(self.children) do
  89. table.insert(t,' ')
  90. table.insert(t,i)
  91. table.insert(t,'. ')
  92. table.insert(t,v)
  93. end
  94. end
  95. return table.concat(t,'')
  96. end
  97. -- factory
  98. local graphics = function()
  99. return setmetatable(
  100. {},
  101. {
  102. __index = static,
  103. }
  104. )
  105. end
  106. return graphics