add attack sound

This commit is contained in:
Quillraven
2025-06-08 21:22:20 +02:00
parent bcac289738
commit 9baae0a410
7 changed files with 31 additions and 4 deletions

BIN
assets/audio/swing.wav Normal file

Binary file not shown.

View File

@@ -5,6 +5,7 @@
<properties>
<property name="animation" value="IDLE"/>
<property name="animationSpeed" type="float" value="1"/>
<property name="attackSound" value="SWING"/>
<property name="damage" type="float" value="7"/>
<property name="damageDelay" type="float" value="0.2"/>
<property name="life" type="int" value="12"/>

View File

@@ -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;

View File

@@ -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<Attack> 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;
}
}

View File

@@ -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.

View File

@@ -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();

View File

@@ -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) {