/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slickfred;

import gamelib.Actor;
import gamelib.Level;
import gamelib.Player;
import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;

/**
 *
 * @author jakub
 */
class Fred extends Player{

    
    private float x, y;
    private int delta;
    private Animation animation;
    private String name;

    public Fred() throws SlickException {
        this.x = 0;
        this.y = 0;
        this.delta = 0;
        this.name = "fred";
        
        Image[] images = {new Image("resources/animation_images/fred/walk_left1.png"),
            new Image("resources/animation_images/fred/walk_left2.png"),
            new Image("resources/animation_images/fred/walk_left3.png"),
            new Image("resources/animation_images/fred/walk_left4.png"),
            new Image("resources/animation_images/fred/walk_left5.png"),
            new Image("resources/animation_images/fred/walk_left6.png"),
            new Image("resources/animation_images/fred/walk_left7.png"),
            new Image("resources/animation_images/fred/walk_left8.png"),
            new Image("resources/animation_images/fred/walk_left9.png"),
            new Image("resources/animation_images/fred/walk_left10.png"),
            new Image("resources/animation_images/fred/walk_left11.png"),
            new Image("resources/animation_images/fred/walk_left12.png"),
            new Image("resources/animation_images/fred/walk_left13.png"),
            new Image("resources/animation_images/fred/walk_left14.png"),
            new Image("resources/animation_images/fred/walk_left15.png"),
            new Image("resources/animation_images/fred/walk_left16.png"),
            new Image("resources/animation_images/fred/walk_left17.png"),
            new Image("resources/animation_images/fred/walk_left18.png"),
            new Image("resources/animation_images/fred/walk_left19.png"),
            new Image("resources/animation_images/fred/walk_left20.png"),
            new Image("resources/animation_images/fred/walk_left21.png"),
            new Image("resources/animation_images/fred/walk_left22.png"),
            new Image("resources/animation_images/fred/walk_left23.png"),
            new Image("resources/animation_images/fred/walk_left24.png"),
            new Image("resources/animation_images/fred/walk_left25.png"),
            new Image("resources/animation_images/fred/walk_left26.png")
        };
        this.animation = new Animation(images, 40, true);
    }

    @Override
    public void update(GameContainer container, int delta, Level level) {
        Input input = container.getInput();
        if( input.isKeyDown(Input.KEY_LEFT)){
            this.x--;
        }
        
        else if( input.isKeyDown(Input.KEY_RIGHT)){
            this.x++;
        }
        
        else if( input.isKeyDown(Input.KEY_SPACE))
            this.animation.setAutoUpdate(false);
        
        //moveX(this.delta);
    }
    
    @Override
    public boolean intersect(Actor actor){
        Rectangle fred, itemRect;
	int fredWidth = this.animation.getCurrentFrame().getWidth();
	int fredHeight = this.animation.getCurrentFrame().getHeight();
	
	int itemWidth = actor.getCurrentAnimation().getCurrentFrame().getWidth();
	int itemHeight = actor.getCurrentAnimation().getCurrentFrame().getHeight();

	fred = new Rectangle(this.getX(), this.getY(), fredWidth, fredHeight);
	itemRect = new Rectangle(actor.getX(), actor.getY(), itemWidth, itemHeight);
	return fred.intersects(itemRect);
    }

    @Override
    public void render(GameContainer container, Graphics g, Level level) {
        //this.image.draw(this.x, this.y);
        this.animation.draw(this.x, this.y);
    }

    private void moveX(int i) {
        this.x+=i;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public float getX() {
        return this.x;
    }

    @Override
    public float getY() {
        return this.y;
    }

    @Override
    public void setX(float x) {
        this.x = x;
    }

    @Override
    public void setY(float y) {
        this.y = y;
    }

    @Override
    public Animation getCurrentAnimation() {
        return this.animation;
    }

    @Override
    public void jump() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
