Class: Zyps::Behavior

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

Overview

A behavior that a Creature engages in. The target can have its tags or colors changed, it can be “herded”, it can be destroyed, or any other action the library user can dream up. Likewise, the subject can change its own attributes, it can approach or flee from the target, it can spawn new Creatures or GameObjects (like bullets), or anything else.

Constant Summary collapse

@@condition_order =

Will be used to distribute condition processing time between all Behaviors with the same condition_frequency.

Hash.new {|h, k| h[k] = 0}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Behavior

Takes a hash with these keys and defaults: :actions => [] :conditions => [] :condition_frequency => 1



322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/zyps.rb', line 322

def initialize (options = {})
	options = {
		:actions => [],
		:conditions => [],
		:condition_frequency => 1
	}.merge(options)
	self.actions = options[:actions]
	self.conditions = options[:conditions]
	self.condition_frequency = options[:condition_frequency]
	#Tracks number of calls to perform() so conditions can be evaluated with appropriate frequency.
	@condition_evaluation_count = 0
	#Targets currently selected to act upon.
	@current_targets = []
end

Instance Attribute Details

#actionsObject

An array of Action subclasses. Action#start(actor, targets) and action.do(actor, targets) will be called on each when all conditions are true. Action#stop(actor, targets) will be called when any condition is false.



311
312
313
# File 'lib/zyps.rb', line 311

def actions
  @actions
end

#condition_frequencyObject

Number of updates before behavior is allowed to select a new group of targets to act on.



313
314
315
# File 'lib/zyps.rb', line 313

def condition_frequency
  @condition_frequency
end

#conditionsObject

An array of Condition subclasses. Condition#select(actor, targets) will be called on each.



307
308
309
# File 'lib/zyps.rb', line 307

def conditions
  @conditions
end

Instance Method Details

#copyObject

Make a deep copy.



346
347
348
349
350
351
352
353
354
355
# File 'lib/zyps.rb', line 346

def copy
	copy = self.clone #Currently, we overwrite everything anyway, but we may add some clonable attributes later.
	#Make a deep copy of all actions.
	copy.actions = []
	@actions.each {|action| copy.actions << action.copy}
	#Make a deep copy of all conditions.
	copy.conditions = []
	@conditions.each {|condition| copy.conditions << condition.copy}
	copy
end

#perform(actor, targets) ⇒ Object

Finds targets that meet all conditions, then acts on them. Calls select(actor, targets) on each Condition, each time discarding targets that fail. Then on each Action, calls Action#start(actor, targets) (if not already started) followed by Action#do(actor, targets). If no matching targets are found, calls Action#stop(actor, targets) on each Action.



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/zyps.rb', line 361

def perform(actor, targets)
	
	if condition_evaluation_turn?
		@current_targets = targets.clone
		conditions.each {|condition| @current_targets = condition.select(actor, @current_targets)}
	end
	actions.each do |action|
		if ! @current_targets.empty?
			action.start(actor, @current_targets) unless action.started?
			action.do(actor, @current_targets)
		else
			action.stop(actor, targets) #Not @current_targets; that array is empty.
		end
	end
	
	
end