WireWorld.lua 944 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. local WireWorld = {}
  2. function WireWorld.new()
  3. local self = setmetatable({}, {__index = WireWorld})
  4. self.world = {}
  5. return self
  6. end
  7. function WireWorld.getCollisions(world1, world2)
  8. local collisions = {}
  9. for xPos, yTable in pairs(world1.world) do
  10. for yPos, world1Steps in pairs(yTable) do
  11. local pos = Vector2.new(xPos,yPos)
  12. local world2Steps = world2:getTraversed(pos)
  13. local isColliding = world1Steps and world2Steps
  14. if isColliding then
  15. local contactInfo = {
  16. pos = pos,
  17. combinedSteps = world1Steps + world2Steps
  18. }
  19. table.insert(collisions, contactInfo)
  20. end
  21. end
  22. end
  23. return collisions
  24. end
  25. function WireWorld:getTraversed(p)
  26. local x, y = p.X, p.Y
  27. if not self.world[x] then return nil end
  28. return self.world[x][y]
  29. end
  30. function WireWorld:setTraversed(p, value)
  31. local x, y = p.X, p.Y
  32. if not self.world[x] then self.world[x] = {} end
  33. self.world[x][y] = value
  34. end
  35. return WireWorld