Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

#So how does my algorithm work

So how does my algorithm work

#So how does my algorithm work

So how does my algorithm work

Notice removed Draw attention by Ben Beri
Bounty Ended with Ilmari Karonen's answer chosen by Ben Beri
Tweeted twitter.com/StackCodeReview/status/916855232973467648
Notice added Draw attention by Ben Beri
Bounty Started worth 100 reputation by Ben Beri
edited tags; edited title
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

A better way to handle Handling collision on a turn-based 2D game

deleted 4 characters in body
Source Link
/**
 * Checks if a player has a collision according to his move, in the given turn and move-phase
 * @param p The player to check
 * @param turn The turn
 * @param phase The move-phase step
 * @param setPosition If to set the next position or not on non-collided result
 * @return <code>TRUE</code> If the player was collided, <code>FALSE</code> if not.
 */
public boolean checkCollision(Player p, int turn, int phase, boolean setPosition) {
 // The current selected move of the player
 MoveType move = p.getMoves().getMove(turn);
 // If this player was bumped, and a move was not selected, we want to process the bump animation
 // But we have to check if the position to be bumped is available to be claimed
 if (move == MoveType.NONE && p.getCollisionStorage().isBumped()) {
 Position pos = p.getCollisionStorage().getBumpAnimation().getPositionForAnimation(p);
 Player claimed = players.getPlayerByPosition(pos.getX(), pos.getY());
 // Claiming checking for the new position for bump
 return claimed != null && (claimed.getMoves().getMove(turn) == MoveType.NONE || checkCollision(claimed, turn, phase, false));
 }
 // Use the current position as default.txt, imply we have already set it
 Position position = p;
 // If not set by default.txt on previous loops, gets the next position on the map for the given phase on the given move
 if (!p.getCollisionStorage().isPositionChanged()) {
 position = move.getNextPositionWithPhase(p, p.getFace(), phase);
 }
 // If the player has moved since his last position
 if (!position.equals(p)) {
 // Check for bounds collision with the border
 if (checkBoundCollision(p, turn, phase) || checkRockCollision(p, turn, phase)) {
 return true;
 }
 // Check if the next position is claimed by another player, null result if not
 Player claimed = players.getPlayerByPosition(position.getX(), position.getY());
 // If the result is not null, the position is claimed
 if (claimed != null) {
 Position claimedNextPos = claimed;
 if (!claimed.getCollisionStorage().isPositionChanged()) {
 claimedNextPos = claimed.getMoves().getMove(turn).getNextPositionWithPhase(claimed, claimed.getFace(), phase);
 }
 // Check if the claimed position doesn't move away
 if (claimed.getMoves().getMove(turn) == MoveType.NONE || claimedNextPos.equals(claimed)) {
 if (move != MoveType.FORWARD || claimed.getVessel().getSize() >= p.getVessel().getSize()) {
 collide(p, claimed, turn, phase);
 }
 if (move == MoveType.FORWARD && canBumpPlayer(p, claimed, turn, phase)) {
 bumpPlayer(claimed, p, turn, phase);
 p.set(position);
 p.getCollisionStorage().setPositionChanged(true);
 }
 claimed.getVessel().appendDamage(p.getVessel().getRamDamage());
 return true;
 }
 else if (claimedNextPos.equals(p)) { // If they switched positions (e.g nose to nose, F, F move)
 collide(p, claimed, turn, phase);
 collide(claimed, p, turn, phase);
 return true;
 }
 else {
 // Make sure that the claimed position moves away successfully
 if (!checkCollision(claimed, turn, phase, false)) {
 if (setPosition) {
 // Moved successfully, claim position
 p.set(position);
 p.getCollisionStorage().setPositionChanged(true);
 }
 }
 else {
 // did not move successfully, collide
 collide(p, claimed, turn, phase);
 collide(claimed, p, turn, phase);
 return true;
 }
 }
 } else {
 // List of players that collided with this player, while performing this move, in this phase
 List<Player> collisions = getPlayersTryingToClaim(p, position, turn, phase);
 if (collisions.size() > 0) { // Collision has happened
 collisions.add(p);
 Player largest = getLargestSize(collisions);
 if (countPlayersForSize(collisions, largest.getVessel().getSize()) > 1) {
 // Stop players from movement
 for (Player pl : collisions) {
 pl.getCollisionStorage().setCollided(turn, phase);
 collide(pl, pl, turn, phase);
 }
 }
 else {
 for (Player pl : collisions) {
 if (pl == largest) {
 continue;
 }
 pl.getCollisionStorage().setCollided(turn, phase);
 collide(pl, largest, turn, phase);
 }
 if (!largest.getCollisionStorage().isPositionChanged()) {
 largest.set(position);
 largest.getCollisionStorage().setPositionChanged(true);
 }
 }
 return true;
 } else {
 if (setPosition) {
 p.set(position);
 p.getCollisionStorage().setPositionChanged(true);
 }
 }
 }
 }
 return false;
}
/**
 * Checks if a player has a collision according to his move, in the given turn and move-phase
 * @param p The player to check
 * @param turn The turn
 * @param phase The move-phase step
 * @param setPosition If to set the next position or not on non-collided result
 * @return <code>TRUE</code> If the player was collided, <code>FALSE</code> if not.
 */
public boolean checkCollision(Player p, int turn, int phase, boolean setPosition) {
 // The current selected move of the player
 MoveType move = p.getMoves().getMove(turn);
 // If this player was bumped, and a move was not selected, we want to process the bump animation
 // But we have to check if the position to be bumped is available to be claimed
 if (move == MoveType.NONE && p.getCollisionStorage().isBumped()) {
 Position pos = p.getCollisionStorage().getBumpAnimation().getPositionForAnimation(p);
 Player claimed = players.getPlayerByPosition(pos.getX(), pos.getY());
 // Claiming checking for the new position for bump
 return claimed != null && (claimed.getMoves().getMove(turn) == MoveType.NONE || checkCollision(claimed, turn, phase, false));
 }
 // Use the current position as default.txt, imply we have already set it
 Position position = p;
 // If not set by default.txt on previous loops, gets the next position on the map for the given phase on the given move
 if (!p.getCollisionStorage().isPositionChanged()) {
 position = move.getNextPositionWithPhase(p, p.getFace(), phase);
 }
 // If the player has moved since his last position
 if (!position.equals(p)) {
 // Check for bounds collision with the border
 if (checkBoundCollision(p, turn, phase) || checkRockCollision(p, turn, phase)) {
 return true;
 }
 // Check if the next position is claimed by another player, null result if not
 Player claimed = players.getPlayerByPosition(position.getX(), position.getY());
 // If the result is not null, the position is claimed
 if (claimed != null) {
 Position claimedNextPos = claimed;
 if (!claimed.getCollisionStorage().isPositionChanged()) {
 claimedNextPos = claimed.getMoves().getMove(turn).getNextPositionWithPhase(claimed, claimed.getFace(), phase);
 }
 // Check if the claimed position doesn't move away
 if (claimed.getMoves().getMove(turn) == MoveType.NONE || claimedNextPos.equals(claimed)) {
 if (move != MoveType.FORWARD || claimed.getVessel().getSize() >= p.getVessel().getSize()) {
 collide(p, claimed, turn, phase);
 }
 if (move == MoveType.FORWARD && canBumpPlayer(p, claimed, turn, phase)) {
 bumpPlayer(claimed, p, turn, phase);
 p.set(position);
 p.getCollisionStorage().setPositionChanged(true);
 }
 claimed.getVessel().appendDamage(p.getVessel().getRamDamage());
 return true;
 }
 else if (claimedNextPos.equals(p)) { // If they switched positions (e.g nose to nose, F, F move)
 collide(p, claimed, turn, phase);
 collide(claimed, p, turn, phase);
 return true;
 }
 else {
 // Make sure that the claimed position moves away successfully
 if (!checkCollision(claimed, turn, phase, false)) {
 if (setPosition) {
 // Moved successfully, claim position
 p.set(position);
 p.getCollisionStorage().setPositionChanged(true);
 }
 }
 else {
 // did not move successfully, collide
 collide(p, claimed, turn, phase);
 collide(claimed, p, turn, phase);
 return true;
 }
 }
 } else {
 // List of players that collided with this player, while performing this move, in this phase
 List<Player> collisions = getPlayersTryingToClaim(p, position, turn, phase);
 if (collisions.size() > 0) { // Collision has happened
 collisions.add(p);
 Player largest = getLargestSize(collisions);
 if (countPlayersForSize(collisions, largest.getVessel().getSize()) > 1) {
 // Stop players from movement
 for (Player pl : collisions) {
 pl.getCollisionStorage().setCollided(turn, phase);
 collide(pl, pl, turn, phase);
 }
 }
 else {
 for (Player pl : collisions) {
 if (pl == largest) {
 continue;
 }
 pl.getCollisionStorage().setCollided(turn, phase);
 collide(pl, largest, turn, phase);
 }
 if (!largest.getCollisionStorage().isPositionChanged()) {
 largest.set(position);
 largest.getCollisionStorage().setPositionChanged(true);
 }
 }
 return true;
 } else {
 if (setPosition) {
 p.set(position);
 p.getCollisionStorage().setPositionChanged(true);
 }
 }
 }
 }
 return false;
}
/**
 * Checks if a player has a collision according to his move, in the given turn and move-phase
 * @param p The player to check
 * @param turn The turn
 * @param phase The move-phase step
 * @param setPosition If to set the next position or not on non-collided result
 * @return <code>TRUE</code> If the player was collided, <code>FALSE</code> if not.
 */
public boolean checkCollision(Player p, int turn, int phase, boolean setPosition) {
 // The current selected move of the player
 MoveType move = p.getMoves().getMove(turn);
 // If this player was bumped, and a move was not selected, we want to process the bump animation
 // But we have to check if the position to be bumped is available to be claimed
 if (move == MoveType.NONE && p.getCollisionStorage().isBumped()) {
 Position pos = p.getCollisionStorage().getBumpAnimation().getPositionForAnimation(p);
 Player claimed = players.getPlayerByPosition(pos.getX(), pos.getY());
 // Claiming checking for the new position for bump
 return claimed != null && (claimed.getMoves().getMove(turn) == MoveType.NONE || checkCollision(claimed, turn, phase, false));
 }
 // Use the current position as default, imply we have already set it
 Position position = p;
 // If not set by default.txt on previous loops, gets the next position on the map for the given phase on the given move
 if (!p.getCollisionStorage().isPositionChanged()) {
 position = move.getNextPositionWithPhase(p, p.getFace(), phase);
 }
 // If the player has moved since his last position
 if (!position.equals(p)) {
 // Check for bounds collision with the border
 if (checkBoundCollision(p, turn, phase) || checkRockCollision(p, turn, phase)) {
 return true;
 }
 // Check if the next position is claimed by another player, null result if not
 Player claimed = players.getPlayerByPosition(position.getX(), position.getY());
 // If the result is not null, the position is claimed
 if (claimed != null) {
 Position claimedNextPos = claimed;
 if (!claimed.getCollisionStorage().isPositionChanged()) {
 claimedNextPos = claimed.getMoves().getMove(turn).getNextPositionWithPhase(claimed, claimed.getFace(), phase);
 }
 // Check if the claimed position doesn't move away
 if (claimed.getMoves().getMove(turn) == MoveType.NONE || claimedNextPos.equals(claimed)) {
 if (move != MoveType.FORWARD || claimed.getVessel().getSize() >= p.getVessel().getSize()) {
 collide(p, claimed, turn, phase);
 }
 if (move == MoveType.FORWARD && canBumpPlayer(p, claimed, turn, phase)) {
 bumpPlayer(claimed, p, turn, phase);
 p.set(position);
 p.getCollisionStorage().setPositionChanged(true);
 }
 claimed.getVessel().appendDamage(p.getVessel().getRamDamage());
 return true;
 }
 else if (claimedNextPos.equals(p)) { // If they switched positions (e.g nose to nose, F, F move)
 collide(p, claimed, turn, phase);
 collide(claimed, p, turn, phase);
 return true;
 }
 else {
 // Make sure that the claimed position moves away successfully
 if (!checkCollision(claimed, turn, phase, false)) {
 if (setPosition) {
 // Moved successfully, claim position
 p.set(position);
 p.getCollisionStorage().setPositionChanged(true);
 }
 }
 else {
 // did not move successfully, collide
 collide(p, claimed, turn, phase);
 collide(claimed, p, turn, phase);
 return true;
 }
 }
 } else {
 // List of players that collided with this player, while performing this move, in this phase
 List<Player> collisions = getPlayersTryingToClaim(p, position, turn, phase);
 if (collisions.size() > 0) { // Collision has happened
 collisions.add(p);
 Player largest = getLargestSize(collisions);
 if (countPlayersForSize(collisions, largest.getVessel().getSize()) > 1) {
 // Stop players from movement
 for (Player pl : collisions) {
 pl.getCollisionStorage().setCollided(turn, phase);
 collide(pl, pl, turn, phase);
 }
 }
 else {
 for (Player pl : collisions) {
 if (pl == largest) {
 continue;
 }
 pl.getCollisionStorage().setCollided(turn, phase);
 collide(pl, largest, turn, phase);
 }
 if (!largest.getCollisionStorage().isPositionChanged()) {
 largest.set(position);
 largest.getCollisionStorage().setPositionChanged(true);
 }
 }
 return true;
 } else {
 if (setPosition) {
 p.set(position);
 p.getCollisionStorage().setPositionChanged(true);
 }
 }
 }
 }
 return false;
}
Source Link
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /