Class: Zyps::Environment
Overview
A virtual environment.
Instance Attribute Summary collapse
-
#environmental_factors ⇒ Object
An array of EnvironmentalFactor objects that act on any GameObject in the Environment.
-
#objects ⇒ Object
An array of GameObject objects that reside in the Environment.
Instance Method Summary collapse
-
#copy ⇒ Object
Make a deep copy.
-
#initialize(options = {}) ⇒ Environment
constructor
Takes a hash with these keys and defaults: :objects => [], :environmental_factors => [].
-
#interact ⇒ Object
Allow everything in the environment to interact with each other.
Constructor Details
#initialize(options = {}) ⇒ Environment
Takes a hash with these keys and defaults: :objects => [], :environmental_factors => []
36 37 38 39 40 41 42 43 |
# File 'lib/zyps.rb', line 36 def initialize ( = {}) = { :objects => [], :environmental_factors => [] }.merge() self.objects, self.environmental_factors = [:objects], [:environmental_factors] @clock = Clock.new end |
Instance Attribute Details
#environmental_factors ⇒ Object
An array of EnvironmentalFactor objects that act on any GameObject in the Environment.
31 32 33 |
# File 'lib/zyps.rb', line 31 def environmental_factors @environmental_factors end |
#objects ⇒ Object
An array of GameObject objects that reside in the Environment.
29 30 31 |
# File 'lib/zyps.rb', line 29 def objects @objects end |
Instance Method Details
#copy ⇒ Object
Make a deep copy.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/zyps.rb', line 46 def copy copy = self.clone #Currently, we overwrite everything anyway, but we may add some clonable attributes later. #Make a deep copy of all objects. copy.objects = [] @objects.each {|object| copy.objects << object.copy} #Make a deep copy of all environmental_factors. copy.environmental_factors = [] @environmental_factors.each {|environmental_factor| copy.environmental_factors << environmental_factor.copy} copy end |
#interact ⇒ Object
Allow everything in the environment to interact with each other. Objects are first moved according to their preexisting vectors and the amount of time since the last call. Then, each GameObject with an act() method is allowed to act on the environment. Finally, each EnvironmentalFactor is allowed to act on the Environment.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/zyps.rb', line 61 def interact #Get time since last interaction. elapsed_time = @clock.elapsed_time objects.each do |object| #Move each object according to its vector. begin object.move(elapsed_time) #Remove misbehaving objects. rescue Exception => exception puts exception, exception.backtrace objects.delete(object) next end #Have all creatures interact with the environment. if object.respond_to?(:act) begin #Have creature act on all GameObjects other than itself. object.act(objects.reject{|target| target.equal?(object)}) #Remove misbehaving objects. rescue Exception => exception puts exception, exception.backtrace objects.delete(object) next end end end #Have all environmental factors interact with environment. environmental_factors.each do |factor| begin factor.act(self) #Remove misbehaving environmental factors. rescue Exception => exception environmental_factors.delete(factor) puts exception, exception.backtrace next end end #Mark environment as changed. changed #Alert observers. notify_observers(self) end |