Class: Zyps::Behavior
- Inherits:
-
Object
- Object
- Zyps::Behavior
- 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
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#conditions ⇒ Object
Returns the value of attribute conditions.
Instance Method Summary collapse
-
#copy ⇒ Object
Make a deep copy.
-
#initialize(options = {}) ⇒ Behavior
constructor
Takes a hash with these keys and defaults: :actions => [] :conditions => [].
-
#perform(actor, targets) ⇒ Object
Finds targets that meet all conditions, then acts on them.
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 ( = {}) = { :actions => [], :conditions => [] }.merge() self.actions, self.conditions = [:actions], [:conditions] #Tracks current target. @active_target = nil end |
Instance Attribute Details
#actions ⇒ Object
Returns the value of attribute actions.
306 307 308 |
# File 'lib/zyps.rb', line 306 def actions @actions end |
#conditions ⇒ Object
Returns the value of attribute conditions.
305 306 307 |
# File 'lib/zyps.rb', line 305 def conditions @conditions end |
Instance Method Details
#copy ⇒ Object
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 |