Class: Abstracta::World
- Inherits:
-
Object
- Object
- Abstracta::World
- Extended by:
- Forwardable
- Defined in:
- lib/abstracta/world.rb
Instance Attribute Summary collapse
-
#age ⇒ Object
readonly
Returns the value of attribute age.
-
#developer ⇒ Object
readonly
Returns the value of attribute developer.
-
#grid ⇒ Object
readonly
Returns the value of attribute grid.
-
#territories ⇒ Object
readonly
Returns the value of attribute territories.
Instance Method Summary collapse
- #age! ⇒ Object
- #available_adjacent(territory) ⇒ Object
- #compute_occupied ⇒ Object
- #compute_projected_targets(territory, n = territory.growth) ⇒ Object
- #create_territories(n = 1) ⇒ Object
-
#initialize(geometry = [100,100], opts = {}) ⇒ World
constructor
def_delegators :compass, :distance_from.
-
#occupied ⇒ Object
def update_territories @territories.each do |territory| update_map targets = compute_projected_targets(territory) territory.step(targets) end end.
- #occupied?(xy) ⇒ Boolean
- #territory_class ⇒ Object
- #update_map ⇒ Object
Constructor Details
#initialize(geometry = [100,100], opts = {}) ⇒ World
def_delegators :compass, :distance_from
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/abstracta/world.rb', line 10 def initialize(geometry=[100,100], opts={}) @grid = Grid.new(geometry) #@compass = Compass.default @density = opts.delete(:density) { 0.05 } @territory_count = opts.delete(:territory_count) { width * height * @density } @territories = [] @territories = create_territories(@territory_count) update_map @age = 0 @developer = WorldDeveloper.new(self) end |
Instance Attribute Details
#age ⇒ Object (readonly)
Returns the value of attribute age.
4 5 6 |
# File 'lib/abstracta/world.rb', line 4 def age @age end |
#developer ⇒ Object (readonly)
Returns the value of attribute developer.
4 5 6 |
# File 'lib/abstracta/world.rb', line 4 def developer @developer end |
#grid ⇒ Object (readonly)
Returns the value of attribute grid.
4 5 6 |
# File 'lib/abstracta/world.rb', line 4 def grid @grid end |
#territories ⇒ Object (readonly)
Returns the value of attribute territories.
4 5 6 |
# File 'lib/abstracta/world.rb', line 4 def territories @territories end |
Instance Method Details
#age! ⇒ Object
35 36 37 |
# File 'lib/abstracta/world.rb', line 35 def age! @age = @age + 1 end |
#available_adjacent(territory) ⇒ Object
69 70 71 |
# File 'lib/abstracta/world.rb', line 69 def available_adjacent(territory) @grid.clip territory.adjacent.reject(&method(:occupied?)) # { |xy| occupied.include?(xy) } # & available # - occupied end |
#compute_occupied ⇒ Object
57 58 59 |
# File 'lib/abstracta/world.rb', line 57 def compute_occupied territories.map(&:occupants).flatten.map(&:location) end |
#compute_projected_targets(territory, n = territory.growth) ⇒ Object
65 66 67 |
# File 'lib/abstracta/world.rb', line 65 def compute_projected_targets(territory, n=territory.growth) available_adjacent(territory).take(n) end |
#create_territories(n = 1) ⇒ Object
30 31 32 33 |
# File 'lib/abstracta/world.rb', line 30 def create_territories(n=1) seeds = @grid.sample(n) Array.new(n) { territory_class.new([seeds.pop]) } end |
#occupied ⇒ Object
def update_territories @territories.each do |territory| update_map targets = compute_projected_targets(territory) territory.step(targets) end end
53 54 55 |
# File 'lib/abstracta/world.rb', line 53 def occupied @occupied ||= compute_occupied end |
#occupied?(xy) ⇒ Boolean
61 62 63 |
# File 'lib/abstracta/world.rb', line 61 def occupied?(xy) @occupied.include?(xy) end |
#territory_class ⇒ Object
29 |
# File 'lib/abstracta/world.rb', line 29 def territory_class; Territory end |
#update_map ⇒ Object
25 26 27 |
# File 'lib/abstracta/world.rb', line 25 def update_map @occupied = compute_occupied end |