Permalink
Browse files

Added animation for Bob

  • Loading branch information...
1 parent 555867b commit 88ea6a5db6158b07bcb372b23e9f75b47bb01452 Impaler committed Feb 8, 2013
@@ -10,45 +10,45 @@ block
orig: 48, 48
offset: 0, 0
index: -1
-bob
+bob-01
rotate: false
xy: 51, 31
size: 24, 28
orig: 24, 28
offset: 0, 0
- index: 1
-bob
+ index: -1
+bob-02
rotate: false
xy: 77, 31
size: 24, 28
orig: 24, 28
offset: 0, 0
- index: 2
-bob
+ index: -1
+bob-05
rotate: false
xy: 103, 31
size: 24, 28
orig: 24, 28
offset: 0, 0
- index: 5
-bob
+ index: -1
+bob-03
rotate: false
xy: 51, 3
size: 24, 26
orig: 24, 26
offset: 0, 0
- index: 3
-bob
+ index: -1
+bob-04
rotate: false
xy: 77, 3
size: 24, 26
orig: 24, 26
offset: 0, 0
- index: 4
-bob
+ index: -1
+bob-06
rotate: false
xy: 103, 1
size: 24, 28
orig: 24, 28
offset: 0, 0
- index: 6
+ index: -1
@@ -19,6 +19,7 @@
Rectangle bounds = new Rectangle();
State state = State.IDLE;
boolean facingLeft = true;
+ float stateTime = 0;
public Bob(Vector2 position) {
this.position = position;
@@ -58,8 +59,15 @@ public State getState() {
public void setState(State newState) {
this.state = newState;
}
-
+
+ public float getStateTime() {
+ return stateTime;
+ }
+
+
public void update(float delta) {
+ stateTime += delta;
position.add(velocity.tmp().mul(delta));
}
+
}
@@ -5,6 +5,7 @@
//import com.badlogic.gdx.tools.imagepacker.TexturePacker2;
+
/**
* @author tamas
*
@@ -2,13 +2,16 @@
import net.obviam.starassault.model.Block;
import net.obviam.starassault.model.Bob;
+import net.obviam.starassault.model.Bob.State;
import net.obviam.starassault.model.World;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
-import com.badlogic.gdx.graphics.Texture;
+import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+import com.badlogic.gdx.graphics.g2d.TextureAtlas;
+import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
@@ -25,8 +28,14 @@
ShapeRenderer debugRenderer = new ShapeRenderer();
/** Textures **/
- private Texture bobTexture;
- private Texture blockTexture;
+ private TextureRegion bobIdleLeft;
+ private TextureRegion bobIdleRight;
+ private TextureRegion blockTexture;
+ private TextureRegion bobFrame;
+
+ /** Animations **/
+ private Animation walkLeftAnimation;
+ private Animation walkRightAnimation;
private SpriteBatch spriteBatch;
private boolean debug = false;
@@ -52,8 +61,24 @@ public WorldRenderer(World world, boolean debug) {
}
private void loadTextures() {
- bobTexture = new Texture(Gdx.files.internal("images/bob_01.png"));
- blockTexture = new Texture(Gdx.files.internal("images/block.png"));
+ TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("images/textures/textures.pack"));
+ bobIdleLeft = atlas.findRegion("bob-01");
+ bobIdleRight = new TextureRegion(bobIdleLeft);
+ bobIdleRight.flip(true, false);
+ blockTexture = atlas.findRegion("block");
+ TextureRegion[] walkLeftFrames = new TextureRegion[5];
+ for (int i = 0; i < 5; i++) {
+ walkLeftFrames[i] = atlas.findRegion("bob-0" + (i + 2));
+ }
+ walkLeftAnimation = new Animation(0.1f, walkLeftFrames);
+
+ TextureRegion[] walkRightFrames = new TextureRegion[5];
+
+ for (int i = 0; i < 5; i++) {
+ walkRightFrames[i] = new TextureRegion(walkLeftFrames[i]);
+ walkRightFrames[i].flip(true, false);
+ }
+ walkRightAnimation = new Animation(0.1f, walkRightFrames);
}
@@ -75,7 +100,11 @@ private void drawBlocks() {
private void drawBob() {
Bob bob = world.getBob();
- spriteBatch.draw(bobTexture, bob.getPosition().x * ppuX, bob.getPosition().y * ppuY, Bob.SIZE * ppuX, Bob.SIZE * ppuY);
+ bobFrame = bobIdleLeft;
+ if(bob.getState().equals(State.WALKING)) {
+ bobFrame = bob.isFacingLeft() ? walkLeftAnimation.getKeyFrame(bob.getStateTime(), true) : walkRightAnimation.getKeyFrame(bob.getStateTime(), true);
+ }
+ spriteBatch.draw(bobFrame, bob.getPosition().x * ppuX, bob.getPosition().y * ppuY, Bob.SIZE * ppuX, Bob.SIZE * ppuY);
}
private void drawDebug() {

0 comments on commit 88ea6a5

Please sign in to comment.