Skip to content

Commit

Permalink
clean up terrain and animal api for doing path finding
Browse files Browse the repository at this point in the history
  • Loading branch information
towski committed Dec 26, 2009
1 parent 9cdb927 commit bb91030
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
*.swp
7 changes: 7 additions & 0 deletions lib/animal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ class Animal
key :y, Integer
key :species, String

def find_path_to(other)
path = Path.new(:start => self, :goal => other)
path.calculate
path.save
path
end

def step
case rand(4)
when 0: self.x = x + 1;
Expand Down
2 changes: 1 addition & 1 deletion lib/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Path
many :nodes

def build_nodes
@terrain = Terrain.all("$where" => "(this.x >= #{start.x} || this.x <= #{goal.x}) && (this.y >= #{start.y} || this.y <= #{goal.y})")
@terrain = Terrain.within(start, goal)
@node_grid = []
@width = goal.x - start.x + 1
@height = goal.y - start.y + 1
Expand Down
4 changes: 2 additions & 2 deletions lib/terrain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def no_overlapping_terrain
end
end

def self.within(x_start, y_start, x_end, y_end)
all("$where" => "(this.x >= #{x_start} || this.x <= #{x_end}) && (this.y >= #{y_start} || this.y <= #{y_end})")
def self.within(start, finish)
all("$where" => "(this.x >= #{start.x} || this.x <= #{finish.x}) && (this.y >= #{start.y} || this.y <= #{finish.y})")
end

key :x, Integer
Expand Down

0 comments on commit bb91030

Please sign in to comment.