effect_sprite.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. --- Sprites of the battle effects, made from the sprite files (.SPT) of the original game.
  2. --
  3. -- A sprite file is a list of frames, and a frame a list of quads (Game.EffectAnimations, written by
  4. -- the installer). The quads are not billboards: they lie on a plane of the space of the effect, that
  5. -- the effect rotates with angles (a spinning lightning is a quad that rotates around the vertical
  6. -- axis). The corners of a quad are in units of 8 (the draw function, 0x598a7a, shifts them 3 bits).
  7. --
  8. -- Quads are drawn with dynamic meshes, one for every texture, that are filled again every frame.
  9. EffectSprite = {}
  10. --- Gets the frames of a sprite file.
  11. --
  12. -- @param name Name of the file, like "summon_lamu_jo_c04".
  13. -- @return List of frames, nil if it isn't installed.
  14. function EffectSprite.get_frames(name)
  15. return Game ~= nil and Game.EffectAnimations ~= nil and Game.EffectAnimations[name] or nil
  16. end
  17. --- Rotates a point, with angles in degrees around X, then Y, then Z.
  18. function EffectSprite.rotate(x, y, z, ax, ay, az)
  19. if ax ~= 0 then
  20. local c, s = math.cos(math.rad(ax)), math.sin(math.rad(ax))
  21. y, z = y * c - z * s, y * s + z * c
  22. end
  23. if ay ~= 0 then
  24. local c, s = math.cos(math.rad(ay)), math.sin(math.rad(ay))
  25. x, z = x * c + z * s, -x * s + z * c
  26. end
  27. if az ~= 0 then
  28. local c, s = math.cos(math.rad(az)), math.sin(math.rad(az))
  29. x, y = x * c - y * s, x * s + y * c
  30. end
  31. return x, y, z
  32. end
  33. EffectSpriteBatch = {}
  34. EffectSpriteBatch.__index = EffectSpriteBatch
  35. --- Creates a batch of quads that share a texture.
  36. --
  37. -- @param texture Name of the image.
  38. -- @param width Width of the texture of the original game, in pixels.
  39. -- @param height Height of the texture.
  40. -- @param blend 1 for additive, 2 for alpha blending, 0 for opaque.
  41. function EffectSpriteBatch.new(texture, width, height, blend)
  42. local name = battle_manager:create_dynamic_mesh(texture, blend or 1)
  43. if name == nil or name == "" then return nil end
  44. local self = setmetatable({}, EffectSpriteBatch)
  45. self.name, self.width, self.height = name, width, height
  46. self.list, self.count = {}, 0
  47. return self
  48. end
  49. --- Adds a quad.
  50. --
  51. -- @param c1 Corner of the top left, a table with x, y, z in the scene.
  52. -- @param c2 Top right, c3 bottom left and c4 bottom right.
  53. -- @param u0 Position in the texture (pixels): left, v0 top, u1 right and v1 bottom.
  54. -- @param colour Table with r, g, b, a (0 to 1).
  55. function EffectSpriteBatch:add_quad(c1, c2, c3, c4, u0, v0, u1, v1, colour)
  56. local list, n = self.list, self.count
  57. local w, h = self.width, self.height
  58. local function vertex(c, u, v)
  59. list[n + 1], list[n + 2], list[n + 3] = c[1], c[2], c[3]
  60. list[n + 4], list[n + 5] = u / w, v / h
  61. list[n + 6], list[n + 7], list[n + 8], list[n + 9] = colour[1], colour[2], colour[3], colour[4]
  62. n = n + 9
  63. end
  64. vertex(c1, u0, v0) vertex(c2, u1, v0) vertex(c3, u0, v1)
  65. vertex(c2, u1, v0) vertex(c4, u1, v1) vertex(c3, u0, v1)
  66. self.count = n
  67. end
  68. --- Adds a triangle with colours (no texture).
  69. --
  70. -- @param c1 First corner, a table with x, y, z in the scene. c2 and c3 the others.
  71. -- @param colour Table with r, g, b, a (0 to 1).
  72. function EffectSpriteBatch:add_triangle(c1, c2, c3, colour)
  73. local list, n = self.list, self.count
  74. for _, c in ipairs({c1, c2, c3}) do
  75. list[n + 1], list[n + 2], list[n + 3] = c[1], c[2], c[3]
  76. list[n + 4], list[n + 5] = 0, 0
  77. list[n + 6], list[n + 7], list[n + 8], list[n + 9] = colour[1], colour[2], colour[3], colour[4]
  78. n = n + 9
  79. end
  80. self.count = n
  81. end
  82. --- Shows the quads added since the last time, and starts again.
  83. function EffectSpriteBatch:flush()
  84. -- The list can have vertices of a previous frame at the end.
  85. for i = self.count + 1, #self.list do self.list[i] = nil end
  86. battle_manager:set_dynamic_mesh(self.name, self.list)
  87. self.count = 0
  88. end
  89. --- Removes the mesh of the batch.
  90. function EffectSpriteBatch:remove()
  91. if self.name ~= nil then battle_manager:remove_dynamic_mesh(self.name) end
  92. self.name = nil
  93. end