From 43150f0de196610c3ee0394caed3661f4f64b330 Mon Sep 17 00:00:00 2001 From: Jeasonfire Date: Mon, 18 May 2015 01:10:20 +0300 Subject: [PATCH] Added sounds to actions. (Credit: Allexit) --- .../gladiator/components/CPhysics.java | 3 +++ .../gui/creators/GameOverGUICreator.java | 2 ++ .../gui/creators/MainMenuGUICreator.java | 2 ++ .../gladiator/gui/creators/WinGUICreator.java | 2 ++ .../gladiator/gui/nodes/ButtonNode.java | 13 ++++++++++ .../listeners/BasicDeathListener.java | 19 ++++++++++++++ .../listeners/SwingHitboxListener.java | 10 ++++++++ .../gladiator/state/InGameState.java | 6 +++++ .../gladiator/state/MainMenuState.java | 6 +++++ .../gladiator/systems/CombatSystem.java | 16 +++++++++--- .../gladiator/systems/PhysicsSystem.java | 6 +++++ .../gladiator/systems/RenderingSystem.java | 25 +++++++++++++++---- .../com/saltosion/gladiator/util/AppUtil.java | 3 +++ .../com/saltosion/gladiator/util/Jukebox.java | 4 +++ 14 files changed, 109 insertions(+), 8 deletions(-) diff --git a/core/src/com/saltosion/gladiator/components/CPhysics.java b/core/src/com/saltosion/gladiator/components/CPhysics.java index 2f3ac03..27ca2d8 100644 --- a/core/src/com/saltosion/gladiator/components/CPhysics.java +++ b/core/src/com/saltosion/gladiator/components/CPhysics.java @@ -28,6 +28,9 @@ public class CPhysics extends Component { // Stores information about the direction last time moved in public boolean movedLeftLast = false; + + // Stores a float that tells how long the physics object must wait until it can play sounds with it's walking again. + public float stepCD = 0; /** * @param movable Toggles if the entity can move by itself diff --git a/core/src/com/saltosion/gladiator/gui/creators/GameOverGUICreator.java b/core/src/com/saltosion/gladiator/gui/creators/GameOverGUICreator.java index 3153fd4..017031a 100644 --- a/core/src/com/saltosion/gladiator/gui/creators/GameOverGUICreator.java +++ b/core/src/com/saltosion/gladiator/gui/creators/GameOverGUICreator.java @@ -31,10 +31,12 @@ public class GameOverGUICreator implements GUICreator { SpriteLoader.loadSprite(Name.BUTTON_BIG_HOVER)) { @Override public void pressed(int x, int y, int mouseButton) { + playButtonPressSound(); } @Override public void released(int x, int y, int mouseButton) { + playButtonReleaseSound(); shouldReturn = true; } }; diff --git a/core/src/com/saltosion/gladiator/gui/creators/MainMenuGUICreator.java b/core/src/com/saltosion/gladiator/gui/creators/MainMenuGUICreator.java index 958feb1..174376d 100644 --- a/core/src/com/saltosion/gladiator/gui/creators/MainMenuGUICreator.java +++ b/core/src/com/saltosion/gladiator/gui/creators/MainMenuGUICreator.java @@ -27,10 +27,12 @@ public class MainMenuGUICreator implements GUICreator { SpriteLoader.loadSprite(Name.BUTTON_BIG_HOVER)) { @Override public void pressed(int x, int y, int mouseButton) { + playButtonPressSound(); } @Override public void released(int x, int y, int mouseButton) { + playButtonReleaseSound(); shouldPlay = true; } }; diff --git a/core/src/com/saltosion/gladiator/gui/creators/WinGUICreator.java b/core/src/com/saltosion/gladiator/gui/creators/WinGUICreator.java index 743bdb1..7f1d117 100644 --- a/core/src/com/saltosion/gladiator/gui/creators/WinGUICreator.java +++ b/core/src/com/saltosion/gladiator/gui/creators/WinGUICreator.java @@ -31,10 +31,12 @@ public class WinGUICreator implements GUICreator { SpriteLoader.loadSprite(Name.BUTTON_BIG_HOVER)) { @Override public void pressed(int x, int y, int mouseButton) { + playButtonPressSound(); } @Override public void released(int x, int y, int mouseButton) { + playButtonReleaseSound(); shouldReturn = true; } }; diff --git a/core/src/com/saltosion/gladiator/gui/nodes/ButtonNode.java b/core/src/com/saltosion/gladiator/gui/nodes/ButtonNode.java index ed6cad2..829acb8 100644 --- a/core/src/com/saltosion/gladiator/gui/nodes/ButtonNode.java +++ b/core/src/com/saltosion/gladiator/gui/nodes/ButtonNode.java @@ -4,6 +4,9 @@ import com.saltosion.gladiator.gui.properties.ImageProperty; import com.saltosion.gladiator.gui.properties.InteractiveProperty; import com.saltosion.gladiator.gui.nodes.GUINode; import com.badlogic.gdx.graphics.g2d.Sprite; +import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.AudioLoader; +import com.saltosion.gladiator.util.Name; public abstract class ButtonNode extends GUINode implements InteractiveProperty, ImageProperty { private final Sprite onHover; @@ -15,6 +18,16 @@ public abstract class ButtonNode extends GUINode implements InteractiveProperty, this.onHover = onHover; this.normal = normal; } + + public void playButtonPressSound() { + AppUtil.jukebox.playSound(AudioLoader.getSound(Name.SOUND_BUTTON_PRESS), + AppUtil.sfxVolume/2); + } + + public void playButtonReleaseSound() { + AppUtil.jukebox.playSound(AudioLoader.getSound(Name.SOUND_BUTTON_RELEASE), + AppUtil.sfxVolume/2); + } @Override public void mouseEnter(float x, float y) { diff --git a/core/src/com/saltosion/gladiator/listeners/BasicDeathListener.java b/core/src/com/saltosion/gladiator/listeners/BasicDeathListener.java index 8e3660d..320fb79 100644 --- a/core/src/com/saltosion/gladiator/listeners/BasicDeathListener.java +++ b/core/src/com/saltosion/gladiator/listeners/BasicDeathListener.java @@ -1,10 +1,13 @@ package com.saltosion.gladiator.listeners; import com.badlogic.ashley.core.Entity; +import com.badlogic.gdx.audio.Sound; import com.saltosion.gladiator.components.CParticle; import com.saltosion.gladiator.components.CPhysics; import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.AudioLoader; import com.saltosion.gladiator.util.Global; +import com.saltosion.gladiator.util.Name; public class BasicDeathListener implements CombatListener { @@ -16,6 +19,14 @@ public class BasicDeathListener implements CombatListener { target.flags &= ~Global.FLAG_ALIVE; CPhysics cp = target.getComponent(CPhysics.class); + + Sound s = AppUtil.jukebox.returnRandomSound(AudioLoader.getSound(Name.SOUND_HIT01), + AudioLoader.getSound(Name.SOUND_HIT02), + AudioLoader.getSound(Name.SOUND_HIT03), + AudioLoader.getSound(Name.SOUND_HIT04), + AudioLoader.getSound(Name.SOUND_HIT05)); + s.play(AppUtil.sfxVolume); + for (int i = 0; i < FX_DEAD_AMT; i++) { Entity fx = new Entity(); fx.add(new CParticle().setColor(1, 0, 0, 1).setDecayTime(2).setGravity(0, FX_GRAV) @@ -31,6 +42,14 @@ public class BasicDeathListener implements CombatListener { @Override public void damageTaken(Entity source, Entity target, int damageTaken) { CPhysics cp = target.getComponent(CPhysics.class); + + Sound s = AppUtil.jukebox.returnRandomSound(AudioLoader.getSound(Name.SOUND_HIT01), + AudioLoader.getSound(Name.SOUND_HIT02), + AudioLoader.getSound(Name.SOUND_HIT03), + AudioLoader.getSound(Name.SOUND_HIT04), + AudioLoader.getSound(Name.SOUND_HIT05)); + s.play(AppUtil.sfxVolume); + for (int i = 0; i < FX_HIT_AMT; i++) { Entity fx = new Entity(); fx.add(new CParticle().setColor(1, 0, 0, 1).setDecayTime(2).setGravity(0, FX_GRAV) diff --git a/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java b/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java index d9dcabd..8698a44 100644 --- a/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java +++ b/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java @@ -4,10 +4,14 @@ import java.util.ArrayList; import com.badlogic.ashley.core.ComponentMapper; import com.badlogic.ashley.core.Entity; +import com.badlogic.gdx.audio.Sound; import com.saltosion.gladiator.components.CCombat; import com.saltosion.gladiator.components.CPhysics; import com.saltosion.gladiator.systems.CombatSystem; +import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.AudioLoader; import com.saltosion.gladiator.util.Direction; +import com.saltosion.gladiator.util.Name; public class SwingHitboxListener implements CollisionListener { @@ -46,6 +50,12 @@ public class SwingHitboxListener implements CollisionListener { } float force = cm.get(source).getSwingForce(); pm.get(source).setSimVelocity(x * force, 0); + + Sound s = AppUtil.jukebox.returnRandomSound(AudioLoader.getSound(Name.SOUND_CLANG01), + AudioLoader.getSound(Name.SOUND_CLANG02), + AudioLoader.getSound(Name.SOUND_CLANG03), + AudioLoader.getSound(Name.SOUND_CLANG04)); + s.play(AppUtil.sfxVolume); } } diff --git a/core/src/com/saltosion/gladiator/state/InGameState.java b/core/src/com/saltosion/gladiator/state/InGameState.java index 1e7a44a..df63f0f 100644 --- a/core/src/com/saltosion/gladiator/state/InGameState.java +++ b/core/src/com/saltosion/gladiator/state/InGameState.java @@ -5,6 +5,8 @@ import com.saltosion.gladiator.level.Level; import com.saltosion.gladiator.level.premade.Round1Level; import com.saltosion.gladiator.level.premade.Round2Level; import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.AudioLoader; +import com.saltosion.gladiator.util.Name; public class InGameState extends BaseState { @@ -29,6 +31,10 @@ public class InGameState extends BaseState { @Override public void create() { + // Play music + AppUtil.jukebox.playMusic(AudioLoader.getMusic(Name.MUSIC_BATTLE)); + AppUtil.jukebox.setMusicVolume(AppUtil.musicVolume/2); + // Start from a clean slate AppUtil.engine.removeAllEntities(); AppUtil.guiManager.clearGUI(); diff --git a/core/src/com/saltosion/gladiator/state/MainMenuState.java b/core/src/com/saltosion/gladiator/state/MainMenuState.java index 221bc15..f7e6c27 100644 --- a/core/src/com/saltosion/gladiator/state/MainMenuState.java +++ b/core/src/com/saltosion/gladiator/state/MainMenuState.java @@ -2,6 +2,8 @@ package com.saltosion.gladiator.state; import com.saltosion.gladiator.gui.creators.MainMenuGUICreator; import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.AudioLoader; +import com.saltosion.gladiator.util.Name; public class MainMenuState extends BaseState { @@ -9,6 +11,10 @@ public class MainMenuState extends BaseState { @Override public void create() { + // Play music + AppUtil.jukebox.playMusic(AudioLoader.getMusic(Name.MUSIC_THEME)); + AppUtil.jukebox.setMusicVolume(AppUtil.musicVolume); + // Start from a clean slate AppUtil.guiManager.clearGUI(); diff --git a/core/src/com/saltosion/gladiator/systems/CombatSystem.java b/core/src/com/saltosion/gladiator/systems/CombatSystem.java index 0c77494..6e67193 100644 --- a/core/src/com/saltosion/gladiator/systems/CombatSystem.java +++ b/core/src/com/saltosion/gladiator/systems/CombatSystem.java @@ -6,18 +6,17 @@ import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.EntitySystem; import com.badlogic.ashley.core.Family; import com.badlogic.ashley.utils.ImmutableArray; -import com.badlogic.gdx.graphics.g2d.Sprite; +import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.math.Vector2; import com.saltosion.gladiator.components.CCombat; import com.saltosion.gladiator.components.CDestructive; import com.saltosion.gladiator.components.CPhysics; -import com.saltosion.gladiator.components.CRenderedObject; import com.saltosion.gladiator.listeners.CombatListener; import com.saltosion.gladiator.listeners.SwingHitboxListener; import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.AudioLoader; import com.saltosion.gladiator.util.Direction; import com.saltosion.gladiator.util.Name; -import com.saltosion.gladiator.util.SpriteLoader; public class CombatSystem extends EntitySystem { @@ -60,6 +59,8 @@ public class CombatSystem extends EntitySystem { } if (!combat.getSwing().isZero() && combat.swingCdCounter <= 0) { + + // Swinging Vector2 pos = obj.getPosition().cpy(); if (combat.getSwingDirection() == Direction.LEFT) { @@ -72,7 +73,16 @@ public class CombatSystem extends EntitySystem { pos.add(0, -combat.getSwingSize().y / 3 * 2); } createSwingHitbox(e, combat.getSwingDirection(), pos); + + // SFX + Sound s = AppUtil.jukebox.returnRandomSound(AudioLoader.getSound(Name.SOUND_SWING01), + AudioLoader.getSound(Name.SOUND_SWING02), + AudioLoader.getSound(Name.SOUND_SWING03)); + s.play(AppUtil.sfxVolume); + + // After-swing + combat.swingCdCounter = combat.getSwingDuration(); } } diff --git a/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java b/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java index f21a458..26f31a5 100644 --- a/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java +++ b/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java @@ -8,8 +8,11 @@ import com.badlogic.ashley.core.Family; import com.badlogic.ashley.utils.ImmutableArray; import com.saltosion.gladiator.components.CCombat; import com.saltosion.gladiator.components.CPhysics; +import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.AudioLoader; import com.saltosion.gladiator.util.Direction; import com.saltosion.gladiator.util.Log; +import com.saltosion.gladiator.util.Name; public class PhysicsSystem extends EntitySystem { @@ -58,6 +61,9 @@ public class PhysicsSystem extends EntitySystem { if (obj.jumping && obj.isGrounded()) { obj.setGrounded(false); obj.getVelocity().y = obj.getJumpForce(); + + // Sound effect! + AppUtil.jukebox.playSound(AudioLoader.getSound(Name.SOUND_STEP), AppUtil.sfxVolume); } obj.getVelocity().x += obj.getSimVelocity().x; diff --git a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java index 88da657..8216231 100644 --- a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java +++ b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java @@ -28,6 +28,7 @@ import com.saltosion.gladiator.gui.properties.ImageProperty; import com.saltosion.gladiator.gui.nodes.TextNode; import com.saltosion.gladiator.gui.properties.TextProperty; import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.AudioLoader; import com.saltosion.gladiator.util.Global; import com.saltosion.gladiator.util.Name; import com.saltosion.gladiator.util.SpriteLoader; @@ -101,7 +102,7 @@ public class RenderingSystem extends EntitySystem { Gdx.gl.glClearColor(0, 0, 0, 0); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - updateEntityAnimations(); + updateEntityAnimations(deltaTime); renderEntities(deltaTime); renderParticles(); renderDebug(); @@ -134,13 +135,13 @@ public class RenderingSystem extends EntitySystem { return deltaString; } - private void updateEntityAnimations() { + private void updateEntityAnimations(float deltaTime) { for (int i = 0; i < entities.size(); i++) { - updateEntityAnimation(entities.get(i)); + updateEntityAnimation(entities.get(i), deltaTime); } } - private void updateEntityAnimation(Entity entity) { + private void updateEntityAnimation(Entity entity, float deltaTime) { CRenderedObject ro = rom.get(entity); CPhysics po = pm.get(entity); CCombat co = cm.get(entity); @@ -184,21 +185,35 @@ public class RenderingSystem extends EntitySystem { } } - // Play animations + // Play animations & play sounds + if (po.stepCD > 0) { + po.stepCD -= deltaTime; + } + if (moving && combat) { ro.playAnimation("torso", "Torso-Combat-" + dirSwing); ro.playAnimation("legs", "Legs-Run-" + dirMove); + tryToMakeStepSound(po); } else if (combat) { ro.playAnimation("torso", "Torso-Combat-" + dirSwing); ro.playAnimation("legs", "Legs-Idle-" + dirMove); } else if (moving) { ro.playAnimation("torso", "Torso-Run-" + dirMove); ro.playAnimation("legs", "Legs-Run-" + dirMove); + tryToMakeStepSound(po); } else { ro.playAnimation("torso", "Torso-Idle-" + dirMove); ro.playAnimation("legs", "Legs-Idle-" + dirMove); } } + + private void tryToMakeStepSound(CPhysics po) { + if (po.stepCD > 0 || !po.isGrounded()) { + return; + } + po.stepCD = 0.3f; + AppUtil.jukebox.playSound(AudioLoader.getSound(Name.SOUND_STEP), AppUtil.sfxVolume/3*2); + } private void renderEntities(float deltaTime) { if (AppUtil.player == null) { diff --git a/core/src/com/saltosion/gladiator/util/AppUtil.java b/core/src/com/saltosion/gladiator/util/AppUtil.java index a9007c1..39b178c 100644 --- a/core/src/com/saltosion/gladiator/util/AppUtil.java +++ b/core/src/com/saltosion/gladiator/util/AppUtil.java @@ -17,6 +17,9 @@ public class AppUtil { public static GUIManager guiManager; public static final int VPHEIGHT_CONST = 24; + + public static float sfxVolume = 0.3f; + public static float musicVolume = 0.7f; public static final Vector2 JUMP_FORCE = new Vector2(0, 12000); } diff --git a/core/src/com/saltosion/gladiator/util/Jukebox.java b/core/src/com/saltosion/gladiator/util/Jukebox.java index e75eaed..d4dc4eb 100644 --- a/core/src/com/saltosion/gladiator/util/Jukebox.java +++ b/core/src/com/saltosion/gladiator/util/Jukebox.java @@ -72,4 +72,8 @@ public class Jukebox { sound.setPan(id, pan, volume); return id; } + + public Sound returnRandomSound(Sound ... args) { + return args[(int) Math.floor(Math.random()*args.length)]; + } }