From 24cc6af14779a64cf1adc665e9ebf7bcea260adf Mon Sep 17 00:00:00 2001 From: Preston Driver Date: Mon, 16 Sep 2024 23:06:07 -0500 Subject: [PATCH] Added player collision with other players in PlayerCharacter class. --- src/engine/objects/PlayerCharacter.java | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/engine/objects/PlayerCharacter.java b/src/engine/objects/PlayerCharacter.java index 35dd8c5a..b60a8024 100644 --- a/src/engine/objects/PlayerCharacter.java +++ b/src/engine/objects/PlayerCharacter.java @@ -174,6 +174,7 @@ public class PlayerCharacter extends AbstractCharacter { private boolean isTeleporting = false; private boolean dirtyLoad = true; private final ReadWriteLock dirtyLock = new ReentrantReadWriteLock(true); + private Bounds playerBounds; /** * No Id Constructor @@ -206,6 +207,10 @@ public class PlayerCharacter extends AbstractCharacter { this.guildStatus = new AtomicInteger(0); this.bindBuildingID = -1; + + this.playerBounds = Bounds.borrow(); + playerBounds.setBounds(this.getLoc()); + this.playerBounds.setBounds(this); } /** @@ -269,11 +274,39 @@ public class PlayerCharacter extends AbstractCharacter { this.hash = rs.getString("hash"); + this.playerBounds = Bounds.borrow(); + playerBounds.setBounds(this.getLoc()); + this.playerBounds.setBounds(this); + // For debugging skills // CharacterSkill.printSkills(this); } + public void updateBounds() { + this.playerBounds.setBounds(this); + this.checkCollisionsWithOtherPlayers(); + } + + public void checkCollisionsWithOtherPlayers() { + HashSet nearbyObjects = WorldGrid.getObjectsInRangePartial(this, MBServerStatics.CHARACTER_LOAD_RANGE, MBServerStatics.MASK_PLAYER); + for (AbstractWorldObject obj : nearbyObjects) { + if (obj instanceof PlayerCharacter && obj != this) { + PlayerCharacter otherPlayer = (PlayerCharacter) obj; + if (Bounds.collide(this.getBounds(), otherPlayer.getBounds(), 0.1f)) { + // Handle collision with other player + handleCollisionWithPlayer(otherPlayer); + } + } + } + } + + private void handleCollisionWithPlayer(PlayerCharacter otherPlayer) { + // Implement collision response here + System.out.println("Collision detected with player: " + otherPlayer.getFirstName()); + ChatManager.chatSystemInfo(otherPlayer, "Has Collided with YOU"); + } + public static Building getUpdatedBindBuilding(PlayerCharacter player) { Building returnBuilding = null; @@ -4882,6 +4915,7 @@ public class PlayerCharacter extends AbstractCharacter { @Override public void updateLocation() { + this.updateBounds(); if (!this.isMoving()) return;