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();
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 {
findSoundType(soundID).initSpeaker(musicSpeaker);

View File

@ -19,14 +19,9 @@
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);
@ -34,57 +29,18 @@ public class Music extends Namespaced {
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();
}
}

View File

@ -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;
public SoundEffect(String id) {
super(id);
protected SoundType soundType;
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();
}
}

View File

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