summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/game/map/Map3Animations.java
blob: d109366caec12eef1f88bc22eee4bf5711b84336 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package ch.asynk.rustanddust.game.map;

import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;

import ch.asynk.rustanddust.RustAndDust;
import ch.asynk.rustanddust.engine.Move;
import ch.asynk.rustanddust.engine.SelectedTile;
import ch.asynk.rustanddust.engine.gfx.Moveable;
import ch.asynk.rustanddust.engine.gfx.animations.AnimationSequence;
import ch.asynk.rustanddust.engine.gfx.animations.BounceAnimation;
import ch.asynk.rustanddust.engine.gfx.animations.DiceAnimation;
import ch.asynk.rustanddust.engine.gfx.animations.FireAnimation;
import ch.asynk.rustanddust.engine.gfx.animations.TankFireAnimation;
import ch.asynk.rustanddust.engine.gfx.animations.InfantryFireAnimation;
import ch.asynk.rustanddust.engine.gfx.animations.PromoteAnimation;
import ch.asynk.rustanddust.engine.gfx.animations.DestroyAnimation;
import ch.asynk.rustanddust.engine.gfx.animations.SoundAnimation;
import ch.asynk.rustanddust.engine.gfx.animations.RunnableAnimation;
import ch.asynk.rustanddust.engine.gfx.animations.MoveToAnimation.MoveToAnimationCb;
import ch.asynk.rustanddust.game.Hex;
import ch.asynk.rustanddust.game.Unit;
import ch.asynk.rustanddust.game.Army;
import ch.asynk.rustanddust.game.Player;
import ch.asynk.rustanddust.game.Ctrl.MsgType;

public abstract class Map3Animations extends Map2Moves implements MoveToAnimationCb
{
    protected final DestroyAnimation destroyAnimation;
    private final Sound tankMoveSound;
    private final Sound infantryMoveSound;
    private Sound sound;
    private long soundId = -1;

    protected static final float BOUNCE_DURATION = 0.3f;

    public Map3Animations(final RustAndDust game, Texture map, SelectedTile hex)
    {
        super(game, map, hex);

        this.destroyAnimation = new DestroyAnimation();
        this.tankMoveSound = game.manager.get(game.SND_TANK_MOVE, Sound.class);
        this.infantryMoveSound = game.manager.get(game.SND_INF_MOVE, Sound.class);

        DiceAnimation.init(
                game.manager.get(game.PNG_DICE, Texture.class), 16, 9, game.manager.get(game.SND_DICE, Sound.class)
                );
        PromoteAnimation.init(
                game.factory.getHudRegion(game.factory.HUD_STARS),
                game.manager.get(game.SND_PROMOTE_US, Sound.class),
                game.manager.get(game.SND_PROMOTE_GE, Sound.class)
                );
        FireAnimation.init(
                game.manager.get(game.PNG_INF_FIRE, Texture.class), 1, 8,
                game.manager.get(game.PNG_TANK_FIRE, Texture.class), 1, 8,
                game.manager.get(game.PNG_EXPLOSIONS, Texture.class), 16, 8,
                game.manager.get(game.SND_INF_FIRE, Sound.class),
                game.manager.get(game.SND_TANK_FIRE, Sound.class),
                game.manager.get(game.SND_TANK_FIRE_SHORT, Sound.class),
                game.manager.get(game.SND_EXPLOSION, Sound.class),
                game.manager.get(game.SND_EXPLOSION_SHORT, Sound.class)
                );
    }

    @Override
    public void dispose()
    {
        super.dispose();
        destroyAnimation.dispose();
        tankMoveSound.dispose();
        infantryMoveSound.dispose();
        DiceAnimation.free();
        PromoteAnimation.free();
        FireAnimation.free();
    }

    // -> implement MoveToAnimationCb

    @Override
    public void moveToAnimationEnter(Moveable moveable, float x, float y, float r)
    {
        claim(moveable, getHexAt(x, y));
    }

    @Override
    public void moveToAnimationLeave(Moveable moveable, float x, float y, float r)
    {
        unclaim(moveable, getHexAt(x, y));
    }

    @Override
    public void moveToAnimationDone(Moveable moveable, float x, float y, float r)
    {
    }

    // <- implement MoveToAnimationCb

    protected void moveUnit(final Unit unit, final Move move, MoveToAnimationCb cb, boolean bounce)
    {
        removePawn(unit);

        AnimationSequence seq = AnimationSequence.get(move.steps() + (bounce ? 2 : 1));
        if (bounce) seq.addAnimation(BounceAnimation.get(unit, BOUNCE_DURATION));
        unit.getMoveAnimation(move.iterator(), seq, cb);
        seq.addAnimation(RunnableAnimation.get(unit, new Runnable() {
            @Override
            public void run() {
                unit.move(move);
                if (!move.to.isOffMap())
                    setPawnOnto(unit, move.to, move.orientation);
            }
        }));
        addAnimation(seq);
    }

    protected void revertLastUnitMove(final Unit unit, final Move move)
    {
        removePawn(unit);
        revertClaim(unit, move);

        AnimationSequence seq = AnimationSequence.get(1);
        unit.getRevertLastMoveAnimation(seq);
        seq.addAnimation(RunnableAnimation.get(unit, new Runnable() {
            @Override
            public void run() {
                setPawnOnto(unit, move.to, move.orientation);
            }
        }));
        addAnimation(seq);
        unit.revertLastMove();
    }

    protected void addBounceAnimation(final Unit unit)
    {
        addAnimation(BounceAnimation.get(unit, BOUNCE_DURATION));
    }

    protected void addPromoteAnimation(final Unit unit, final Player player)
    {
        Hex hex = unit.getHex();
        addAnimation(PromoteAnimation.get((unit.getArmy() == Army.US), hex.getX(), hex.getY(), game.config.fxVolume));
    }

    protected void addDestroyAnimation(Unit unit)
    {
        destroyAnimation.set(2f, unit);
        addAnimation(destroyAnimation);
    }

    protected void addEngagementAnimation(Unit target)
    {
        FireAnimation.reset();
        Hex to = target.getHex();
        for (Unit u : activatedUnits) {
            Hex from = u.getHex();
            float halfWidth = (u.getWidth() / 2f);
            if (u.isA(Unit.UnitType.INFANTRY))
                addAnimation(InfantryFireAnimation.get(game.config.fxVolume, from.getX(), from.getY(), to.getX(), to.getY(), halfWidth));
            else
                addAnimation(TankFireAnimation.get(game.config.fxVolume, u, from.getX(), from.getY(), to.getX(), to.getY(), halfWidth));
        }
    }

    protected void playMoveSound(Unit unit)
    {
        if (unit.isA(Unit.UnitType.INFANTRY))
            sound = infantryMoveSound;
        else
            sound = tankMoveSound;
        soundId = sound.play(game.config.fxVolume);
    }

    @Override
    protected void animationsDone()
    {
        if (soundId >= 0) {
            addAnimation( SoundAnimation.get(SoundAnimation.Action.FADE_OUT, sound, soundId, game.config.fxVolume, 0.5f));
            soundId = -1;
        } else
            game.ctrl.sendMsg(MsgType.ANIMATIONS_DONE);
    }
}