diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2014-12-03 15:24:02 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2014-12-03 15:24:02 +0100 |
commit | daf256e629dbe272ab0267e31e19bbca31d24e1f (patch) | |
tree | 8768b168e1de5e4afca692de20e35d7658babaf0 /core/src/ch | |
parent | 3a406d85d5287d0ac3e8bc190ff4059b148e61f6 (diff) | |
download | RustAndDust-daf256e629dbe272ab0267e31e19bbca31d24e1f.zip RustAndDust-daf256e629dbe272ab0267e31e19bbca31d24e1f.tar.gz |
add FireAnimation, static part of TankFireAnimation
Diffstat (limited to 'core/src/ch')
3 files changed, 94 insertions, 66 deletions
diff --git a/core/src/ch/asynk/tankontank/engine/gfx/animations/FireAnimation.java b/core/src/ch/asynk/tankontank/engine/gfx/animations/FireAnimation.java new file mode 100644 index 0000000..26c8e6c --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/gfx/animations/FireAnimation.java @@ -0,0 +1,73 @@ +package ch.asynk.tankontank.engine.gfx.animations; + +import java.util.Random; + +import com.badlogic.gdx.audio.Sound; +import com.badlogic.gdx.graphics.Texture; + +public class FireAnimation +{ + public static Random random = new Random(); + + public static Sprites tankFire; + public static Sprites explosion; + + public static Sound tankFireSnd; + public static Sound tankFireSndLong; + public static Sound explosionSnd; + public static Sound explosionSndLong; + + public static double tankFireSndLongId; + public static double explosionSndLongId; + + public static void init( + Texture tankFireT, int sCols, int sRows, + Texture explosionT, int eCols, int eRows, + Sound tankFireS, + Sound tankFireLongS, + Sound explosionS, + Sound explosionLongS) + { + tankFire = new Sprites(tankFireT, sCols, sRows); + explosion = new Sprites(explosionT, eCols, eRows); + tankFireSnd = tankFireS; + tankFireSndLong = tankFireLongS; + explosionSnd = explosionS; + explosionSndLong = explosionLongS; + + reset(); + } + + public static void reset() + { + tankFireSndLongId = -1; + explosionSndLongId = -1; + } + + public static void free() + { + tankFire.dispose(); + explosion.dispose(); + + tankFireSnd.dispose(); + tankFireSndLong.dispose(); + explosionSnd.dispose(); + explosionSndLong.dispose(); + } + + public static void tankFireSndPlay(float volume) + { + if (tankFireSndLongId == -1) + tankFireSndLongId = tankFireSndLong.play(volume); + else + tankFireSnd.play(volume); + } + + public static void explosionSndPlay(float volume) + { + if (explosionSndLongId == -1) + explosionSndLongId = explosionSndLong.play(volume); + else + explosionSnd.play(volume); + } +} diff --git a/core/src/ch/asynk/tankontank/engine/gfx/animations/TankFireAnimation.java b/core/src/ch/asynk/tankontank/engine/gfx/animations/TankFireAnimation.java index 0bd6392..582d1ac 100644 --- a/core/src/ch/asynk/tankontank/engine/gfx/animations/TankFireAnimation.java +++ b/core/src/ch/asynk/tankontank/engine/gfx/animations/TankFireAnimation.java @@ -5,8 +5,6 @@ import java.util.Random; import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.utils.Pool; -import com.badlogic.gdx.audio.Sound; -import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; @@ -22,16 +20,6 @@ public class TankFireAnimation implements Disposable, Animation, Pool.Poolable private static final float SHOT_SPEED = 700f; private static final float EXPLOSION_FRAME_DURATION = 0.05f; - private static Random random = new Random(); - private static Sound fireSnd; - private static Sound fireSndLong; - private static Sound explosionSnd; - private static Sound explosionSndLong; - private static Sprites fire; - private static Sprites explosion; - private static double fireSndLongId; - private static double explosionSndLongId; - private TextureRegion fireRegion; private float fire_a; private float fire_x; @@ -73,37 +61,9 @@ public class TankFireAnimation implements Disposable, Animation, Pool.Poolable return a; } - public static void init(Texture fire_texture, int scols, int srows, Texture explosion_texture, int ecols, int erows, Sound ls, Sound ss, Sound le, Sound se) - { - fireSndLong = ls; - fireSnd = ss; - explosionSndLong = le; - explosionSnd = se; - fire = new Sprites(fire_texture, scols, srows); - explosion = new Sprites(explosion_texture, ecols, erows); - fireSndLongId = -1; - explosionSndLongId = -1; - } - - public static void resetSound() - { - fireSndLongId = -1; - explosionSndLongId = -1; - } - - public static void free() - { - fireSnd.dispose(); - fireSndLong.dispose(); - explosionSnd.dispose(); - explosionSndLong.dispose(); - fire.dispose(); - explosion.dispose(); - } - public TankFireAnimation() { - this.fireRegion = new TextureRegion(fire.frames[0]); + this.fireRegion = new TextureRegion(FireAnimation.tankFire.frames[0]); } private void set(float volume, float offset, float x0, float y0, float x1, float y1) @@ -113,9 +73,9 @@ public class TankFireAnimation implements Disposable, Animation, Pool.Poolable this.volume = volume; // fire geometry - y0 -= (fire.height / 2.0f); - x1 += ((SHOT_SCATTERING * random.nextFloat()) - (SHOT_SCATTERING / 2f)); - y1 += ((SHOT_SCATTERING * random.nextFloat()) - (SHOT_SCATTERING / 2f)); + y0 -= (FireAnimation.tankFire.height / 2.0f); + x1 += ((SHOT_SCATTERING * FireAnimation.random.nextFloat()) - (SHOT_SCATTERING / 2f)); + y1 += ((SHOT_SCATTERING * FireAnimation.random.nextFloat()) - (SHOT_SCATTERING / 2f)); double r = Math.atan2((y0 - y1), (x0 - x1)); float xadj = (float) (Math.cos(r) * offset); @@ -129,9 +89,9 @@ public class TankFireAnimation implements Disposable, Animation, Pool.Poolable float w = (float) Math.sqrt((dx * dx) + (dy * dy)); // timing - float delay = START_DELAY + (random.nextFloat() * TIME_SCATTERING); - float fire_duration = ((random.nextFloat() * TIME_SCATTERING) + (w / SHOT_SPEED)); - float explosion_duration = (explosion.cols * EXPLOSION_FRAME_DURATION); + float delay = START_DELAY + (FireAnimation.random.nextFloat() * TIME_SCATTERING); + float fire_duration = ((FireAnimation.random.nextFloat() * TIME_SCATTERING) + (w / SHOT_SPEED)); + float explosion_duration = (FireAnimation.explosion.cols * EXPLOSION_FRAME_DURATION); this.elapsed = 0f; this.fire_time = delay; @@ -149,14 +109,14 @@ public class TankFireAnimation implements Disposable, Animation, Pool.Poolable // smoke var this.smoke_dx = 0f; - this.smoke_df = (fire.rows / explosion_duration); + this.smoke_df = (FireAnimation.tankFire.rows / explosion_duration); this.smoke_frame = 0; // explosion vars - this.explosion_x = (x1 - (explosion.width / 2.0f)); - this.explosion_y = (y1 - (explosion.height / 2.0f)); - this.explosion_df = (explosion.cols / explosion_duration); - this.explosion_frame = (random.nextInt(explosion.rows) * explosion.cols); + this.explosion_x = (x1 - (FireAnimation.explosion.width / 2.0f)); + this.explosion_y = (y1 - (FireAnimation.explosion.height / 2.0f)); + this.explosion_df = (FireAnimation.explosion.cols / explosion_duration); + this.explosion_frame = (FireAnimation.random.nextInt(FireAnimation.explosion.rows) * FireAnimation.explosion.cols); } @Override @@ -180,10 +140,7 @@ public class TankFireAnimation implements Disposable, Animation, Pool.Poolable if (!fired) { fired = true; - if (fireSndLongId == -1) - fireSndLongId = fireSndLong.play(volume); - else - fireSnd.play(volume); + FireAnimation.tankFireSndPlay(volume); } if (!hit && (elapsed < hit_time)) { @@ -196,17 +153,14 @@ public class TankFireAnimation implements Disposable, Animation, Pool.Poolable if (!hit) { hit = true; - if (explosionSndLongId == -1) - explosionSndLongId = explosionSndLong.play(volume); - else - explosionSnd.play(volume); + FireAnimation.explosionSndPlay(volume); } if (elapsed < end_time) { int frame = (int) ((elapsed - hit_time) * smoke_df); if (frame != smoke_frame) { smoke_frame = frame; - fireRegion.setRegion(fire.frames[smoke_frame]); + fireRegion.setRegion(FireAnimation.tankFire.frames[smoke_frame]); fireRegion.setRegionWidth((int) fire_w); } return false; @@ -223,7 +177,7 @@ public class TankFireAnimation implements Disposable, Animation, Pool.Poolable if (hit) { int frame = (explosion_frame + (int) ((elapsed - hit_time) * explosion_df)); - batch.draw(explosion.frames[frame], explosion_x, explosion_y); + batch.draw(FireAnimation.explosion.frames[frame], explosion_x, explosion_y); } } @@ -236,7 +190,7 @@ public class TankFireAnimation implements Disposable, Animation, Pool.Poolable debugShapes.translate(fire_x, fire_y, 0); debugShapes.rotate(0, 0, 1, fire_a); debugShapes.translate(-fire_x, -fire_y, 0); - debugShapes.rect(fire_x, fire_y, fire_w, fire.height); + debugShapes.rect(fire_x, fire_y, fire_w, FireAnimation.tankFire.height); debugShapes.end(); debugShapes.begin(ShapeRenderer.ShapeType.Line); debugShapes.identity(); diff --git a/core/src/ch/asynk/tankontank/game/Map.java b/core/src/ch/asynk/tankontank/game/Map.java index 1693b75..ff418a0 100644 --- a/core/src/ch/asynk/tankontank/game/Map.java +++ b/core/src/ch/asynk/tankontank/game/Map.java @@ -18,6 +18,7 @@ import ch.asynk.tankontank.engine.PossiblePaths; import ch.asynk.tankontank.engine.gfx.Animation; import ch.asynk.tankontank.engine.gfx.animations.AnimationSequence; import ch.asynk.tankontank.engine.gfx.animations.DiceAnimation; +import ch.asynk.tankontank.engine.gfx.animations.FireAnimation; import ch.asynk.tankontank.engine.gfx.animations.TankFireAnimation; import ch.asynk.tankontank.engine.gfx.animations.PromoteAnimation; import ch.asynk.tankontank.engine.gfx.animations.SoundAnimation; @@ -93,7 +94,7 @@ public abstract class Map extends Board this.moveSound = game.manager.get("sounds/move.mp3", Sound.class); DiceAnimation.init(game.manager.get("data/dice.png", Texture.class), 16, 9, game.manager.get("sounds/dice.mp3", Sound.class)); PromoteAnimation.init(game.manager.get("data/hud.atlas", TextureAtlas.class), game.manager.get("sounds/promote.mp3", Sound.class)); - TankFireAnimation.init( + FireAnimation.init( game.manager.get("data/tank_fire.png", Texture.class), 1, 7, game.manager.get("data/explosions.png", Texture.class), 16, 8, game.manager.get("sounds/tank_fire.mp3", Sound.class), @@ -127,7 +128,7 @@ public abstract class Map extends Board moveSound.dispose(); DiceAnimation.free(); PromoteAnimation.free(); - TankFireAnimation.free(); + FireAnimation.free(); } public void clearAll() @@ -402,7 +403,7 @@ public abstract class Map extends Board public void addEngagementAnimation(Unit target) { - TankFireAnimation.resetSound(); + FireAnimation.reset(); Hex to = target.getHex(); for (Unit u : activatedUnits) { Hex from = u.getHex(); |