summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/ch/asynk')
-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