Class: TwoDGridMap
- Inherits:
-
Object
- Object
- TwoDGridMap
- Defined in:
- lib/two_d_grid_map.rb
Overview
TwoDGridMap exibits the contract that the map requires. Works on an X,Y grid that uses Ftors for 2D vectors
Constant Summary collapse
- TRAVEL_COST_DIAG =
14
- TRAVEL_COST_STRAIGHT =
10
Instance Attribute Summary collapse
-
#h ⇒ Object
Returns the value of attribute h.
-
#w ⇒ Object
Returns the value of attribute w.
Instance Method Summary collapse
-
#blocked?(location, type = nil) ⇒ Boolean
is the location available for the specified type.
- #clear(location) ⇒ Object
-
#cost(from, to) ⇒ Object
return the cost to go from => to (assuming from and to are neighbors).
- #distance(from, to) ⇒ Object
-
#initialize(w, h) ⇒ TwoDGridMap
constructor
A new instance of TwoDGridMap.
-
#neighbors(location) ⇒ Object
returns a list of neighbors and their distance.
- #occupant(location) ⇒ Object
-
#place(location, thing = nil) ⇒ Object
place thing at location.
- #size ⇒ Object
Constructor Details
#initialize(w, h) ⇒ TwoDGridMap
Returns a new instance of TwoDGridMap.
10 11 12 13 14 |
# File 'lib/two_d_grid_map.rb', line 10 def initialize(w,h) @w = w @h = h @grid = {} end |
Instance Attribute Details
#h ⇒ Object
Returns the value of attribute h.
6 7 8 |
# File 'lib/two_d_grid_map.rb', line 6 def h @h end |
#w ⇒ Object
Returns the value of attribute w.
6 7 8 |
# File 'lib/two_d_grid_map.rb', line 6 def w @w end |
Instance Method Details
#blocked?(location, type = nil) ⇒ Boolean
is the location available for the specified type
37 38 39 40 41 42 43 44 45 |
# File 'lib/two_d_grid_map.rb', line 37 def blocked?(location, type=nil) return true if type == :blocked return true if location.x >= @w || location.y >= @h || location.x < 0 || location.y < 0 if @grid[location.x] and @grid[location.x][location.y] return true else return false end end |
#clear(location) ⇒ Object
31 32 33 34 |
# File 'lib/two_d_grid_map.rb', line 31 def clear(location) @grid[location.x] ||= {} @grid[location.x][location.y] = nil end |
#cost(from, to) ⇒ Object
return the cost to go from => to (assuming from and to are neighbors)
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/two_d_grid_map.rb', line 70 def cost(from, to) if from.x == to.x or from.y == to.y if from.x == to.x and from.y == to.y 0 else TRAVEL_COST_STRAIGHT end else TRAVEL_COST_DIAG end end |
#distance(from, to) ⇒ Object
63 64 65 66 67 |
# File 'lib/two_d_grid_map.rb', line 63 def distance(from,to) h_diagonal = [(from.x-to.x).abs, (from.y-to.y).abs].min h_straight = ((from.x-to.x).abs + (from.y-to.y).abs) return TRAVEL_COST_DIAG * h_diagonal + TRAVEL_COST_STRAIGHT * (h_straight - 2*h_diagonal) end |
#neighbors(location) ⇒ Object
returns a list of neighbors and their distance
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/two_d_grid_map.rb', line 48 def neighbors(location) x = location.x y = location.y [ TwoDGridLocation.new(x-1, y-1), TwoDGridLocation.new(x-1, y+1), TwoDGridLocation.new(x+1, y-1), TwoDGridLocation.new(x+1, y+1), TwoDGridLocation.new(x-1, y), TwoDGridLocation.new(x+1, y), TwoDGridLocation.new(x, y-1), TwoDGridLocation.new(x, y+1) ] end |
#occupant(location) ⇒ Object
27 28 29 |
# File 'lib/two_d_grid_map.rb', line 27 def occupant(location) @grid[location.x][location.y] if @grid[location.x] end |
#place(location, thing = nil) ⇒ Object
place thing at location. If thing is nil, location will be placed in the map
21 22 23 24 25 |
# File 'lib/two_d_grid_map.rb', line 21 def place(location, thing=nil) thing ||= location @grid[location.x] ||= {} @grid[location.x][location.y] = thing end |
#size ⇒ Object
16 17 18 |
# File 'lib/two_d_grid_map.rb', line 16 def size [@w,@h] end |