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

import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;

import ch.asynk.tankontank.engine.Order;
import ch.asynk.tankontank.engine.Move;
import ch.asynk.tankontank.engine.Pawn;

public  class Command extends Order
{
    public enum CommandType implements Order.OrderType
    {
        NONE,
        MOVE,
        ENGAGE,
        PROMOTE,
        END_OF_TURN;
    }

    private static final Pool<Command> commandPool = new Pool<Command>()
    {
        @Override
        protected Command newObject() {
            return new Command();
        }
    };

    public static void clearPool()
    {
        commandPool.clear();
    }

    public static Command get(Player player)
    {
        Command c = commandPool.obtain();
        c.player = player;
        return c;
    }

    public CommandType type;
    public Player player;
    public Unit unit;
    public Move move;
    public Engagement engagement;

    private Command()
    {
        reset();
    }

    @Override
    public void dispose()
    {
        commandPool.free(this);
    }

    @Override
    public void reset()
    {
        this.type = CommandType.NONE;
        this.player = null;
        this.unit = null;
        if (this.move != null) {
            this.move.dispose();
            this.move = null;
        }
        if (this.engagement != null) {
            this.engagement.dispose();
            this.engagement = null;
        }
    }

    @Override
    public int compareTo(Pawn pawn)
    {
        if (pawn == unit)
            return 0;
        return 1;
    }

    @Override
    public boolean isA(OrderType type)
    {
        return (type == this.type);
    }

    @Override
    public String toString()
    {
        return String.format("%s : %s", type, unit.id);
    }

    public void setMove(Unit unit, Move move)
    {
        this.type = CommandType.MOVE;
        this.unit = unit;
        this.move = move;
    }

    public void setPromote(Unit unit)
    {
        this.type = CommandType.PROMOTE;
        this.unit = unit;
    }

    public void setEngage(Unit unit, Unit target)
    {
        this.type = CommandType.ENGAGE;
        this.unit = unit;
        this.engagement = Engagement.get(unit, target);
    }

    @Override
    public void write(Json json)
    {
        // FIXME Command.write(Json);
    }

    @Override
    public void read(Json json, JsonValue jsonMap)
    {
        // FIXME Command.read(Json, JsonValue);
    }
}