diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2018-11-23 15:43:24 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2018-11-23 15:43:24 +0100 |
commit | e62c838c3da5cd091c86844d3e8649ba4c3fe5dd (patch) | |
tree | cfa8823479e5fdf7189077bfec675f747501ec2a | |
parent | 5591bf3f826eff842e019bc82db5ae53aacba416 (diff) | |
download | gdx-boardgame-e62c838c3da5cd091c86844d3e8649ba4c3fe5dd.zip gdx-boardgame-e62c838c3da5cd091c86844d3e8649ba4c3fe5dd.tar.gz |
FramedSprite : add x,y,a .. vars
3 files changed, 87 insertions, 64 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/FramedSprite.java b/core/src/ch/asynk/gdx/boardgame/FramedSprite.java index 33ca5ce..33a6eb2 100644 --- a/core/src/ch/asynk/gdx/boardgame/FramedSprite.java +++ b/core/src/ch/asynk/gdx/boardgame/FramedSprite.java @@ -1,31 +1,53 @@ package ch.asynk.gdx.boardgame; import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.TextureRegion; -public class FramedSprite +public class FramedSprite implements Drawable { - public Texture texture; - public TextureRegion[] frames; - public final int width; - public final int height; - public final int cols; + private TextureRegion[][] frames; + private TextureRegion frame; public final int rows; + public final int cols; + public float x; + public float y; + public float a; - public FramedSprite(Texture texture, int cols, int rows) + public FramedSprite(Texture texture, int rows, int cols) { - this.cols = cols; + this.frames = TextureRegion.split(texture, (texture.getWidth() / cols), (texture.getHeight() / rows)); + this.frame = frames[0][0]; this.rows = rows; - this.width = (texture.getWidth() / cols); - this.height = (texture.getHeight() / rows); - this.texture = texture; - TextureRegion[][] tmp = TextureRegion.split(texture, width, height); - frames = new TextureRegion[cols * rows]; - int idx = 0; - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - frames[idx++] = tmp[i][j]; - } - } + this.cols = cols; + this.x = 0; + this.y = 0; + this.a = 0; + } + + public FramedSprite(FramedSprite other) + { + this.frames = other.frames; + this.frame = other.frame; + this.rows = other.rows; + this.cols = other.cols; + this.x = other.x; + this.y = other.y; + this.a = other.a; + } + + public void setFrame(int row, int col) + { + this.frame = frames[row][col]; + } + + public TextureRegion getFrame() + { + return frame; + } + + @Override public void draw(Batch batch) + { + batch.draw(frame, x, y, 0, 0, frame.getRegionWidth(), frame.getRegionHeight(), 1f, 1f, a); } } diff --git a/core/src/ch/asynk/gdx/boardgame/animations/ShellFireAnimation.java b/core/src/ch/asynk/gdx/boardgame/animations/ShellFireAnimation.java index ee041e5..6cc7227 100644 --- a/core/src/ch/asynk/gdx/boardgame/animations/ShellFireAnimation.java +++ b/core/src/ch/asynk/gdx/boardgame/animations/ShellFireAnimation.java @@ -7,7 +7,6 @@ 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.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.utils.Pool; @@ -53,15 +52,15 @@ public class ShellFireAnimation extends TimedAnimation implements Pool.Poolable float shellSpeed, float smokeDuration, float explosionDuration, - final Texture shellTexture, int shellC, int shellR, - final Texture explosionTexture, int explosionC, int explosionR, + 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, maxShootScattering, shellSpeed, smokeDuration, explosionDuration, - new FramedSprite(shellTexture, shellC, shellR), - new FramedSprite(explosionTexture, explosionC, explosionR), + new FramedSprite(shellTexture, shellR, shellC), + new FramedSprite(explosionTexture, explosionR, explosionC), shellFireSnd, explosionSnd ); configs.put(name, cfg); @@ -119,22 +118,18 @@ public class ShellFireAnimation extends TimedAnimation implements Pool.Poolable private float smokeEndTime; private float explosionEndTime; - private TextureRegion fireRegion; - private float fireX; - private float fireY; - private float fireA; - private float fireW; - private float fireDx; - private float fireDy; - private float fireDw; + private FramedSprite shellSprites; + private float shellW; + private float shellDx; + private float shellDy; + private float shellDw; private int smokeFrame; private float smokeDf; - private int explosionStart; + private FramedSprite explosionSprites; private int explosionFrame; - private float explosionX; - private float explosionY; + private int explosionRow; private float explosionDf; private ShellFireAnimation() @@ -168,26 +163,28 @@ public class ShellFireAnimation extends TimedAnimation implements Pool.Poolable this.explosionEndTime = this.hitTime + explosionDuration; float endTime = (this.smokeEndTime > this.explosionEndTime ? this.smokeEndTime : this.explosionEndTime); - // fire vars - this.fireX = x0; - this.fireY = y0; - this.fireA = a; - this.fireW = 0f; - this.fireDw = (w / fireDuration); - this.fireDx = (dx / fireDuration); - this.fireDy = (dy / fireDuration); - this.fireRegion = new TextureRegion(cfg.shellSprites.frames[0]); + // 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 = (cfg.shellSprites.rows / smokeDuration); + this.smokeDf = ((shellSprites.rows - 1) / smokeDuration); // explosion vars - this.explosionX = (x1 - (cfg.explosionSprites.width / 2.0f)); - this.explosionY = (y1 - (cfg.explosionSprites.height / 2.0f)); - this.explosionDf = (cfg.explosionSprites.cols / explosionDuration); - this.explosionStart = (random.nextInt(cfg.explosionSprites.rows) * cfg.explosionSprites.cols); - this.explosionFrame = this.explosionStart; + 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; @@ -214,10 +211,10 @@ public class ShellFireAnimation extends TimedAnimation implements Pool.Poolable } if (!hit && (elapsed < hitTime)) { - fireW += (fireDw * delta); - fireX += (fireDx * delta); - fireY += (fireDy * delta); - fireRegion.setRegionWidth((int) fireW); + this.shellW += (shellDw * delta); + this.shellSprites.x += (shellDx * delta); + this.shellSprites.y += (shellDy * delta); + this.shellSprites.getFrame().setRegionWidth((int) shellW); return; } @@ -232,18 +229,22 @@ public class ShellFireAnimation extends TimedAnimation implements Pool.Poolable float dt = (elapsed - hitTime); if (elapsed < smokeEndTime) { - int frame = (int) (dt * smokeDf); + int frame = (int) (dt * smokeDf) + 1; if (frame != smokeFrame) { smokeFrame = frame; - fireRegion.setRegion(cfg.shellSprites.frames[smokeFrame]); - fireRegion.setRegionWidth((int) fireW); + this.shellSprites.setFrame(smokeFrame, 0); + this.shellSprites.getFrame().setRegionWidth((int) shellW); } } else { drawFire = false; } if (elapsed < explosionEndTime) { - explosionFrame = (explosionStart + (int) (dt * explosionDf)); + int frame = (int) (dt * explosionDf); + if (frame != explosionFrame) { + explosionFrame = frame; + explosionSprites.setFrame(explosionRow, explosionFrame); + } } else { drawExplosion = false; } @@ -252,11 +253,11 @@ public class ShellFireAnimation extends TimedAnimation implements Pool.Poolable @Override public void draw(Batch batch) { if (drawFire) { - batch.draw(fireRegion, fireX, fireY, 0, 0, fireRegion.getRegionWidth(), fireRegion.getRegionHeight(), 1f, 1f, fireA); + this.shellSprites.draw(batch); } if (drawExplosion) { - batch.draw(cfg.explosionSprites.frames[explosionFrame], explosionX, explosionY); + this.explosionSprites.draw(batch); } } } diff --git a/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java b/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java index 27295e6..48c1991 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java @@ -54,13 +54,13 @@ public class AnimationsScreen extends AbstractScreen AnimationBatch batch; ShellFireAnimation.register("cfg0", 1f, 66f, 400f, 1.3f, 1f, - app.assets.getTexture(app.assets.SHELL_FIRE), 1, 8, - app.assets.getTexture(app.assets.EXPLOSIONS), 16, 8, + app.assets.getTexture(app.assets.SHELL_FIRE), 8, 1, + app.assets.getTexture(app.assets.EXPLOSIONS), 8, 16, app.assets.getSound(app.assets.SHELL_FIRE_SND), app.assets.getSound(app.assets.EXPLOSION_SND)); ShellFireAnimation.register("cfg1", .5f, 11f, 500f, .8f, 1f, - app.assets.getTexture(app.assets.SHELL_FIRE), 1, 8, - app.assets.getTexture(app.assets.EXPLOSIONS), 16, 8, + app.assets.getTexture(app.assets.SHELL_FIRE), 8, 1, + app.assets.getTexture(app.assets.EXPLOSIONS), 8, 16, app.assets.getSound(app.assets.SHELL_FIRE_SND), app.assets.getSound(app.assets.EXPLOSION_SND)); |