bresenham.lua implements Line Of Sight (LOS) and line drawing in a generic way. Here is its github repo:
https://github.com/kikito/bresenham.lua
Basically, if you have a map like this:
Code: Select all
########
#A #
# #
# ####
# #
# G #
# #
########
Code: Select all
########
#A #
# \ #
# \####
# \ #
# G #
# #
########
- The coordinates of the origin point (A)
- The coordinates of the "Goal" point (G)
- A function that takes two parameters (x,y) and returns true if the point at x,y is "passable" and false if it's "blocked"
Code: Select all
local bresenham = require 'bresenham'
...
local playerSeesEnemy = bresenham.los(player.x, player.y, enemy.x, enemy.y, function(x, y)
return map[x][y] == " "
end)
I'm sorry but I don't have time to make a pretty demo with graphics and everything. I hope this pitiful post gets the idea through. There is a bit more information on the library's README.