summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2014-12-02 16:14:07 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2014-12-02 16:14:07 +0100
commit0814a114662e7feb4cbde6283660a9abaa12db45 (patch)
treeeed8d124fe83191cafe0fb2caf6016594810c369 /core
parentc84d4c7f75a7fa4c96737874b737dea01bcd0d82 (diff)
downloadRustAndDust-0814a114662e7feb4cbde6283660a9abaa12db45.zip
RustAndDust-0814a114662e7feb4cbde6283660a9abaa12db45.tar.gz
SpriteAnimation: use Sprites
Diffstat (limited to 'core')
-rw-r--r--core/src/ch/asynk/tankontank/engine/gfx/animations/SpriteAnimation.java33
1 files changed, 10 insertions, 23 deletions
diff --git a/core/src/ch/asynk/tankontank/engine/gfx/animations/SpriteAnimation.java b/core/src/ch/asynk/tankontank/engine/gfx/animations/SpriteAnimation.java
index ee498b3..714323d 100644
--- a/core/src/ch/asynk/tankontank/engine/gfx/animations/SpriteAnimation.java
+++ b/core/src/ch/asynk/tankontank/engine/gfx/animations/SpriteAnimation.java
@@ -15,8 +15,7 @@ import ch.asynk.tankontank.engine.gfx.Animation;
public class SpriteAnimation implements Disposable, Animation
{
private static Random random = new Random();
- private Texture texture;
- private TextureRegion[] frames;
+ private Sprites sprites;
private float duration;
private float frameDuration;
private float elapsed;
@@ -24,46 +23,34 @@ public class SpriteAnimation implements Disposable, Animation
private float y0;
private float x1;
private float y1;
- private int w;
- private int h;
private int randFreq;
public SpriteAnimation(Texture texture, int cols, int rows, int randFreq)
{
- this.texture = texture;
+ this.sprites = new Sprites(texture, cols, rows);
this.randFreq = randFreq;
- this.w = (texture.getWidth() / cols);
- this.h = (texture.getHeight() / rows);
- TextureRegion[][] tmp = TextureRegion.split(texture, w, h);
- this.frames = new TextureRegion[cols * rows];
- int idx = 0;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- this.frames[idx++] = tmp[i][j];
- }
- }
}
@Override
public void dispose()
{
- this.texture.dispose();
+ sprites.texture.dispose();
}
public void init(float duration, float x, float y)
{
this.duration = duration;
- this.frameDuration = (duration / (float) frames.length);
- this.x0 = x - (w/ 2);
- this.y0 = y - (h / 2);
+ this.frameDuration = (duration / (float) sprites.frames.length);
+ this.x0 = x - (sprites.width / 2f);
+ this.y0 = y - (sprites.height / 2f);
this.elapsed = 0f;
randPos();
}
private void randPos()
{
- this.x1 = this.x0 + (random.nextInt(w / 1) - (w / 2));
- this.y1 = this.y0 + (random.nextInt(h / 1) - (h / 2));
+ this.x1 = this.x0 + (random.nextInt(sprites.width) - (sprites.width / 2));
+ this.y1 = this.y0 + (random.nextInt(sprites.height) - (sprites.height / 2));
}
@Override
@@ -76,10 +63,10 @@ public class SpriteAnimation implements Disposable, Animation
@Override
public void draw(Batch batch)
{
- int n = (((int)(elapsed / frameDuration)) % frames.length);
+ int n = (((int)(elapsed / frameDuration)) % sprites.frames.length);
if ((n > 0) && (n % randFreq) == 0)
randPos();
- batch.draw(frames[n], x1, y1, w, h);
+ batch.draw(sprites.frames[n], x1, y1);
}
@Override