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

import java.util.Random;

import com.badlogic.gdx.utils.Disposable;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
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.gfx.Image;
import ch.asynk.tankontank.engine.gfx.Drawable;
import ch.asynk.tankontank.game.hud.Msg;

public class Player extends ch.asynk.tankontank.engine.Player implements Drawable, Disposable
{
    private static final float MOVE_TIME = 0.4f;

    private static Random rand = new Random();

    private int turn;
    private int apSpent;
    private int actionPoints;
    private Image flag;
    private Msg status;

    public Player(final TankOnTank game, Army army, BitmapFont font, TextureAtlas atlas, String name, int n)
    {
        super(army, n);
        this.turn = 0;
        this.actionPoints = 0;
        this.flag = new Image(atlas.findRegion(name));
        this.status = new Msg(font, atlas.findRegion("disabled"));
    }

    public String toString()
    {
        return faction + " AP: " + actionPoints +
            " units:" + units.size() + " casualties:" + casualties.size() + " reinforcement:" + reinforcement.size();
    }

    @Override
    public void dispose()
    {
        flag.dispose();
    }

    public boolean apExhausted()
    {
        return (apSpent == actionPoints);
    }

    public void burnDownOneAp()
    {
        apSpent += 1;
        updateInfo();
        if (apSpent > actionPoints) System.err.println("ERROR: spent too much AP, please report");
    }

    @Override
    public void turnEnd()
    {
    }

    @Override
    public void turnStart()
    {
        turn += 1;
        for (Pawn pawn : units)
            pawn.reset();
        computeActionPoints();
        updateInfo();
    }

    public int d6()
    {
        return rand.nextInt(6) + 1;
    }

    private void computeActionPoints()
    {
        this.actionPoints = 2;
        if (d6() > 2) {
            this.actionPoints += 1;
            if (d6() > 3)
                this.actionPoints += 1;
        }
        apSpent = 0;
    }

    private void updateInfo()
    {
        status.write("Turn: " + turn + " AP: " + (apSpent + 1), flag.getX(), (flag.getY() - 40), 0, 10);
    }

    public boolean canPromote(Pawn pawn)
    {
        if (pawn.isHq()) return false;
        for (Pawn p: casualties)
            if (p.isHqOf(pawn)) return true;
        return false;
    }

    public Pawn promote(Pawn pawn)
    {
        for (Pawn p: casualties) {
            if (p.isHqOf(pawn)) {
                units.remove(pawn);
                casualties.add(pawn);
                units.add(p);
                casualties.remove(p);
                return p;
            }
        }
        return null;
    }

    public boolean contains(float x, float y)
    {
        return flag.contains(x, y);
    }

    public void setTopLeft(float x, float y)
    {
        flag.setPosition(x, (y - flag.getHeight()));
    }

    @Override
    public void draw(Batch batch)
    {
        flag.draw(batch);
        status.draw(batch);
    }

    @Override
    public void drawDebug(ShapeRenderer debugShapes)
    {
        flag.drawDebug(debugShapes);
    }
}