Created
June 9, 2017 15:57
-
-
Save YeOldeDM/4114457a064e8ddeaee8ea1c2dda795a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cast a fov line, stopping at first blocking cell | |
func cast_fov_ray(data,wall_index,from,to): | |
var cells = [] | |
var line = get_line(from,to) | |
for cell in line: | |
# Check for blocking cell | |
if not is_wall(data, wall_index, cell): | |
cells.append(cell) | |
else: | |
# include the blocking cell in the list | |
cells.append(cell) | |
return cells | |
return cells | |
# Returns an array of datamap cells that lie | |
# under the from map cell to map cell | |
func get_line(from,to): | |
# setup | |
var x1 = from.x | |
var y1 = from.y | |
var x2 = to.x | |
var y2 = to.y | |
var dx = x2 - x1 | |
var dy = y2 - y1 | |
# determine steepness of line | |
var is_steep = abs(dy) > abs(dx) | |
# rotate line if steep | |
if is_steep: | |
# swap x1/y1 | |
var ox = x1 | |
x1 = y1 | |
y1 = ox | |
# swap x2/y2 | |
ox = x2 | |
x2 = y2 | |
y2 = ox | |
# swap start points if needed | |
var swapped = false | |
if x1 > x2: | |
# swap x1/x2 | |
var ox = x1 | |
x1 = x2 | |
x2 = ox | |
# swap y1/y2 | |
var oy = y1 | |
y1 = y2 | |
y2 = oy | |
swapped = true | |
# recalculate differentials | |
dx = x2-x1 | |
dy = y2-y1 | |
# calculate error | |
var error = int(dx / 2.0) | |
var ystep = 1 if y1 < y2 else -1 | |
# iterate over bounding box generating points between | |
var y = y1 | |
var points = [] | |
for x in range(x1, x2+1): | |
var coord = Vector2(y,x) if is_steep else Vector2(x,y) | |
points.append(coord) | |
error -= abs(dy) | |
if error < 0: | |
y += ystep | |
error += dx | |
# reverse list if coordinates were swapped | |
if swapped: | |
points.invert() | |
return points |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment