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

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

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);

    protected abstract void setup();

    public Map(Board.Config cfg, Texture texture, TextureAtlas hexAtlas)
    {
        super(cfg, texture, new Hex(hexAtlas));
        setup();
    }

    protected Hex getHex(int col, int row)
    {
        return (Hex) board[row][col];
    }

    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);
        }
    }

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

    private void debugMap()
    {
        if (hexOn && (t == Hex.Terrain.CLEAR)) {
            hexOn = false;
        } else {
            hexOn = true;
            if (roadsOn) {
                roadsOn = false;
                t = Hex.Terrain.CLEAR;
            }
            else if (t == Hex.Terrain.CLEAR)
                t = Hex.Terrain.WOODS;
            else if (t == Hex.Terrain.WOODS)
                t = Hex.Terrain.HILLS;
            else if (t == Hex.Terrain.HILLS)
                t = Hex.Terrain.TOWN;
            else if (t == Hex.Terrain.TOWN)
                roadsOn = true;
        }

        boolean evenRow = true;
        for (int j = 0; j < cfg.rows; j++) {
            int c = (evenRow ? cfg.cols : cfg.cols - 1);
            for (int i = 0; i < c; i++) {
                Hex hex = getHex(i,j);
                clearOverlaysOn(i, j);
                if (hexOn) {
                    if (roadsOn) {
                        if (hex.roads != 0)
                            enableOverlayOn(i, j, 1, true);
                    } else if (hex.terrain == t)
                        enableOverlayOn(i, j, 1, true);
                }
            }
            evenRow = !evenRow;
        }
    }
}