Class: Abstracta::World

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/abstracta/world.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#ageObject (readonly)

Returns the value of attribute age.



4
5
6
# File 'lib/abstracta/world.rb', line 4

def age
  @age
end

#developerObject (readonly)

Returns the value of attribute developer.



4
5
6
# File 'lib/abstracta/world.rb', line 4

def developer
  @developer
end

#gridObject (readonly)

Returns the value of attribute grid.



4
5
6
# File 'lib/abstracta/world.rb', line 4

def grid
  @grid
end

#territoriesObject (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_occupiedObject



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

#occupiedObject

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

Returns:

  • (Boolean)


61
62
63
# File 'lib/abstracta/world.rb', line 61

def occupied?(xy)
  @occupied.include?(xy)
end

#territory_classObject



29
# File 'lib/abstracta/world.rb', line 29

def territory_class; Territory end

#update_mapObject



25
26
27
# File 'lib/abstracta/world.rb', line 25

def update_map
  @occupied = compute_occupied
end