summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/engine/ObjectiveSet.java
blob: 2effb6b7cbb711da9095deff7266bf7b7b0a3f04 (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
package ch.asynk.tankontank.engine;

import java.util.Collection;
import java.util.HashMap;

public class ObjectiveSet extends HashMap<Tile, Objective>
{
    public interface ObjectiveCb
    {
        public void showObjective(Tile tile, Faction faction);
    }

    private final Board board;
    private final HashMap<Objective, Tile> modified;

    public ObjectiveSet(Board board, int n)
    {
        super(n);
        this.board = board;
        this.modified = new HashMap<Objective, Tile>(10);
    }

    public void add(Tile tile, Faction faction, boolean persistent)
    {
        put(tile, new Objective(faction, persistent));
    }

    public int count(Faction faction)
    {
        int n = 0;
        for (Objective objective : values()) {
            if (objective.is(faction))
                n += 1;
        }
        return n;
    }

    public Faction claim(Tile tile, Faction faction)
    {
        Objective objective = get(tile);
        if (objective == null)
            return null;

        if (objective.set(faction))
            modified.put(objective, tile);
        return objective.faction();
    }

    public Faction unclaim(Tile tile)
    {
        Objective objective = get(tile);
        if (objective == null)
            return null;

        if (objective.unset())
            modified.remove(objective);
        return objective.faction();
    }

    public void forget()
    {
        modified.clear();
    }

    public int modifiedCount()
    {
        return modified.size();
    }

    public void revert(ObjectiveCb cb)
    {
        for (Objective objective : modified.keySet()) {
            objective.revert();
            cb.showObjective(modified.get(objective), objective.faction());
        }
        modified.clear();
    }
}