Skip to content

Commit

Permalink
Prevent placement of structure that was not built upon 'construction
Browse files Browse the repository at this point in the history
completed'

- by checking for the 'isAwaitplacement' for the icon/buildable if that
is the one being completed.
  • Loading branch information
stefanhendriks committed Aug 4, 2017
1 parent d73d985 commit 032434a
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fundynamic.d2tm.game.entities.Entity;
import com.fundynamic.d2tm.game.entities.entitybuilders.AbstractBuildableEntity;
import com.fundynamic.d2tm.game.types.EntityData;

import java.util.List;

Expand All @@ -14,8 +15,8 @@ public interface EntityBuilder extends Updateable {
List<AbstractBuildableEntity> getBuildList();

/**
* Returns true if construction of an entity is still busy.
* Returns true when construction is finished, but requires placement.
* Returns if construction of an entity is still busy or when construction is finished, but requires placement / spawning.
*
* @return
*/
boolean isBuildingAnEntity();
Expand All @@ -29,10 +30,18 @@ public interface EntityBuilder extends Updateable {
void buildEntity(AbstractBuildableEntity placementBuildableEntity);

/**
* Returns true when construction is completed and awaits placements - ie, needs player interaction
* Returns true when construction is completed and awaits placements - ie, needs player interaction.
* Uses entityData comparison to confirm, taken from abstractBuildableEntity
* @return
*/
boolean isAwaitingPlacement();
boolean isAwaitingPlacement(AbstractBuildableEntity placementBuildableEntity);

/**
* Same as above, by using entityData
* @param entityData
* @return
*/
boolean isAwaitingPlacement(EntityData entityData);

/**
* Returns true when construction is completed and awaits being spawned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

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

/**
* Renders a normal mouse cursor with the Structure to be placed, along with visual information
* about what happens when placing structure. Ie, it will show if a structure can be placed or not, but also
* how 'well' the structure can be placed. (latter one to be developed, but implementation should be here)
*/
public class PlacingStructureMouse extends AbstractBattleFieldMouseBehavior {

private EntityData entityDataToPlace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,11 @@ public float getProgress() {
public int getBuildCost() {
return entityData.buildCost;
}

public boolean isSameConstructedEntity(AbstractBuildableEntity placementBuildableEntity) {
if (placementBuildableEntity == null) return false;
return entityData.equals(placementBuildableEntity.getEntityData());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ public void buildEntity(AbstractBuildableEntity abstractBuildableEntity) {
}

@Override
public boolean isAwaitingPlacement() {
return isBuildingAnEntity() && buildingEntity.awaitsPlacement();
public boolean isAwaitingPlacement(AbstractBuildableEntity placementBuildableEntity) {
return isBuildingAnEntity() && buildingEntity.isSameConstructedEntity(placementBuildableEntity) && buildingEntity.awaitsPlacement();
}

@Override
public boolean isAwaitingPlacement(EntityData entityData) {
return isBuildingAnEntity() && buildingEntity.getEntityData().equals(entityData) && buildingEntity.awaitsPlacement();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public void update(float deltaInSeconds) {

this.entityBuilder.update(deltaInSeconds);

handleUnitConstructedAndNeedsToBeSpawnedLogic();
}

public void handleUnitConstructedAndNeedsToBeSpawnedLogic() {
if (isAwaitingSpawning()) {
List<MapCoordinate> allSurroundingCellsAsCoordinates = getAllSurroundingCellsAsCoordinates();
Unit firstEntityThatBlocksExit = null;
Expand Down Expand Up @@ -138,6 +142,7 @@ public void update(float deltaInSeconds) {
// we did not succeed in spawning an entity
if (isAwaitingSpawning()) {
// TODO: fly it in, nudge other units to move away, etc.
// For now we just kill the blocking unit or we forget about it

// THIS IS JUST FOR FUN
if (firstEntityThatBlocksExit != null) {
Expand Down Expand Up @@ -258,8 +263,13 @@ public void buildEntity(AbstractBuildableEntity abstractBuildableEntity) {
}

@Override
public boolean isAwaitingPlacement() {
return this.entityBuilder.isAwaitingPlacement();
public boolean isAwaitingPlacement(AbstractBuildableEntity placementBuildableEntity) {
return this.entityBuilder.isAwaitingPlacement(placementBuildableEntity);
}

@Override
public boolean isAwaitingPlacement(EntityData entityData) {
return this.entityBuilder.isAwaitingPlacement(entityData);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,5 @@ public Map getMap() {
public void entityPlacedOnMap(Entity entity) {
guiComposite.entityPlacedOnMap(entity);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,27 @@ public void render(Graphics graphics) {

@Override
public void leftClicked() {
if (focussedRenderableBuildableEntity != null) {
if (!entityBuilder.isBuildingAnEntity()) {
// construct it:
// 1. tell it to construct
entityBuilder.buildEntity(focussedRenderableBuildableEntity.getAbstractBuildableEntity());
} else {
// TODO: Is done constructing.
if (entityBuilder.isAwaitingPlacement()) {
// TODO move to BattlefieldInteractable interface?!
AbstractBuildableEntity abstractBuildableEntity = focussedRenderableBuildableEntity.getAbstractBuildableEntity();
guiComposite.wantsToPlaceBuildableEntityOnBattlefield(abstractBuildableEntity);
}
}
if (focussedRenderableBuildableEntity == null) return;
AbstractBuildableEntity abstractBuildableEntity = focussedRenderableBuildableEntity.getAbstractBuildableEntity();

// not constructing anything, so start building it.
if (!entityBuilder.isBuildingAnEntity()) {
// construct it:
// 1. tell it to construct
entityBuilder.buildEntity(abstractBuildableEntity);
} else {
// it is building anything, so lets check the progress:

// does it await placement?
if (entityBuilder.isAwaitingPlacement(abstractBuildableEntity)) {
// TODO move to BattlefieldInteractable interface?!

// update state of this entity
focussedRenderableBuildableEntity.leftClicked();
guiComposite.wantsToPlaceBuildableEntityOnBattlefield(abstractBuildableEntity);
}
}

// update state of this entity
focussedRenderableBuildableEntity.leftClicked();
}

@Override
Expand Down Expand Up @@ -131,13 +135,13 @@ public void entityPlacedOnMap(Entity entity) {
if (!entityBuilder.isBuildingAnEntity()) {
// not building anything, that is weird?
throw new IllegalStateException("Did not expect this");
} else {
if (entityBuilder.isAwaitingPlacement()) {
entityBuilder.entityIsDelivered(entity);
} else {
// for now...
throw new IllegalStateException("Would expect to have an entityBuilder selected with the entity that just has been placed.");
}
}

// if nothing was awaiting placement, throw an exception as well
if (!entityBuilder.isAwaitingPlacement(entity.getEntityData())) {
throw new IllegalStateException("Would expect to have an entityBuilder selected with the entity that just has been placed.");
}

entityBuilder.entityIsDelivered(entity);
}
}
84 changes: 84 additions & 0 deletions src/main/java/com/fundynamic/d2tm/game/types/EntityData.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,88 @@ public Vector2D getSize() {
public List<String> getEntityDataKeysToBuild() {
return StringUtils.splitLenientToList(buildList, ",");
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

EntityData that = (EntityData) o;

if (Float.compare(that.buildTimeInSeconds, buildTimeInSeconds) != 0) return false;
if (Float.compare(that.buildRange, buildRange) != 0) return false;
if (buildCost != that.buildCost) return false;
if (facings != that.facings) return false;
if (width != that.width) return false;
if (height != that.height) return false;
if (widthInCells != that.widthInCells) return false;
if (heightInCells != that.heightInCells) return false;
if (maxAscensionHeight != that.maxAscensionHeight) return false;
if (Float.compare(that.startToDescendPercentage, startToDescendPercentage) != 0) return false;
if (Float.compare(that.maxAscensionAtFlightPercentage, maxAscensionAtFlightPercentage) != 0) return false;
if (sight != that.sight) return false;
if (Float.compare(that.moveSpeed, moveSpeed) != 0) return false;
if (Float.compare(that.turnSpeed, turnSpeed) != 0) return false;
if (Float.compare(that.turnSpeedCannon, turnSpeedCannon) != 0) return false;
if (Float.compare(that.attackRate, attackRate) != 0) return false;
if (Float.compare(that.attackRange, attackRange) != 0) return false;
if (damage != that.damage) return false;
if (hitPoints != that.hitPoints) return false;
if (Float.compare(that.animationSpeed, animationSpeed) != 0) return false;
if (recolor != that.recolor) return false;
if (Float.compare(that.chop, chop) != 0) return false;
if (Float.compare(that.halfChop, halfChop) != 0) return false;
if (isHarvester != that.isHarvester) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (type != that.type) return false;
if (entityBuilderType != that.entityBuilderType) return false;
if (buildIcon != null ? !buildIcon.equals(that.buildIcon) : that.buildIcon != null) return false;
if (buildList != null ? !buildList.equals(that.buildList) : that.buildList != null) return false;
if (image != null ? !image.equals(that.image) : that.image != null) return false;
if (barrelImage != null ? !barrelImage.equals(that.barrelImage) : that.barrelImage != null) return false;
if (weaponId != null ? !weaponId.equals(that.weaponId) : that.weaponId != null) return false;
if (explosionId != null ? !explosionId.equals(that.explosionId) : that.explosionId != null) return false;
if (key != null ? !key.equals(that.key) : that.key != null) return false;
return soundData != null ? soundData.equals(that.soundData) : that.soundData == null;
}

@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (type != null ? type.hashCode() : 0);
result = 31 * result + (entityBuilderType != null ? entityBuilderType.hashCode() : 0);
result = 31 * result + (buildTimeInSeconds != +0.0f ? Float.floatToIntBits(buildTimeInSeconds) : 0);
result = 31 * result + (buildRange != +0.0f ? Float.floatToIntBits(buildRange) : 0);
result = 31 * result + buildCost;
result = 31 * result + (buildIcon != null ? buildIcon.hashCode() : 0);
result = 31 * result + (buildList != null ? buildList.hashCode() : 0);
result = 31 * result + (image != null ? image.hashCode() : 0);
result = 31 * result + (barrelImage != null ? barrelImage.hashCode() : 0);
result = 31 * result + facings;
result = 31 * result + width;
result = 31 * result + height;
result = 31 * result + widthInCells;
result = 31 * result + heightInCells;
result = 31 * result + maxAscensionHeight;
result = 31 * result + (startToDescendPercentage != +0.0f ? Float.floatToIntBits(startToDescendPercentage) : 0);
result = 31 * result + (maxAscensionAtFlightPercentage != +0.0f ? Float.floatToIntBits(maxAscensionAtFlightPercentage) : 0);
result = 31 * result + sight;
result = 31 * result + (moveSpeed != +0.0f ? Float.floatToIntBits(moveSpeed) : 0);
result = 31 * result + (turnSpeed != +0.0f ? Float.floatToIntBits(turnSpeed) : 0);
result = 31 * result + (turnSpeedCannon != +0.0f ? Float.floatToIntBits(turnSpeedCannon) : 0);
result = 31 * result + (attackRate != +0.0f ? Float.floatToIntBits(attackRate) : 0);
result = 31 * result + (attackRange != +0.0f ? Float.floatToIntBits(attackRange) : 0);
result = 31 * result + (weaponId != null ? weaponId.hashCode() : 0);
result = 31 * result + damage;
result = 31 * result + hitPoints;
result = 31 * result + (explosionId != null ? explosionId.hashCode() : 0);
result = 31 * result + (animationSpeed != +0.0f ? Float.floatToIntBits(animationSpeed) : 0);
result = 31 * result + (key != null ? key.hashCode() : 0);
result = 31 * result + (recolor ? 1 : 0);
result = 31 * result + (chop != +0.0f ? Float.floatToIntBits(chop) : 0);
result = 31 * result + (halfChop != +0.0f ? Float.floatToIntBits(halfChop) : 0);
result = 31 * result + (isHarvester ? 1 : 0);
result = 31 * result + (soundData != null ? soundData.hashCode() : 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,21 @@ public void multipleBuildListWithSpaces() {
Assert.assertEquals("REFINERY", entityDataKeysToBuild.get(1));
}

@Test
public void equals() {
EntityData one = new EntityData();
one.type = EntityType.UNIT;
one.name = "Foo";
one.setHeight(32);
one.setWidth(32);

EntityData two = new EntityData();
two.type = EntityType.UNIT;
two.name = "Foo";
two.setHeight(32);
two.setWidth(32);

Assert.assertEquals(one, two);
}

}

0 comments on commit 032434a

Please sign in to comment.