Skip to content
Browse files

+ target acquisition, + nukes

  • Loading branch information...
1 parent 568f849 commit d851151a76961d8a636f381908083711a481ded0 @floere committed May 10, 2009
Showing with 86 additions and 25 deletions.
  1. +17 −13 lib/game_window.rb
  2. +41 −4 lib/nuke.rb
  3. +9 −8 lib/player.rb
  4. +17 −0 lib/target_acquisition.rb
  5. BIN media/nuke.png
  6. +2 −0 multiroids.rb
View
30 lib/game_window.rb
@@ -36,6 +36,10 @@ def setup_objects
add_player2
register Earth.new(self)
+
+ nuke = Nuke.new self
+ nuke.warp CP::Vec2.new(SCREEN_WIDTH - 50, SCREEN_HEIGHT - 50)
+ register nuke
end
def setup_collisions
@@ -105,19 +109,19 @@ def add_player1
#
def add_player2
@player2 = Player.new self
- @player2.warp CP::Vec2.new(SCREEN_WIDTH-100, 20) # move to the center of the window
- @player2.colorize 0, 255, 0
-
- @controls << Controls.new(self, @player2,
- Gosu::Button::KbA => :turn_left,
- Gosu::Button::KbD => :turn_right,
- Gosu::Button::KbW => :accelerate,
- Gosu::Button::KbLeftControl => :boost,
- Gosu::Button::KbS => :reverse,
- Gosu::Button::KbLeftShift => :shoot
- )
-
- register @player2
+ # @player2.warp CP::Vec2.new(SCREEN_WIDTH-100, 20) # move to the center of the window
+ # @player2.colorize 0, 255, 0
+ #
+ # @controls << Controls.new(self, @player2,
+ # Gosu::Button::KbA => :turn_left,
+ # Gosu::Button::KbD => :turn_right,
+ # Gosu::Button::KbW => :accelerate,
+ # Gosu::Button::KbLeftControl => :boost,
+ # Gosu::Button::KbS => :reverse,
+ # Gosu::Button::KbLeftShift => :shoot
+ # )
+ #
+ # register @player2
end
def remove_collided
View
45 lib/nuke.rb
@@ -2,6 +2,8 @@
#
class Nuke < Moveable
+ include TargetAcquisition
+
attr_reader :score
def initialize window
@@ -15,18 +17,53 @@ def initialize window
@image = Gosu::Image.new window, "media/nuke.png", false
# up-/downgradeable
- @rotation = 100.0
- @acceleration = 800.0
- @deceleration = 100.0
- @max_speed = 200.0
+ @rotation = 0.1
+ @acceleration = 0.1
+ @deceleration = 0.1
+ @top_speed = 3
+
+ self.rotation = -2*Math::PI/3
@shape.collision_type = :nuke
end
# TODO extract into module
#
def target *targets
+ target = acquire *targets
+ follow target
+ end
+
+ # Tries to hit the target.
+ #
+ def follow target
+ direct = target.position - self.position
+ angle = (self.rotation - direct.to_angle) % (Math::PI*2)
+
+ case angle
+ when 0..Math::PI : turn_left
+ when Math::PI..(2*Math::PI) : turn_right
+ end
+ case angle + Math::PI/2
+ when 0..Math::PI : accelerate
+ when Math::PI..(2*Math::PI) : reverse
+ end
+ end
+
+ def turn_left
+ self.rotation -= @rotation / SUBSTEPS
+ end
+ def turn_right
+ self.rotation += @rotation / SUBSTEPS
+ end
+ def accelerate
+ acceleration = [@acceleration, (@top_speed - self.current_speed)].min
+ @shape.body.apply_force((rotation_vector * (acceleration / SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
+ end
+ def reverse
+ deceleration = [@deceleration, (@top_speed - self.current_speed)].min
+ @shape.body.apply_force(-(rotation_vector * (deceleration / SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
end
def draw
View
17 lib/player.rb
@@ -22,11 +22,11 @@ def initialize window
@image = Gosu::Image.new window, "media/spaceship.png", false
# up-/downgradeable
- @rotation = 100.0
+ @rotation = 0.1
@acceleration = 800.0
@boost_acceleration = 100_000.0
@deceleration = 200.0
- @max_speed = 200.0
+ @top_speed = 200.0
# Keep in mind that down the screen is positive y, which means that PI/2 radians,
# which you might consider the top in the traditional Trig unit circle sense is actually
@@ -51,15 +51,15 @@ def colorize red, green, blue
# even if the number of steps per update are adjusted
#
def turn_left
- @shape.body.t -= @rotation / SUBSTEPS
+ self.rotation = self.rotation - @rotation / SUBSTEPS
end
# Apply positive Torque; Chipmunk will do the rest
# SUBSTEPS is used as a divisor to keep turning rate constant
# even if the number of steps per update are adjusted
#
def turn_right
- @shape.body.t += @rotation / SUBSTEPS
+ self.rotation = self.rotation + @rotation / SUBSTEPS
end
# Apply forward force; Chipmunk will do the rest
@@ -70,24 +70,25 @@ def turn_right
# and with a magnitude representing the force we want to apply.
#
def accelerate
- acceleration = [@acceleration, (@max_speed-self.current_speed)].min
- @shape.body.apply_force((rotation_vector * (acceleration/SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
+ acceleration = [@acceleration, (@top_speed-self.current_speed)].min
+ @shape.body.apply_force((rotation_vector * (acceleration / SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
end
# Apply even more forward force.
# See accelerate for more details.
#
def boost
sometimes :boost_enabled, 5 do
- @shape.body.apply_force((rotation_vector * (@boost_acceleration/SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
+ @shape.body.apply_force((rotation_vector * (@boost_acceleration / SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
end
end
# Apply reverse force
# See accelerate for more details
#
def reverse
- @shape.body.apply_force(-(rotation_vector * (@deceleration / SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
+ deceleration = [@deceleration, (@top_speed-self.current_speed)].min
+ @shape.body.apply_force(-(rotation_vector * (deceleration / SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
end
def shoot
View
17 lib/target_acquisition.rb
@@ -0,0 +1,17 @@
+module TargetAcquisition
+
+ # Returns the closest target.
+ #
+ def acquire *targets
+ closest = nil
+ lowest_distance = nil
+ targets.each do |target|
+ distance = (target.position - self.position).length
+ next if lowest_distance && distance > lowest_distance
+ lowest_distance = distance
+ closest = target
+ end
+ return closest
+ end
+
+end
View
BIN media/nuke.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
2 multiroids.rb
@@ -22,6 +22,8 @@
require 'lib/numeric'
require 'lib/zorder'
+
+require 'lib/target_acquisition'
require 'lib/moveable'
require 'lib/player'
require 'lib/asteroid'

0 comments on commit d851151

Please sign in to comment.
Something went wrong with that request. Please try again.