Refactored Sound Engine

- Renamed SoundEffect to Sound
- Now Music inherits Sound
- Now every sound has parameter timeLength
This commit is contained in:
Евгений Смирнов 2021-01-29 18:35:05 +03:00
parent 26a35f306c
commit 127d1c3d87
4 changed files with 45 additions and 70 deletions

View File

@ -94,16 +94,27 @@ public class AudioManager {
); );
} }
public static Speaker initSpeaker(String soundID) { public static Speaker initSpeaker(SoundType st) {
Speaker speaker = getLastSpeaker(); Speaker speaker = getLastSpeaker();
try { try {
findSoundType(soundID).initSpeaker(speaker); st.initSpeaker(speaker);
} catch (Exception ex) { } catch (Exception ex) {
throw new RuntimeException(); throw new RuntimeException();
} }
return speaker; 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) { public static Speaker initMusicSpeaker(String soundID) {
try { try {
findSoundType(soundID).initSpeaker(musicSpeaker); findSoundType(soundID).initSpeaker(musicSpeaker);

View File

@ -19,14 +19,9 @@
package ru.windcorp.progressia.client.audio; package ru.windcorp.progressia.client.audio;
import glm.vec._3.Vec3; 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 { public class Music
private Vec3 position = new Vec3(); extends Sound {
private Vec3 velocity = new Vec3();
private float pitch = 1.0f;
private float gain = 1.0f;
public Music(String id) { public Music(String id) {
super(id); super(id);
@ -34,57 +29,18 @@ public class Music extends Namespaced {
public Music( public Music(
String id, String id,
Vec3 position,
Vec3 velocity, Vec3 velocity,
float pitch, float pitch,
float gain float gain
) { ) {
this(id); this(id);
this.position = position; super.velocity = velocity;
this.velocity = velocity; super.pitch = pitch;
this.pitch = pitch; super.gain = gain;
this.gain = gain;
} }
public void play(boolean loop) { @Override
Speaker speaker = AudioManager.initMusicSpeaker(this.getId()); public void setPosition(Vec3 position) {
speaker.setGain(gain); throw new UnsupportedOperationException();
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;
} }
} }

View File

@ -19,23 +19,26 @@
package ru.windcorp.progressia.client.audio; package ru.windcorp.progressia.client.audio;
import glm.vec._3.Vec3; 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.client.audio.backend.Speaker;
import ru.windcorp.progressia.common.util.namespaces.Namespaced;
public class SoundEffect public class Sound {
extends Namespaced {
private Vec3 position = new Vec3(); protected Vec3 position = new Vec3(0f, 0f, 0f);
private Vec3 velocity = new Vec3(); protected Vec3 velocity = new Vec3(0f, 0f, 0f);
private float pitch = 1.0f; protected float pitch = 1.0f;
private float gain = 1.0f; protected float gain = 1.0f;
protected int timeLength = 0;
public SoundEffect(String id) { protected SoundType soundType;
super(id);
public Sound(String id) {
soundType = AudioManager.getSoundType(id);
} }
public SoundEffect( public Sound(
String id, String id,
int timeLength,
Vec3 position, Vec3 position,
Vec3 velocity, Vec3 velocity,
float pitch, float pitch,
@ -49,7 +52,7 @@ public class SoundEffect
} }
public void play(boolean loop) { public void play(boolean loop) {
Speaker speaker = AudioManager.initSpeaker(this.getId()); Speaker speaker = AudioManager.initSpeaker(soundType);
speaker.setGain(gain); speaker.setGain(gain);
speaker.setPitch(pitch); speaker.setPitch(pitch);
speaker.setPosition(position); speaker.setPosition(position);
@ -93,4 +96,9 @@ public class SoundEffect
public float getPitch() { public float getPitch() {
return pitch; return pitch;
} }
public int getTimeLength() {
return soundType.getTimeLength();
}
} }

View File

@ -33,7 +33,7 @@ import org.lwjgl.glfw.GLFW;
import glm.vec._3.Vec3; import glm.vec._3.Vec3;
import glm.vec._3.i.Vec3i; import glm.vec._3.i.Vec3i;
import ru.windcorp.progressia.client.ClientState; 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.comms.controls.*;
import ru.windcorp.progressia.client.graphics.input.KeyEvent; import ru.windcorp.progressia.client.graphics.input.KeyEvent;
import ru.windcorp.progressia.client.graphics.input.KeyMatcher; import ru.windcorp.progressia.client.graphics.input.KeyMatcher;
@ -360,7 +360,7 @@ public class TestContent {
private static void onBlockBreakTrigger(ControlData control) { private static void onBlockBreakTrigger(ControlData control) {
((ControlBreakBlockData) control).setBlockInWorld(getSelection().getBlock()); ((ControlBreakBlockData) control).setBlockInWorld(getSelection().getBlock());
SoundEffect sfx = new SoundEffect("Progressia:BlockDestroy"); Sound sfx = new Sound("Progressia:BlockDestroy");
sfx.setPosition(getSelection().getPoint()); sfx.setPosition(getSelection().getPoint());
sfx.setPitch((float) (Math.random() + 1 * 0.5)); sfx.setPitch((float) (Math.random() + 1 * 0.5));
sfx.play(false); sfx.play(false);