1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- function distanceBetweenPoints(p1, p2)
- -- Very simple util function to grab the distance between two points.
- -- Unit is arbitrary; this is plain old pythagorean/Scratch math.
- -- Takes two tables with x/y values, which are assumed to be of the
- -- same unit.
- return math.sqrt(math.pow(p1.x - p2.x, 2) + math.pow(p1.y - p2.y, 2))
- end
- function sign(n)
- -- Returns the sign of the number - -1, 0, or +1.
- if n == 0 then
- return 0
- else
- return n / math.abs(n)
- end
- end
- function goToWithoutTouchingWall(thing, world, x, y)
- -- Sticks a thing at a position if there's no wall there and tells
- -- whether or not the thing ended up moving.
- if world.grid[y][x].solid then
- return false
- else
- thing.x = x
- thing.y = y
- return true
- end
- end
- function goToWithoutTouchingAnything(thing, world, x, y)
- -- Sticks a thing at a position if there's no wall or bullet or goblin there and tells
- -- whether or not the thing ended up moving.
- if world.grid[y][x].solid then
- return false
- else
- for k, b in pairs(world.bullets) do
- if b.x == x and b.y == y then
- return false
- end
- end
- thing.x = x
- thing.y = y
- return true
- end
- end
- function cloneVec(v)
- return {x = v.x, y = v.y}
- end
|