diff --git a/core/src/com/dungeoncrawler/control/Controller.java b/core/src/com/dungeoncrawler/control/Controller.java index 9e9a3ef..9571a15 100644 --- a/core/src/com/dungeoncrawler/control/Controller.java +++ b/core/src/com/dungeoncrawler/control/Controller.java @@ -126,7 +126,18 @@ public class Controller extends ApplicationAdapter implements InputProcessor{ d.getCurrentEntities()[i].attack(d.getPlayer()); } else{ - + switch(m.entitySprites[i].getAttackState()){ + case 0: + m.entitySprites[i].startAttack(); + break; + case 1: + break; + case 2: + d.getCurrentEntities()[i].attack(d.getPlayer()); + m.entitySprites[i].resetAttackState(); + break; + default: + } } } diff --git a/core/src/com/dungeoncrawler/view/EntitySprite.java b/core/src/com/dungeoncrawler/view/EntitySprite.java index d3c6e7f..fe848dc 100644 --- a/core/src/com/dungeoncrawler/view/EntitySprite.java +++ b/core/src/com/dungeoncrawler/view/EntitySprite.java @@ -22,6 +22,7 @@ public class EntitySprite { private Rectangle fullCollisionSprite; private TextureRegion[][][] regions; private int[] frames; + private int attackState; // 0: links, 1: rechts private int direction; @@ -29,20 +30,52 @@ public class EntitySprite { public EntitySprite(Texture[] textures){ sprites = new Sprite[1]; regions = new TextureRegion[1][][]; - frames = new int[2]; + + // 0: idle, 1: walking, 2: attack + frames = new int[3]; direction = 0; + attackState = 0; for(int i = 0; i < regions.length; i++){ regions[i] = TextureRegion.split(textures[i], 64, 64); sprites[i] = new Sprite(regions[i][0][0]); } - + collisionSprite = new Rectangle(0, 0, 32, 16); fullCollisionSprite = sprites[0].getBoundingRectangle(); } + public void updateAnimation(boolean moves){ + if(attackState == 1){ + updateAttack(); + } + else if(moves){ + updateWalking(); + } + else{ + updateIdle(); + } + } + + public void updateAttack(){ + frames[0] = 0; + frames[1] = 0; + + if(frames[2] >= 9){ + frames[2] = 0; + attackState = 2; + } + else{ + frames[2]++; + + sprites[0].setRegion(regions[0][3][frames[2]]); + updateFlip(); + } + } + public void updateIdle(){ frames[1] = 0; + frames[2] = 0; if(frames[0] >= 9){ frames[0] = 0; @@ -57,6 +90,7 @@ public class EntitySprite { public void updateWalking(){ frames[0] = 0; + frames[2] = 0; if(frames[1] >= 9){ frames[1] = 0; @@ -174,4 +208,16 @@ public class EntitySprite { public Rectangle getFullCollisionSprite() { return fullCollisionSprite; } + + public int getAttackState(){ + return this.attackState; + } + + public void resetAttackState(){ + this.attackState = 0; + } + + public void startAttack(){ + this.attackState = 1; + } } diff --git a/core/src/com/dungeoncrawler/view/GameScreen.java b/core/src/com/dungeoncrawler/view/GameScreen.java index 4c54a6d..e5b8549 100644 --- a/core/src/com/dungeoncrawler/view/GameScreen.java +++ b/core/src/com/dungeoncrawler/view/GameScreen.java @@ -142,6 +142,12 @@ public class GameScreen { else{ player.updateWalking(); } + + for(int i = 0; i < entitySprites.length; i++){ + if(entitySprites[i] != null){ + entitySprites[i].updateAnimation(true); + } + } } }, 0, animationSpeed);