tree.lua 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. local Tree = {}
  2. Tree.name = 'Tree'
  3. Tree.code = 'tree'
  4. Tree.collision = {}
  5. Tree.collision.shape = 'circle'
  6. function Tree:activate(map)
  7. ctx.event:emit('collision.attach', {object = self})
  8. self.depth = -5
  9. if ctx.view then ctx.view:register(self) end
  10. end
  11. function Tree:update()
  12. if ctx.view then
  13. self.depth = -2000 + math.distance(ctx.view.x + ctx.view.width / 2, ctx.view.y + ctx.view.height / 2, self.x, self.y)
  14. end
  15. end
  16. function Tree:draw()
  17. local x, y = ctx.view:three(self.x, self.y, 100)
  18. local image = data.media.graphics.map.tree
  19. if ctx.id and ctx.players:get(ctx.id) and ctx.players:get(ctx.id).active then
  20. local p = ctx.players:get(ctx.id)
  21. local alpha = (math.clamp(math.distance(self.x, self.y, p.x, p.y), 60, 120) / 120) * 255
  22. love.graphics.setColor(255, 255, 255, alpha)
  23. else
  24. love.graphics.setColor(255, 255, 255)
  25. end
  26. love.graphics.draw(image, x, y, 0, self.radius / 20, self.radius / 20, image:getWidth() / 2, image:getHeight() / 2)
  27. end
  28. function Tree:__tostring()
  29. return [[{
  30. kind = 'tree',
  31. x = ]] .. self.x .. [[,
  32. y = ]] .. self.y .. [[,
  33. radius = ]] .. self.radius .. [[
  34. }]]
  35. end
  36. return Tree