diff --git a/src/main/java/com/fundynamic/d2tm/game/entities/EntitiesSet.java b/src/main/java/com/fundynamic/d2tm/game/entities/EntitiesSet.java index b53af55c..e641a8e7 100644 --- a/src/main/java/com/fundynamic/d2tm/game/entities/EntitiesSet.java +++ b/src/main/java/com/fundynamic/d2tm/game/entities/EntitiesSet.java @@ -49,6 +49,10 @@ public boolean hasAny() { return size() > 0; } + public boolean hasOne() { + return size() == 1; + } + /** * * Returns first element or null when size is 0 diff --git a/src/main/java/com/fundynamic/d2tm/game/entities/predicates/BelongsToPlayer.java b/src/main/java/com/fundynamic/d2tm/game/entities/predicates/BelongsToPlayer.java index edf9062b..509d7142 100644 --- a/src/main/java/com/fundynamic/d2tm/game/entities/predicates/BelongsToPlayer.java +++ b/src/main/java/com/fundynamic/d2tm/game/entities/predicates/BelongsToPlayer.java @@ -5,11 +5,16 @@ import com.fundynamic.d2tm.game.entities.Player; import com.fundynamic.d2tm.game.entities.Predicate; +import java.util.HashMap; +import java.util.Map; + public class BelongsToPlayer extends Predicate { + private static Map instances = new HashMap<>(); + private final Player playerItShouldBelongTo; - public BelongsToPlayer(Player playerItShouldBelongTo) { + private BelongsToPlayer(Player playerItShouldBelongTo) { this.playerItShouldBelongTo = playerItShouldBelongTo; } @@ -24,4 +29,12 @@ public String toString() { "playerItShouldBelongTo=" + playerItShouldBelongTo + '}'; } + + public static BelongsToPlayer instance(Player playerItShouldBelongTo) { + if (!instances.containsKey(playerItShouldBelongTo)) { + instances.put(playerItShouldBelongTo, new BelongsToPlayer(playerItShouldBelongTo)); + } + return instances.get(playerItShouldBelongTo); + } + } diff --git a/src/main/java/com/fundynamic/d2tm/game/entities/predicates/NotPredicate.java b/src/main/java/com/fundynamic/d2tm/game/entities/predicates/NotPredicate.java index 2f4fe90b..7a1fe18c 100644 --- a/src/main/java/com/fundynamic/d2tm/game/entities/predicates/NotPredicate.java +++ b/src/main/java/com/fundynamic/d2tm/game/entities/predicates/NotPredicate.java @@ -3,6 +3,9 @@ import com.fundynamic.d2tm.game.entities.Entity; import com.fundynamic.d2tm.game.entities.Predicate; +/** + * Inverses the result of the given predicate. + */ public class NotPredicate extends Predicate { private final Predicate predicateToNegate; diff --git a/src/main/java/com/fundynamic/d2tm/game/entities/predicates/PredicateBuilder.java b/src/main/java/com/fundynamic/d2tm/game/entities/predicates/PredicateBuilder.java index bda1b7d6..360940df 100644 --- a/src/main/java/com/fundynamic/d2tm/game/entities/predicates/PredicateBuilder.java +++ b/src/main/java/com/fundynamic/d2tm/game/entities/predicates/PredicateBuilder.java @@ -7,7 +7,6 @@ import com.fundynamic.d2tm.game.entities.Predicate; import com.fundynamic.d2tm.math.Coordinate; import com.fundynamic.d2tm.math.Rectangle; -import com.fundynamic.d2tm.math.Vector2D; import java.util.LinkedList; import java.util.List; @@ -27,7 +26,7 @@ public PredicateBuilder() { } public PredicateBuilder forPlayer(Player player) { - predicates.add(new BelongsToPlayer(player)); + predicates.add(BelongsToPlayer.instance((player))); return this; } @@ -47,6 +46,16 @@ public Predicate build() { return new AndPredicate(predicates); } + public PredicateBuilder belongsToPlayer(Player playerItShouldBelongTo) { + predicates.add(BelongsToPlayer.instance(playerItShouldBelongTo)); + return this; + } + + public PredicateBuilder isEntityBuilder() { + predicates.add(IsEntityBuilder.instance); + return this; + } + public PredicateBuilder isSelectable() { predicates.add(Predicate.isSelectable()); return this; diff --git a/src/main/java/com/fundynamic/d2tm/game/entities/units/Unit.java b/src/main/java/com/fundynamic/d2tm/game/entities/units/Unit.java index 3a8e4f20..8101d353 100644 --- a/src/main/java/com/fundynamic/d2tm/game/entities/units/Unit.java +++ b/src/main/java/com/fundynamic/d2tm/game/entities/units/Unit.java @@ -109,7 +109,7 @@ public void update(float deltaInSeconds) { // scan environment within range for enemies EntitiesSet entities = entityRepository.findEntitiesOfTypeAtVectorWithinDistance(getCenteredCoordinate(), entityData.sight * 32, EntityType.UNIT, EntityType.STRUCTURE); - EntitiesSet enemyEntities = entities.filter(new NotPredicate(new BelongsToPlayer(player))); + EntitiesSet enemyEntities = entities.filter(new NotPredicate(BelongsToPlayer.instance(player))); if (enemyEntities.isEmpty()) { if (this.getPlayer().isCPU()) { @@ -131,7 +131,7 @@ public void update(float deltaInSeconds) { attack(enemyToAttack); } else { EntitiesSet allUnits = entityRepository.allUnits(); - enemyEntities = allUnits.filter(new NotPredicate(new BelongsToPlayer(player))); + enemyEntities = allUnits.filter(new NotPredicate(BelongsToPlayer.instance((player)))); distance = 131072; // 64X64X32 enemyToAttack = null; diff --git a/src/main/java/com/fundynamic/d2tm/game/rendering/gui/battlefield/BattleField.java b/src/main/java/com/fundynamic/d2tm/game/rendering/gui/battlefield/BattleField.java index 0b184418..abe6ccda 100644 --- a/src/main/java/com/fundynamic/d2tm/game/rendering/gui/battlefield/BattleField.java +++ b/src/main/java/com/fundynamic/d2tm/game/rendering/gui/battlefield/BattleField.java @@ -5,6 +5,7 @@ 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; @@ -264,13 +265,16 @@ public void setViewingVector(Vector2D viewingVector) { @Override public void entitiesSelected(EntitiesSet entities) { System.out.println("Battlefield gets told that " + entities + " are selected"); - EntitiesSet entityBuilders = entities.filter(Predicate.isEntityBuilder()); - if (entityBuilders.size() == 1) { + EntitiesSet entityBuildersForControllingPlayer = entities.filter( + Predicate.builder() + .belongsToPlayer(mouse.getControllingPlayer()) + .isEntityBuilder() + ); + + if (entityBuildersForControllingPlayer.hasOne()) { Entity first = entities.getFirst(); - if (first.isEntityBuilder()) { - guiComposite.entityBuilderSelected(first); - } + guiComposite.entityBuilderSelected(first); } else { guiComposite.allEntityBuildersDeSelected(); } diff --git a/src/test/java/com/fundynamic/d2tm/game/entities/EntitiesSetTest.java b/src/test/java/com/fundynamic/d2tm/game/entities/EntitiesSetTest.java index 29cf7533..1e7aed80 100644 --- a/src/test/java/com/fundynamic/d2tm/game/entities/EntitiesSetTest.java +++ b/src/test/java/com/fundynamic/d2tm/game/entities/EntitiesSetTest.java @@ -72,14 +72,14 @@ public void setUp() throws SlickException { @Test public void filtersForPlayer() { - Set result = entitiesSet.filter(new BelongsToPlayer(player)); + Set result = entitiesSet.filter(BelongsToPlayer.instance(player)); assertEquals(playerOneStructureCount + playerOneUnitCount + playerOneBareEntitiesCount, result.size()); } @Test public void filtersNotForPlayer() { // get everything except player one - Set result = entitiesSet.filter(new NotPredicate(new BelongsToPlayer(player))); + Set result = entitiesSet.filter(new NotPredicate(BelongsToPlayer.instance(player))); // player 2 has 6 entities assertEquals(6, result.size()); }