lib/lotu/systems/steering_system.rb in lotu-0.1.11 vs lib/lotu/systems/steering_system.rb in lotu-0.1.12
- old
+ new
@@ -133,10 +133,27 @@
predicted_position = @user.pursuer.pos + @user.pursuer.vel * look_ahead_time
@user.target = @user.pursuer.pos
return flee
end
+ def evade_multiple
+ return @zero if @user.pursuers.empty?
+ combined_velocities = Vector2d.new
+ combined_positions = Vector2d.new
+ @user.pursuers.each do |p|
+ combined_velocities += p.vel
+ combined_positions += p.pos
+ end
+ combined_velocities /= @user.pursuers.length
+ combined_positions /= @user.pursuers.length
+ to_pursuers = combined_positions - @user.pos
+ look_ahead_time = to_pursuers.length / (@user.max_speed + combined_velocities.length)
+ predicted_position = combined_positions + combined_velocities * look_ahead_time
+ @user.target = combined_positions
+ return flee
+ end
+
# TODO: Fix wander
def wander
wander_jitter = 10
@user.wander_target += Vector2d.new(Gosu.random(-1,1), Gosu.random(-1,1))
@@ -164,21 +181,30 @@
# Create accessors for the user
class << self
attr_accessor :mass, :pos, :heading, :vel, :accel,
:max_speed, :max_turn_rate, :max_force,
:wander_radius, :wander_distance, :wander_target,
- :target, :evader, :pursuer
+ :target, :evader, :pursuer, :pursuers
end
+ # TODO: move these inside the SteeringSystem?
+ # and just delegate with accessors?
# Some defaults
@pos = Vector2d.new(@x, @y)
offset_x = Gosu.offset_x(@angle, 1)
offset_y = Gosu.offset_y(@angle, 1)
@heading = Vector2d.new(offset_x, offset_y)
@vel = Vector2d.new
@accel = Vector2d.new
@wander_target = Vector2d.new
+ @pursuers = []
+
+ @colors = {
+ :position => 0xff666666,
+ :heading => 0xffff0000,
+ :target => rand_color
+ }
end
def activate(behavior)
@systems[SteeringSystem].activate(behavior)
end
@@ -189,18 +215,14 @@
def facing_target?
@heading.facing_to?(@target - @pos)
end
- def draw
- super
- draw_debug if $lotu.debug?
- end
-
def draw_debug
- $lotu.draw_line(0, 0, 0xff999999, @pos.x, @pos.y, 0xff333333)
- $lotu.draw_line(@pos.x, @pos.y, 0xffffffff, (@pos + @heading*50).x, (@pos+@heading*50).y, 0xffff0000)
- $lotu.draw_line(@pos.x, @pos.y, 0xffffffff, @target.x, @target.y, 0xff00ff00) if @target
+ super
+ $lotu.draw_line(0, 0, @colors[:position], @pos.x, @pos.y, @colors[:position])
+ $lotu.draw_line(@pos.x, @pos.y, @colors[:heading], (@pos + @heading*50).x, (@pos+@heading*50).y, @colors[:heading])
+ $lotu.draw_line(@pos.x, @pos.y, @colors[:target], @target.x, @target.y, @colors[:target]) if @target
end
# to_s utility methods
def to_s
["@angle(#{format('%.2f', @angle)}°)",