From 127d1c3d8774dec90d9eea6be7aacd2c5b5a2ec8 Mon Sep 17 00:00:00 2001 From: Eugenuss Date: Fri, 29 Jan 2021 18:35:05 +0300 Subject: [PATCH] Refactored Sound Engine - Renamed SoundEffect to Sound - Now Music inherits Sound - Now every sound has parameter timeLength --- .../progressia/client/audio/AudioManager.java | 15 ++++- .../progressia/client/audio/Music.java | 64 +++---------------- .../audio/{SoundEffect.java => Sound.java} | 32 ++++++---- .../windcorp/progressia/test/TestContent.java | 4 +- 4 files changed, 45 insertions(+), 70 deletions(-) rename src/main/java/ru/windcorp/progressia/client/audio/{SoundEffect.java => Sound.java} (76%) diff --git a/src/main/java/ru/windcorp/progressia/client/audio/AudioManager.java b/src/main/java/ru/windcorp/progressia/client/audio/AudioManager.java index 0078b56..1647e29 100644 --- a/src/main/java/ru/windcorp/progressia/client/audio/AudioManager.java +++ b/src/main/java/ru/windcorp/progressia/client/audio/AudioManager.java @@ -94,15 +94,26 @@ public class AudioManager { ); } - public static Speaker initSpeaker(String soundID) { + public static Speaker initSpeaker(SoundType st) { Speaker speaker = getLastSpeaker(); try { - findSoundType(soundID).initSpeaker(speaker); + st.initSpeaker(speaker); } catch (Exception ex) { throw new RuntimeException(); } return speaker; } + + public static SoundType getSoundType(String id) { + SoundType st; + try { + st = findSoundType(id); + } catch (Exception ex) { + throw new RuntimeException(); + } + return st; + } + public static Speaker initMusicSpeaker(String soundID) { try { diff --git a/src/main/java/ru/windcorp/progressia/client/audio/Music.java b/src/main/java/ru/windcorp/progressia/client/audio/Music.java index 9be9cc3..d461d72 100644 --- a/src/main/java/ru/windcorp/progressia/client/audio/Music.java +++ b/src/main/java/ru/windcorp/progressia/client/audio/Music.java @@ -19,72 +19,28 @@ package ru.windcorp.progressia.client.audio; import glm.vec._3.Vec3; -import ru.windcorp.progressia.client.audio.backend.Speaker; -import ru.windcorp.progressia.common.util.namespaces.Namespaced; - -public class Music extends Namespaced { - private Vec3 position = new Vec3(); - private Vec3 velocity = new Vec3(); - private float pitch = 1.0f; - private float gain = 1.0f; +public class Music + extends Sound { + public Music(String id) { super(id); } public Music( String id, - Vec3 position, Vec3 velocity, float pitch, float gain ) { this(id); - this.position = position; - this.velocity = velocity; - this.pitch = pitch; - this.gain = gain; + super.velocity = velocity; + super.pitch = pitch; + super.gain = gain; } - - public void play(boolean loop) { - Speaker speaker = AudioManager.initMusicSpeaker(this.getId()); - speaker.setGain(gain); - speaker.setPitch(pitch); - speaker.setPosition(position); - speaker.setVelocity(velocity); - - if (loop) { - speaker.playLoop(); - } else { - speaker.play(); - } - } - - public void setGain(float gain) { - this.gain = gain; - } - - public void setPitch(float pitch) { - this.pitch = pitch; - } - - public void setVelocity(Vec3 velocity) { - this.velocity = velocity; - } - - public Vec3 getPosition() { - return position; - } - - public float getGain() { - return gain; - } - - public Vec3 getVelocity() { - return velocity; - } - - public float getPitch() { - return pitch; + + @Override + public void setPosition(Vec3 position) { + throw new UnsupportedOperationException(); } } diff --git a/src/main/java/ru/windcorp/progressia/client/audio/SoundEffect.java b/src/main/java/ru/windcorp/progressia/client/audio/Sound.java similarity index 76% rename from src/main/java/ru/windcorp/progressia/client/audio/SoundEffect.java rename to src/main/java/ru/windcorp/progressia/client/audio/Sound.java index d79c8b2..31dce8c 100644 --- a/src/main/java/ru/windcorp/progressia/client/audio/SoundEffect.java +++ b/src/main/java/ru/windcorp/progressia/client/audio/Sound.java @@ -19,23 +19,26 @@ package ru.windcorp.progressia.client.audio; import glm.vec._3.Vec3; +import ru.windcorp.progressia.client.audio.backend.SoundType; import ru.windcorp.progressia.client.audio.backend.Speaker; -import ru.windcorp.progressia.common.util.namespaces.Namespaced; -public class SoundEffect - extends Namespaced { +public class Sound { - private Vec3 position = new Vec3(); - private Vec3 velocity = new Vec3(); - private float pitch = 1.0f; - private float gain = 1.0f; + protected Vec3 position = new Vec3(0f, 0f, 0f); + protected Vec3 velocity = new Vec3(0f, 0f, 0f); + protected float pitch = 1.0f; + protected float gain = 1.0f; + protected int timeLength = 0; + + protected SoundType soundType; - public SoundEffect(String id) { - super(id); + public Sound(String id) { + soundType = AudioManager.getSoundType(id); } - - public SoundEffect( + + public Sound( String id, + int timeLength, Vec3 position, Vec3 velocity, float pitch, @@ -49,7 +52,7 @@ public class SoundEffect } public void play(boolean loop) { - Speaker speaker = AudioManager.initSpeaker(this.getId()); + Speaker speaker = AudioManager.initSpeaker(soundType); speaker.setGain(gain); speaker.setPitch(pitch); speaker.setPosition(position); @@ -93,4 +96,9 @@ public class SoundEffect public float getPitch() { return pitch; } + + public int getTimeLength() { + return soundType.getTimeLength(); + } + } diff --git a/src/main/java/ru/windcorp/progressia/test/TestContent.java b/src/main/java/ru/windcorp/progressia/test/TestContent.java index 9fabd96..c7ff429 100644 --- a/src/main/java/ru/windcorp/progressia/test/TestContent.java +++ b/src/main/java/ru/windcorp/progressia/test/TestContent.java @@ -33,7 +33,7 @@ import org.lwjgl.glfw.GLFW; import glm.vec._3.Vec3; import glm.vec._3.i.Vec3i; import ru.windcorp.progressia.client.ClientState; -import ru.windcorp.progressia.client.audio.SoundEffect; +import ru.windcorp.progressia.client.audio.Sound; import ru.windcorp.progressia.client.comms.controls.*; import ru.windcorp.progressia.client.graphics.input.KeyEvent; import ru.windcorp.progressia.client.graphics.input.KeyMatcher; @@ -360,7 +360,7 @@ public class TestContent { private static void onBlockBreakTrigger(ControlData control) { ((ControlBreakBlockData) control).setBlockInWorld(getSelection().getBlock()); - SoundEffect sfx = new SoundEffect("Progressia:BlockDestroy"); + Sound sfx = new Sound("Progressia:BlockDestroy"); sfx.setPosition(getSelection().getPoint()); sfx.setPitch((float) (Math.random() + 1 * 0.5)); sfx.play(false);