summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx/boardgame/Piece.java
blob: 6b7f2b4acb847d7aa5d7f2ec0f55082c94e22906 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package ch.asynk.gdx.boardgame;

import java.lang.Math;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.MathUtils;

import ch.asynk.gdx.boardgame.Drawable;
import ch.asynk.gdx.boardgame.Orientation;
import ch.asynk.gdx.boardgame.Positionable;
import ch.asynk.gdx.boardgame.Rotable;
import ch.asynk.gdx.boardgame.Scalable;
import ch.asynk.gdx.boardgame.Tile;

public class Piece extends Sprite implements Drawable, Positionable, Rotable, Scalable
{
    public Piece(Texture texture)
    {
        super(texture);
    }

    @Override public float getScale()
    {
        return getScaleX();
    }

    @Override public void centerOn(float cx, float cy)
    {
        setPosition((cx - (getWidth() / 2f)), (cy - (getHeight() / 2f)));
    }

    public void getPosOn(Tile tile, Orientation orientation, Vector3 v)
    {
        v.set((tile.x - (getWidth() / 2f)), (tile.y- (getHeight() / 2f)), orientation.r());
    }

    public void setPosition(float x, float y, float r)
    {
        setPosition(x, y);
        setRotation(r);
    }

    public boolean isOn(Tile tile)
    {
        return (
                (Math.abs(getX() - (tile.x - (getWidth() / 2f))) < 3) &&
                (Math.abs(getY() - (tile.y- (getHeight() / 2f))) < 3)
                );
    }

    public boolean isFacing(Orientation orientation)
    {
        return (Orientation.fromR(getRotation()) == orientation);
    }

    public void getShootingPoint(Vector2 v, Piece target)
    {
        float x0 = getX();
        float y0 = getY();
        float x1 = target.getX();
        float y1 = target.getY();

        float r = (float) (MathUtils.atan2(y1 - y0, x1 - x0));
        x0 += (Math.cos(r) + 1f) * (getWidth() / 2f);
        y0 += (Math.sin(r) + 1f) * (getHeight() / 2f);

        v.set(x0, y0);
    }

    public void getImpactPoint(Vector2 v)
    {
        v.set(getX()+ (getWidth() / 2f), getY() + (getHeight() / 2f));
    }
}