util.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. function distanceBetweenPoints(p1, p2)
  2. -- Very simple util function to grab the distance between two points.
  3. -- Unit is arbitrary; this is plain old pythagorean/Scratch math.
  4. -- Takes two tables with x/y values, which are assumed to be of the
  5. -- same unit.
  6. return math.sqrt(math.pow(p1.x - p2.x, 2) + math.pow(p1.y - p2.y, 2))
  7. end
  8. function sign(n)
  9. -- Returns the sign of the number - -1, 0, or +1.
  10. if n == 0 then
  11. return 0
  12. else
  13. return n / math.abs(n)
  14. end
  15. end
  16. function goToWithoutTouchingWall(thing, world, x, y)
  17. -- Sticks a thing at a position if there's no wall there and tells
  18. -- whether or not the thing ended up moving.
  19. if world.grid[y][x].solid then
  20. return false
  21. else
  22. thing.x = x
  23. thing.y = y
  24. return true
  25. end
  26. end
  27. function goToWithoutTouchingAnything(thing, world, x, y)
  28. -- Sticks a thing at a position if there's no wall or bullet or goblin there and tells
  29. -- whether or not the thing ended up moving.
  30. if world.grid[y][x].solid then
  31. return false
  32. else
  33. for k, b in pairs(world.bullets) do
  34. if b.x == x and b.y == y then
  35. return false
  36. end
  37. end
  38. thing.x = x
  39. thing.y = y
  40. return true
  41. end
  42. end
  43. function cloneVec(v)
  44. return {x = v.x, y = v.y}
  45. end