package alfredo.sprite;

import alfredo.geom.Point;

/**
 * An Entity represents an object that has a position and a direction.
 * 
 * Entities are useful in that they can be parented to one another.
 * @author TheMonsterFromTheDeep
 */
public abstract class Entity implements Anchor {
    private Anchor parent;
    
    public Entity() {
        parent = new Empty();
    }
    
    public final Point getPosition() {
        return new Point(getX(), getY());
    }
    
    @Override
    public final double getDirection() {
        return parent.getDirection() + getLocalDirection();
    }
    
    @Override
    public final float getX() {
        return parent.getX() + getLocalX();
    }
    
    @Override
    public final float getY() {
        return parent.getY() + getLocalY();
    }
    
    public abstract float getLocalX();
    public abstract float getLocalY();
    public abstract double getLocalDirection();
    public abstract void setLocalX(float x);
    public abstract void setLocalY(float y);
    
    public void setParent(Anchor p, boolean retainPosition) {
        if(p != null) {
            if(retainPosition) {
                float newX = getX() - p.getX();
                float newY = getY() - p.getY();
                setLocalX(newX);
                setLocalY(newY);
            }
            parent = p;
        }
    }
    
    public void clearParent(boolean retainPosition) {
        if(retainPosition) {
            float newX = getX(), newY = getY();
            setLocalX(newX);
            setLocalY(newY);
        }
        parent = new Empty();
    }
}
@@ -645,32 +645,40 @@
 %0A        return 
+(float)(
 parent.getX() + 
@@ -672,24 +672,143 @@
 ent.getX() +
+ Math.cos(Math.toRadians(parent.getDirection())) * getLocalY() + Math.cos(Math.toRadians(parent.getDirection() + 90)) *
  getLocalX()
@@ -803,24 +803,25 @@
  getLocalX()
+)
 ;%0A    %7D%0A    
@@ -874,32 +874,40 @@
 %0A        return 
+(float)(
 parent.getY() + 
@@ -906,26 +906,146 @@
 etY() + 
-getLocalY(
+Math.sin(Math.toRadians(parent.getDirection())) * getLocalY() + Math.sin(Math.toRadians(parent.getDirection() + 90)) * getLocalX()
 );%0A    %7D

Make rotation affect Entity parented movement