Created
September 18, 2010 03:17
-
-
Save tung/585296 to your computer and use it in GitHub Desktop.
This file contains 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
# 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