Skip to content

Instantly share code, notes, and snippets.

@tung
Created September 18, 2010 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tung/585296 to your computer and use it in GitHub Desktop.
Save tung/585296 to your computer and use it in GitHub Desktop.
# Sample usage of Ruby recursive shadowcasting implementation:
# http://roguebasin.roguelikedevelopment.org/index.php?title=Ruby_shadowcasting_implementation
require "ShadowcastingFieldOfView"
class Map
def initialize(map)
@map = map
@view = Array.new(map.length)
@view.length.times do |y|
@view[y] = Array.new(map[y].length)
end
end
def blocked?(x, y)
@map[y][x] == 1
end
def light(x, y)
@view[y][x] = true
end
def to_s
(0...@view.length).map{ |y| (0...@view[y].length).map{ |x| (x == 5 && y == 5) ? "@" : (@view[y][x] ? (@map[y][x] == 1 ? "#" : ".") : " ") }.join }.join("\n")
end
include ShadowcastingFieldOfView
end
m = Map.new([
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
])
m.do_fov(5, 5, 10)
puts(m.to_s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment