Skip to content

Commit

Permalink
update movement
Browse files Browse the repository at this point in the history
  • Loading branch information
DanieleMerighi committed Jul 9, 2024
1 parent c0d8bdb commit 7bc801c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,4 @@ List<GenPair<Integer, Integer>> findShortestPathToPlayer(GameMap map, GenPair<In
* power-up if found, otherwise an empty {@code Optional}
*/
Optional<GenPair<Integer, Integer>> findNearestPowerUp(GameMap map, GenPair<Integer, Integer> enemyCoord);

/**
* Finds the nearest cell within a limited radius from the enemy's current
* coordinates.
*
* @param map the game map
* @param enemyCoord the current coordinates of the enemy
* @return an {@code Optional<Pair>} containing the coordinates of the nearest
* cell if found, otherwise an empty {@code Optional}
*/
Optional<GenPair<Integer, Integer>> findNearestCell(GameMap map, GenPair<Integer, Integer> enemyCoord);
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,6 @@ public Optional<GenPair<Integer, Integer>> findNearestPowerUp(final GameMap map,
calculateDistance(enemyCoord, cell2)));
}

public Optional<GenPair<Integer, Integer>> findNearestCell(final GameMap map, final GenPair<Integer, Integer> enemyCoord) {
final BreadthFirstIterator<GenPair<Integer, Integer>, DefaultWeightedEdge> bfsIterator = new BreadthFirstIterator<>(
graph,
enemyCoord);
return StreamSupport.stream(
Spliterators.spliteratorUnknownSize(bfsIterator, Spliterator.ORDERED), false)
.filter(cell -> bfsIterator.getDepth(cell) > bfsIterator.getDepth(enemyCoord))
.filter(cell -> map.isEmpty(cell))
.findAny();
}

/**
* Updates the internal graph representation of the game map with a new map.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package it.unibo.bombardero.character.ai.impl;

import java.util.Optional;
import java.util.List;
import java.util.EnumSet;
import java.util.Random;
import java.util.stream.Collectors;

import it.unibo.bombardero.character.Direction;
import it.unibo.bombardero.character.Enemy;
import it.unibo.bombardero.character.ai.api.MovementStrategy;
import it.unibo.bombardero.core.api.GameManager;
import it.unibo.bombardero.map.api.Functions;
import it.unibo.bombardero.map.api.GameMap;
import it.unibo.bombardero.map.api.GenPair;

/**
* The RandomMovementStrategy class implements a movement strategy where the
* enemy moves randomly.
*/
public class RandomMovementStrategy implements MovementStrategy {

private static final Random RANDOM = new Random();

/**
* Calculates the next move for the given enemy in a total random way.
*
Expand All @@ -22,6 +32,25 @@ public class RandomMovementStrategy implements MovementStrategy {
*/
@Override
public Optional<GenPair<Integer, Integer>> getNextMove(final Enemy enemy, final GameManager manager) {
return enemy.getGraph().findNearestCell(manager.getGameMap(), enemy.getIntCoordinate());
final GameMap map = manager.getGameMap();
final GenPair<Integer, Integer> currentCoord = enemy.getIntCoordinate();
final List<Direction> dirs = EnumSet.allOf(Direction.class)
.stream()
.filter(d -> !enemy.getNextMove()
.map(move -> move.equals(
currentCoord.apply(Functions.sumInt(new GenPair<Integer, Integer>(d.x(), d.y())))))
.orElse(false))
.collect(Collectors.toList());
while (!dirs.isEmpty()) {
final Direction randomDirection = dirs.remove(RANDOM.nextInt(dirs.size()));
final GenPair<Integer, Integer> p = currentCoord
.apply(Functions.sumInt(new GenPair<Integer, Integer>(randomDirection.x(), randomDirection.y())));
if (enemy.isValidCell(p)
&& (map.isEmpty(p) || map.isBreakableWall(p)
|| map.isPowerUp(p))) {
return Optional.of(p);
}
}
return Optional.empty();
}
}

0 comments on commit 7bc801c

Please sign in to comment.