summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/game/Unit.java
blob: 7894f811700d1006a7de83d632dea19d2d38a1fd (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package ch.asynk.rustanddust.game;

import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;

import ch.asynk.rustanddust.engine.Pawn;
import ch.asynk.rustanddust.engine.Tile;
import ch.asynk.rustanddust.engine.HeadedPawn;
import ch.asynk.rustanddust.game.Hex.Terrain;

import ch.asynk.rustanddust.RustAndDust;

public class Unit extends HeadedPawn
{
    public static final int ACTIVEABLE  = 0;
    public static final int TARGET      = 1;
    public static final int FIRE        = 2;
    public static final int MAY_FIRE    = 3;
    public static final int ACE         = 4;
    public static final int HQ          = 5;
    public static final int HAS_FIRED   = 6;
    public static final int HAS_MOVED   = 7;

    public static final int FLANK_ATTACK_BONUS = 1;

    public static int unit_id = 1;

    public enum UnitType implements Pawn.PawnType
    {
        HARD_TARGET,
        INFANTRY,
        AT_GUN,
        ARTILLERY
    }

    public enum UnitCode implements Pawn.PawnCode
    {
        GE_AT_GUN("German Anti-Tank Gun"),
        GE_INFANTRY("German Infantry"),
        GE_KINGTIGER("German King Tiger"),
        GE_PANZER_IV("German Panzer IV"),
        GE_TIGER("German Tiger"),
        GE_WESPE("German Wespe"),

        US_AT_GUN("US Anti-Tank Gun"),
        US_INFANTRY("US Infantry"),
        US_PERSHING("US Pershing"),
        US_PRIEST("US Priest"),
        US_SHERMAN("US Sherman"),
        US_WOLVERINE("US Wolverine");

        private String s;
        UnitCode(String s) { this.s = s; }
        public String toString() { return s; }
    }

    public int rng;
    public int def;
    public int cdef;
    public int mp;
    public int mpLeft;
    public int id;
    public UnitCode code;
    public UnitType type;
    public boolean hq;
    public boolean ace;
    public boolean hasMoved;
    public boolean hasFired;
    public Zone entryZone;
    public Zone exitZone;

    protected Unit(Army army, AtlasRegion chit, AtlasRegion body, AtlasRegion turret, TextureAtlas overlays)
    {
        super(army, chit, body, turret, overlays);
        hq = false;
        ace = false;
        this.id = unit_id;
        unit_id += 1;
        this.entryZone = null;
        this.exitZone = null;
    }

    private void commonSetup()
    {
        mpLeft = mp;
        enableOverlay(HQ, isHq());
        enableOverlay(ACE, isAce());
        this.hasMoved = false;
        this.hasFired = false;
        updateDescr();
    }

    private void updateDescr()
    {
        this.descr = String.format("[%d] %s%s%s (%d - %d", id, code.toString(), (hq ? " HQ" : ""), (ace ? " Ace" : ""), rng, def);
        if (cdef == -1)
            this.descr += "-" + mp + ")";
        else
            this.descr += "/" + cdef + "-" + mp + ")";
    }

    public Unit(Army army, UnitCode code, UnitType type, boolean hq, boolean ace, int range, int defense, int concealedDefense, int movementPoints,
            AtlasRegion chit, AtlasRegion body, AtlasRegion turret, TextureAtlas overlays)
    {
        this(army, chit, body, turret, overlays);
        this.hq = hq;
        this.ace = ace;
        this.rng = range;
        this.def = defense;
        this.cdef = concealedDefense;
        this.mp = movementPoints;
        this.code = code;
        this.type = type;
        commonSetup();
    }

    public Army getArmy()
    {
        return (Army) getFaction();
    }

    public Hex getHex()
    {
        return (Hex) getTile();
    }

    public boolean isAce()
    {
        return ace;
    }

    @Override
    public int getMovementPoints()
    {
        return mpLeft;
    }

    @Override
    public int getRoadMarchBonus()
    {
        return 1;
    }

    @Override
    public int getEngagementRangeFrom(Tile tile)
    {
        if (tile.isA(Terrain.DEPRESSION))
            return 1;
        if (!isA(UnitType.INFANTRY) && tile.isA(Terrain.HILLS))
            return rng + 1;
        return rng;
    }

    @Override
    public int getAngleOfAttack()
    {
        return orientation.getFrontSides();
    }

    @Override
    public int getFlankSides()
    {
        return orientation.getBackSides();
    }

    @Override
    public int getDefense(Tile tile)
    {
        if (!isHardTarget() && (tile.isA(Terrain.HILLS) || tile.isA(Terrain.WOODS) || tile.isA(Terrain.TOWN)))
            return cdef;

        return def;
    }

    @Override
    public boolean preventDefenseOn(Tile tile)
    {
        if (isA(UnitType.INFANTRY) && (tile.isA(Terrain.WOODS) || tile.isA(Terrain.TOWN)))
            return true;

        return false;
    }

    @Override
    public boolean isUnit()
    {
        return true;
    }

    @Override
    public boolean isA(PawnCode c)
    {
        return (code == c);
    }

    @Override
    public boolean isA(PawnType t)
    {
        return (type == t);
    }

    @Override
    public boolean isHardTarget()
    {
        return (isA(UnitType.HARD_TARGET) || isA(UnitType.ARTILLERY));
    }

    @Override
    public boolean isHq()
    {
        return hq;
    }

    @Override
    public boolean isHqOf(Pawn other)
    {
        return (isHq() && other.isA(code));
    }

    public void promote()
    {
        setHq(true);
    }

    public void degrade()
    {
        setHq(false);
    }

    private void setHq(boolean hq)
    {
        this.hq = hq;
        updateDescr();
        enableOverlay(HQ, hq);
    }

    @Override
    public boolean canRotate()
    {
        return canMove();
    }

    @Override
    public boolean canMove()
    {
        if (isHardTarget()) return !hasMoved;
        return (!hasMoved && !hasFired);
    }

    @Override
    public boolean canEngage()
    {
        if (isHardTarget()) return !hasFired;
        return (!hasMoved && !hasFired);
    }

    @Override
    public boolean canAssistEngagementWithoutLos()
    {
        return isA(UnitType.ARTILLERY);
    }

    @Override
    public boolean canEngage(Pawn other)
    {
        return (isEnemy(other) && canEngage());
    }

    @Override
    public boolean canBreak()
    {
        return isA(UnitType.INFANTRY);
    }

    public boolean canHQMove()
    {
        return (isHq() && ((move == null) || (!move.isEnter())));
    }

    public void setMoved()
    {
        hasMoved = true;
        updateOverlays();
    }

    public void setFired()
    {
        hasFired = true;
        updateOverlays();
    }

    @Override
    public void move()
    {
        int cost = move.cost;

        if (move.roadMarch && (cost > mpLeft))
            cost -= getRoadMarchBonus();

        if (cost > mpLeft)
            RustAndDust.debug("ERROR: Movement point exceeded: " + cost + "/" + mpLeft + " please report");

        if (move.isFinal())
            setMoved();

        mpLeft -= cost;
    }

    @Override
    public void engage()
    {
        setFired();
    }

    @Override
    public void reset()
    {
        super.reset();
        mpLeft = mp;
        hasFired = false;
        hasMoved = false;
        hideHasMoved();
        hideHasFired();
    }

    @Override
    public void revertLastMove()
    {
        hasMoved = false;
        mpLeft = mp;
        updateOverlays();
        move = null;
    }

    private void updateOverlays()
    {
        enableOverlay(HAS_MOVED, !canMove());
        enableOverlay(HAS_FIRED, !canEngage());
    }

    // SHOW / HIDE
    public void showActiveable()    { enableOverlay(ACTIVEABLE, true); }
    public void hideActiveable()    { enableOverlay(ACTIVEABLE, false); }
    public void showTarget()        { enableOverlay(TARGET, true); }
    public void hideTarget()        { enableOverlay(TARGET, false); }
    public void showAttack()        { enableOverlay(FIRE, true); }
    public void hideAttack()        { enableOverlay(FIRE, false); }
    public void showAttackAssist()  { enableOverlay(MAY_FIRE, true); }
    public void hideAttackAssist()  { enableOverlay(MAY_FIRE, false); }
    public void showHasMoved()      { enableOverlay(HAS_MOVED, true); }
    public void hideHasMoved()      { enableOverlay(HAS_MOVED, false); }
    public void showHasFired()      { enableOverlay(HAS_FIRED, true); }
    public void hideHasFired()      { enableOverlay(HAS_FIRED, false); }
}