Browse Source

Added functionality to handlePlayerCollision method in PlayerCharacter.java to actively prevent players from standing in each other.

playercollision-1
Preston Driver 3 months ago
parent
commit
2d1da7679e
  1. 28
      src/engine/objects/PlayerCharacter.java

28
src/engine/objects/PlayerCharacter.java

@ -286,7 +286,6 @@ public class PlayerCharacter extends AbstractCharacter { @@ -286,7 +286,6 @@ public class PlayerCharacter extends AbstractCharacter {
public void updateBounds() {
this.playerBounds.setBounds(this);
this.checkCollisionsWithOtherPlayers();
// This is a test to see if it will push to the docker container
}
public void checkCollisionsWithOtherPlayers() {
@ -295,6 +294,8 @@ public class PlayerCharacter extends AbstractCharacter { @@ -295,6 +294,8 @@ public class PlayerCharacter extends AbstractCharacter {
if (obj instanceof PlayerCharacter && obj != this) {
PlayerCharacter otherPlayer = (PlayerCharacter) obj;
if (Bounds.collide(this.getBounds(), otherPlayer.getBounds(), 0.1f)) {
System.out.println("Collision detected with player: " + otherPlayer.getFirstName());
ChatManager.chatSystemInfo(otherPlayer, "Has Collided with YOU");
// Handle collision with other player
handleCollisionWithPlayer(otherPlayer);
}
@ -303,9 +304,28 @@ public class PlayerCharacter extends AbstractCharacter { @@ -303,9 +304,28 @@ public class PlayerCharacter extends AbstractCharacter {
}
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");
Vector3fImmutable myPos = this.getLoc();
Vector3fImmutable otherPlayerPos = otherPlayer.getLoc();
// Calculate direction vector
Vector3fImmutable direction = myPos.subtract(otherPlayerPos).normalize();
// Move players apart
float separationDistance = 1.0f; // Adjust this value as needed
Vector3fImmutable myNewPos = myPos.add(direction.mult(separationDistance / 2));
Vector3fImmutable otherPlayerNewPos = otherPlayerPos.subtract(direction.mult(separationDistance / 2.0f));
// Update positions
this.setLoc(myNewPos);
otherPlayer.setLoc(otherPlayerNewPos);
// Refresh both players in the world
WorldGrid.updateObject(this);
WorldGrid.updateObject(otherPlayer);
// Refresh both players in the world
WorldGrid.updateObject(this);
WorldGrid.updateObject(otherPlayer);
}
public static Building getUpdatedBindBuilding(PlayerCharacter player) {

Loading…
Cancel
Save