add attack sound
This commit is contained in:
BIN
assets/audio/swing.wav
Normal file
BIN
assets/audio/swing.wav
Normal file
Binary file not shown.
@@ -5,6 +5,7 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<property name="animation" value="IDLE"/>
|
<property name="animation" value="IDLE"/>
|
||||||
<property name="animationSpeed" type="float" value="1"/>
|
<property name="animationSpeed" type="float" value="1"/>
|
||||||
|
<property name="attackSound" value="SWING"/>
|
||||||
<property name="damage" type="float" value="7"/>
|
<property name="damage" type="float" value="7"/>
|
||||||
<property name="damageDelay" type="float" value="0.2"/>
|
<property name="damageDelay" type="float" value="0.2"/>
|
||||||
<property name="life" type="int" value="12"/>
|
<property name="life" type="int" value="12"/>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ public enum SoundAsset {
|
|||||||
SWORD_HIT("sword_hit.wav"),
|
SWORD_HIT("sword_hit.wav"),
|
||||||
LIFE_REG("life_reg.wav"),
|
LIFE_REG("life_reg.wav"),
|
||||||
TRAP("trap.wav"),
|
TRAP("trap.wav"),
|
||||||
|
SWING("swing.wav"),
|
||||||
;
|
;
|
||||||
|
|
||||||
private final String path;
|
private final String path;
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package io.github.com.quillraven.component;
|
|||||||
|
|
||||||
import com.badlogic.ashley.core.Component;
|
import com.badlogic.ashley.core.Component;
|
||||||
import com.badlogic.ashley.core.ComponentMapper;
|
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 class Attack implements Component {
|
||||||
public static final ComponentMapper<Attack> MAPPER = ComponentMapper.getFor(Attack.class);
|
public static final ComponentMapper<Attack> MAPPER = ComponentMapper.getFor(Attack.class);
|
||||||
@@ -9,10 +11,12 @@ public class Attack implements Component {
|
|||||||
private float damage;
|
private float damage;
|
||||||
private float damageDelay;
|
private float damageDelay;
|
||||||
private float attackTimer;
|
private float attackTimer;
|
||||||
|
private SoundAsset sfx;
|
||||||
|
|
||||||
public Attack(float damage, float damageDelay) {
|
public Attack(float damage, float damageDelay, SoundAsset sfx) {
|
||||||
this.damage = damage;
|
this.damage = damage;
|
||||||
this.damageDelay = damageDelay;
|
this.damageDelay = damageDelay;
|
||||||
|
this.sfx = sfx;
|
||||||
this.attackTimer = 0f;
|
this.attackTimer = 0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,6 +28,10 @@ public class Attack implements Component {
|
|||||||
return this.attackTimer > 0f;
|
return this.attackTimer > 0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasAttackStarted() {
|
||||||
|
return MathUtils.isEqual(this.attackTimer, this.damageDelay, 0.0001f);
|
||||||
|
}
|
||||||
|
|
||||||
public void startAttack() {
|
public void startAttack() {
|
||||||
this.attackTimer = this.damageDelay;
|
this.attackTimer = this.damageDelay;
|
||||||
}
|
}
|
||||||
@@ -36,4 +44,7 @@ public class Attack implements Component {
|
|||||||
return damage;
|
return damage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SoundAsset getSfx() {
|
||||||
|
return sfx;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ public class GameScreen extends ScreenAdapter {
|
|||||||
this.engine.addSystem(new PhysicMoveSystem());
|
this.engine.addSystem(new PhysicMoveSystem());
|
||||||
this.engine.addSystem(new PhysicSystem(physicWorld, 1 / 60f));
|
this.engine.addSystem(new PhysicSystem(physicWorld, 1 / 60f));
|
||||||
this.engine.addSystem(new FacingSystem());
|
this.engine.addSystem(new FacingSystem());
|
||||||
this.engine.addSystem(new AttackSystem(physicWorld));
|
this.engine.addSystem(new AttackSystem(physicWorld, audioService));
|
||||||
this.engine.addSystem(new FsmSystem());
|
this.engine.addSystem(new FsmSystem());
|
||||||
// DamagedSystem must run after FsmSystem to correctly
|
// DamagedSystem must run after FsmSystem to correctly
|
||||||
// detect when a damaged animation should be played.
|
// detect when a damaged animation should be played.
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import com.badlogic.gdx.physics.box2d.Shape.Type;
|
|||||||
import com.badlogic.gdx.physics.box2d.World;
|
import com.badlogic.gdx.physics.box2d.World;
|
||||||
import com.badlogic.gdx.utils.Array;
|
import com.badlogic.gdx.utils.Array;
|
||||||
import com.badlogic.gdx.utils.GdxRuntimeException;
|
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.Attack;
|
||||||
import io.github.com.quillraven.component.Damaged;
|
import io.github.com.quillraven.component.Damaged;
|
||||||
import io.github.com.quillraven.component.Facing;
|
import io.github.com.quillraven.component.Facing;
|
||||||
@@ -22,13 +23,15 @@ import io.github.com.quillraven.component.Physic;
|
|||||||
public class AttackSystem extends IteratingSystem {
|
public class AttackSystem extends IteratingSystem {
|
||||||
public static final Rectangle attackAABB = new Rectangle();
|
public static final Rectangle attackAABB = new Rectangle();
|
||||||
|
|
||||||
|
private final AudioService audioService;
|
||||||
private final World world;
|
private final World world;
|
||||||
private final Vector2 tmpVertex;
|
private final Vector2 tmpVertex;
|
||||||
private Body attackerBody;
|
private Body attackerBody;
|
||||||
private float attackDamage;
|
private float attackDamage;
|
||||||
|
|
||||||
public AttackSystem(World world) {
|
public AttackSystem(World world, AudioService audioService) {
|
||||||
super(Family.all(Attack.class, Facing.class, Physic.class).get());
|
super(Family.all(Attack.class, Facing.class, Physic.class).get());
|
||||||
|
this.audioService = audioService;
|
||||||
this.world = world;
|
this.world = world;
|
||||||
this.tmpVertex = new Vector2();
|
this.tmpVertex = new Vector2();
|
||||||
this.attackerBody = null;
|
this.attackerBody = null;
|
||||||
@@ -38,8 +41,13 @@ public class AttackSystem extends IteratingSystem {
|
|||||||
@Override
|
@Override
|
||||||
protected void processEntity(Entity entity, float deltaTime) {
|
protected void processEntity(Entity entity, float deltaTime) {
|
||||||
Attack attack = Attack.MAPPER.get(entity);
|
Attack attack = Attack.MAPPER.get(entity);
|
||||||
|
// can attack = true means that the attack was not started yet
|
||||||
if (attack.canAttack()) return;
|
if (attack.canAttack()) return;
|
||||||
|
|
||||||
|
if (attack.hasAttackStarted() && attack.getSfx() != null) {
|
||||||
|
audioService.playSound(attack.getSfx());
|
||||||
|
}
|
||||||
|
|
||||||
attack.decAttackTimer(deltaTime);
|
attack.decAttackTimer(deltaTime);
|
||||||
if (attack.canAttack()) {
|
if (attack.canAttack()) {
|
||||||
FacingDirection facingDirection = Facing.MAPPER.get(entity).getDirection();
|
FacingDirection facingDirection = Facing.MAPPER.get(entity).getDirection();
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import com.badlogic.gdx.utils.GdxRuntimeException;
|
|||||||
import io.github.com.quillraven.GdxGame;
|
import io.github.com.quillraven.GdxGame;
|
||||||
import io.github.com.quillraven.asset.AssetService;
|
import io.github.com.quillraven.asset.AssetService;
|
||||||
import io.github.com.quillraven.asset.AtlasAsset;
|
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;
|
||||||
import io.github.com.quillraven.component.Animation2D.AnimationType;
|
import io.github.com.quillraven.component.Animation2D.AnimationType;
|
||||||
import io.github.com.quillraven.component.Attack;
|
import io.github.com.quillraven.component.Attack;
|
||||||
@@ -140,7 +141,12 @@ public class TiledAshleyConfigurator {
|
|||||||
if (damage == 0f) return;
|
if (damage == 0f) return;
|
||||||
|
|
||||||
float damageDelay = tile.getProperties().get("damageDelay", 0f, Float.class);
|
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) {
|
private void addEntityPlayer(TiledMapTileMapObject tileMapObject, Entity entity) {
|
||||||
|
|||||||
Reference in New Issue
Block a user