Class: WeewarAI::Faction
Overview
Instances of the Faction class correspond to factions in a Game. They provide some utility methods about factions, such as whether or not a faction is currently playing in a Game, or whether it can afford to buy a unit type.
Instance Attribute Summary collapse
-
#credits ⇒ Object
readonly
Returns the value of attribute credits.
-
#player_id ⇒ Object
(also: #playerId)
readonly
Returns the value of attribute player_id.
-
#player_name ⇒ Object
(also: #playerName)
readonly
Returns the value of attribute player_name.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
-
#can_afford?(type) ⇒ Boolean
True iff the faction has enough credits to purchase a unit of the given type.
-
#current? ⇒ Boolean
Whether or not this Faction is the one whose turn it is in the Game.
-
#initialize(game, xml, ordinal) ⇒ Faction
constructor
You should not need to instantiate any Faction s on your own; they are created for you by Game instances.
- #playing? ⇒ Boolean
- #to_s ⇒ Object
-
#units ⇒ Object
An Array of the Unit s in the Game belonging to this Faction.
Constructor Details
#initialize(game, xml, ordinal) ⇒ Faction
You should not need to instantiate any Faction s on your own; they are created for you by Game instances.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/weewar-ai/faction.rb', line 12 def initialize( game, xml, ordinal ) @game, @ordinal = game, ordinal @credits = xml[ 'credits' ].to_i @current = ( xml[ 'current' ] == 'true' ) @player_id = xml[ 'playerId' ].to_i @player_name = xml[ 'playerName' ] @state = xml[ 'state' ] rescue Exception => e $stderr.puts "Input XML: " + xml.inspect raise e end |
Instance Attribute Details
#credits ⇒ Object (readonly)
Returns the value of attribute credits.
8 9 10 |
# File 'lib/weewar-ai/faction.rb', line 8 def credits @credits end |
#player_id ⇒ Object (readonly) Also known as: playerId
Returns the value of attribute player_id.
8 9 10 |
# File 'lib/weewar-ai/faction.rb', line 8 def player_id @player_id end |
#player_name ⇒ Object (readonly) Also known as: playerName
Returns the value of attribute player_name.
8 9 10 |
# File 'lib/weewar-ai/faction.rb', line 8 def player_name @player_name end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
8 9 10 |
# File 'lib/weewar-ai/faction.rb', line 8 def state @state end |
Instance Method Details
#can_afford?(type) ⇒ Boolean
True iff the faction has enough credits to purchase a unit of the given type.
i = me = my = game.my_faction
if i.can_afford? :hart
my_base.build :hart
end
42 43 44 |
# File 'lib/weewar-ai/faction.rb', line 42 def can_afford?( type ) @credits >= WeewarAI::Unit::UNIT_COSTS[ type ] end |
#current? ⇒ Boolean
Whether or not this Faction is the one whose turn it is in the Game.
i = me = my = game.my_faction
is_my_turn = i.current?
29 30 31 |
# File 'lib/weewar-ai/faction.rb', line 29 def current? @current end |
#playing? ⇒ Boolean
33 34 35 |
# File 'lib/weewar-ai/faction.rb', line 33 def @state == 'playing' end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/weewar-ai/faction.rb', line 53 def to_s @player_name end |
#units ⇒ Object
An Array of the Unit s in the Game belonging to this Faction.
i = me = my = game.my_faction
my_units = my.units
49 50 51 |
# File 'lib/weewar-ai/faction.rb', line 49 def units @game.units.find_all { |u| u.faction == self } end |