Fixed block placement bug, and reloading bug.
This commit is contained in:
parent
29eb46bcaf
commit
4aba806610
|
@ -47,6 +47,13 @@ public class Inventory {
|
||||||
return Optional.empty();
|
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() {
|
public byte getSelectedBlockValue() {
|
||||||
for (var stack : itemStacks) {
|
for (var stack : itemStacks) {
|
||||||
if (stack instanceof BlockItemStack b) {
|
if (stack instanceof BlockItemStack b) {
|
||||||
|
|
|
@ -142,6 +142,12 @@ public class World {
|
||||||
return chunkMap.values().stream().mapToInt(c -> c.getPosition().z * Chunk.SIZE + Chunk.SIZE - 1).max().orElse(0);
|
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.
|
* Clears all data from the world.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -77,7 +77,7 @@ public class ProjectileManager {
|
||||||
direction.z += rand.nextGaussian(0, perturbationFactor);
|
direction.z += rand.nextGaussian(0, perturbationFactor);
|
||||||
|
|
||||||
Vector3f vel = new Vector3f(direction).normalize()
|
Vector3f vel = new Vector3f(direction).normalize()
|
||||||
.mul(200 * MOVEMENT_FACTOR)
|
.mul(300 * MOVEMENT_FACTOR)
|
||||||
.add(player.getVelocity());
|
.add(player.getVelocity());
|
||||||
|
|
||||||
ServerProjectile bullet = new ServerProjectile(id, new Vector3f(pos), vel, Projectile.Type.BULLET, player, gun);
|
ServerProjectile bullet = new ServerProjectile(id, new Vector3f(pos), vel, Projectile.Type.BULLET, player, gun);
|
||||||
|
|
|
@ -26,6 +26,10 @@ import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ForkJoinPool;
|
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 {
|
public class Server implements Runnable {
|
||||||
private final ServerSocket serverSocket;
|
private final ServerSocket serverSocket;
|
||||||
private final DatagramSocket datagramSocket;
|
private final DatagramSocket datagramSocket;
|
||||||
|
|
|
@ -40,6 +40,7 @@ public class PlayerActionManager {
|
||||||
private boolean gunNeedsReCock = false;
|
private boolean gunNeedsReCock = false;
|
||||||
private boolean gunReloading = false;
|
private boolean gunReloading = false;
|
||||||
private long gunReloadingStartedAt = 0;
|
private long gunReloadingStartedAt = 0;
|
||||||
|
private GunItemStack reloadingItemStack = null;
|
||||||
|
|
||||||
private boolean updated = false;
|
private boolean updated = false;
|
||||||
|
|
||||||
|
@ -149,17 +150,23 @@ public class PlayerActionManager {
|
||||||
g.setClipCount(g.getClipCount() - 1);
|
g.setClipCount(g.getClipCount() - 1);
|
||||||
gunReloadingStartedAt = now;
|
gunReloadingStartedAt = now;
|
||||||
gunReloading = true;
|
gunReloading = true;
|
||||||
|
reloadingItemStack = g;
|
||||||
server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ItemStackMessage(player.getInventory()));
|
server.getPlayerManager().getHandler(player.getId()).sendDatagramPacket(new ItemStackMessage(player.getInventory()));
|
||||||
server.getPlayerManager().broadcastUdpMessage(new SoundMessage("reload", 1, player.getPosition(), player.getVelocity()));
|
server.getPlayerManager().broadcastUdpMessage(new SoundMessage("reload", 1, player.getPosition(), player.getVelocity()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (// Check to see if reloading is done.
|
if (// Check to see if reloading is done.
|
||||||
gunReloading &&
|
gunReloading &&
|
||||||
|
reloadingItemStack != null &&
|
||||||
now - gunReloadingStartedAt > gun.getReloadTime() * 1000
|
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;
|
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.
|
// 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())) {
|
if (hit != null && !server.getTeamManager().isProtected(hit.pos())) {
|
||||||
Vector3i placePos = new Vector3i(hit.pos());
|
Vector3i placePos = new Vector3i(hit.pos());
|
||||||
placePos.add(hit.norm());
|
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());
|
world.setBlockAt(placePos.x, placePos.y, placePos.z, stack.getSelectedValue());
|
||||||
lastBlockPlacedAt = now;
|
lastBlockPlacedAt = now;
|
||||||
stack.decrementAmount();
|
stack.decrementAmount();
|
||||||
|
|
|
@ -26,8 +26,9 @@ public class WorldUpdater implements Runnable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
final long msPerTick = (long) (Math.floor(1.0 / ticksPerSecond) * 1_000);
|
double secondsPerTick = 1.0 / ticksPerSecond;
|
||||||
final long nsPerTick = (long) Math.floor((1.0 / ticksPerSecond) * 1_000_000_000.0);
|
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);
|
System.out.printf("Running world updater at %d ms/tick, or %d ns/tick.%n", msPerTick, nsPerTick);
|
||||||
running = true;
|
running = true;
|
||||||
while (running) {
|
while (running) {
|
||||||
|
|
Loading…
Reference in New Issue