Skip to content

Commit

Permalink
Add turn_radius to Hunt.
Browse files Browse the repository at this point in the history
  • Loading branch information
stderr committed Jan 15, 2012
1 parent 1e148f6 commit 1e0e20a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 37 deletions.
28 changes: 0 additions & 28 deletions lib/warp_defender/behaviors/homing.rb

This file was deleted.

25 changes: 20 additions & 5 deletions lib/warp_defender/behaviors/hunt.rb
Expand Up @@ -3,8 +3,10 @@ module Behaviors
class Hunt < Behavior
attr_reader :velocity

def initialize(entity)
def initialize(entity, options = {})
super(entity)

@turn_radius = options[:turn_radius] || 1.0
# distance per second to move
@velocity = Gosu::random(80, 200)
end
Expand All @@ -17,10 +19,23 @@ def position
end

def angle
distance_x = (@entity.target.x - @entity.x)
distance_y = (@entity.target.y - @entity.y)

Math.atan2(distance_y, distance_x).radians_to_gosu
distance_x = @entity.target.x - @entity.x
distance_y = @entity.target.y - @entity.y

optimum_angle = Math.atan2(distance_y, distance_x).radians_to_gosu

diff = Gosu::angle_diff(@entity.angle, optimum_angle)

if diff > 1.0
turn_radius = diff < @turn_radius ? diff : @turn_radius
@entity.angle + turn_radius

elsif diff < -1.0
turn_radius = diff.abs < @turn_radius ? diff.abs : @turn_radius
@entity.angle - turn_radius
else
@entity.angle
end
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/warp_defender/entities/grunt.rb
Expand Up @@ -8,7 +8,7 @@ def initialize(target)
:sprite => "grunt",
:physics => :dynamic)

@behavior = Behaviors::Hunt.new(self)
@behavior = Behaviors::Hunt.new(self, :turn_radius => 10.0)
@target = target

@render.state = "idle"
Expand Down
4 changes: 2 additions & 2 deletions lib/warp_defender/entities/projectiles/projectile.rb
Expand Up @@ -3,10 +3,10 @@ module Entities
module Projectiles

class Projectile < Entity
def initialize(options)
def initialize(options = {})
super(options)

@behavior = Behaviors::Hunt.new(self)
@behavior = Behaviors::Hunt.new(self, :turn_radius => 2.0)
@render.state = "idle"
end

Expand Down
2 changes: 1 addition & 1 deletion lib/warp_defender/entities/projectiles/rocket.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(angle, x, y, vel_x, vel_y)
:physics => :dynamic,
:friction => 0)
@render.state = "idle"
@behavior = Behaviors::Homing.new(self)
@behavior = Behaviors::Hunt.new(self, :turn_radius => 3.0)
@target = nil
@detection_range = 500
end
Expand Down

0 comments on commit 1e0e20a

Please sign in to comment.