# To change this template, choose Tools | Templates
# and open the template in the editor.


require 'gosu'
require 'chipmunk'

load 'Rocket.rb'
load 'Bullet.rb'
load 'Scores.rb'
load 'Crate.rb'
load 'Ammo.rb'
load 'Explosion.rb'
load 'GameMenuScore.rb'
load 'SaveLoadGame.rb'

class GameWindow < Gosu::Window

  #first create x and y coordinate values
  attr_accessor :x,:y
   

  def initialize(difficulty)
    

    #create a 640 x 480 window.
    #set fullscreen : false
    #update interval : 20ms
    #you can only use one instance of this class at a time.
    super(800,600,false,20)

    #set the caption of the game window
    self.caption = 'Sky Rocket V1.0'

    
    #load music
    @music = Gosu::Song.new(self,"./sound/Cod.ogg")
  
    
    @musicexp = Gosu::Sample.new(self,"./sound/expsound.ogg")
    @musiclaser = Gosu::Sample.new(self,"./sound/laser.ogg")
    @musicupgrade = Gosu::Sample.new(self,"./sound/upgrade.ogg")
    
     #loading backround image
     @background_image = Gosu::Image.new(self,"./images/map2.png",true)
     
    @background_loading =Gosu::Image.new(self,"./images/loading.png",true)
     
    #loading crate
    @crateimage = Gosu::Image.new(self,"./images/Crate.png",false)
    @ammoimage = Gosu::Image.new(self,"./images/Ammo.png",false)
    @bulletimage = Gosu::Image.new(self,"./images/Bullet.png",false)
       
    #setting backgrounds picture initial positions
     @background_x1 =0
     @background_x2 =8000
     
     @rocket = Rocket.new(self)
     
     @bullets = []
     @crates = []
     @ammo = []
     @explosion = []
     
     @score = Scores.new(self,difficulty)
     
     @starttime =Gosu::milliseconds
     @ammostarttme =Gosu::milliseconds
     @tempcurrenttime =Gosu::milliseconds/1000
     @gameloading = Gosu::milliseconds
     @gamestarttime =Gosu::milliseconds
     @starter =true
  
    #creating physics
       
    @space = CP::Space.new
    @space.gravity = CP::Vec2.new(0.0,0.0)
    
    @space.add_body(@rocket.rocket_body)
    @space.add_shape(@rocket.rocket_shape)
    
    @ending =false
    @animation = Gosu::Image::load_tiles(self, "./images/explosion.png", 45, 45, false)
        
    @space.add_collision_func(:rocket, :crate) do |rocket, crate|
      if(@ending ==false) then
        @music.stop
        close
        GameMenuScore.new("lost",@score.kills).show
        @ending =true
       
      end
     
      true

    end
    
    
    @space.add_collision_func(:rocket, :ammo) do |rocket, ammo|
    
      if(@ending ==false) then
        @music.stop
        close
        GameMenuScore.new("lost",@score.kills).show
        @ending =true
        
      end
     
      true

    end
    
    
   @space.add_collision_func(:bullet, :ammo) do |bullet, ammo|
   
     
     @musicupgrade.play(vol = 1, speed = 1, looping = false) 
      @ammo.each_with_index do |ammoner|
         if (ammoner.ammo_shape == ammo) then
           @ammo.delete(ammoner)
         end
        
       end
       
         @bullets.each_with_index do |bulleter|
         if (bulleter.bullet_shape == bullet) then
           @bullets.delete(bulleter)
         end
        
       end
       
      @space.remove_body(ammo.body)
      @space.remove_shape(ammo)
      
      @space.remove_body(bullet.body)
      @space.remove_shape(bullet)
      
      @score.increaseammunition
      true

    end
    
    
    
    
    
    @space.add_collision_func(:bullet, :crate) do |bullet, crate|
     
     @musicexp.play(vol = 1, speed = 1, looping = false) 

      my_explosion = Explosion.new(self,crate.body.p.x,crate.body.p.y,@animation)
      @explosion << my_explosion
      
       @bullets.each_with_index do |bulleter|
         if (bulleter.bullet_shape == bullet) then
           @bullets.delete(bulleter)
         end
        
       end
       
      @crates.each_with_index do |crater|
         if (crater.crate_shape == crate) then
         @crates.delete(crater)
         end
        
       end
      
      
      @crates.delete(crate)
        
      @space.remove_body(crate.body)
      @space.remove_shape(crate)
        
      @space.remove_body(bullet.body)
      @space.remove_shape(bullet)
     
      @score.increasekill
      true

    end

    
      @music.play(looping = true)

  end

  
   def backgroundmanager
   #makes a background image loop
   if @background_x2 > 0 then @background_x2 -=1 else @background_x2 =8000 end
   if @background_x1 > -8000  then @background_x1 -=1 else @background_x1 =0 end

   end
   
   
   def explosionmanager
     @explosion.each_with_index do |explosion|
       if(explosion.increaseindex > 5) then
         @explosion.delete(explosion)
       end
       
     end
     
   end
   
  def update
  
    
     cratemanager
    ammomanager
    timecheck
    
    
    explosionmanager
    backgroundmanager
    
       if button_down? Gosu::KbUp  then @rocket.rocketposition("up")
          else if 
            button_down? Gosu::KbDown  then @rocket.rocketposition("down")
          end
       end
    
       if button_down? Gosu::KbLeft then @rocket.rocketrotation("rotup") end
       if button_down? Gosu::KbRight then @rocket.rocketrotation("rotdown") end
    
    bulletmanager
    
    if @starter ==true then
      crategenerator
      @starter=false
    end
    
    if ((Gosu::milliseconds - @starttime)/1000) >1 then
      crategenerator 
      @starttime = Gosu::milliseconds
     
    end
    if ((Gosu::milliseconds - @ammostarttme)/1000) >5 then
      ammogenerator
      @ammostarttme  = Gosu::milliseconds
     
    end 
    

 
 
    @space.step(1.0/5.0)

  end

  #updating by key stroke.  
  def button_down(id)
    if id == Gosu::KbS then 
      bulleta =[]
     
      @bullets.each_with_index do |bulleter|
        bulleta << { :x =>bulleter.bullet_shape.body.p.x, :y => bulleter.bullet_shape.body.p.y, :velx => bulleter.velx, :vely => bulleter.vely}
      end
      
      cratesa = []
      
      @crates.each_with_index do |crater|
        cratesa<< { :x =>crater.crate_shape.body.p.x, :y => crater.crate_shape.body.p.y}
      end
      
      ammoa = []
      
      @ammo.each_with_index do |amoner|
        ammoa<< { :x =>amoner.ammo_shape.body.p.x, :y => amoner.ammo_shape.body.p.y}
      end
      
      scorea = []
      
     
      scorea<< { :ammunition =>@score.ammunition, :kills => @score.kills, :timeleft =>@score.timeleft,:killwinning =>@score.killwinning}
      
      
      s = SaveLoadGame.new
      s.save(@background_x1,@background_x2,@rocket.rocket_shape.body.p.x,@rocket.rocket_shape.body.p.y,@rocket.rocket_shape.body.a,bulleta, cratesa,ammoa,scorea,@starter,@ending)
    end
    
    
    if id == Gosu::KbL then 

  
      
      @starttime =Gosu::milliseconds
     @ammostarttme =Gosu::milliseconds
     @tempcurrenttime =Gosu::milliseconds/1000
     
     @gamestarttime =Gosu::milliseconds
     
      l = SaveLoadGame.new
      a = l.load     
     
      
      @background_x1 = a.background_x1
      @background_x2 = a.background_x2
      
      @rocket.rocket_shape.body.p.x = a.rocketpx 
      @rocket.rocket_shape.body.p.y = a.rocketpy
      @rocket.rocket_shape.body.a = a.rocketa
      
   
       @bullets.each_with_index do |bulleter|
        
         @space.remove_body(bulleter.bullet_body)
         @space.remove_shape(bulleter.bullet_shape)
         @bullets.delete(bulleter)
        
       end

      bulleta = a.bullets
        bulleta.each do |entry|
          my_bullet = Bullet.new(self,entry[:x],entry[:y],entry[:velx], entry[:vely],@bulletimage)  
           @space.add_body(my_bullet.bullet_body)
           @space.add_shape(my_bullet.bullet_shape)
           @bullets << my_bullet
         end
      
        @crates.each_with_index do |crater|
        @crates.delete(crater)
         @space.remove_body(crater.crate_body)
         @space.remove_shape(crater.crate_shape)
         
          
        end

      puts @crates.length
     if @crates.length == 1 then 
       @space.remove_body(@crates.at(0).crate_body)
       @space.remove_shape(@crates.at(0).crate_shape)
       @crates.delete_at(0) 
     end
        
      
    
      
    
      cratesa =a.crates
        
        cratesa.each do |entry|
           my_crate = Crate.new(self,@crateimage)  
           my_crate.crate_shape.body.p.x = entry[:x]
           my_crate.crate_shape.body.p.y = entry[:y]
           
           @space.add_body(my_crate.crate_body)
           @space.add_shape(my_crate.crate_shape)
           @crates << my_crate
        
         end
         

        
        @ammo.each_with_index do |amoner|
        
         @space.remove_body(amoner.ammo_body)
         @space.remove_shape(amoner.ammo_shape)
         @ammo.delete(amoner)
        
       end
      
      
        amoner =a.ammo 
      
        amoner.each do |entry|
          my_ammo = Ammo.new(self,@ammoimage)  
           my_ammo.ammo_shape.body.p.x = entry[:x]
           my_ammo.ammo_shape.body.p.y = entry[:y]
           
           @space.add_body(my_ammo.ammo_body)
           @space.add_shape(my_ammo.ammo_shape)
        
           @ammo << my_ammo
         end
      
  
       scorer=a.score
       scorer.each do |entry|
           
          @score.ammunition = entry[:ammunition]
          @score.kills = entry[:kills]
          @score.timeleft = entry[:timeleft]
          @score.killwinning = entry[:killwinning]
         end
      
      
      
      
     
      @starter =a.starter
      @ending = a.ending
      
 
    
    end
    
    if id == Gosu::KbSpace then 
      bulletcreator 
     
    end
    
  end
  
  def crategenerator
    
   #rnumber = rand(400)+100
   
    my_crate = Crate.new(self,@crateimage)
  
     
    @crates << my_crate

    
     @space.add_body(my_crate.crate_body)
     @space.add_shape(my_crate.crate_shape)
     
 
    
    #@rnumber2 = rand(400+100)
    
    #while (!(((@rnumber2-rnumber) < -20) || ((@rnumber2-rnumber) > 20 ))) do
       
     # @rnumber2 = rand(400+100)
     
      #puts @rnumber2.to_s+" d"
    #end

  #  my_crate = Crate.new(self,@crateimage)
    
     
  #  @crates << my_crate

   #  @space.add_body(my_crate.crate_body)
   #  @space.add_shape(my_crate.crate_shape)
     
      
  end
  
  def ammogenerator
    my_ammo = Ammo.new(self,@ammoimage)
      
      @ammo << my_ammo

      @space.add_body(my_ammo.ammo_body)
      @space.add_shape(my_ammo.ammo_shape)
    
  end
  
    def ammomanager
      @ammo.each_with_index do |ammo|
      if ammo.ammo_shape.body.p.x < -20 then
        @ammo.delete(ammo)
        
        @space.remove_body(ammo.ammo_body)
        @space.remove_shape(ammo.ammo_shape)
      end
     end
    end
  def cratemanager
    @crates.each_with_index do |crate|
      if crate.crate_shape.body.p.x < -20 then
        @crates.delete(crate)
        
        @space.remove_body(crate.crate_body)
        @space.remove_shape(crate.crate_shape)
      end
    end
    
  end
  
  def bulletmanager
    
    @bullets.each_with_index do |bullet|
     
      if bullet.bullet_shape.body.p.x > 801 then
        @bullets.delete(bullet)
        
        @space.remove_body(bullet.bullet_body)
        @space.remove_shape(bullet.bullet_shape)
        
        
      end
    end
    
    
  end
  
  def bulletcreator
    
    if @score.ammunition > 0 then
        #my_bullet = Bullet.new(self,@rocket.rocket_shape.body.p.x+50, @rocket.rocket_shape.body.p.y+Gosu::offset_y(@rocket.rocket_shape.body.a.radians_to_degrees, 50),Gosu::offset_x(@rocket.rocket_shape.body.a.radians_to_degrees, 50),Gosu::offset_y(@rocket.rocket_shape.body.a.radians_to_degrees, 50))  
       @musiclaser.play(vol = 1, speed = 1, looping = false) 
      angle= @rocket.rocket_shape.body.a.radians_to_degrees+90
     
      ypos =23.0
      if angle > 90 then ypos = (angle/110)*30 end
      if angle < 90 then ypos = (angle/70)*12 end
      
      my_bullet = Bullet.new(self,@rocket.rocket_shape.body.p.x+90,@rocket.rocket_shape.body.p.y+ypos+(Gosu::offset_y(@rocket.rocket_shape.body.a.radians_to_degrees+90,1)),Gosu::offset_x(@rocket.rocket_shape.body.a.radians_to_degrees+90, 90),Gosu::offset_y(@rocket.rocket_shape.body.a.radians_to_degrees+90, 90),@bulletimage)  
        @bullets << my_bullet

        @space.add_body(my_bullet.bullet_body)
        @space.add_shape(my_bullet.bullet_shape)
    end
        
        @score.decreaseammunition
  end
  
 def timecheck
   
  
  
   if  ((Gosu::milliseconds/1000) - @tempcurrenttime) == 1  then
     if @score.timeleft > 0 then
         @score.timeleft =  ((@score.timeleft) - 1)
         @tempcurrenttime = (Gosu::milliseconds/1000)
     else
      
       @music.stop
       close
       if(@score.kills >@score.killwinning) then
         GameMenuScore.new("win",@score.kills).show
       else
         GameMenuScore.new("lost",@score.kills).show
       end
       
     end
   end
   
 end
   
  def draw
    #if ((Gosu::milliseconds - @gameloading) /1000 >3) then
  #draw the background environment
  @background_image.draw(@background_x1,0,0)
  @background_image.draw(@background_x2,0,0)

  
  #draw the rocket
  @rocket.draw
    
  #draw bullets
  @bullets.each{|bullet|bullet.draw}
  
  #draw text
  @score.draw
  
   #draw crates
   @crates.each{|crate|crate.draw} 
   
    #draw ammo
    @ammo.each{|ammo|ammo.draw} 
    
    #draw explosions
    @explosion.each{|explosion|explosion.draw} 
    
   # else
      
      # @background_loading.draw(0,0,0)
       
   # end
  end
end
  





#create an instance of the window
#w = GameWindow.new
#show the window
#w.show
