Class: Zyps::Creature

Inherits:
GameObject show all
Defined in:
lib/zyps.rb

Overview

A Creature is a GameObject that can sense and respond to other GameObjects (including other Creature objects).

Instance Attribute Summary collapse

Attributes inherited from GameObject

#color, #identifier, #location, #name, #size, #tags, #vector

Instance Method Summary collapse

Methods inherited from GameObject

#age, #age=, #move

Constructor Details

#initialize(options = {}) ⇒ Creature

Identical to the GameObject constructor, except that it also takes a list of Behavior objects. Takes a hash with these keys and defaults: :name => nil :location => Location.new :color => Color.new :vector => Vector.new :age => 0 :size => 1 :tags => [] :behaviors => []



217
218
219
220
221
222
223
# File 'lib/zyps.rb', line 217

def initialize (options = {})
	options = {
		:behaviors => []
	}.merge(options)
	super
	self.behaviors = options[:behaviors]
end

Instance Attribute Details

#behaviorsObject

A list of Behavior objects that determine the creature’s response to its environment.



205
206
207
# File 'lib/zyps.rb', line 205

def behaviors
  @behaviors
end

Instance Method Details

#act(targets) ⇒ Object

Performs all assigned behaviors on the targets.



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

def act(targets)
	behaviors.each {|behavior| behavior.perform(self, targets)}
end

#copyObject

Make a deep copy.



226
227
228
229
230
231
232
# File 'lib/zyps.rb', line 226

def copy
	copy = super
	#Make deep copy of each behavior.
	copy.behaviors = []
	@behaviors.each {|behavior| copy.behaviors << behavior.copy}
	copy
end