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

import com.badlogic.gdx.utils.Disposable;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import ch.asynk.rustanddust.engine.gfx.Drawable;
import ch.asynk.rustanddust.engine.gfx.Animation;
import ch.asynk.rustanddust.engine.gfx.animations.Sprites;

public class SelectedTile implements Disposable, Drawable, Animation
{
    private Sprites sprites;
    public Tile tile;
    public boolean visible;
    public float x;
    public float y;
    private float elapsed;
    private int frame;
    private float[] seq;

    public SelectedTile(Texture texture, float[] seq)
    {
        this.sprites = new Sprites(texture, seq.length, 1);
        this.visible = false;
        this.tile = null;
        this.elapsed = 0f;
        this.seq = seq;
    }

    public void hide()
    {
        tile = null;
        visible = false;
    }

    public void set(Tile tile)
    {
        this.visible = true;
        this.tile = tile;
        this.frame = 0;
        this.elapsed = 0f;
        this.x = (tile.getX() - (sprites.width / 2f));
        this.y = (tile.getY() - (sprites.height / 2f));
    }

    public void dispose()
    {
        sprites.dispose();
    }

    @Override
    public boolean animate(float delta)
    {
        if (visible) {
            elapsed += delta;
            if (elapsed > seq[frame]) {
                frame = ((frame + 1) % sprites.frames.length);
                elapsed = 0f;
            }
        }
        return false;
    }

    @Override
    public void draw(Batch batch)
    {
        if (visible)
            batch.draw(sprites.frames[frame], x, y);
    }

    @Override
    public void drawDebug(ShapeRenderer debugShapes)
    {
        if (visible)
            debugShapes.rect(x, y, sprites.width, sprites.height);
    }
}