Room.lua 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. -- ================
  2. -- Private helpers
  3. -- ================
  4. local setmetatable = setmetatable
  5. -- Internal class constructor
  6. local class = function(...)
  7. local klass = {}
  8. klass.__index = klass
  9. klass.__call = function(_,...) return klass:new(...) end
  10. function klass:new(...)
  11. local instance = setmetatable({}, klass)
  12. klass.__init(instance, ...)
  13. return instance
  14. end
  15. return setmetatable(klass,{__call = klass.__call})
  16. end
  17. local Room = class()
  18. Room.__tostring = function(r) return ('Room x=%f y=%f w=%d h=%d'):format(r.x, r.y, r.width, r.height) end
  19. function Room:__init(x, y, width, height)
  20. self.x, self.y, self.width, self.height = x, y, width, height
  21. self.vx, self.vy = 0,0
  22. end
  23. function Room:intersects(r)
  24. local a = {
  25. x=self.x+self.width/2,
  26. y=self.y+self.height/2,
  27. wh = self.width/2,
  28. hh = self.height/2
  29. }
  30. local b = {
  31. x=r.x+r.width/2,
  32. y=r.y+r.height/2,
  33. wh = r.width/2+1,
  34. hh = r.height/2+1
  35. }
  36. return not (math.abs(a.x - b.x) > (a.wh + b.wh) or math.abs(a.y - b.y) > (a.hh + b.hh))
  37. end
  38. return Room