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

import java.util.ArrayList;
import java.util.HashSet;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import ch.asynk.tankontank.engine.Pawn;
import ch.asynk.tankontank.engine.Board;
import ch.asynk.tankontank.engine.Orientation;
import ch.asynk.tankontank.engine.gfx.animations.RunnableAnimation;

public abstract class Map extends Board
{
    private final Ctrl ctrl;

    private final ArrayList<Vector3> finalPath = new ArrayList<Vector3>(10);
    private final ArrayList<GridPoint2> possibleMoves = new ArrayList<GridPoint2>(40);
    private final ArrayList<GridPoint2> possibleTargets = new ArrayList<GridPoint2>(10);
    private final HashSet<GridPoint2> possiblePaths = new HashSet<GridPoint2>(10);
    private final ArrayList<GridPoint2> moveAssists = new ArrayList<GridPoint2>(6);

    private final ArrayList<Pawn> activablePawns = new ArrayList<Pawn>(7);
    private final ArrayList<Pawn> activatedPawns = new ArrayList<Pawn>(7);

    protected abstract void setup();

    public Map(Ctrl ctrl, Factory factory, Board.Config cfg, Texture texture)
    {
        super(factory, cfg, texture);
        this.ctrl = ctrl;
        setup();
    }

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

    public void clearAll()
    {
        moveAssists.clear();
        activablePawns.clear();
        activatedPawns.clear();
        clearPointSet(possiblePaths);
        clearPointVector(possibleMoves);
        clearPointVector(possibleTargets);
        clearCoordinateVector(finalPath);
    }

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

    protected Hex getHexSafe(GridPoint2 hex)
    {
        return (Hex) getTileSafe(hex.x, hex.y);
    }

    public GridPoint2 getFirstMoveAssist()
    {
        return moveAssists.get(0);
    }

    public int activablePawnsCount()
    {
        return activablePawns.size();
    }

    public int activatedPawnsCount()
    {
        return activatedPawns.size();
    }

    public boolean isInPossibleMoves(GridPoint2 hex)
    {
        return possibleMoves.contains(hex);
    }

    public boolean isInPossibleMoveAssists(GridPoint2 hex)
    {
        return moveAssists.contains(hex);
    }

    public boolean isInPossiblePaths(GridPoint2 hex)
    {
        return possiblePaths.contains(hex);
    }

    public boolean isInPossibleTargets(GridPoint2 hex)
    {
        return possibleTargets.contains(hex);
    }

    public void showPossibleMoves(boolean enable)
    {
        for(GridPoint2 hex : possibleMoves)
            enableOverlayOn(hex, Hex.MOVE1, enable);
    }

    public void showMoveAssists(boolean enable)
    {
        for(GridPoint2 hex : moveAssists)
            enableOverlayOn(hex, Hex.ASSIST, enable);
    }

    public void showPossibleTargets(boolean enable)
    {
        for(GridPoint2 hex : possibleTargets)
            enableOverlayOn(hex, Hex.TARGET, enable);
    }

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

    public void showFinalPath(GridPoint2 dst, boolean enable)
    {
        for(GridPoint2 hex : possiblePaths) {
            enableOverlayOn(hex, Hex.MOVE1, false);
            enableOverlayOn(hex, Hex.MOVE2, enable);
        }
    }

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

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

    public int possiblePathsSize()
    {
        return possiblePaths.size();
    }

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

    public int buildPossibleMoves(Pawn pawn, GridPoint2 hex)
    {
        buildPossibleMovesFrom(pawn, hex, possibleMoves);
        return possibleMoves.size();
    }

    public int buildPossibleTargets(Pawn pawn, GridPoint2 hex)
    {
        buildPossibleTargetsFrom(pawn, hex, possibleTargets);
        return possibleTargets.size();
    }

    public int buildMoveAssists(Pawn pawn, GridPoint2 hex)
    {
        if (pawn.isHq()) {
            buildMoveAssists(pawn, hex, moveAssists);
        } else {
            moveAssists.clear();
        }
        return moveAssists.size();
    }

    public void buildAndShowMovesAndAssits(Pawn pawn, GridPoint2 hex)
    {
        showPossibleMoves(false);
        showMoveAssists(false);
        activablePawns.clear();
        activatedPawns.clear();
        buildPossibleMoves(pawn, hex);
        buildMoveAssists(pawn, hex);
        activablePawns.add(pawn);
        for (GridPoint2 p : moveAssists)
            activablePawns.add(getTopPawnAt(p));
        showPossibleMoves(true);
        showMoveAssists(true);
    }

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

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

    public void clearPossiblePaths()
    {
        clearPointSet(possiblePaths);
    }

    public int movePawn(Pawn pawn, GridPoint2 from, Orientation o)
    {
        System.err.println(" movePawn : " + from.x + ";" + from.y + " " + o);
        int cost = getPathCost(pawn, 0);
        int s = getCoordinatePath(pawn, 0, finalPath, o);
        if (s > 0) {
            movePawn(pawn, cost, finalPath, RunnableAnimation.get(pawn, new Runnable() {
                @Override
                public void run() {
                    ctrl.animationDone();
                }
            }));
        }

        return finishMove(pawn, from);
    }

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

        return finishMove(pawn, from);
    }

    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 finishMove(Pawn pawn, GridPoint2 from) {
        moveAssists.remove(from);
        activablePawns.remove(pawn);
        activatedPawns.add(pawn);
        return activablePawns.size();
    }
}