Class: Zyps::Friction
- Inherits:
-
EnvironmentalFactor
- Object
- EnvironmentalFactor
- Zyps::Friction
- Defined in:
- lib/zyps/environmental_factors.rb
Overview
A force that slows all objects.
Instance Attribute Summary collapse
-
#force ⇒ Object
Rate of slowing.
Instance Method Summary collapse
-
#act(environment) ⇒ Object
Reduce the target’s speed at the given rate.
-
#initialize(force) ⇒ Friction
constructor
A new instance of Friction.
Constructor Details
#initialize(force) ⇒ Friction
Returns a new instance of Friction.
186 187 188 189 190 |
# File 'lib/zyps/environmental_factors.rb', line 186 def initialize(force) self.force = force #Track time since last action. @clock = Clock.new end |
Instance Attribute Details
#force ⇒ Object
Rate of slowing.
184 185 186 |
# File 'lib/zyps/environmental_factors.rb', line 184 def force @force end |
Instance Method Details
#act(environment) ⇒ Object
Reduce the target’s speed at the given rate.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/zyps/environmental_factors.rb', line 193 def act(environment) elapsed_time = @clock.elapsed_time environment.objects.each do |object| #Slow object. acceleration = @force * elapsed_time speed = object.vector.speed if speed > 0 speed -= acceleration speed = 0 if speed < 0 elsif speed < 0 speed += acceleration speed = 0 if speed > 0 end object.vector.speed = speed end end |