
require 'chipmunk'
require 'gosu'
class Rocket
  attr_accessor :x,:y, :angle, :rocket_body, :rocket_shape

  def initialize(window)
    #create a new image on the arrow
   
    @animation = Gosu::Image::load_tiles(window, "./images/Rocket2.png", 100, 50, false)
      #@animation = Gosu::Image.new(window,"./images/rocketblue.png",false)
    @angle = 0.0 #the angle of the wheel
    
  
    @x = @y  = @angle = 0.0
    
    
    #chipmunk properties
    
     # thevertices = [
     # CP::Vec2.new(0,0), #x1 first corner
     # CP::Vec2.new(0,50), #x2 second corner
    #  CP::Vec2.new(70,50), #x3 third corner
    #  CP::Vec2.new(70,0) #x4 fourth corner
   # ]
    
      thevertices = [
      CP::Vec2.new(0,0), #x1 first corner
      CP::Vec2.new(0,50), #x2 second corner
      CP::Vec2.new(100,50), #x3 third corner
      CP::Vec2.new(100,0) #x4 fourth corner
    ]
   
    
    
    
    @rocket_body = CP::Body.new((1.0/0.0),(1.0/0.0))
    @rocket_body.m =1

   
   
    #create a square with the vertices as defined above
    @rocket_shape = CP::Shape::Poly.new(@rocket_body,thevertices,CP::Vec2.new(0.0,0.0))
  
    @rocket_shape.collision_type = :rocket
 
    @rocket_shape.body.p.x =100
    @rocket_shape.body.p.y =250
    
    @rocket_shape.body.a = 0.degrees_to_radians
    
    
    
    
  end
  

  def update
    
    
   

  end
  
  def rocketrotation(id)


    temprotation = @rocket_shape.body.a
    if (id=="rotup") then
  
      @rocket_shape.body.a -=1.degrees_to_radians    
    
    else if (id == "rotdown")
      @rocket_shape.body.a +=1.degrees_to_radians
    end
    end
    if @rocket_shape.body.a.radians_to_degrees > 20   then @rocket_shape.body.a = temprotation end
    if @rocket_shape.body.a.radians_to_degrees < -20 then @rocket_shape.body.a = temprotation end
    
  end
  
  
  
  def rocketposition(id)
    

    temp =@rocket_shape.body.p.y
    
    if id == "up" then 
          @rocket_shape.body.p.y -=3    
          
    end
    
    if id == "down" then 
    
      @rocket_shape.body.p.y +=3
    end
    #creating boundary 
    
    if @rocket_shape.body.p.y >400 then @rocket_shape.body.p.y = temp end
    if @rocket_shape.body.p.y <200 then @rocket_shape.body.p.y = temp end
      
      
    end

  
 
  def draw
    
  
   
   img = @animation[rand(6)]; #creating rocket fire effect

   img.draw_rot( @rocket_shape.body.p.x+50, @rocket_shape.body.p.y+25, 1, @rocket_shape.body.a.radians_to_degrees,center_x = 0.5, center_y = 0.5, factor_x = 1, factor_y = 1, color = 0xffffffff, mode = :default)
    #img.draw( @rocket_shape.body.p.x, @rocket_shape.body.p.y, 1,  factor_x = 1, factor_y = 1, color = 0xffffffff, mode = :default)
   
  end
  
end