Class: Zyps::BreedAction
- Inherits:
-
Action
- Object
- Action
- Zyps::BreedAction
- Defined in:
- lib/zyps/actions.rb
Constant Summary collapse
- DEFAULT_DELAY =
60
Instance Attribute Summary collapse
-
#delay ⇒ Object
Delay between actions.
-
#environment ⇒ Object
Environment to place children into.
Instance Method Summary collapse
- #do(actor, targets) ⇒ Object
-
#initialize(environment, delay = DEFAULT_DELAY) ⇒ BreedAction
constructor
A new instance of BreedAction.
Constructor Details
#initialize(environment, delay = DEFAULT_DELAY) ⇒ BreedAction
Returns a new instance of BreedAction.
237 238 239 240 241 |
# File 'lib/zyps/actions.rb', line 237 def initialize(environment, delay = DEFAULT_DELAY) self.environment, self.delay = environment, delay @clock = Clock.new @time_since_last_action = 0 end |
Instance Attribute Details
#delay ⇒ Object
Delay between actions.
236 237 238 |
# File 'lib/zyps/actions.rb', line 236 def delay @delay end |
#environment ⇒ Object
Environment to place children into.
234 235 236 |
# File 'lib/zyps/actions.rb', line 234 def environment @environment end |
Instance Method Details
#do(actor, targets) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/zyps/actions.rb', line 242 def do(actor, targets) targets.each do |target| #Skip action if target is not a Creature. next unless target.is_a?(Creature) #Get time since last action, and skip if it hasn't been long enough. @time_since_last_action += @clock.elapsed_time return unless @time_since_last_action >= @delay #Create a child. child = Creature.new #Combine colors. child.color = actor.color + target.color #Combine behaviors EXCEPT those with BreedActions. behaviors = (actor.behaviors + target.behaviors).find_all do |behavior| ! behavior.actions.any?{|action| action.is_a?(BreedAction)} end behaviors.each {|behavior| child.behaviors << behavior.copy} #Location should equal actor's. child.location = actor.location.copy #Add parents' vectors to get child's vector. child.vector = actor.vector + target.vector #Child's size should be half the average size of the parents'. child.size = ((actor.size + target.size) / 2) / 2 #Add child to environment. @environment.objects << child #Reset elapsed time. @time_since_last_action = 0 end end |