Skip to content

Commit

Permalink
Merge pull request #42 from martinjonsson01/12-build-using-resources
Browse files Browse the repository at this point in the history
12 build using resources
  • Loading branch information
JacobBredin authored Oct 18, 2021
2 parents 2d54b0e + b3d0da2 commit 6029179
Show file tree
Hide file tree
Showing 30 changed files with 1,087 additions and 129 deletions.
1 change: 1 addition & 0 deletions config/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<suppressions>
<suppress checks="ClassDataAbstractionCoupling" files="Pawntastic"/>
<suppress checks="[a-zA-Z0-9]*" files="[\\/]test[\\/]"/>
<suppress checks="NoWhitespaceAfter" files="MatrixUtilsTests" />
<suppress checks="ClassDataAbstractionCoupling" files="BeingTests" />
</suppressions>
2 changes: 1 addition & 1 deletion src/main/java/com/thebois/Pawntastic.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void createModels() throws IOException, ClassNotFoundException {
world = new World(WORLD_SIZE, 0);
colony = new Colony(
world.findEmptyPositions(PAWN_POSITIONS),
new AstarPathFinder(world));
new AstarPathFinder(world), world);
}
}

Expand Down
17 changes: 0 additions & 17 deletions src/main/java/com/thebois/models/IFinder.java

This file was deleted.

32 changes: 32 additions & 0 deletions src/main/java/com/thebois/models/IStructureFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.thebois.models;

import java.util.Optional;

import com.thebois.models.world.structures.IStructure;
import com.thebois.models.world.structures.StructureType;

/**
* Allows for locating of specific types of objects.
*/
public interface IStructureFinder {

/**
* Finds and returns the closest IStructure to the passed position.
*
* @param origin The position to find a structure closest to.
* @param type The type of structure to find.
*
* @return The found IStructure.
*/
Optional<IStructure> getNearbyStructureOfType(Position origin, StructureType type);

/**
* Finds and returns the closest incomplete IStructure to the passed position.
*
* @param origin The position to find an incomplete structure closest to.
*
* @return The found IStructure.
*/
Optional<IStructure> getNearbyIncompleteStructure(Position origin);

}
8 changes: 4 additions & 4 deletions src/main/java/com/thebois/models/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ public int hashCode() {
}

@Override
public boolean equals(final Object o) {
if (this == o) {
public boolean equals(final Object other) {
if (this == other) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (other == null || getClass() != other.getClass()) {
return false;
}
final Position position = (Position) o;
final Position position = (Position) other;
return Float.compare(position.posX, posX) == 0 && Float.compare(position.posY, posY) == 0;
}

Expand Down
53 changes: 46 additions & 7 deletions src/main/java/com/thebois/models/beings/AbstractBeing.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.thebois.models.beings.pathfinding.IPathFinder;
import com.thebois.models.beings.roles.AbstractRole;
import com.thebois.models.beings.roles.RoleFactory;
import com.thebois.models.IStructureFinder;

/**
* An abstract implementation of IBeing.
Expand All @@ -28,19 +29,25 @@ public abstract class AbstractBeing implements IBeing {
private Stack<Position> path;
private Position position;
private AbstractRole role;
private final IStructureFinder finder;

/**
* Creates an AbstractBeing with an initial position.
*
* @param startPosition the initial position of the AbstractBeing.
* @param destination the initial destination of the AbstractBeing.
* @param startPosition The initial position of the AbstractBeing.
* @param destination The initial destination of the AbstractBeing.
* @param pathFinder The generator of paths to positions in the world.
* @param finder Used to find structures in the world.
*/
public AbstractBeing(
final Position startPosition, final Position destination, final IPathFinder pathFinder) {
final Position startPosition,
final Position destination,
final IPathFinder pathFinder,
final IStructureFinder finder) {
this.position = startPosition;
this.role = RoleFactory.idle();
this.pathFinder = pathFinder;
this.finder = finder;
setPath(pathFinder.path(startPosition, destination));
Pawntastic.getEventBus().register(this);
}
Expand All @@ -64,10 +71,10 @@ public int hashCode() {
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof AbstractBeing)) return false;
final AbstractBeing that = (AbstractBeing) o;
public boolean equals(final Object other) {
if (this == other) return true;
if (!(other instanceof AbstractBeing)) return false;
final AbstractBeing that = (AbstractBeing) other;
return Objects.equals(getPosition(), that.getPosition()) && Objects.equals(getRole(),
that.getRole());
}
Expand Down Expand Up @@ -166,8 +173,40 @@ protected Optional<Position> getDestination() {
return Optional.of(path.peek().deepClone());
}

protected Position getFinalDestination() {
if (path.isEmpty()) return null;
return path.firstElement().deepClone();
}

protected IPathFinder getPathFinder() {
return pathFinder;
}

protected IStructureFinder getStructureFinder() {
return this.finder;
}

protected Position nearestNeighborOf(final Position destination) {
final int[][] positionOffsets = {
{-1, -1}, {0, -1}, {1, -1},
{-1, 0}, {1, 0},
{-1, 1}, {0, 1}, {1, 1},
};

Position nearestNeighbor = new Position(Float.MAX_VALUE, Float.MAX_VALUE);
Position lastPosition;

for (int i = 0; i < positionOffsets.length; i++) {
final float x = destination.getPosX() + positionOffsets[i][0];
final float y = destination.getPosY() + positionOffsets[i][1];
lastPosition = new Position(x, y);

if (getPosition().distanceTo(nearestNeighbor)
> getPosition().distanceTo(lastPosition)) {
nearestNeighbor = lastPosition;
}
}
return nearestNeighbor;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ public Collection<IBeing> getBeings() {
protected void setBeings(final Collection<IBeing> beings) {
this.beings = beings;
}

}
7 changes: 5 additions & 2 deletions src/main/java/com/thebois/models/beings/Colony.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.thebois.models.IStructureFinder;
import com.thebois.models.Position;
import com.thebois.models.beings.pathfinding.IPathFinder;
import com.thebois.models.beings.roles.AbstractRole;
Expand All @@ -31,12 +32,14 @@ public class Colony extends AbstractBeingGroup implements IRoleAllocator, IInven
*
* @param vacantPositions Positions in the world where pawns can be created.
* @param pathFinder The pathfinder that the pawns will use.
* @param finder Used to find things in the world.
*/
public Colony(final Iterable<Position> vacantPositions, final IPathFinder pathFinder) {
public Colony(final Iterable<Position> vacantPositions, final IPathFinder pathFinder,
final IStructureFinder finder) {
final Collection<IBeing> pawns = new ArrayList<>();
final Random random = new Random();
for (final Position vacantPosition : vacantPositions) {
pawns.add(new Pawn(vacantPosition, vacantPosition, random, pathFinder));
pawns.add(new Pawn(vacantPosition, vacantPosition, random, pathFinder, finder));
}
setBeings(pawns);
}
Expand Down
50 changes: 45 additions & 5 deletions src/main/java/com/thebois/models/beings/Pawn.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.thebois.models.beings;

import java.util.Collection;
import java.util.Optional;
import java.util.Random;

import com.thebois.models.IStructureFinder;
import com.thebois.models.Position;
import com.thebois.models.beings.pathfinding.IPathFinder;
import com.thebois.models.inventory.items.ItemFactory;
import com.thebois.models.inventory.items.ItemType;
import com.thebois.models.world.structures.IStructure;

/**
* Pawn.
Expand All @@ -14,28 +19,40 @@ public class Pawn extends AbstractBeing {
/* Temporary hard-coded world size. Should be removed when pathfinding is implemented. */
private static final int WORLD_SIZE = 50;
private final Random random;
private IStructure closestIncompleteStructure;

/**
* Instantiates with an initial position and a destination to travel to.
*
* @param startPosition initial position.
* @param destination initial destination to travel to.
* @param random the generator of random numbers.
* @param startPosition Initial position.
* @param destination Initial destination to travel to.
* @param random The generator of random numbers.
* @param pathFinder The generator of paths to positions in the world.
* @param finder Used to find things in the world.
*/
public Pawn(
final Position startPosition,
final Position destination,
final Random random,
final IPathFinder pathFinder) {
super(startPosition, destination, pathFinder);
final IPathFinder pathFinder,
final IStructureFinder finder) {
super(startPosition, destination, pathFinder, finder);
this.random = random;
}

@Override
public void update() {
super.update();

if (closestIncompleteStructure == null || closestIncompleteStructure.isCompleted()) {
closestIncompleteStructure = findNearestIncompleteStructure();
}

if (closestIncompleteStructure != null) {
setFinalDestination(closestIncompleteStructure.getPosition());
tryDeliverItemToStructure(closestIncompleteStructure);
}

if (getDestination().isEmpty()) setRandomDestination();
}

Expand All @@ -48,4 +65,27 @@ private void setRandomDestination() {
setPath(newPath);
}

protected IStructure findNearestIncompleteStructure() {
final Optional<IStructure> structure =
getStructureFinder().getNearbyIncompleteStructure(
this.getPosition());
return structure.orElse(null);
}

private void setFinalDestination(final Position destination) {
final Position nextTo = nearestNeighborOf(destination);
// If finalDestination is empty or is not set to the given destination
if (getFinalDestination() == null || !getFinalDestination().equals(nextTo)) {
final Collection<Position> newPath = getPathFinder().path(this.getPosition(), nextTo);
setPath(newPath);
}
}

protected void tryDeliverItemToStructure(final IStructure structure) {
if (structure.getPosition().distanceTo(this.getPosition()) < 2f) {
structure.tryDeliverItem(ItemFactory.fromType(ItemType.ROCK));
structure.tryDeliverItem(ItemFactory.fromType(ItemType.LOG));
}
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/thebois/models/beings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

## UML Class Diagram

![com.thebois.models.beings](./../../../../../../../documents/diagrams/com.thebois.models.beings.jpg "com.thebois.models.beings")
![com.thebois.models.beings](../../../../../../../documents/diagrams/com.thebois.models.beings.jpg "com.thebois.models.beings")
1 change: 0 additions & 1 deletion src/main/java/com/thebois/models/inventory/IInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,4 @@ public interface IInventory extends Serializable {
* @return The counter.
*/
int numberOf(ItemType itemType);

}
18 changes: 17 additions & 1 deletion src/main/java/com/thebois/models/inventory/items/Item.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.thebois.models.inventory.items;

import java.io.Serializable;
import java.util.Objects;

/**
* A generic item of a specific type.
*/
public class Item implements IItem {
public class Item implements IItem, Serializable {

private final ItemType type;

Expand All @@ -21,4 +24,17 @@ public ItemType getType() {
return type;
}

@Override
public boolean equals(final Object other) {
if (this == other) return true;
if (other == null) return false;
final Item item = (Item) other;
return type == item.type;
}

@Override
public int hashCode() {
return Objects.hash(type);
}

}
Loading

0 comments on commit 6029179

Please sign in to comment.