summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/game/Map.java
blob: 0bdd46b87682b3fceb9febffb82b609c3eb9e998 (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
package ch.asynk.tankontank.game;

import java.util.ArrayList;
import java.util.Iterator;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import ch.asynk.tankontank.TankOnTank;
import ch.asynk.tankontank.engine.Pawn;
import ch.asynk.tankontank.engine.PawnSet;
import ch.asynk.tankontank.engine.TileSet;
import ch.asynk.tankontank.engine.Board;
import ch.asynk.tankontank.engine.Orientation;
import ch.asynk.tankontank.engine.gfx.animations.AnimationSequence;
import ch.asynk.tankontank.engine.gfx.animations.SpriteAnimation;
import ch.asynk.tankontank.engine.gfx.animations.RunnableAnimation;


public abstract class Map extends Board
{
    public class HexSet extends TileSet
    {
        public HexSet(Map map, int overlay, int n)
        {
            super(map, overlay, n);
        }
    }

    public class UnitSet extends PawnSet
    {
        public UnitSet(Map map, int overlay, int n)
        {
            super(map, overlay, n);
        }
    }

    private final Ctrl ctrl;

    public final Board.TileCollection possibleMoves;
    public final Board.TileCollection possiblePaths;
    public final Board.PawnCollection moveablePawns;
    public final Board.PawnCollection possibleTargets;
    public final Board.PawnCollection attackAssists;
    public final ArrayList<Pawn> activatedPawns = new ArrayList<Pawn>(7);

    private final SpriteAnimation explosion;
    private final SpriteAnimation explosions;

    protected abstract void setup();

    public Map(final TankOnTank game, Board.Config cfg, String textureName)
    {
        super(game.factory, cfg, game.manager.get(textureName, Texture.class));
        this.ctrl = game.ctrl;
        this.explosion = new SpriteAnimation(game.manager.get("data/explosion.png", Texture.class), 10, 4, 40);
        this.explosions = new SpriteAnimation(game.manager.get("data/explosions.png", Texture.class), 16, 8, 15);
        setup();

        possibleMoves = new HexSet(this, Hex.MOVE1, 40);
        possiblePaths = new HexSet(this, Hex.MOVE1, 10);        // Hex.MOVE2
        moveablePawns = new UnitSet(this, Unit.MOVE, 6);

        possibleTargets = new UnitSet(this, Unit.TARGET, 10);
        attackAssists = new UnitSet(this, Unit.ATTACK_ASSIST, 6);
    }

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

    public void clearAll()
    {
        possibleMoves.clear();
        possibleTargets.clear();
        possiblePaths.clear();
        moveablePawns.clear();
        attackAssists.clear();
        activatedPawns.clear();
    }

    public Hex getHexAt(float x, float y)
    {
        return (Hex) getTileAt(x, y);
    }

    public Hex getHex(int col, int row)
    {
        return (Hex) getTile(col, row);
    }

    public void selectHex(Hex hex, boolean enable)
    {
        enableOverlayOn(hex, Hex.SELECT, enable);
    }

    public void showAssist(Hex hex, boolean enable)
    {
        enableOverlayOn(hex, Hex.ASSIST, enable);
    }

    public void showTarget(Hex hex, boolean enable)
    {
        enableOverlayOn(hex, Hex.TARGET, enable);
    }

    public void showPossiblePaths(boolean enable, boolean keepFinal)
    {
        if (keepFinal) {
            possiblePaths.enable(Hex.MOVE1, enable);
        } else {
            possiblePaths.enable(Hex.MOVE1, enable);
            possiblePaths.enable(Hex.MOVE2, false);
        }
    }

    public void showFinalPath(Hex dst, boolean enable)
    {
        possiblePaths.enable(Hex.MOVE1, false);
        possiblePaths.enable(Hex.MOVE2, enable);
    }

    public void showDirections(Hex hex, boolean enable)
    {
        enableOverlayOn(hex, Hex.DIRECTIONS, enable);
    }

    public void showOrientation(Hex hex, boolean enable, Orientation o)
    {
        enableOverlayOn(hex, Hex.ORIENTATION, enable, o);
    }

    public void togglePathOverlay(Hex hex)
    {
        boolean enable= !hex.isOverlayEnabled(Hex.MOVE2);
        enableOverlayOn(hex, Hex.MOVE2, enable);
    }

    public int buildPossibleMoves(Pawn pawn)
    {
        if (!pawn.canMove()) {
            possibleMoves.clear();
            return 0;
        }
        return buildPossibleMoves(pawn, possibleMoves);
    }

    public int buildPossibleTargets(Pawn pawn, Iterator<Pawn> foes)
    {
        if (!pawn.canAttack()) {
            possibleTargets.clear();
            return 0;
        }
        // return collectPossibleTargets(pawn, possibleTargets);
        return collectPossibleTargets(pawn, foes, possibleTargets);
    }

    public int collectMoveablePawns(Pawn pawn)
    {
        if (pawn.isHq()) {
            collectMoveAssists(pawn, moveablePawns);
        } else {
            moveablePawns.clear();
        }
        if (pawn.canMove())
            moveablePawns.add(pawn);
        return moveablePawns.size();
    }

    public int buildAttackAssists(Pawn pawn, Pawn target, Iterator<Pawn> units)
    {
        int s = collectAttackAssists(pawn, target, units, attackAssists);
        activatedPawns.add(pawn);
        return s;
    }

    public boolean toggleAttackAssist(Pawn pawn)
    {
        if (activatedPawns.contains(pawn)) {
            activatedPawns.remove(pawn);
            return false;
        } else {
            activatedPawns.add(pawn);
            return true;
        }
    }

    public void buildAndShowMovesAndAssits(Pawn pawn)
    {
        possibleMoves.hide();
        moveablePawns.hide();
        buildPossibleMoves(pawn);
        collectMoveablePawns(pawn);
        possibleMoves.show();
        moveablePawns.show();
        activatedPawns.clear();
    }

    public int buildPossiblePaths(Pawn pawn, Hex to)
    {
        return buildPossiblePaths(pawn, to, possiblePaths);
    }

    public int possiblePathsPointToggle(Hex hex)
    {
        return possiblePathsFilterToggle(hex, possiblePaths);
    }

    public boolean attackPawn(Pawn pawn, final Pawn target, int dice)
    {
        int activatedUnits = activatedPawns.size();

        final boolean success;
        if (dice == 2) {
            success = false;
        } else if (dice == 12) {
            success = true;
        } else {
            int flankAttacks = 0;
            for (Pawn assist : activatedPawns) {
                if (assist.isFlankAttack()) {
                    flankAttacks = 1;
                    break;
                }
            }
            System.err.print(" + " + activatedUnits + " + " + flankAttacks);
            success = ((dice + activatedUnits + flankAttacks) >= target.getTile().defenseFor(target, activatedPawns));
        }

        AnimationSequence seq = AnimationSequence.get(2);
        if (success) {
            explosions.init(1, target.getCenter().x, target.getCenter().y);
            seq.addAnimation(explosions);
        } else {
            explosion.init(1, target.getCenter().x, target.getCenter().y);
            seq.addAnimation(explosion);
        }
        seq.addAnimation(RunnableAnimation.get(pawn, new Runnable() {
            @Override
            public void run() {
                if (success) {
                    removePawn(target);
                }
                ctrl.animationDone();
            }
        }));

        addAnimation(seq);

        for (Pawn p : activatedPawns)
            pawn.attack(target);
        if ((activatedPawns.size() == 1) && pawn.isA(Unit.UnitType.AT_GUN) && target.isHardTarget())
            activatedPawns.clear();

        return success;
    }

    public int movePawn(Pawn pawn, Orientation o)
    {
        System.err.println("    movePawn : " + pawn.getTile() + " " + o);
        int cost = getPathCost(pawn, 0);
        movePawn(pawn, cost, o, RunnableAnimation.get(pawn, new Runnable() {
            @Override
            public void run() {
                ctrl.animationDone();
            }
        }));

        return startMove(pawn);
    }

    public int rotatePawn(Pawn pawn, Orientation o)
    {
        System.err.println("    rotatePawn : " + pawn.getTile() + " " +o);
        rotatePawn(pawn, o, RunnableAnimation.get(pawn, new Runnable() {
            @Override
            public void run() {
                ctrl.animationDone();
            }
        }));

        return startMove(pawn);
    }

    public void revertMoves()
    {
        System.err.println("    revertMoves()");
        for (Pawn pawn : activatedPawns) {
            revertLastPawnMove(pawn, RunnableAnimation.get(pawn, new Runnable() {
                @Override
                public void run() {
                    ctrl.animationDone();
                }
            }));
        }
        activatedPawns.clear();
    }

    private int startMove(Pawn pawn)
    {
        moveablePawns.remove(pawn);
        activatedPawns.add(pawn);
        return moveablePawns.size();
    }

    public void promote(Pawn pawn, Pawn with)
    {
        removePawn(pawn);
        setPawnOnto(with, pawn.getTile(), pawn.getOrientation());
        activatedPawns.add(with);
    }
}