package ludumdare.game;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import org.crusty.math.Vec2double;

public class Entity {

	Vec2double pos, vel, acc, rotatePoint;
	Sprite sprite;
	public int depth = 0;
	double rotation = 0;
	public double rotationalVel = 0;
	
	public Entity(Sprite sprite) {
		pos = new Vec2double(0, 0);
		vel = new Vec2double(0, 0);
		acc = new Vec2double(0, 0);
		this.sprite = sprite;
		rotatePoint = new Vec2double(sprite.image.getWidth()/2, sprite.image.getHeight()/2);
	}
	
	public void logic(double dt) {
		vel.x += acc.x / 1000;
		vel.y += acc.y / 1000;
		pos.x += vel.x * dt / 1000000;
		pos.y += vel.y * dt / 1000000;
		
		rotation += rotationalVel * dt / 1000000;
	}
	
	public void draw(Graphics2D g) {
//		AffineTransform old = g.getTransform();
		
		//Vec2double rotatedRotatePoint = rotatePoint.rotate(new Vec2double(0, 0), rotation);
		
		AffineTransform save = g.getTransform();
		
//		AffineTransform atNew = g.getTransform();
		g.rotate(rotation, pos.x + rotatePoint.x, pos.y + rotatePoint.y); //  + rotatePoint.x  + rotatePoint.y
//		g.setTransform(atNew);
		g.drawImage(sprite.image, 
					(int) pos.x, 
					(int) pos.y, 
					null);
		g.rotate(-rotation , pos.x + rotatePoint.x, pos.y + rotatePoint.y); //  + rotatePoint.x  + rotatePoint.y
		
		g.setTransform(save);
		
		// Root point
//		g.setColor(Color.RED);
//		g.drawRect((int) (pos.x), (int) (pos.y), 5, 5); //  + rotatePoint.x  + rotatePoint.y
		
//		g.setTransform(old);
	}
	
}
