Added gunshot raytracing for environmental damage.

This commit is contained in:
Andrew Lalis 2022-07-20 18:51:24 +02:00
parent 219ccd94db
commit 3d695e0312
5 changed files with 27 additions and 39 deletions

View File

@ -1,6 +1,5 @@
package nl.andrewl.aos2_client.sound; package nl.andrewl.aos2_client.sound;
import nl.andrewl.aos2_client.model.ClientPlayer;
import org.joml.Vector3f; import org.joml.Vector3f;
import org.lwjgl.openal.AL; import org.lwjgl.openal.AL;
import org.lwjgl.openal.ALC; import org.lwjgl.openal.ALC;
@ -11,9 +10,12 @@ import java.nio.IntBuffer;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static org.lwjgl.openal.ALC10.*;
import static org.lwjgl.openal.AL10.*; import static org.lwjgl.openal.AL10.*;
import static org.lwjgl.openal.ALC10.*;
/**
* Main class for managing the OpenAL audio interface.
*/
public class SoundManager { public class SoundManager {
private final long alContext; private final long alContext;
private final Map<String, Integer> audioBuffers = new HashMap<>(); private final Map<String, Integer> audioBuffers = new HashMap<>();
@ -24,7 +26,6 @@ public class SoundManager {
alContext = alcCreateContext(device, (IntBuffer) null); alContext = alcCreateContext(device, (IntBuffer) null);
alcMakeContextCurrent(alContext); alcMakeContextCurrent(alContext);
AL.createCapabilities(capabilities); AL.createCapabilities(capabilities);
} }
public void load(String name, String resource) { public void load(String name, String resource) {

View File

@ -44,7 +44,7 @@ public class WaveData {
} }
} }
private ByteBuffer loadData() { private void loadData() {
try { try {
int bytesRead = audioStream.read(dataArray, 0, totalBytes); int bytesRead = audioStream.read(dataArray, 0, totalBytes);
data.clear(); data.clear();
@ -54,7 +54,6 @@ public class WaveData {
e.printStackTrace(); e.printStackTrace();
System.err.println("Couldn't read bytes from audio stream!"); System.err.println("Couldn't read bytes from audio stream!");
} }
return data;
} }
@ -62,7 +61,7 @@ public class WaveData {
InputStream stream = WaveData.class.getClassLoader().getResourceAsStream(file); InputStream stream = WaveData.class.getClassLoader().getResourceAsStream(file);
if (stream == null) { if (stream == null) {
System.err.println("Couldn't find file: " + file); System.err.println("Couldn't find file: " + file);
return null; throw new RuntimeException();
} }
InputStream bufferedInput = new BufferedInputStream(stream); InputStream bufferedInput = new BufferedInputStream(stream);
AudioInputStream audioStream; AudioInputStream audioStream;

View File

@ -14,7 +14,7 @@ public class Gun extends Item {
private final float reloadTime; private final float reloadTime;
private final float baseDamage; private final float baseDamage;
private final float recoil; private final float recoil;
private boolean automatic; private final boolean automatic;
public Gun( public Gun(
int id, int id,

View File

@ -180,43 +180,26 @@ public class World {
float factorY = Float.MAX_VALUE; float factorY = Float.MAX_VALUE;
float factorZ = Float.MAX_VALUE; float factorZ = Float.MAX_VALUE;
if (dir.x != 0) { if (dir.x != 0) factorX = factorToNextValue(pos.x, dir.x);
float nextValue; if (dir.y != 0) factorY = factorToNextValue(pos.y, dir.y);
if (dir.x > 0) { if (dir.z != 0) factorZ = factorToNextValue(pos.z, dir.z);
nextValue = (Math.ceil(pos.x) == pos.x) ? pos.x + 1 : Math.ceil(pos.x);
} else {
nextValue = Math.floor(pos.x) - DELTA;
}
float diff = nextValue - pos.x;
factorX = Math.abs(diff / dir.x);
}
if (dir.y != 0) {
float nextValue;
if (dir.y > 0) {
nextValue = (Math.ceil(pos.y) == pos.y) ? pos.y + 1 : Math.ceil(pos.y);
} else {
nextValue = Math.floor(pos.y) - DELTA;
}
float diff = nextValue - pos.y;
factorY = Math.abs(diff / dir.y);
}
if (dir.z != 0) {
float nextValue;
if (dir.z > 0) {
nextValue = (Math.ceil(pos.z) == pos.z) ? pos.z + 1 : Math.ceil(pos.z);
} else {
nextValue = Math.floor(pos.z) - DELTA;
}
float diff = nextValue - pos.z;
factorZ = Math.abs(diff / dir.z);
}
float minFactor = Math.min(factorX, Math.min(factorY, factorZ)); float minFactor = Math.min(factorX, Math.min(factorY, factorZ));
dir.mulAdd(minFactor, pos, pos); dir.mulAdd(minFactor, pos, pos);
} }
private static float factorToNextValue(float n, float dir) {
if (dir == 0) return 0;
float nextValue;
if (dir > 0) {
nextValue = (Math.ceil(n) == n) ? n + 1 : Math.ceil(n);
} else {
nextValue = Math.floor(n) - DELTA;
}
float diff = nextValue - n;
return Math.abs(diff / dir);
}
/** /**
* Gets the chunk position at the specified world position. * Gets the chunk position at the specified world position.
* @param x The x coordinate. * @param x The x coordinate.

View File

@ -101,6 +101,11 @@ public class PlayerActionManager {
(gun.isAutomatic() || !gunNeedsReCock) (gun.isAutomatic() || !gunNeedsReCock)
) { ) {
// TODO: trace a ray from gun to see if players intersect with it. // TODO: trace a ray from gun to see if players intersect with it.
var hit = world.getLookingAtPos(player.getEyePosition(), player.getViewVector(), 100);
if (hit != null) {
world.setBlockAt(hit.pos().x, hit.pos().y, hit.pos().z, (byte) 0);
server.getPlayerManager().broadcastUdpMessage(ChunkUpdateMessage.fromWorld(hit.pos(), world));
}
g.setBulletCount(g.getBulletCount() - 1); g.setBulletCount(g.getBulletCount() - 1);
gunLastShotAt = now; gunLastShotAt = now;
if (!gun.isAutomatic()) { if (!gun.isAutomatic()) {