Object.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. -- Copyright (c) 2011-2012 Casey Baxter
  2. -- See LICENSE file for details
  3. ---------------------------------------------------------------------------------------------------
  4. -- -= Object =-
  5. ---------------------------------------------------------------------------------------------------
  6. -- Setup
  7. local Object = {class = "Object"}
  8. Object.__index = Object
  9. ---------------------------------------------------------------------------------------------------
  10. -- Returns a new Object
  11. function Object.init(o)
  12. local obj = setmetatable(o, Object)
  13. obj.drawInfo = {
  14. -- x and y are the drawing location of the object. This is different than the object's x and
  15. -- y value which is the object's placement on the map.
  16. x = 0, -- The x draw location
  17. y = 0, -- The y draw location
  18. -- These limit the drawing of the object. If the object falls out of the bounds of
  19. -- the map's drawRange then the object will not be drawn.
  20. left = 0, -- The leftmost point on the object
  21. right = 0, -- The rightmost point on the object
  22. top = 0, -- The highest point on the object
  23. bottom = 0, -- The lowest point on the object
  24. -- The order to draw the object in relation to other objects. Usually equal to bottom.
  25. order = 0,
  26. -- In addition to this, other drawing information can be stored in the numerical
  27. -- indicies which is context sensitive to the map's orientation, if the object has a gid, or
  28. -- of the object is a polygon or polyline object.
  29. }
  30. -- Update the draw info
  31. Object.updateDrawInfo(obj)
  32. -- Return our object
  33. return obj
  34. end
  35. ---------------------------------------------------------------------------------------------------
  36. -- Updates the draw information. Call this every time the object moves or changes size.
  37. function Object:updateDrawInfo()
  38. local di = self.drawInfo
  39. local map = self.layer.map
  40. local verts = self.polygon or self.polyline
  41. local iso = map.orientation == "isometric"
  42. local ox, oy, x, y = map.offsetX, map.offsetY, self.x, self.y
  43. if verts then
  44. self.drawInfo = {}
  45. di = self.drawInfo
  46. if iso then
  47. for k = 1, #verts, 2 do
  48. di[k], di[k + 1] = map:fromIso(x + verts[k], y + verts[k + 1])
  49. di[k], di[k + 1] = di[k] - ox, di[k + 1] - oy
  50. end
  51. else
  52. for k = 1, #verts, 2 do
  53. di[k], di[k + 1] = x + verts[k] - ox, y + verts[k+1] - oy
  54. end
  55. end
  56. di.x, di.y = di[1], di[2]
  57. di.left, di.right, di.top, di.bottom = di.x, di.x, di.y, di.y
  58. for k = 1, #di, 2 do
  59. x, y = di[k], di[k + 1]
  60. if x < di.left then di.left = x end
  61. if x > di.right then di.right = x end
  62. if y < di.top then di.top = y end
  63. if y > di.bottom then di.bottom = y end
  64. end
  65. di.order = di.bottom
  66. elseif self.gid then
  67. local t = map.tiles[self.gid]
  68. if iso then
  69. di.x, di.y = map:fromIso(x, y)
  70. di.x, di.y = di.x - ox, di.y - oy
  71. di.x = di.x - map.tileWidth / 2
  72. else
  73. di.x, di.y = x - ox, y - oy
  74. end
  75. di.order = di.y
  76. di.left, di.right = di.x, di.x + t.width
  77. di.top, di.bottom = di.y, di.y + t.height
  78. elseif iso then
  79. di[1], di[2] = map:fromIso(x, y)
  80. di[3], di[4] = map:fromIso(x + self.width, y)
  81. di[5], di[6] = map:fromIso(x + self.width, y + self.height)
  82. di[7], di[8] = map:fromIso(x, y + self.height)
  83. for i =1, 7, 2 do
  84. di[i], di[i + 1] = di[i] - ox, di[i + 1] - oy
  85. end
  86. di.left, di.right, di.top, di.bottom = di[7], di[3], di[2], di[6]
  87. di.order = 1
  88. else
  89. di.x, di.y = x - ox, y - oy
  90. di[1], di[2] = di.x, di.y
  91. di[3], di[4] = self.width, self.height
  92. di.left, di.top, di.right, di.bottom = di.x, di.y , di.x + di[3], di.y + di[4]
  93. di.order = 1
  94. end
  95. end
  96. ---------------------------------------------------------------------------------------------------
  97. -- Draw the object. The passed color is the color of the object layer the object belongs to.
  98. function Object:draw(x, y, r, g, b, a)
  99. if not self.visible then return end
  100. local di = self.drawInfo
  101. love.graphics.setLineWidth(1)
  102. if self.text then
  103. love.graphics.setColor(r, g, b, a)
  104. love.graphics.print(self.text, di[1], di[2])
  105. elseif self.ellipse then
  106. local rx, ry = di[3] / 2, di[4] / 2
  107. local cx, cy = di[1] + rx, di[2] + ry
  108. love.graphics.setColor(r, g, b, a / 4)
  109. love.graphics.ellipse("fill", cx, cy, rx, ry)
  110. love.graphics.setColor(r, g, b, a)
  111. love.graphics.ellipse("line", cx, cy, rx, ry)
  112. elseif self.point then
  113. love.graphics.setColor(r, g, b, a)
  114. love.graphics.circle("fill", di[1], di[2], 2)
  115. -- The object is a polyline.
  116. elseif self.polyline then
  117. love.graphics.setColor(r, g, b, a)
  118. love.graphics.line(di)
  119. -- The object is a polygon.
  120. elseif self.polygon then
  121. love.graphics.setColor(r, g, b, a / 4)
  122. love.graphics.polygon( "fill", di)
  123. love.graphics.setColor(r, g, b, a)
  124. love.graphics.polygon( "line", di)
  125. -- The object is a tile object. Draw the tile.
  126. elseif self.gid then
  127. love.graphics.setColor(1,1,1,a)
  128. local tile = self.layer.map.tiles[self.gid]
  129. tile:draw(di.x, di.y - self.height, 0 , self.width / tile.width, self.height / tile.height)
  130. -- Map is isometric. Draw a parallelogram.
  131. elseif self.layer.map.orientation == "isometric" then
  132. love.graphics.setColor(r, g, b, a / 4)
  133. love.graphics.polygon("fill", di)
  134. love.graphics.setColor(r,g,b,a)
  135. love.graphics.polygon("line", di)
  136. -- Map is orthogonal. Draw a rectangle.
  137. else
  138. local rx,ry,rw,rh = unpack(di)
  139. love.graphics.setColor(r, g, b, a / 4)
  140. love.graphics.rectangle("fill", rx,ry,rw,rh)
  141. love.graphics.setColor(r, g, b, a)
  142. love.graphics.rectangle("line", rx,ry,rw,rh)
  143. end
  144. end
  145. ---------------------------------------------------------------------------------------------------
  146. -- Returns the Object class
  147. return Object