Class: Zyps::GameObject

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

Overview

An object in the virtual environment.

Direct Known Subclasses

Creature

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GameObject

Takes a hash with these keys and defaults: :name => nil, :location => Location.new, :color => Color.new, :vector => Vector.new, :age => 0, :size => 1, :tags => []



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/zyps.rb', line 145

def initialize (options = {})
	options = {
		:name => nil,
		:location => Location.new,
		:color => Color.new,
		:vector => Vector.new,
		:age => 0,
		:size => 1,
		:tags => []
	}.merge(options)
	self.name, self.location, self.color, self.vector, self.age, self.size, self.tags = options[:name], options[:location], options[:color], options[:vector], options[:age], options[:size], options[:tags]
	@identifier = generate_identifier
end

Instance Attribute Details

#colorObject

A Color that will be used to draw the object.



127
128
129
# File 'lib/zyps.rb', line 127

def color
  @color
end

#identifierObject

A universal identifier for the object. Needed for DRb transmission, etc.



123
124
125
# File 'lib/zyps.rb', line 123

def identifier
  @identifier
end

#locationObject

The object’s Location in space.



125
126
127
# File 'lib/zyps.rb', line 125

def location
  @location
end

#nameObject

A String with the object’s name.



133
134
135
# File 'lib/zyps.rb', line 133

def name
  @name
end

#sizeObject

Radius of the object.



129
130
131
# File 'lib/zyps.rb', line 129

def size
  @size
end

#tagsObject

An array of Strings with tags that determine how the object will be treated by Creature and EnvironmentalFactor objects in its environment.



135
136
137
# File 'lib/zyps.rb', line 135

def tags
  @tags
end

#vectorObject

A Vector with the object’s current speed and direction of travel.



131
132
133
# File 'lib/zyps.rb', line 131

def vector
  @vector
end

Instance Method Details

#ageObject

Time since the object was created, in seconds.



181
# File 'lib/zyps.rb', line 181

def age; Time.new.to_f - @birth_time; end

#age=(age) ⇒ Object



182
# File 'lib/zyps.rb', line 182

def age=(age); @birth_time = Time.new.to_f - age; end

#copyObject

Make a deep copy.



160
161
162
163
164
165
166
167
168
169
# File 'lib/zyps.rb', line 160

def copy
	copy = self.clone
	copy.vector = @vector.copy
	copy.color = @color.copy
	copy.location = @location.copy
	copy.tags = @tags.clone
	copy.identifier = generate_identifier
	copy.name = @name ? "Copy of " + @name : nil
	copy
end

#move(elapsed_time) ⇒ Object

Move according to vector over the given number of seconds.



175
176
177
178
# File 'lib/zyps.rb', line 175

def move (elapsed_time)
	@location.x += @vector.x * elapsed_time
	@location.y += @vector.y * elapsed_time
end