001package squidpony.squidgrid.gui.gdx;
002
003import com.badlogic.gdx.graphics.Color;
004import com.badlogic.gdx.scenes.scene2d.Actor;
005import com.badlogic.gdx.scenes.scene2d.ui.Label;
006import squidpony.squidgrid.Direction;
007
008import java.util.Collection;
009
010/**
011 * A simple class that wraps an Actor with its grid position, animating state, and if it is a double-width Actor.
012 * Created by Tommy Ettinger on 7/22/2015.
013 */
014public class AnimatedEntity {
015    public Actor actor;
016    public int gridX, gridY;
017    public boolean animating;
018    public boolean doubleWidth;
019    public AnimatedEntity(Actor actor, int x, int y)
020    {
021        this.actor = actor;
022        gridX = x;
023        gridY = y;
024    }
025    public AnimatedEntity(Actor actor, int x, int y, boolean doubleWidth)
026    {
027        this.actor = actor;
028        gridX = x;
029        gridY = y;
030        this.doubleWidth = doubleWidth;
031    }
032    public void setText(String text)
033    {
034        if(actor instanceof Label)
035        {
036            ((Label)actor).setText(text);
037        }
038    }
039
040    /**
041     * Rotates this so that "up" points in the specified direction. Only some Actors can actually be rotated; Images
042     * can, for example, but Labels cannot. This method is most likely to be used with
043     * {@link TextCellFactory#makeDirectionMarker(Color)},
044     * {@link TextCellFactory#makeDirectionMarker(Collection, float, boolean)}, or one of the directionMarker methods
045     * in SquidPanel or SquidLayers, since those produce an Image (or {@link ColorChangeImage}) that can be sensibly
046     * rotated to indicate a direction over a cell.
047     * @param dir the direction that "up" for this should point toward
048     */
049    public void setDirection(Direction dir) {
050        actor.setRotation(directionToDegrees(dir));
051    }
052    private static float directionToDegrees(Direction d)
053    {
054        switch (d)
055        {
056            case UP_LEFT: return 45f;
057            case LEFT: return 90f;
058            case DOWN_LEFT: return 135f;
059            case DOWN: return 180f;
060            case DOWN_RIGHT: return 225f;
061            case RIGHT: return 270f;
062            case UP_RIGHT: return 315f;
063            default: return 0f;
064        }
065    }
066}