summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/engine/Path.java
blob: 68f1cdc33fa4a3aa8be73756799cbd2f34c3fd86 (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
package ch.asynk.rustanddust.engine;

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

import ch.asynk.rustanddust.engine.util.IterableArray;

public class Path implements Disposable, Pool.Poolable
{
    private static final Pool<Path> pathPool = new Pool<Path>() {
        @Override
        protected Path newObject() {
            return new Path();
        }
    };

    public static Path get(int size)
    {
        Path p = pathPool.obtain();
        p.init(size);
        return p;
    }

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

    public int cost;
    public int fitness;
    public boolean roadMarch;
    public IterableArray<Tile> tiles;

    public Path()
    {
        this.cost = -1;
        this.roadMarch = true;
        this.tiles = null;
        this.fitness = 0;
    }

    protected void init(int size)
    {
        if (tiles == null)
            tiles = new IterableArray<Tile>(size);
        else
            tiles.ensureCapacity(size);
        cost = -1;
        roadMarch = true;
        fitness = 0;
    }

    @Override
    public void reset()
    {
        cost = -1;
        roadMarch = true;
        tiles.clear();
    }

    @Override
    public void dispose()
    {
        tiles.clear();
        pathPool.free(this);
    }

    @Override
    public String toString()
    {
        String s = String.format("path(c:%d - r:%b - f:%d)\n", cost, roadMarch, fitness);
        for (Tile t : tiles)
            s += String.format("  %s\n", t.toString());
        return s;
    }
}