package de.heatup.objects;

import java.awt.Point;
import de.heatup.cfg.Config;
import de.heatup.mapengine.CollisionHandler;


// TODO: Magische Nummern entfernen

public class Player extends StaticObject {
	private static final long serialVersionUID = 1L;
	public boolean startMove = false;
	public boolean animate = false;
	protected int speedMultiplicator = Config.initialSpeedMultiplicator;
	protected char direction;
	protected int i;
	final int stepx = 25;
	final int stepy = 25;
	int startX;
	int startY;
	int stepCounter = 25;
	private Point currentGridPosition;
	
	public Player() {
		super('p', new Point());
		CollisionHandler.addMovingObject(this);
		if (super.bf1 == null) {
			loadBufferedImage();
		}
	}
	
	public void setSpeedMultiplicator(int factor) {
		this.speedMultiplicator *= factor;
	}
	
	public void setSpeedMultiplicatorAbsolute(int value) {
		this.speedMultiplicator = value;
	}
	
	public void resetSpeedMultiplicator() {
		this.speedMultiplicator = Config.initialSpeedMultiplicator;
	}
	
	
 	public void setGridLocation(int x, int y) {
		currentGridPosition.setLocation(x, y);
	}
	
	public Point getGridLocation() {
		return currentGridPosition;
	}
	
	public void spawnAt(Point spawnPoint) {
		this.currentGridPosition = spawnPoint;
		super.setAutoBounds(spawnPoint.x*25, spawnPoint.y*25);
		this.repaint();
	}
	
	public void step(char d, int i) {
		this.direction = d;
		this.i = i;
	}

	public void tick() {
		if ( startMove || animate ) {
			int newx = this.getX();
			int newy = this.getY();
			for (int s = 0; s < this.speedMultiplicator; s++) { // speed multiplikator
				if (direction == 'x' && i == -1) {
					newx = newx - 1;
				} else if (direction == 'x' && i == 1) {
					newx = newx + 1;
				} else if (direction == 'y' && i == -1) {
					newy = newy - 1;
				} else if (direction == 'y' && i == 1) {
					newy = newy + 1;
				}
				this.setAutoBounds(newx, newy);
				stepCounter--;
				if ( stepCounter <= 0 ) {
					animate = false;
					break;
				}
			}
			if (stepCounter <= 0) {
				stepCounter = 25;
			}
		}		
		startMove = false;
	}
}