summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/engine/HeadedPawn.java
blob: 9b4ed83ed968fd8e4a49178cc4e1dd94740f49f4 (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
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;
    protected Orientation orientation;

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

    public HeadedPawn(Faction faction, AtlasRegion chit, AtlasRegion body, AtlasRegion turret, TextureAtlas overlays)
    {
        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;
    }

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

    @Override
    public boolean canAim()
    {
        return (turret != null);
    }

    @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 getTurretRotation()
    {
        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 setTurretRotation(float r)
    {
        if (canAim()) {
            turretR = r;
            turret.setRotation(body.getRotation() + turretR);
        }
    }

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

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

    @Override
    public void drawDebug(ShapeRenderer debugShapes)
    {
        float w = sprite.getWidth();
        float h = sprite.getHeight();
        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());
        }
        overlays.drawDebug(debugShapes);
    }
}