Fixed block placement bug, and reloading bug.

This commit is contained in:
Andrew Lalis 2022-07-29 23:30:09 +02:00
parent 29eb46bcaf
commit 4aba806610
6 changed files with 34 additions and 6 deletions

View File

@ -47,6 +47,13 @@ public class Inventory {
return Optional.empty();
}
public int getIndex(ItemStack stack) {
for (int i = 0; i < itemStacks.size(); i++) {
if (itemStacks.get(i).equals(stack)) return i;
}
return -1;
}
public byte getSelectedBlockValue() {
for (var stack : itemStacks) {
if (stack instanceof BlockItemStack b) {

View File

@ -142,6 +142,12 @@ public class World {
return chunkMap.values().stream().mapToInt(c -> c.getPosition().z * Chunk.SIZE + Chunk.SIZE - 1).max().orElse(0);
}
public boolean containsPoint(Vector3i pos) {
return pos.x >= getMinX() && pos.x < getMaxX() &&
pos.y >= getMinY() && pos.y < getMaxY() &&
pos.z >= getMinZ() && pos.z < getMaxZ();
}
/**
* Clears all data from the world.
*/

View File

@ -77,7 +77,7 @@ public class ProjectileManager {
direction.z += rand.nextGaussian(0, perturbationFactor);
Vector3f vel = new Vector3f(direction).normalize()
.mul(200 * MOVEMENT_FACTOR)
.mul(300 * MOVEMENT_FACTOR)
.add(player.getVelocity());
ServerProjectile bullet = new ServerProjectile(id, new Vector3f(pos), vel, Projectile.Type.BULLET, player, gun);

View File

@ -26,6 +26,10 @@ import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
/**
* The central server, which mainly contains all the different managers and
* other components that make up the server's state and logic.
*/
public class Server implements Runnable {
private final ServerSocket serverSocket;
private final DatagramSocket datagramSocket;

View File

@ -40,6 +40,7 @@ public class PlayerActionManager {
private boolean gunNeedsReCock = false;
private boolean gunReloading = false;
private long gunReloadingStartedAt = 0;
private GunItemStack reloadingItemStack = null;
private boolean updated = false;
@ -149,17 +150,23 @@ public class PlayerActionManager {
g.setClipCount(g.getClipCount() - 1);
gunReloadingStartedAt = now;
gunReloading = true;
reloadingItemStack = g;
server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ItemStackMessage(player.getInventory()));
server.getPlayerManager().broadcastUdpMessage(new SoundMessage("reload", 1, player.getPosition(), player.getVelocity()));
}
if (// Check to see if reloading is done.
gunReloading &&
reloadingItemStack != null &&
now - gunReloadingStartedAt > gun.getReloadTime() * 1000
) {
g.setBulletCount(gun.getMaxBulletCount());
reloadingItemStack.setBulletCount(gun.getMaxBulletCount());
int idx = player.getInventory().getIndex(reloadingItemStack);
if (idx != -1) {
server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ItemStackMessage(idx, reloadingItemStack));
}
gunReloading = false;
server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ItemStackMessage(player.getInventory()));
reloadingItemStack = null;
}
// Check to see if the player released the trigger, for non-automatic weapons.
@ -195,7 +202,10 @@ public class PlayerActionManager {
if (hit != null && !server.getTeamManager().isProtected(hit.pos())) {
Vector3i placePos = new Vector3i(hit.pos());
placePos.add(hit.norm());
if (!isSpaceOccupied(placePos)) { // Ensure that we can't place blocks in space we're occupying.
boolean canPlace = server.getPlayerManager().getPlayers().stream()
.noneMatch(p -> p.isSpaceOccupied(placePos)) &&
world.containsPoint(placePos);
if (canPlace) { // Ensure that we can't place blocks in space we're occupying.
world.setBlockAt(placePos.x, placePos.y, placePos.z, stack.getSelectedValue());
lastBlockPlacedAt = now;
stack.decrementAmount();

View File

@ -26,8 +26,9 @@ public class WorldUpdater implements Runnable {
@Override
public void run() {
final long msPerTick = (long) (Math.floor(1.0 / ticksPerSecond) * 1_000);
final long nsPerTick = (long) Math.floor((1.0 / ticksPerSecond) * 1_000_000_000.0);
double secondsPerTick = 1.0 / ticksPerSecond;
final long msPerTick = (long) Math.floor(secondsPerTick * 1_000);
final long nsPerTick = (long) Math.floor(secondsPerTick * 1_000_000_000);
System.out.printf("Running world updater at %d ms/tick, or %d ns/tick.%n", msPerTick, nsPerTick);
running = true;
while (running) {