Added sounds to actions. (Credit: Allexit)

This commit is contained in:
Jeasonfire 2015-05-18 01:10:20 +03:00
parent 814f9c9788
commit 43150f0de1
14 changed files with 109 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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)];
}
}