diff options
Diffstat (limited to 'core/src/ch/asynk/gdx/boardgame')
4 files changed, 382 insertions, 267 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/FramedSprite.java b/core/src/ch/asynk/gdx/boardgame/FramedSprite.java index d6c5fc7..1f60f9c 100644 --- a/core/src/ch/asynk/gdx/boardgame/FramedSprite.java +++ b/core/src/ch/asynk/gdx/boardgame/FramedSprite.java @@ -15,7 +15,7 @@ public class FramedSprite implements Drawable, Positionable public final int cols; public float x; public float y; - public float a; + public float r; public FramedSprite(Texture texture, int rows, int cols) { @@ -25,7 +25,7 @@ public class FramedSprite implements Drawable, Positionable this.cols = cols; this.x = 0; this.y = 0; - this.a = 0; + this.r = 0; if (trim > 0 || offset > 0) { for (int r = 0; r < rows; r++) { @@ -53,7 +53,7 @@ public class FramedSprite implements Drawable, Positionable this.frame = frames[0][0]; this.x = other.x; this.y = other.y; - this.a = other.a; + this.r = other.r; } public void setFrame(int row, int col) @@ -99,6 +99,6 @@ public class FramedSprite implements Drawable, Positionable @Override public void draw(Batch batch) { - batch.draw(frame, x, y, 0, 0, frame.getRegionWidth(), frame.getRegionHeight(), 1f, 1f, a); + batch.draw(frame, x, y, 0, 0, frame.getRegionWidth(), frame.getRegionHeight(), 1f, 1f, r); } } diff --git a/core/src/ch/asynk/gdx/boardgame/animations/FireAnimation.java b/core/src/ch/asynk/gdx/boardgame/animations/FireAnimation.java new file mode 100644 index 0000000..1bedb10 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/animations/FireAnimation.java @@ -0,0 +1,192 @@ +package ch.asynk.gdx.boardgame.animations; + +import java.util.Hashtable; +import java.util.Map; + +import com.badlogic.gdx.audio.Sound; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.utils.Pool; + +import ch.asynk.gdx.boardgame.FramedSprite; +import ch.asynk.gdx.boardgame.Piece; + +public class FireAnimation implements Animation, Pool.Poolable +{ + public static class Config + { + public int burstCount; + public float burstDelay; + public float maxFireDelay; + public float maxFireScattering; + public float shellSpeed; + public float smokeDuration; + public float explosionDuration; + public FramedSprite shellSprites; + public FramedSprite explosionSprites; + public Sound shellFireSnd; + public Sound explosionSnd; + public Config(int burstCount, float burstDelay, + float maxFireDelay, float maxFireScattering, + float shellSpeed, float smokeDuration, float explosionDuration, + FramedSprite shellSprites, FramedSprite explosionSprites, + Sound shellFireSnd, Sound explosionSnd) + { + this.burstCount = burstCount; + this.burstDelay = burstDelay; + this.maxFireDelay = maxFireDelay; + this.maxFireScattering = maxFireScattering; + this.shellSpeed = shellSpeed; + this.smokeDuration = smokeDuration; + this.explosionDuration = explosionDuration; + this.shellSprites = shellSprites; + this.explosionSprites = explosionSprites; + this.shellFireSnd = shellFireSnd; + this.explosionSnd = explosionSnd; + } + } + + private static Map<String, Config> configs = new Hashtable<String, Config>(); + + public static void register(final String name, + int burstCount, + float burstDelay, + float maxFireDelay, + float maxFireScattering, + float shellSpeed, + float smokeDuration, + float explosionDuration, + final Texture shellTexture, int shellR, int shellC, + final Texture explosionTexture, int explosionR, int explosionC, + final Sound shellFireSnd, + final Sound explosionSnd + ) + { + Config cfg = new Config(burstCount, burstDelay, + maxFireDelay, maxFireScattering, + shellSpeed, smokeDuration, (explosionTexture == null ? 0f : explosionDuration), + (shellTexture == null ? null : new FramedSprite(shellTexture, shellR, shellC)), + (explosionTexture == null ? null : new FramedSprite(explosionTexture, explosionR, explosionC)), + shellFireSnd, explosionSnd + ); + configs.put(name, cfg); + } + + public static void free() + { + // for(String key : configs.keySet()) { + // Config cfg = configs.get(key); + // } + configs.clear(); + } + + private static final Pool<FireAnimation> shellFireAnimationPool = new Pool<FireAnimation>() + { + @Override protected FireAnimation newObject() + { + return new FireAnimation(); + } + }; + + public static Vector2 sv = new Vector2(); + public static Vector2 tv = new Vector2(); + public static FireAnimation obtain(final String configName, Piece shooter, Piece target) + { + shooter.getFireingPoint(sv, target); + target.getImpactPoint(tv); + return obtain(configName, sv.x, sv.y, tv.x, tv.y); + } + + public static FireAnimation obtain(final String configName, float x0, float y0, float x1, float y1) + { + FireAnimation a = shellFireAnimationPool.obtain(); + + Config cfg = configs.get(configName); + if (cfg == null) { + throw new RuntimeException(String.format("FireAnimation : no configuration named : '%s'", configName)); + } + a.compute(cfg, x0, y0, x1, y1); + + return a; + } + + @Override public void reset() + { + this.shot = null; + this.shots = null; + } + + @Override public void dispose() + { + if (this.shot != null) { + this.shot.dispose(); + } + if (this.shots != null) { + for (ShotAnimation shot : shots) { + shot.dispose(); + } + } + shellFireAnimationPool.free(this); + } + + private boolean single; + private ShotAnimation shot; + private ShotAnimation[] shots; + + private FireAnimation() + { + } + + private void compute(Config cfg, float x0, float y0, float x1, float y1) + { + this.single = (cfg.burstCount == 1); + if (single) { + this.shot = ShotAnimation.obtain(); + this.shot.compute(0, cfg, x0, y0, x1, y1); + } else { + this.shots = new ShotAnimation[cfg.burstCount]; + for (int i = 0; i < cfg.burstCount; i++) { + this.shots[i] = ShotAnimation.obtain(); + this.shots[i].compute(i, cfg, x0, y0, x1, y1); + } + } + } + + @Override public boolean completed() + { + if (single) { + return this.shot.completed(); + } else { + boolean completed = true; + for (ShotAnimation shot : this.shots) { + completed &= shot.completed(); + } + return completed; + } + } + + @Override public boolean animate(float delta) + { + if (single) { + return this.shot.animate(delta); + } else { + boolean completed = true; + for (ShotAnimation shot : this.shots) { + completed &= shot.animate(delta); + } + return completed; + } + } + + @Override public void draw(Batch batch) + { + if (single) { + this.shot.draw(batch); + } else { + for (ShotAnimation shot : this.shots) { + shot.draw(batch); + } + } + } +} diff --git a/core/src/ch/asynk/gdx/boardgame/animations/ShellFireAnimation.java b/core/src/ch/asynk/gdx/boardgame/animations/ShellFireAnimation.java deleted file mode 100644 index 1cb6f15..0000000 --- a/core/src/ch/asynk/gdx/boardgame/animations/ShellFireAnimation.java +++ /dev/null @@ -1,263 +0,0 @@ -package ch.asynk.gdx.boardgame.animations; - -import java.util.Hashtable; -import java.util.Map; -import java.util.Random; - -import com.badlogic.gdx.audio.Sound; -import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.Batch; -import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.math.MathUtils; -import com.badlogic.gdx.utils.Pool; - -import ch.asynk.gdx.boardgame.FramedSprite; -import ch.asynk.gdx.boardgame.Piece; - -public class ShellFireAnimation extends TimedAnimation implements Pool.Poolable -{ - private static class Config - { - public float maxFireDelay; - public float maxFireScattering; - public float shellSpeed; - public float smokeDuration; - public float explosionDuration; - public FramedSprite shellSprites; - public FramedSprite explosionSprites; - public Sound shellFireSnd; - public Sound explosionSnd; - public Config(float maxFireDelay, float maxFireScattering, float shellSpeed, - float smokeDuration, float explosionDuration, - FramedSprite shellSprites, FramedSprite explosionSprites, - Sound shellFireSnd, Sound explosionSnd) - { - this.maxFireDelay = maxFireDelay; - this.maxFireScattering = maxFireScattering; - this.shellSpeed = shellSpeed; - this.smokeDuration = smokeDuration; - this.explosionDuration = explosionDuration; - this.shellSprites = shellSprites; - this.explosionSprites = explosionSprites; - this.shellFireSnd = shellFireSnd; - this.explosionSnd = explosionSnd; - } - } - - private static Map<String, Config> configs = new Hashtable<String, Config>(); - - public static void register(final String name, - float maxFireDelay, - float maxFireScattering, - float shellSpeed, - float smokeDuration, - float explosionDuration, - final Texture shellTexture, int shellR, int shellC, - final Texture explosionTexture, int explosionR, int explosionC, - final Sound shellFireSnd, - final Sound explosionSnd - ) - { - Config cfg = new Config(maxFireDelay, maxFireScattering, shellSpeed, smokeDuration, explosionDuration, - new FramedSprite(shellTexture, shellR, shellC), - new FramedSprite(explosionTexture, explosionR, explosionC), - shellFireSnd, explosionSnd - ); - configs.put(name, cfg); - } - - public static void free() - { - // for(String key : configs.keySet()) { - // Config cfg = configs.get(key); - // } - configs.clear(); - } - - private static Random random = new Random(); - - private static final Pool<ShellFireAnimation> shellFireAnimationPool = new Pool<ShellFireAnimation>() - { - @Override protected ShellFireAnimation newObject() - { - return new ShellFireAnimation(); - } - }; - - public static Vector2 sv = new Vector2(); - public static Vector2 tv = new Vector2(); - public static ShellFireAnimation obtain(final String configName, Piece shooter, Piece target) - { - shooter.getFireingPoint(sv, target); - target.getImpactPoint(tv); - return obtain(configName, sv.x, sv.y, tv.x, tv.y); - } - - public static ShellFireAnimation obtain(final String configName, float x0, float y0, float x1, float y1) - { - ShellFireAnimation a = shellFireAnimationPool.obtain(); - - Config cfg = configs.get(configName); - if (cfg == null) { - throw new RuntimeException(String.format("ShellFireAnimation : no configuration named : '%s'", configName)); - } - a.cfg = cfg; - a.compute(x0, y0, x1, y1); - - return a; - } - - private Config cfg; - - private boolean fired; - private boolean hit; - private boolean drawFire; - private boolean drawExplosion; - private float fireTime; - private float hitTime; - private float smokeEndTime; - private float explosionEndTime; - - private FramedSprite shellSprites; - private float shellW; - private float shellDx; - private float shellDy; - private float shellDw; - - private int smokeFrame; - private float smokeDf; - - private FramedSprite explosionSprites; - private int explosionFrame; - private int explosionRow; - private float explosionDf; - - private ShellFireAnimation() - { - } - - @Override public void dispose() - { - shellFireAnimationPool.free(this); - } - - private void compute(float x0, float y0, float x1, float y1) - { - // scattering - x1 = (x1 + (random.nextFloat() * cfg.maxFireScattering) - (cfg.maxFireScattering / 2f)); - y1 = (y1 + (random.nextFloat() * cfg.maxFireScattering) - (cfg.maxFireScattering / 2f)); - - // geometry - float dx = (x1 - x0); - float dy = (y1 - y0); - float a = (float) (MathUtils.atan2(y0 - y1, x0 - x1)) * MathUtils.radiansToDegrees; - float w = (float) Math.sqrt((dx * dx) + (dy * dy)); - - // timing - float fireDuration = (w / cfg.shellSpeed); - float smokeDuration = cfg.smokeDuration; - float explosionDuration = cfg.explosionDuration; - this.fireTime = (random.nextFloat() * cfg.maxFireDelay); - this.hitTime = this.fireTime + fireDuration; - this.smokeEndTime = this.hitTime + smokeDuration; - this.explosionEndTime = this.hitTime + explosionDuration; - float endTime = (this.smokeEndTime > this.explosionEndTime ? this.smokeEndTime : this.explosionEndTime); - - // shell vars - this.shellSprites = new FramedSprite(cfg.shellSprites); - this.shellSprites.x = x0; - this.shellSprites.y = y0; - this.shellSprites.a = a; - this.shellW = 0f; - this.shellDw = (w / fireDuration); - this.shellDx = (dx / fireDuration); - this.shellDy = (dy / fireDuration); - - // smoke vars - this.smokeFrame = 0; - this.smokeDf = ((shellSprites.rows - 1) / smokeDuration); - - // explosion vars - this.explosionFrame = 0; - this.explosionSprites = new FramedSprite(cfg.explosionSprites); - this.explosionSprites.x = (x1 - (explosionSprites.getFrame().getRegionWidth() / 2.0f)); - this.explosionSprites.y = (y1 - (explosionSprites.getFrame().getRegionHeight() / 2.0f)); - this.explosionDf = (explosionSprites.cols / explosionDuration); - this.explosionRow = random.nextInt(explosionSprites.rows); - this.explosionSprites.setFrame(explosionRow, 0); - - this.fired = false; - this.hit = false; - this.drawFire = false; - this.drawExplosion = false; - setDuration(endTime); - } - - @Override public void begin() { } - @Override public void end() { } - - @Override public void update(float delta) - { - if (!fired && (elapsed < fireTime)) { - return; - } - - if (!fired) { - fired = true; - drawFire = true; - if(cfg.shellFireSnd != null) { - cfg.shellFireSnd.play(); - } - } - - if (!hit && (elapsed < hitTime)) { - this.shellW += (shellDw * delta); - this.shellSprites.x += (shellDx * delta); - this.shellSprites.y += (shellDy * delta); - this.shellSprites.getFrame().setRegionWidth((int) shellW); - return; - } - - if (!hit) { - hit = true; - drawExplosion = true; - if(cfg.explosionSnd != null) { - cfg.explosionSnd.play(); - } - } - - float dt = (elapsed - hitTime); - - if (elapsed < smokeEndTime) { - int frame = (int) (dt * smokeDf) + 1; - if (frame != smokeFrame) { - smokeFrame = frame; - this.shellSprites.setFrame(smokeFrame, 0); - this.shellSprites.getFrame().setRegionWidth((int) shellW); - } - } else { - drawFire = false; - } - - if (elapsed < explosionEndTime) { - int frame = (int) (dt * explosionDf); - if (frame != explosionFrame) { - explosionFrame = frame; - explosionSprites.setFrame(explosionRow, explosionFrame); - } - } else { - drawExplosion = false; - } - } - - @Override public void draw(Batch batch) - { - if (drawFire) { - this.shellSprites.draw(batch); - } - - if (drawExplosion) { - this.explosionSprites.draw(batch); - } - } -} diff --git a/core/src/ch/asynk/gdx/boardgame/animations/ShotAnimation.java b/core/src/ch/asynk/gdx/boardgame/animations/ShotAnimation.java new file mode 100644 index 0000000..890b6a4 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/animations/ShotAnimation.java @@ -0,0 +1,186 @@ +package ch.asynk.gdx.boardgame.animations; + +import java.util.Random; + +import com.badlogic.gdx.audio.Sound; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.math.MathUtils; +import com.badlogic.gdx.utils.Pool; + +import ch.asynk.gdx.boardgame.FramedSprite; +import ch.asynk.gdx.boardgame.Drawable; + +public class ShotAnimation extends TimedAnimation implements Drawable, Pool.Poolable +{ + private static final Pool<ShotAnimation> shotAnimationPool = new Pool<ShotAnimation>() + { + @Override protected ShotAnimation newObject() + { + return new ShotAnimation(); + } + }; + + public static ShotAnimation obtain() + { + return shotAnimationPool.obtain(); + } + + @Override public void dispose() + { + shotAnimationPool.free(this); + } + + private boolean fired; + private boolean hit; + private boolean drawFire; + private boolean drawExplosion; + private float fireTime; + private float hitTime; + private float smokeEndTime; + private float explosionEndTime; + + private FramedSprite shellSprites; + private float shellW; + private float shellDx; + private float shellDy; + private float shellDw; + private Sound fireSnd; + + private int smokeFrame; + private float smokeDf; + + private FramedSprite explosionSprites; + private int explosionFrame; + private int explosionRow; + private float explosionDf; + private Sound explosionSnd; + + private static Random random = new Random(); + + public void compute(int i, FireAnimation.Config cfg, float x0, float y0, float x1, float y1) + { + // scattering + x1 = (x1 + (random.nextFloat() * cfg.maxFireScattering) - (cfg.maxFireScattering / 2f)); + y1 = (y1 + (random.nextFloat() * cfg.maxFireScattering) - (cfg.maxFireScattering / 2f)); + + // geometry + float dx = (x1 - x0); + float dy = (y1 - y0); + float r = (float) (MathUtils.atan2(y0 - y1, x0 - x1)) * MathUtils.radiansToDegrees; + float w = (float) Math.sqrt((dx * dx) + (dy * dy)); + + // timing + float fireDuration = (w / cfg.shellSpeed); + float smokeDuration = cfg.smokeDuration; + float explosionDuration = cfg.explosionDuration; + this.fireTime = (random.nextFloat() * cfg.maxFireDelay) + (i * cfg.burstDelay); + this.hitTime = this.fireTime + fireDuration; + this.smokeEndTime = this.hitTime + smokeDuration; + this.explosionEndTime = this.hitTime + explosionDuration; + float endTime = (this.smokeEndTime > this.explosionEndTime ? this.smokeEndTime : this.explosionEndTime); + + // shell vars + this.shellSprites = new FramedSprite(cfg.shellSprites); + this.shellSprites.x = x0; + this.shellSprites.y = y0; + this.shellSprites.r = r; + this.shellW = 0f; + this.shellDw = (w / fireDuration); + this.shellDx = (dx / fireDuration); + this.shellDy = (dy / fireDuration); + this.fireSnd = (i == 0 ? cfg.shellFireSnd : null); + + // smoke vars + this.smokeFrame = 0; + this.smokeDf = ((shellSprites.rows - 1) / smokeDuration); + + // explosion vars + if (cfg.explosionSprites == null) { + this.explosionSnd = null; + this.explosionSprites = null; + } else { + this.explosionFrame = 0; + this.explosionSprites = new FramedSprite(cfg.explosionSprites); + this.explosionSprites.x = (x1 - (explosionSprites.getFrame().getRegionWidth() / 2.0f)); + this.explosionSprites.y = (y1 - (explosionSprites.getFrame().getRegionHeight() / 2.0f)); + this.explosionDf = (explosionSprites.cols / explosionDuration); + this.explosionRow = random.nextInt(explosionSprites.rows); + this.explosionSprites.setFrame(explosionRow, 0); + this.explosionSnd = cfg.explosionSnd; + } + + this.fired = false; + this.hit = false; + this.drawFire = false; + this.drawExplosion = false; + setDuration(endTime); + } + + @Override public void begin() { } + @Override public void end() { } + + @Override public void update(float delta) + { + if (!fired && (elapsed < fireTime)) { + return; + } + + if (!fired) { + fired = true; + drawFire = true; + if(fireSnd != null) { + fireSnd.play(); + } + } + + if (!hit && (elapsed < hitTime)) { + this.shellW += (shellDw * delta); + this.shellSprites.x += (shellDx * delta); + this.shellSprites.y += (shellDy * delta); + this.shellSprites.getFrame().setRegionWidth((int) shellW); + return; + } + + if (!hit) { + hit = true; + drawExplosion = true; + if(explosionSnd != null) { + explosionSnd.play(); + } + } + + float dt = (elapsed - hitTime); + + if (elapsed < smokeEndTime) { + int frame = (int) (dt * smokeDf) + 1; + if (frame != smokeFrame) { + smokeFrame = frame; + this.shellSprites.setFrame(smokeFrame, 0); + this.shellSprites.getFrame().setRegionWidth((int) shellW); + } + } else { + drawFire = false; + } + + if (elapsed < explosionEndTime) { + int frame = (int) (dt * explosionDf); + if (frame != explosionFrame) { + explosionFrame = frame; + explosionSprites.setFrame(explosionRow, explosionFrame); + } + } else { + drawExplosion = false; + } + } + + @Override public void draw(Batch batch) + { + if (drawFire) { + this.shellSprites.draw(batch); + } + + if (drawExplosion) { + this.explosionSprites.draw(batch); + } + } +} |