fork download
  1. -- BuilderController.lua
  2.  
  3. local BuilderController = class (
  4. function(self, parent, component)
  5. self.parent = parent
  6. self.component = component
  7.  
  8. self.mouseDown = false
  9. self.rightMouseDown = false
  10. self.initMX = 0
  11. self.initMY = 0
  12. self.mX = 0
  13. self.mY = 0
  14.  
  15. self.hasSprite = false
  16. self.spriteKey = ""
  17. self.r = 255
  18. self.g = 0
  19. self.b = 0
  20. self.a = 255
  21.  
  22. self.activeEntity = nil
  23. self.editValues = false
  24.  
  25. self.isBuilding = false
  26. self.isMoving = false
  27. self.isTiling = false
  28.  
  29. self.isTileArea = false
  30. self.pos1 = nil
  31. self.pos2 = nil
  32.  
  33. self.tileset = ""
  34.  
  35. self.snap = true
  36. P = Position
  37. self.brushes = {
  38. {P(0, 0)},
  39. {P(0, 0), P(0, -1), P(-1, 0), P(1, 0), P(0, 1)},
  40. {P(0, 0), P(0, -1), P(-1, 0), P(1, 0), P(0, 1), P(1, 1), P(-1, -1), P(1, -1), P(-1, 1)},
  41. {P(0, 0), P(-1, -1), P(1, 1), P(-2, -2), P(2, 2), P(0, -1), P(-1, 0), P(1, 0), P(0, 1), P(-2, -1), P(2, 1), P(-1, -2), P(1, 2)}
  42. }
  43. self.brush = 2
  44. end
  45. )
  46.  
  47. function BuilderController:onComponentAdd(comp)
  48. if(comp == "Standard/ChatController") then
  49. chatController = self.parent:getComponent(comp)
  50. chatController:registerCommand("build", self)
  51. chatController:registerCommand("place", self)
  52. chatController:registerCommand("move", self)
  53. chatController:registerCommand("remove", self)
  54. chatController:registerCommand("color", self)
  55.  
  56. chatController:registerCommand("tile", self)
  57. chatController:registerCommand("tilearea", self)
  58. chatController:registerCommand("brush", self)
  59. end
  60. if(comp == "Standard/TriggerController") then
  61. triggerController = self.parent:getComponent(comp)
  62. triggerController:registerTrigger(Box(0, 0, 100, 100), self)
  63. end
  64. end
  65.  
  66. function BuilderController:onTrigger(in)
  67. print("hello")
  68. end
  69.  
  70. function BuilderController:createActiveEntity()
  71. if(self.hasSprite) then
  72. self.activeEntity = self.parent.entityManager:createSpriteEntity(Box(self.mX, self.mY, 0, 0), self.spriteKey)
  73. else
  74. self.activeEntity = self.parent.entityManager:createColoredEntity(Box(self.mX, self.mY, 0, 0), Color(self.r, self.g, self.b, self.a))
  75. end
  76.  
  77. self.parent.entityManager:addEntity(self.activeEntity, "")
  78. end
  79.  
  80. function BuilderController:build(commands)
  81. self.isBuilding = true
  82. end
  83.  
  84. function BuilderController:place(commands)
  85. if(commands[2] and commands[3]) then
  86. self.isMoving = true
  87. self:createActiveEntity()
  88. self.activeEntity.body:setDimension(tonumber(commands[2]), tonumber(commands[3]))
  89.  
  90. flagsToToggle = BitOR(EntityProperty.COLLIDABLE, EntityProperty.GRAVITY_AFFECT)
  91. self.activeEntity:removeProperty(flagsToToggle)
  92. end
  93. end
  94.  
  95. function BuilderController:move(commands)
  96. self.activeEntity = self.parent.entityManager:getEntityAtCoordinate(self.mX, self.mY, nil)
  97. if(not self.activeEntity) then do return end end
  98.  
  99. self.activeEntity:removeProperty(BitOR(EntityProperty.COLLIDABLE, EntityProperty.GRAVITY_AFFECT))
  100. self.isMoving = true
  101. end
  102.  
  103. function BuilderController:remove(commands)
  104. self.activeEntity = self.parent.entityManager:getEntityAtCoordinate(self.mX, self.mY, nil)
  105. if(not self.activeEntity) then do return end end
  106.  
  107. self.activeEntity.isDead = true
  108. self.activeEntity = nil
  109. end
  110.  
  111. function BuilderController:color(commands)
  112. if(commands[3] == nil) then
  113. self.spriteKey = commands[2]
  114. self.hasSprite = true
  115. else
  116. self.r = tonumber(commands[2])
  117. self.g = tonumber(commands[3])
  118. self.b = tonumber(commands[4])
  119. if(commands[5]) then
  120. self.a = tonumber(commands[5])
  121. end
  122. self.hasSprite = false
  123. end
  124. end
  125.  
  126. function BuilderController:tile(commands)
  127.  
  128. if(not commands[2]) then
  129. self.isTiling = false
  130. do return end
  131. end
  132.  
  133. self.tileset = commands[2]
  134. self.isTiling = true
  135. end
  136.  
  137. function BuilderController:tilearea(commands)
  138. if(self.isTileArea) then
  139. self.isTileArea = false
  140. do return end
  141. end
  142.  
  143. if(self.tileset == "") then
  144. do return end
  145. end
  146.  
  147. self.isTileArea = true
  148. self.pos1 = Position(self.mX, self.mY)
  149.  
  150. end
  151.  
  152. function BuilderController:changeBrush(commands)
  153. if(not commands[2]) then do return end end
  154.  
  155. self.brush = tonumber(commands[2])
  156. if(self.brush < 1) then self.brush = 1 end
  157. while (self.brush > table.getn(self.brushes)) do
  158. self.brush = self.brush - 1
  159. end
  160. end
  161.  
  162. function BuilderController:onChatCommand(commands)
  163. if(commands[1] == "build") then self:build(commands) end
  164. if(commands[1] == "place") then self:place(commands) end
  165. if(commands[1] == "move") then self:move(commands) end
  166. if(commands[1] == "remove") then self:remove(commands) end
  167. if(commands[1] == "color") then self:color(commands) end
  168. if(commands[1] == "tile") then self:tile(commands) end
  169. if(commands[1] == "tilearea") then self:tilearea(commands) end
  170. if(commands[1] == "brush") then self:changeBrush(commands) end
  171. end
  172.  
  173. function BuilderController:onLoop()
  174. if(self.isBuilding) then
  175. if(self.mouseDown and self.activeEntity == nil) then
  176. self.initMX = self.mX
  177. self.initMY = self.mY
  178. self:createActiveEntity()
  179.  
  180. flagsToToggle = BitOR(EntityProperty.COLLIDABLE, EntityProperty.GRAVITY_AFFECT)
  181. self.activeEntity:removeProperty(flagsToToggle)
  182. end
  183.  
  184. if(not self.mouseDown and self.activeEntity ~= nil) then
  185. flagsToToggle = EntityProperty.COLLIDABLE
  186. self.activeEntity:addProperty(flagsToToggle)
  187.  
  188. self.activeEntity = nil
  189. self.editValues = true
  190. self.isBuilding = false
  191. --self.component.instance.game.ignoreEvents = true
  192. end
  193.  
  194. if(self.activeEntity ~= nil) then
  195. body = self.activeEntity.body
  196.  
  197. dX = self.mX - self.initMX
  198. dY = self.mY - self.initMY
  199. self.activeEntity.body:setDimension(math.abs(dX), math.abs(dY))
  200.  
  201. if(dX < 0) then
  202. body:setPosition(self.mX, body.box.y)
  203. end
  204. if(dY < 0) then
  205. body:setPosition(body.box.x, self.mY)
  206. end
  207. end
  208. end
  209.  
  210. if(self.isMoving) then
  211. if(self.activeEntity ~= nil) then
  212. self.activeEntity.body:setPosition(self.mX, self.mY)
  213. else
  214. self.isMoving = false
  215. end
  216.  
  217. if(self.mouseDown) then
  218. self.isMoving = false
  219.  
  220. self.activeEntity:addProperty(EntityProperty.COLLIDABLE)
  221. self.activeEntity = nil
  222. end
  223. end
  224.  
  225. if(self.isTiling) then
  226. tileSize = game.tileSize()
  227.  
  228. if(self.mouseDown) then
  229. for k, v in pairs(self.brushes[self.brush]) do
  230. self.parent.entityManager:addTile(self.mX + v.x * tileSize, self.mY + v.y * tileSize, self.tileset)
  231. end
  232. end
  233. if(self.rightMouseDown) then
  234. for k, v in pairs(self.brushes[self.brush]) do
  235. self.parent.entityManager:removeTile(self.mX + v.x * tileSize, self.mY + v.y * tileSize)
  236. end
  237. end
  238. end
  239.  
  240. if(self.isTileArea) then
  241. if(self.mouseDown) then
  242. self.isTileArea = false
  243. self.pos2 = Position(self.mX, self.mY)
  244.  
  245. tileSize = game.tileSize()
  246.  
  247. if(self.pos1.x > self.pos2.x) then
  248. temp = self.pos1.x
  249. self.pos1.x = self.pos2.x
  250. self.pos2.x = temp
  251. end
  252. if(self.pos1.y > self.pos2.y) then
  253. temp = self.pos1.y
  254. self.pos1.y = self.pos2.y
  255. self.pos2.y = temp
  256. end
  257.  
  258. for y = self.pos1.y, self.pos2.y, tileSize do
  259. for x = self.pos1.x, self.pos2.x, tileSize do
  260. self.parent.entityManager:addTile(x, y, self.tileset)
  261. end
  262. end
  263.  
  264. self.pos1 = nil
  265. self.pos2 = nil
  266. end
  267. end
  268. end
  269.  
  270. function BuilderController:onKeyStates(state)
  271. self.mX, self.mY = self.component:getRelativeMouse()
  272. self.mouseDown = game:leftMousePressed()
  273. self.rightMouseDown = game:rightMousePressed()
  274. end
  275.  
  276. function BuilderController:onRenderAdditional()
  277. tileSize = game.tileSize()
  278. if(self.isTiling) then
  279. colorR = 0
  280. colorG = 255
  281. if(self.rightMouseDown) then
  282. colorR = 255
  283. colorG = 0
  284. end
  285. for k, v in pairs(self.brushes[self.brush]) do
  286. posX = self.mX + v.x * tileSize
  287. posY = self.mY + v.y * tileSize
  288. if(self.snap) then
  289. posX = math.floor(posX / tileSize) * tileSize
  290. posY = math.floor(posY / tileSize) * tileSize
  291. else
  292. posX = posX - tileSize / 2
  293. posY = posY - tileSize / 2
  294. end
  295.  
  296. self.component:renderRect(posX - self.component.camera:offsetX(), posY - self.component.camera:offsetY(), tileSize, tileSize, colorR, colorG, 0, 100)
  297. end
  298. end
  299. if(self.isTileArea) then
  300. self.component:renderRect(math.floor(self.pos1.x / tileSize) * tileSize - self.component.camera:offsetX(), math.floor(self.pos1.y / tileSize) * tileSize - self.component.camera:offsetY(), math.ceil((self.mX - self.pos1.x) / tileSize) * tileSize, math.ceil((self.mY - self.pos1.y) / tileSize) * tileSize, 255, 255, 0, 100)
  301. end
  302. end
  303.  
  304. function create(parent, component)
  305. return BuilderController(parent, component)
  306. end
  307.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
luac: prog.lua:66: <name> or '...' expected near 'in'
stdout
Standard output is empty