Skip to content

Commit

Permalink
Merge pull request #134 from Fundynamic/fix-bug/build-only-stuff-from…
Browse files Browse the repository at this point in the history
…-structure-owned-by-player

Fix bug/build only stuff from structure owned by player
  • Loading branch information
stefanhendriks authored Oct 1, 2016
2 parents c710028 + e3114ae commit ce45e92
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity> {

private static Map<Player, BelongsToPlayer> instances = new HashMap<>();

private final Player playerItShouldBelongTo;

public BelongsToPlayer(Player playerItShouldBelongTo) {
private BelongsToPlayer(Player playerItShouldBelongTo) {
this.playerItShouldBelongTo = playerItShouldBelongTo;
}

Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity> {

private final Predicate<Entity> predicateToNegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +26,7 @@ public PredicateBuilder() {
}

public PredicateBuilder forPlayer(Player player) {
predicates.add(new BelongsToPlayer(player));
predicates.add(BelongsToPlayer.instance((player)));
return this;
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ public void setUp() throws SlickException {

@Test
public void filtersForPlayer() {
Set<Entity> result = entitiesSet.filter(new BelongsToPlayer(player));
Set<Entity> result = entitiesSet.filter(BelongsToPlayer.instance(player));
assertEquals(playerOneStructureCount + playerOneUnitCount + playerOneBareEntitiesCount, result.size());
}

@Test
public void filtersNotForPlayer() {
// get everything except player one
Set<Entity> result = entitiesSet.filter(new NotPredicate(new BelongsToPlayer(player)));
Set<Entity> result = entitiesSet.filter(new NotPredicate(BelongsToPlayer.instance(player)));
// player 2 has 6 entities
assertEquals(6, result.size());
}
Expand Down

0 comments on commit ce45e92

Please sign in to comment.