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

import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.utils.Disposable;

import ch.asynk.rustanddust.engine.Move;

public class Order implements Disposable, Pool.Poolable, Comparable<Unit>
{
    public static int orderId = 1;

    public static final Order END = new Order(OrderType.END);

    public enum OrderType
    {
        NONE,
        MOVE,
        ENGAGE,
        PROMOTE,
        END,
    }

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

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

    public static Order get()
    {
        Order o = orderPool.obtain();
        o.id = orderId;
        orderId += 1;
        return o;
    }

    public int id;
    public int cost;
    public boolean replay;
    public Unit leader;
    public OrderType type;
    public Move move;
    public Engagement engagement;
    public UnitList activables = new UnitList(4);

    private Order()
    {
        reset();
    }

    private Order(OrderType type)
    {
        this();
        this.type = type;
    }

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

    @Override
    public void reset()
    {
        this.id = -1;
        this.cost = 1;
        this.replay = false;
        this.leader = null;
        this.type = OrderType.NONE;
        this.activables.clear();
        if (this.move != null) {
            this.move.dispose();
            this.move = null;
        }
        if (this.engagement != null) {
            this.engagement.dispose();
            this.engagement = null;
        }
    }

    @Override
    public int compareTo(Unit other)
    {
        if (leader == other)
            return 0;
        return 1;
    }

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

    @Override
    public String toString()
    {
        if (type == OrderType.END)
            return String.format("[00] END");
        else
            return String.format("[%d] %s(%d) : %s", id, type, cost, leader.code);
    }

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

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

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

    public void setActivables(UnitList l)
    {
        for(Unit u : l)
            activables.add(u);
    }
}