summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/engine/Tile.java
blob: f47f33f9a619d2c29f003f84bccaaefe8b199228 (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
package ch.asynk.rustanddust.engine;

import java.util.Iterator;
import java.util.ArrayDeque;

import com.badlogic.gdx.utils.Disposable;

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

import ch.asynk.rustanddust.engine.gfx.Drawable;
import ch.asynk.rustanddust.engine.gfx.StackedImages;

public abstract class Tile implements Drawable, Disposable, Iterable<Pawn>
{
    public interface TileTerrain
    {
    }

    public enum Objective
    {
        NONE,
        PERSISTENT,
        VERSATILE,
        FINAL
    }

    protected int col;
    protected int row;
    protected float x;
    protected float y;
    private StackedImages overlays;
    protected ArrayDeque<Pawn> stack;

    protected Faction curFaction;
    protected Faction prevFaction;
    protected Objective objective;

    public abstract int defense();
    public abstract int exitCost();
    public abstract int costFrom(Pawn pawn, Orientation side);

    public abstract boolean isOffMap();
    public abstract boolean isA(TileTerrain terrain);
    public abstract boolean road(Orientation side);
    public abstract boolean atLeastOneMove(Pawn pawn);
    public abstract boolean blockLineOfSight(Tile from, Tile to);

    protected Tile(int col, int row, Faction defaultFaction)
    {
        this.col = col;
        this.row = row;
        this.stack = new ArrayDeque<Pawn>();
        this.curFaction = defaultFaction;
        this.prevFaction = defaultFaction;
        this.objective = Objective.NONE;
    }

    public Tile(float x, float y, int col, int row, TextureAtlas atlas, Faction defaultFaction)
    {
        this(col, row, defaultFaction);
        this.x = x;
        this.y = y;
        this.overlays = new StackedImages(atlas);
        this.overlays.centerOn(x, y);
    }

    public float getX() { return x; }
    public float getY() { return y; }
    public int getCol() { return col; }
    public int getRow() { return row; }

    @Override
    public String toString()
    {
        return String.format("(%d;%d) %s", col, row, (isOffMap() ? "x" : ""));
    }

    public String toShort()
    {
        return String.format("(%d;%d)", col, row);
    }

    @Override
    public void dispose()
    {
        stack.clear();
        overlays.dispose();
    }

    // STACK

    public boolean isEmpty()
    {
        return stack.isEmpty();
    }

    public Iterator<Pawn> iterator()
    {
        return stack.iterator();
    }

    public int push(Pawn pawn)
    {
        if (!stack.contains(pawn))
            stack.push(pawn);
        return stack.size();
    }

    public int remove(Pawn pawn)
    {
        stack.remove(pawn);
        return stack.size();
    }

    private Pawn getTopPawn()
    {
        if (isEmpty()) return null;
        return stack.getFirst();
    }

    public boolean hasUnits()
    {
        if (isEmpty()) return false;
        Iterator<Pawn> itr = iterator();
        while(itr.hasNext()) {
            if (itr.next().isUnit())
                return true;
        }
        return false;
    }

    // OBJECTIVE

    public void setObjective(Faction faction, Objective objective)
    {
        this.curFaction = faction;
        this.prevFaction = faction;
        this.objective = objective;
    }

    public boolean isPersistent()
    {
        return ((objective == Objective.PERSISTENT) || (objective == Objective.FINAL));
    }

    public boolean isFinal()
    {
        return (objective == Objective.FINAL);
    }

    public Faction belongsTo()
    {
        return curFaction;
    }

    public boolean belongsTo(Faction faction)
    {
        return (faction == curFaction);
    }

    public boolean isObjective()
    {
        return (objective != Objective.NONE);
    }

    public boolean isOwnedObjective(Faction faction)
    {
        return (isObjective() && belongsTo(faction));
    }

    public boolean isObjectiveFor(Pawn pawn)
    {
        if (!isObjective())
            return false;

        if (belongsTo(pawn.getFaction()))
            return false;

        return isPersistent();
    }

    public boolean claim(Faction faction)
    {
        if (belongsTo(faction))
            return false;

        if (isFinal() && (curFaction != prevFaction))
            return false;

        prevFaction = curFaction;
        curFaction = faction;
        return true;
    }

    public boolean unclaim()
    {
        if (isPersistent())
            return false;
        revertClaim();
        return true;
    }

    public Faction revertClaim()
    {
        curFaction = prevFaction;
        return curFaction;
    }

    // OVERLAYS

    public boolean mustBeDrawn()
    {
        if (!isEmpty()) return true;
        return hasOverlayEnabled();
    }

    public boolean disableOverlays()
    {
        overlays.disableAll();
        return !isEmpty();
    }

    public boolean hasOverlayEnabled()
    {
        return overlays.isEnabled();
    }

    public boolean isOverlayEnabled(int i)
    {
        return overlays.isEnabled(i);
    }

    public boolean enableOverlay(int i, boolean enable)
    {
        if (i >= 0) {
            overlays.enable(i, enable);
            if (enable) return true;
        }
        return mustBeDrawn();
    }

    public boolean enableOverlay(int i, boolean enable, float r)
    {
        if (i >= 0) {
            overlays.enable(i, enable);
            overlays.rotate(i, r);
            if (enable) return true;
        }
        return mustBeDrawn();
    }

    @Override
    public void draw(Batch batch)
    {
        overlays.draw(batch);
        Pawn pawn = getTopPawn();
        if (pawn != null)
            pawn.draw(batch);
    }

    @Override
    public void drawDebug(ShapeRenderer debugShapes)
    {
        overlays.drawDebug(debugShapes);
        Pawn pawn = getTopPawn();
        if (pawn != null)
            pawn.drawDebug(debugShapes);
    }
}