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

import java.util.List;
import java.util.LinkedList;
import java.util.Random;

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

public class Engagement implements Disposable, Pool.Poolable
{
    private static Random rand = new Random();

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

    public static Engagement get(Unit attacker, Unit defender)
    {
        Engagement e = engagementPool.obtain();
        e.attacker = attacker;
        e.defender = defender;
        e.diceRoll();

        return e;
    }

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

    public Unit attacker;
    public Unit defender;
    public List<Unit> assists;
    public Army attackerArmy;
    public Army defenderArmy;
    public boolean success;
    public int d1;
    public int d2;
    public int d3;
    public int d4;
    public int unitCount;
    public int flankBonus;
    public int unitDefense;
    public int terrainDefense;
    public int weatherDefense;
    public int attackSum;
    public int defenseSum;

    public Engagement()
    {
        assists = new LinkedList<Unit>();
        reset();
    }

    @Override
    public void reset()
    {
        attacker = null;
        defender = null;
        assists.clear();
    }

    @Override
    public void dispose()
    {
        assists.clear();
        engagementPool.free(this);
    }

    public void addAssist(Unit unit)
    {
        assists.add(unit);
    }

    private void diceRoll()
    {
        d1 = rand.nextInt(6) + 1;
        d2 = rand.nextInt(6) + 1;
        d3 = rand.nextInt(6) + 1;
        d4 = rand.nextInt(6) + 1;
    }

    public void set(int cnt, int flk, int def, int tdf, int wdf)
    {
        this.unitCount = cnt;
        this.flankBonus = flk;
        this.unitDefense = def;
        this.terrainDefense = tdf;
        this.weatherDefense = wdf;
        if (d3 == 0)
            this.attackSum = (d1 + d2 + unitCount + flankBonus);
        else
            this.attackSum = (d3 + d4 + unitCount + flankBonus);
        this.defenseSum = (unitDefense + terrainDefense + weatherDefense);
    }


    @Override
    public String toString()
    {
        int a, b;
        if (d3 == 0) {
            a = d1;
            b = d2;
        } else {
            a = d3;
            b = d4;
        }
        return String.format("Engagement : (%d + %d + %d + %d) vs (%d + %d + %d) -> %b", a, b, unitCount, flankBonus, unitDefense, terrainDefense, weatherDefense, success);
    }
}