Skip to content

Commit

Permalink
Improve movement of units by simply choosing the 'best' option in the
Browse files Browse the repository at this point in the history
surrounding cells.

This is no path finding, but it greatly improves moving of units
  • Loading branch information
stefanhendriks committed Aug 2, 2017
1 parent 11de065 commit 00fef14
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.fundynamic.d2tm.game.map.Cell;
import com.fundynamic.d2tm.game.rendering.gui.battlefield.BattleField;
import com.fundynamic.d2tm.math.Coordinate;
import com.fundynamic.d2tm.math.MapCoordinate;

/**
*
Expand Down Expand Up @@ -63,7 +64,8 @@ public void leftClicked() {
attackDestructibleIfApplicable(hoveringOverEntity);
}
} else {
Coordinate target = cell.getMapCoordinate().toCoordinate();
MapCoordinate mapCoordinate = cell.getMapCoordinate();
Coordinate target = mapCoordinate.toCoordinate();

EntitiesSet harvestersSelected = entitiesSetOfAllMovable.filter(Predicate.isHarvester());
if (harvestersSelected.hasAny() && entitiesSetOfAllMovable.sameSizeAs(harvestersSelected)) {
Expand Down
34 changes: 31 additions & 3 deletions src/main/java/com/fundynamic/d2tm/game/entities/units/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
import com.fundynamic.d2tm.game.rendering.gui.battlefield.RenderQueue;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.math.Coordinate;
import com.fundynamic.d2tm.math.MapCoordinate;
import com.fundynamic.d2tm.math.Random;
import com.fundynamic.d2tm.math.Vector2D;
import org.newdawn.slick.Graphics;

import java.util.List;

import static com.fundynamic.d2tm.game.map.Cell.TILE_SIZE;

/**
Expand Down Expand Up @@ -236,22 +239,47 @@ public void chaseOrAttack(float deltaInSeconds) {
public boolean canMoveToCell(Coordinate intendedMapCoordinatesToMoveTo) {
EntitiesSet entities = entityRepository.findAliveEntitiesOfTypeAtVector(intendedMapCoordinatesToMoveTo.addHalfTile(), EntityType.UNIT, EntityType.STRUCTURE);
entities = entities.exclude(this); // do not count ourselves as blocking
Cell cell = map.getCellByAbsoluteMapCoordinates(new Coordinate(intendedMapCoordinatesToMoveTo));
Cell cell = map.getCellByAbsoluteMapCoordinates(intendedMapCoordinatesToMoveTo);

return entities.isEmpty() &&
cell.isPassable(this) &&
UnitMoveIntents.instance.isVectorClaimableBy(intendedMapCoordinatesToMoveTo, this);
}

public Coordinate getNextIntendedCellToMoveToTarget() {
List<MapCoordinate> allSurroundingCellsAsCoordinates = getAllSurroundingCellsAsCoordinates();
Coordinate bestCoordinate = null;
float closest = map.getHeight() * map.getHeight();

for (MapCoordinate mapCoordinate : allSurroundingCellsAsCoordinates) {
Coordinate coordinate = mapCoordinate.toCoordinate();
boolean canMoveToCell = canMoveToCell(coordinate);
if (!canMoveToCell) continue;

float distance = coordinate.addHalfTile().distance(target);
if (distance <= closest) {
closest = distance;
bestCoordinate = mapCoordinate.toCoordinate();
}
}

if (bestCoordinate != null) {
return bestCoordinate;
}

// fall back to old behavior if we can't find a good destination, which basically means it will try to go to the
// direct path which is probably blocked (else it was found in the previous statements above)
int nextXCoordinate = coordinate.getXAsInt();
int nextYCoordinate = coordinate.getYAsInt();

// the most direct way to target
Coordinate bestNextIntendedCoordinateToMoveTo = Coordinate.create(nextXCoordinate, nextYCoordinate);

if (target.getXAsInt() < coordinate.getXAsInt()) nextXCoordinate -= TILE_SIZE;
if (target.getXAsInt() > coordinate.getXAsInt()) nextXCoordinate += TILE_SIZE;
if (target.getYAsInt() < coordinate.getYAsInt()) nextYCoordinate -= TILE_SIZE;
if (target.getYAsInt() > coordinate.getYAsInt()) nextYCoordinate += TILE_SIZE;

return Coordinate.create(nextXCoordinate, nextYCoordinate);
return bestNextIntendedCoordinateToMoveTo;
}

public boolean hasNoNextCellToMoveTo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.fundynamic.d2tm.game.controls.battlefield.CellBasedMouseBehavior;
import com.fundynamic.d2tm.game.controls.battlefield.NormalMouse;
import com.fundynamic.d2tm.game.entities.*;
import com.fundynamic.d2tm.game.entities.predicates.PredicateBuilder;
import com.fundynamic.d2tm.game.map.Cell;
import com.fundynamic.d2tm.game.map.Map;
import com.fundynamic.d2tm.game.map.Perimeter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.fundynamic.d2tm.game.controls.battlefield;

import com.fundynamic.d2tm.game.AbstractD2TMTest;
import com.fundynamic.d2tm.game.controls.battlefield.MovableSelectedMouse;
import com.fundynamic.d2tm.game.entities.Player;
import com.fundynamic.d2tm.game.entities.entitiesdata.EntitiesData;
import com.fundynamic.d2tm.game.entities.units.Unit;
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/fundynamic/d2tm/game/entities/EntityTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fundynamic.d2tm.game.entities;

import com.fundynamic.d2tm.game.AbstractD2TMTest;
import com.fundynamic.d2tm.game.map.Cell;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.math.Coordinate;
import com.fundynamic.d2tm.math.MapCoordinate;
Expand Down Expand Up @@ -37,6 +38,7 @@ public void setUp() throws SlickException {

entity1 = new TestableEntity(topLeftCoordinate, mock(SpriteSheet.class), entityData, player, entityRepository).
setName("Entity1");

entity2 = new TestableEntity(topLeftCoordinate, mock(SpriteSheet.class), entityData, player, entityRepository).
setName("Entity2");
}
Expand Down Expand Up @@ -76,6 +78,32 @@ public void getAllSurroundingCoordinatesOfAnEntity() {
Assert.assertEquals(3, upLeftOfTopLeft.getYAsInt());
}

@Test
public void getAllSurroundingCoordinatesOfAnEntityThatIsOneCellBig() {
EntityData entityData = new EntityData();
entityData.setWidth(Cell.TILE_SIZE);
entityData.setHeight(Cell.TILE_SIZE);

TestableEntity smallEntity = new TestableEntity(topLeftCoordinate, mock(SpriteSheet.class), entityData, player, entityRepository).
setName("SmallEntity");

List<MapCoordinate> allSurroundingCellsAsCoordinates = smallEntity.getAllSurroundingCellsAsCoordinates();

Assert.assertEquals(8, allSurroundingCellsAsCoordinates.size());

// we expect the first tile to be at 1 tile above and 1 tile left to the entity1:
MapCoordinate upLeftOfTopLeft = allSurroundingCellsAsCoordinates.get(0);

MapCoordinate topLeftMapCoordinate = this.topLeftCoordinate.toMapCoordinate();
Assert.assertEquals(topLeftMapCoordinate.getXAsInt() - 1, upLeftOfTopLeft.getXAsInt());
Assert.assertEquals(topLeftMapCoordinate.getYAsInt() - 1, upLeftOfTopLeft.getYAsInt());

MapCoordinate downRight = allSurroundingCellsAsCoordinates.get(7); // last one

Assert.assertEquals(topLeftMapCoordinate.getXAsInt() + 1, downRight.getXAsInt());
Assert.assertEquals(topLeftMapCoordinate.getYAsInt() + 1, downRight.getYAsInt());
}

//////////////////////////////////////////////////
// Event handling, subscribers, etc
/////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ public void whenUnitDiesItSpawnsAnExplosion() {

@Test
public void verifyUnitMovesToDesiredCellItWantsToMoveToDownRightCell() {
Unit unit = makeUnit(UnitFacings.DOWN, unitAbsoluteMapCoordinates);
Unit unit = makeUnit(UnitFacings.DOWN, unitAbsoluteMapCoordinates); // 320, 320

Vector2D mapCoordinateToMoveTo = unitAbsoluteMapCoordinates.add(Vector2D.create(32, 32)); // move to right-down
Vector2D mapCoordinateToMoveTo = unitAbsoluteMapCoordinates.add(Vector2D.create(32, 32)); // move to right-down (352, 352)
unit.moveTo(mapCoordinateToMoveTo); // translate to absolute coordinates

assertThat(unit.getCoordinate(), is(unitAbsoluteMapCoordinates));
Expand Down

0 comments on commit 00fef14

Please sign in to comment.