Class: LineOfSite
- Inherits:
-
Object
- Object
- LineOfSite
- Defined in:
- lib/line_of_sight.rb
Overview
LineOfSite is a class for finding neighbors in a map that are visible
Instance Method Summary collapse
-
#brensenham_line(x, y, x2, y2) ⇒ Object
Brensenham line algorithm.
-
#initialize(map) ⇒ LineOfSite
constructor
A new instance of LineOfSite.
- #losline(x, y, x2, y2) ⇒ Object
Constructor Details
#initialize(map) ⇒ LineOfSite
Returns a new instance of LineOfSite.
3 4 5 |
# File 'lib/line_of_sight.rb', line 3 def initialize(map) @map = map end |
Instance Method Details
#brensenham_line(x, y, x2, y2) ⇒ Object
Brensenham line algorithm
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/line_of_sight.rb', line 20 def brensenham_line(x,y,x2,y2) steep = false coords = [] dx = (x2 - x).abs if (x2 - x) > 0 sx = 1 else sx = -1 end dy = (y2 - y).abs if (y2 - y) > 0 sy = 1 else sy = -1 end if dy > dx steep = true x,y = y,x dx,dy = dy,dx sx,sy = sy,sx end d = (2 * dy) - dx dx.times do if steep coords << [y,x] else coords << [x,y] end while d >= 0 y = y + sy d = d - (2 * dx) end x = x + sx d = d + (2 * dy) end coords << [x2,y2] coords end |
#losline(x, y, x2, y2) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/line_of_sight.rb', line 7 def losline(x, y, x2, y2) brensenham_line(x, y, x2, y2).each do |i| iterx, itery = *i occ = @map.occupant(loc2(iterx,itery)) return unless occ occ.lit = true occ.seen = true return if occ.solid? end end |