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

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

import ch.asynk.tankontank.engine.Board;
import ch.asynk.tankontank.engine.Pawn;

public abstract class Map extends Board
{
    private boolean roadsOn = false;
    private boolean hexOn = false;
    private Hex.Terrain t = Hex.Terrain.CLEAR;

    private Pawn currentPawn;
    private GridPoint2 currentHex = new GridPoint2(-1, -1);
    private GridPoint2 lineHex = new GridPoint2(-1, -1);
    private Vector2 line0 = new Vector2(0, 0);
    private Vector2 line1 = new Vector2(0, 0);

    protected abstract void setup();

    public Map(GameFactory gameFactory, Board.Config cfg, Texture texture)
    {
        super(gameFactory, cfg, texture);
        setup();
    }

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

    public boolean drag(float dx, float dy)
    {
        if (currentPawn == null) return false;
        currentPawn.translate(dx, dy);
        return true;
    }

    public void touchDown(float x, float y)
    {
        getHexAt(currentHex, x, y);
        if (currentHex.x != -1) {
            currentPawn = removeTopPawnFrom(currentHex);
            if (currentPawn != null) pawnsToDraw.add(currentPawn);
            line0 = getTileCenter(currentHex.x, currentHex.y);
        }
    }

    public void touchUp(float x, float y)
    {
        getHexAt(currentHex, x, y);
        if (currentPawn != null) {
            pawnsToDraw.remove(currentPawn);
            movePawnTo(currentPawn, currentHex);
            currentPawn = null;
        } else {
            debugMap();
        }
    }

    public void showMoves(float x, float y)
    {
        for(GridPoint2 hex : areaPoints)
            enableOverlayOn(hex.x, hex.y, Hex.GREEN, false);

        getHexAt(currentHex, x, y);
        Pawn pawn = getTopPawnAt(currentHex);
        if (pawn == null) return;
        for(GridPoint2 hex : reachableFrom(pawn, currentHex.x, currentHex.y))
            enableOverlayOn(hex.x, hex.y, Hex.GREEN, true);
        for(GridPoint2 hex : openToAttackFrom(pawn, currentHex.x, currentHex.y))
            enableOverlayOn(hex.x, hex.y, Hex.RED, true);
    }

    public void lineOfSight(float x, float y)
    {
        getHexAt(lineHex, x, y);
        line1 = getTileCenter(lineHex.x, lineHex.y);

        for(GridPoint2 hex : areaPoints)
            enableOverlayOn(hex.x, hex.y, Hex.RED, false);

        for(GridPoint2 hex : lineOfSight(currentHex.x, currentHex.y, lineHex.x, lineHex.y))
            enableOverlayOn(hex.x, hex.y, Hex.RED, true);
    }

    @Override
    public void drawDebug(ShapeRenderer debugShapes)
    {
        super.drawDebug(debugShapes);
        debugShapes.line(line0.x, line0.y, line1.x, line1.y);
    }

    private void debugMap()
    {
        int o = Hex.FOG;
        if (hexOn && (t == Hex.Terrain.CLEAR)) {
            hexOn = false;
        } else {
            hexOn = true;
            if (roadsOn) {
                roadsOn = false;
                t = Hex.Terrain.OFFMAP;
            } else if (t == Hex.Terrain.CLEAR) {
                o = Hex.GREEN;
                t = Hex.Terrain.WOODS;
            } else if (t == Hex.Terrain.WOODS) {
                o = Hex.BLUE;
                t = Hex.Terrain.HILLS;
            } else if (t == Hex.Terrain.HILLS) {
                o = Hex.RED;
                t = Hex.Terrain.TOWN;
            } else if (t == Hex.Terrain.TOWN) {
                o = Hex.FOG;
                roadsOn = true;
            } else if (t == Hex.Terrain.OFFMAP) {
                o = Hex.FOG;
                t = Hex.Terrain.CLEAR;
            }
        }

        for (int j = 0; j < cfg.rows; j++) {
            int colOffset = ((j +1) / 2);
            for (int i = colOffset; i < (cfg.cols + colOffset); i++) {
                Hex hex = getHex(i,j);
                disableOverlaysOn(i, j);
                if (hexOn) {
                    if (roadsOn) {
                        if (hex.roads != 0)
                            enableOverlayOn(i, j, o, true);
                    } else if (hex.terrain == t)
                        enableOverlayOn(i, j, o, true);
                }
            }
        }
    }
}