Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 1 commit
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jan 15, 2012
@stderr Add turn_radius to Hunt. 1e0e20a
View
28 lib/warp_defender/behaviors/homing.rb
@@ -1,28 +0,0 @@
-module Behaviors
- class Homing < Behavior
- attr_reader :velocity
-
- def initialize(entity)
- super(entity)
- end
-
- def angle
- 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
- #optimum_angle += optimum_angle if optimum_angle < 0
-
- diff = Gosu::angle_diff(@entity.angle, optimum_angle)
-
- if diff > 1.0
- @entity.angle + 3.0
- elsif diff < -1.0
- @entity.angle - 3.0
- else
- @entity.angle
- end
-
- end
- end
-end
View
25 lib/warp_defender/behaviors/hunt.rb
@@ -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
@@ -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
View
2 lib/warp_defender/entities/grunt.rb
@@ -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"
View
4 lib/warp_defender/entities/projectiles/projectile.rb
@@ -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
View
2 lib/warp_defender/entities/projectiles/rocket.rb
@@ -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

No commit comments for this range

Something went wrong with that request. Please try again.