summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/engine/HeadedPawn.java
blob: faac25a604fe2e3db92877a1db84d3a3b228fd2e (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package ch.asynk.rustanddust.engine;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public abstract class HeadedPawn extends Pawn
{
    private Sprite turret;
    private Sprite body;
    private float turretR;
    private boolean canAim;
    private boolean selectable;
    private boolean selected;
    protected Orientation orientation;

    protected HeadedPawn()
    {
        super();
        this.orientation = Orientation.KEEP;
    }

    public HeadedPawn(Faction faction, AtlasRegion chit, AtlasRegion body, AtlasRegion turret, TextureAtlas overlays, boolean selectable)
    {
        super(faction, chit, overlays);
        this.body = new Sprite(body);
        this.turret = ((turret == null) ? null : new Sprite(turret));
        this.turretR = 0f;
        this.orientation = Orientation.KEEP;
        this.descr += " " + orientation;
        this.canAim = (turret != null);
        this.selectable = selectable;
        this.selected = !selectable;
    }

    public void select(boolean s)
    {
        if (!selectable) return;
        this.selected = s;
    }

    @Override
    public void dispose()
    {
        super.dispose();
    }

    @Override
    public boolean canAim()
    {
        return canAim;
    }

    @Override
    public void setAlpha(float alpha)
    {
        super.setAlpha(alpha);
        body.setAlpha(alpha);
        if (canAim) turret.setAlpha(alpha);
    }

    @Override
    public float getRotation()
    {
        return orientation.r();
    }

    @Override
    public Orientation getOrientation()
    {
        return orientation;
    }

    @Override
    public float getAiming()
    {
        return turretR;
    }

    @Override
    public void setPosition(float x, float y)
    {
        super.setPosition(x, y);
        float cx = x + (getWidth() / 2f);
        float cy = y + (getHeight() / 2f);
        body.setPosition((cx - (body.getWidth() / 2f)), (cy - (body.getHeight() / 2f)));
        if (canAim) turret.setPosition((cx - (turret.getWidth() / 2f)), (cy - (turret.getHeight() / 2f)));
    }

    @Override
    public void setRotation(float z)
    {
        getPosition().z = z;
        body.setRotation(z);
        if (canAim) turret.setRotation(z + turretR);
        this.orientation = Orientation.fromRotation(z);
    }

    @Override
    public void aimAt(float r)
    {
        if (canAim)
            turret.setRotation(body.getRotation() + r);
        else {
            float d = (r - turretR);
            body.setRotation(body.getRotation() + d);
        }
        turretR = r;
    }

    @Override
    public void setPosition(float x, float y, float z)
    {
        setPosition(x, y);
        setRotation(z);
    }

    @Override
    public void draw(Batch batch)
    {
        if (selected) sprite.draw(batch);
        body.draw(batch);
        if (canAim) turret.draw(batch);
        if (selected) overlays.draw(batch);
    }

    @Override
    public void drawDebug(ShapeRenderer debugShapes)
    {
        float w = sprite.getWidth();
        float h = sprite.getHeight();
        if (selected) debugShapes.rect(sprite.getX(), sprite.getY(), (w / 2f), (h / 2f), w, h, sprite.getScaleX(), sprite.getScaleY(), sprite.getRotation());
        w = body.getWidth();
        h = body.getHeight();
        debugShapes.rect(body.getX(), body.getY(), (w / 2f), (h / 2f), w, h, body.getScaleX(), body.getScaleY(), body.getRotation());
        if (canAim) {
            w = turret.getWidth();
            h = turret.getHeight();
            debugShapes.rect(turret.getX(), turret.getY(), (w / 2f), (h / 2f), w, h, turret.getScaleX(), turret.getScaleY(), turret.getRotation());
        }
        if (selected) overlays.drawDebug(debugShapes);
    }
}