diff --git a/assets/audio/swing.wav b/assets/audio/swing.wav new file mode 100644 index 0000000..e59c6ae Binary files /dev/null and b/assets/audio/swing.wav differ diff --git a/assets/maps/objects.tsx b/assets/maps/objects.tsx index 37acace..e6bf845 100644 --- a/assets/maps/objects.tsx +++ b/assets/maps/objects.tsx @@ -5,6 +5,7 @@ + diff --git a/core/src/main/java/io/github/com/quillraven/asset/SoundAsset.java b/core/src/main/java/io/github/com/quillraven/asset/SoundAsset.java index 805bd8c..abf99fe 100644 --- a/core/src/main/java/io/github/com/quillraven/asset/SoundAsset.java +++ b/core/src/main/java/io/github/com/quillraven/asset/SoundAsset.java @@ -4,6 +4,7 @@ public enum SoundAsset { SWORD_HIT("sword_hit.wav"), LIFE_REG("life_reg.wav"), TRAP("trap.wav"), + SWING("swing.wav"), ; private final String path; diff --git a/core/src/main/java/io/github/com/quillraven/component/Attack.java b/core/src/main/java/io/github/com/quillraven/component/Attack.java index 5a1b53d..b5c9aab 100644 --- a/core/src/main/java/io/github/com/quillraven/component/Attack.java +++ b/core/src/main/java/io/github/com/quillraven/component/Attack.java @@ -2,6 +2,8 @@ package io.github.com.quillraven.component; import com.badlogic.ashley.core.Component; import com.badlogic.ashley.core.ComponentMapper; +import com.badlogic.gdx.math.MathUtils; +import io.github.com.quillraven.asset.SoundAsset; public class Attack implements Component { public static final ComponentMapper MAPPER = ComponentMapper.getFor(Attack.class); @@ -9,10 +11,12 @@ public class Attack implements Component { private float damage; private float damageDelay; private float attackTimer; + private SoundAsset sfx; - public Attack(float damage, float damageDelay) { + public Attack(float damage, float damageDelay, SoundAsset sfx) { this.damage = damage; this.damageDelay = damageDelay; + this.sfx = sfx; this.attackTimer = 0f; } @@ -24,6 +28,10 @@ public class Attack implements Component { return this.attackTimer > 0f; } + public boolean hasAttackStarted() { + return MathUtils.isEqual(this.attackTimer, this.damageDelay, 0.0001f); + } + public void startAttack() { this.attackTimer = this.damageDelay; } @@ -36,4 +44,7 @@ public class Attack implements Component { return damage; } + public SoundAsset getSfx() { + return sfx; + } } diff --git a/core/src/main/java/io/github/com/quillraven/screen/GameScreen.java b/core/src/main/java/io/github/com/quillraven/screen/GameScreen.java index 5331989..dd0eb9a 100644 --- a/core/src/main/java/io/github/com/quillraven/screen/GameScreen.java +++ b/core/src/main/java/io/github/com/quillraven/screen/GameScreen.java @@ -69,7 +69,7 @@ public class GameScreen extends ScreenAdapter { this.engine.addSystem(new PhysicMoveSystem()); this.engine.addSystem(new PhysicSystem(physicWorld, 1 / 60f)); this.engine.addSystem(new FacingSystem()); - this.engine.addSystem(new AttackSystem(physicWorld)); + this.engine.addSystem(new AttackSystem(physicWorld, audioService)); this.engine.addSystem(new FsmSystem()); // DamagedSystem must run after FsmSystem to correctly // detect when a damaged animation should be played. diff --git a/core/src/main/java/io/github/com/quillraven/system/AttackSystem.java b/core/src/main/java/io/github/com/quillraven/system/AttackSystem.java index 307eb38..2376418 100644 --- a/core/src/main/java/io/github/com/quillraven/system/AttackSystem.java +++ b/core/src/main/java/io/github/com/quillraven/system/AttackSystem.java @@ -12,6 +12,7 @@ import com.badlogic.gdx.physics.box2d.Shape.Type; import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.GdxRuntimeException; +import io.github.com.quillraven.audio.AudioService; import io.github.com.quillraven.component.Attack; import io.github.com.quillraven.component.Damaged; import io.github.com.quillraven.component.Facing; @@ -22,13 +23,15 @@ import io.github.com.quillraven.component.Physic; public class AttackSystem extends IteratingSystem { public static final Rectangle attackAABB = new Rectangle(); + private final AudioService audioService; private final World world; private final Vector2 tmpVertex; private Body attackerBody; private float attackDamage; - public AttackSystem(World world) { + public AttackSystem(World world, AudioService audioService) { super(Family.all(Attack.class, Facing.class, Physic.class).get()); + this.audioService = audioService; this.world = world; this.tmpVertex = new Vector2(); this.attackerBody = null; @@ -38,8 +41,13 @@ public class AttackSystem extends IteratingSystem { @Override protected void processEntity(Entity entity, float deltaTime) { Attack attack = Attack.MAPPER.get(entity); + // can attack = true means that the attack was not started yet if (attack.canAttack()) return; + if (attack.hasAttackStarted() && attack.getSfx() != null) { + audioService.playSound(attack.getSfx()); + } + attack.decAttackTimer(deltaTime); if (attack.canAttack()) { FacingDirection facingDirection = Facing.MAPPER.get(entity).getDirection(); diff --git a/core/src/main/java/io/github/com/quillraven/tiled/TiledAshleyConfigurator.java b/core/src/main/java/io/github/com/quillraven/tiled/TiledAshleyConfigurator.java index 9c925cc..a729403 100644 --- a/core/src/main/java/io/github/com/quillraven/tiled/TiledAshleyConfigurator.java +++ b/core/src/main/java/io/github/com/quillraven/tiled/TiledAshleyConfigurator.java @@ -24,6 +24,7 @@ import com.badlogic.gdx.utils.GdxRuntimeException; import io.github.com.quillraven.GdxGame; import io.github.com.quillraven.asset.AssetService; import io.github.com.quillraven.asset.AtlasAsset; +import io.github.com.quillraven.asset.SoundAsset; import io.github.com.quillraven.component.Animation2D; import io.github.com.quillraven.component.Animation2D.AnimationType; import io.github.com.quillraven.component.Attack; @@ -140,7 +141,12 @@ public class TiledAshleyConfigurator { if (damage == 0f) return; float damageDelay = tile.getProperties().get("damageDelay", 0f, Float.class); - entity.add(new Attack(damage, damageDelay)); + String soundAssetStr = tile.getProperties().get("attackSound", String.class); + SoundAsset soundAsset = null; + if (soundAssetStr != null) { + soundAsset = SoundAsset.valueOf(soundAssetStr); + } + entity.add(new Attack(damage, damageDelay, soundAsset)); } private void addEntityPlayer(TiledMapTileMapObject tileMapObject, Entity entity) {