Skip to content

Commit

Permalink
javadoc and some code opt
Browse files Browse the repository at this point in the history
  • Loading branch information
AMatutat committed Jan 19, 2023
1 parent c3aaffd commit c16e3ee
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 66 deletions.
25 changes: 15 additions & 10 deletions game/src/ecs/components/ai/AIComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
import ecs.components.ai.transition.RangeTransition;
import ecs.entities.Entity;

/** AIComponent is a component that stores the idle and combat behavior of AI controlled entities */
public class AIComponent extends Component {

public static String name = "AIComponent";
private IFightAI fightAI;
private IIdleAI idleAI;
private ITransition transition;
private final IFightAI fightAI;
private final IIdleAI idleAI;
private final ITransition transition;

/**
* @param entity associated entity
* @param fightAI combat behavior
* @param idleAI idle behavior
* @param transition Determines when to fight
*/
public AIComponent(Entity entity, IFightAI fightAI, IIdleAI idleAI, ITransition transition) {
super(entity);
this.fightAI = fightAI;
Expand All @@ -30,17 +37,15 @@ public AIComponent(Entity entity) {
idleAI = new RadiusWalk(5);
transition = new RangeTransition(1.5f);
fightAI =
new IFightAI() {
@Override
public void fight(Entity entity) {
System.out.println("TIME TO FIGHT!");
// todo replace with melee skill
}
entity1 -> {
System.out.println("TIME TO FIGHT!");
// todo replace with melee skill
};
}

/** Excecute the ai behavior */
public void execute() {
if (transition.goFightMode(entity)) fightAI.fight(entity);
if (transition.isInFightMode(entity)) fightAI.fight(entity);
else idleAI.idle(entity);
}
}
69 changes: 29 additions & 40 deletions game/src/ecs/components/ai/AITools.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ecs.entities.Entity;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import level.elements.ILevel;
import level.elements.tile.Tile;
Expand All @@ -14,10 +15,12 @@
import tools.Point;

public class AITools {
private static Random random = new Random();
private static final Random random = new Random();

/**
* Finds the path to a random (accessible) tile in the given radius, starting from the position of the given entity.
* Finds the path to a random (accessible) tile in the given radius, starting from the position
* of the given entity.
*
* @param entity Entity whose position is the center point
* @param radius Search radius
* @return Path from the position of the entity to the randomly selected tile
Expand All @@ -28,30 +31,28 @@ public static GraphPath<Tile> calculateNewPath(Entity entity, float radius) {
if (pc != null && vc != null) {
ILevel level = ECS.currentLevel;
Point position = pc.getPosition();
Tile currentTile = level.getTileAt(position.toCoordinate());
List<Tile> tiles = new ArrayList<>();
for (float x = position.x - radius; x <= position.x + radius; x++) {
for (float y = position.y - radius; y <= position.y + radius; y++) {
tiles.add(level.getTileAt(new Point(x, y).toCoordinate()));
}
}
tiles.removeIf(tile -> tile == null);
tiles.removeIf(Objects::isNull);
tiles.removeIf(tile -> !tile.isAccessible());
Coordinate newPosition = tiles.get(random.nextInt(tiles.size())).getCoordinate();
GraphPath path =
level.findPath(
level.getTileAt(position.toCoordinate()), level.getTileAt(newPosition));
return path;
return level.findPath(
level.getTileAt(position.toCoordinate()), level.getTileAt(newPosition));
}
return null;
}

/**
* Finds the path from the position of one entity to the position of another entity.
* @param from Entity whose position is the start point
* @param to Entity whose position is the goal point
* @return Path
*/
/**
* Finds the path from the position of one entity to the position of another entity.
*
* @param from Entity whose position is the start point
* @param to Entity whose position is the goal point
* @return Path
*/
public static GraphPath<Tile> calculateNewPath(Entity from, Entity to) {
PositionComponent myPositionComponent =
(PositionComponent) from.getComponent(PositionComponent.name);
Expand All @@ -67,7 +68,9 @@ public static GraphPath<Tile> calculateNewPath(Entity from, Entity to) {
}

/**
* Sets the velocity of the passed entity so that it takes the next necessary step to get to the end of the path.
* Sets the velocity of the passed entity so that it takes the next necessary step to get to the
* end of the path.
*
* @param entity Entity moving on the path
* @param path Path on which the entity moves
*/
Expand All @@ -87,38 +90,24 @@ public static void move(Entity entity, GraphPath<Tile> path) {
} while (nextTile == null);

switch (currentTile.directionTo(nextTile)[0]) {
case N:
vc.setY(vc.getySpeed());
break;
case S:
vc.setY(-vc.getySpeed());
break;
case E:
vc.setX(vc.getxSpeed());
break;
case W:
vc.setX(-vc.getxSpeed());
break;
case N -> vc.setY(vc.getySpeed());
case S -> vc.setY(-vc.getySpeed());
case E -> vc.setX(vc.getxSpeed());
case W -> vc.setX(-vc.getxSpeed());
}
if (currentTile.directionTo(nextTile).length > 1)
switch (currentTile.directionTo(nextTile)[1]) {
case N:
vc.setY(vc.getySpeed());
break;
case S:
vc.setY(-vc.getySpeed());
break;
case E:
vc.setX(vc.getxSpeed());
break;
case W:
vc.setX(-vc.getxSpeed());
break;
case N -> vc.setY(vc.getySpeed());
case S -> vc.setY(-vc.getySpeed());
case E -> vc.setX(vc.getxSpeed());
case W -> vc.setX(-vc.getxSpeed());
}
}

/**
* Checks if the position of the player is within the given radius of the position of the given entity.
* Checks if the position of the player is within the given radius of the position of the given
* entity.
*
* @param entity Entity whose position specifies the center point
* @param range Reichweite die betrachtet werden soll
* @return Ob sich der Spieler in Reichweite befindet
Expand Down
7 changes: 6 additions & 1 deletion game/src/ecs/components/ai/fight/IFightAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@

public interface IFightAI {

public void fight(Entity entity);
/**
* Implements the combat behavior of an AI controlled entity
*
* @param entity associated entity
*/
void fight(Entity entity);
}
15 changes: 10 additions & 5 deletions game/src/ecs/components/ai/fight/MeleeAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@
import tools.Constants;

public class MeleeAI implements IFightAI {
private float attackRange;
private final float attackRange;
private final int delay = Constants.FRAME_RATE;
private int timeSinceLastUpdate = 0;
private Skill fightSkill;
private final Skill fightSkill;
private GraphPath<Tile> path;

/**
* Attacks the player if he is within the given range. Otherwise, it will move towards the
* player.
*
* @param attackRange Range in which the attack skill should be executed
* @param fightSkill Skill to be used when an attack is performed
*/
public MeleeAI(float attackRange, Skill fightSkill) {
this.attackRange = attackRange;
this.fightSkill = fightSkill;
Expand All @@ -26,9 +33,7 @@ public void fight(Entity entity) {
if (AITools.playerInRange(entity, attackRange)) {
try {
fightSkill.execute(entity);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
} else {
Expand Down
7 changes: 6 additions & 1 deletion game/src/ecs/components/ai/idle/IIdleAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@

public interface IIdleAI {

public void idle(Entity entity);
/**
* Implements the idle behavior of an AI controlled entity
*
* @param entity associated entity
*/
void idle(Entity entity);
}
13 changes: 7 additions & 6 deletions game/src/ecs/components/ai/idle/RadiusWalk.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
import ecs.components.PositionComponent;
import ecs.components.ai.AITools;
import ecs.entities.Entity;
import java.util.*;
import level.elements.ILevel;
import level.elements.tile.Tile;
import mydungeon.ECS;
import tools.Constants;

public class RadiusWalk implements IIdleAI {

private static Random random = new Random();
private float radius;
private final float radius;
private GraphPath<Tile> path;

private final int breakTime = Constants.FRAME_RATE * 5;
private int currentBreak = 0;

/**
* Finds a point in the radius and then moves there. When the point has been reached, a new
* point in the radius is searched for from there.
*
* @param radius Radius in which a target point is to be searched for
*/
public RadiusWalk(float radius) {
this.radius = radius;
}
Expand All @@ -38,7 +40,6 @@ public void idle(Entity entity) {
}

private boolean pathFinished(Entity entity) {

PositionComponent pc = (PositionComponent) entity.getComponent(PositionComponent.name);
ILevel level = ECS.currentLevel;
return path.get(path.getCount() - 1)
Expand Down
9 changes: 8 additions & 1 deletion game/src/ecs/components/ai/transition/ITransition.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

import ecs.entities.Entity;

/** Determines when an ai switches between idle and fight */
public interface ITransition {

public boolean goFightMode(Entity entity);
/**
* Function that determines whether an entity should be in combat mode
*
* @param entity associated entity
* @return if the entity should fight
*/
boolean isInFightMode(Entity entity);
}
9 changes: 7 additions & 2 deletions game/src/ecs/components/ai/transition/RangeTransition.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@

public class RangeTransition implements ITransition {

private float range;
private final float range;

/**
* Switches to combat mode when the player is within range of the entity.
*
* @param range Range of the entity.
*/
public RangeTransition(float range) {
this.range = range;
}

@Override
public boolean goFightMode(Entity entity) {
public boolean isInFightMode(Entity entity) {
return AITools.playerInRange(entity, range);
}
}
1 change: 1 addition & 0 deletions game/src/ecs/systems/AISystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ecs.entities.Entity;
import mydungeon.ECS;

/** Controls the AI */
public class AISystem extends ECS_System {
@Override
public void update() {
Expand Down

0 comments on commit c16e3ee

Please sign in to comment.