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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Behavior

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



311
312
313
314
315
316
317
318
319
# File 'lib/zyps.rb', line 311

def initialize (options = {})
	options = {
		:actions => [],
		:conditions => []
	}.merge(options)
	self.actions, self.conditions = options[:actions], options[:conditions]
	#Tracks current target.
	@active_target = nil
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



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

def actions
  @actions
end

#conditionsObject

Returns the value of attribute conditions.



305
306
307
# File 'lib/zyps.rb', line 305

def conditions
  @conditions
end

Instance Method Details

#copyObject

Make a deep copy.



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

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.



337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/zyps.rb', line 337

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