Class: Zyps::BreedAction

Inherits:
Action
  • Object
show all
Defined in:
lib/zyps/actions.rb

Constant Summary collapse

DEFAULT_DELAY =
60

Instance Attribute Summary collapse

Attributes inherited from Action

#started

Instance Method Summary collapse

Methods inherited from Action

#copy, #start, #started?, #stop

Constructor Details

#initialize(environment, delay = DEFAULT_DELAY) ⇒ BreedAction

Returns a new instance of BreedAction.



233
234
235
236
237
# File 'lib/zyps/actions.rb', line 233

def initialize(environment, delay = DEFAULT_DELAY)
	self.environment, self.delay = environment, delay
	@clock = Clock.new
	@time_since_last_action = 0
end

Instance Attribute Details

#delayObject

Delay between actions.



232
233
234
# File 'lib/zyps/actions.rb', line 232

def delay
  @delay
end

#environmentObject

Environment to place children into.



230
231
232
# File 'lib/zyps/actions.rb', line 230

def environment
  @environment
end

Instance Method Details

#do(actor, targets) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/zyps/actions.rb', line 238

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