# Player class.
class Player
  attr_reader :x, :y, :status 
  attr_accessor :levelno
  
  def initialize(window, x, y)
    @x, @y = x, y
    @vel_x = @vel_y = @angle = 0.0
    @dir = :left
    @vy = 0 # Vertical velocity
    @map = window.map
    @status = "alive" #player status
    @levelno = 1
    # Load all animation frames [add death frames]
    @standing, @walk1, @walk2, @dead, @jump =
      *Gosu::Image.load_tiles(window, "media/Player.png", 50, 50, false)
    # This always points to the frame that is currently drawn.    
    @cur_image = @standing
  end
  
  def draw
    # Flip vertically when facing to the left.
    if @dir == :left then
      offs_x = -25
      factor = 1.0
    else
      offs_x = 25
      factor = -1.0
    end
    @cur_image.draw(@x + offs_x, @y - 49, 0, factor, 1.0)
  end
  
  def check_time(time)
    if time > 0
      return "alive"
    else
      set_dead
      return "dead"      
    end
  end

  # Could the object be placed at x + offs_x/y + offs_y without being stuck?
  def would_fit(offs_x, offs_y)
    # Check at the center/top and center/bottom for map collisions
    not @map.solid?(@x + offs_x, @y + offs_y) and
      not @map.solid?(@x + offs_x, @y + offs_y - 45)
  end

  def update(move_x)
    if (@cur_image != @dead)      
      # Select image depending on action    
      if (move_x == 0)
        @cur_image = @standing
        @vel_x = 0 #reset vel right
        @vel_y = 0 #reset vel left
      else
        @cur_image = (Gosu::milliseconds / 175 % 2 == 0) ? @walk1 : @walk2
      end
    
      if (@vy < 0)
        @cur_image = @jump
      end

      # Directional walking, horizontal movement
      if move_x > 0 then
        @dir = :right
        @vel_y = 0 #reset vel right
        @vel_x += Gosu::offset_x(90, 0.2)      
        move_x.times { if would_fit(10, 0) then @x += @vel_x end }
        @vel_x *= 0.95      
      end
      if move_x < 0 then
        @dir = :left 
        @vel_x = 0 #reset vel right   
        @vel_y += Gosu::offset_x(270, 0.2)         
        (-move_x).times { if would_fit(-10, 0) then @x += @vel_y end } 
        @vel_y *= 0.95
      end
    
      # Acceleration/gravity  
      if @vy < 0 then
        (-@vy).times { if would_fit(0, -1) then @y -= 1 else @vy = 0 end }
      end
    end
    @vy += 1 # Vertical movement    
    if @vy > 0 then
      @vy.times { if would_fit(0, 1) then @y += 1 else @vy = 0 end }
    end
  end
  
  def try_to_jump
    if @map.solid?(@x, @y + 1) then
      @vy = -15
    end
  end

  def detect_ghost(ghost)         
    ghost.each do |c|      
      c.vx == 100 && c.vy > 0      
      if Gosu::distance(@x,@y,c.x,c.y+c.vx) <25 then   
        set_dead
      end            
    end  
  end
  
  def detect_spikes(spikes)    
    spikes.each do |c|
      if Gosu::distance(@x,@y,c.x,c.y) < 40 then
        set_dead
      end 
    end  
  end
  
  def set_dead
    #play sound
    #@deadSound = Gosu::Sample.new("media/splat.wav")
    #@deadSound.play(9,1,true)
    
    @cur_image = @dead
    @status = "dead"
  end
  
  def detect_finish(finish)    
    finish.each do |c|
      if Gosu::distance(@x,@y,c.x,c.y) < 25 then
        @cur_image = @standing
        @status = "Exit"
      end 
    end  
  end
  
end
