Skip to content

Commit

Permalink
Merge pull request #169 from Fundynamic/bugfix/155-ability-to-place-a…
Browse files Browse the repository at this point in the history
…ny-structure

Bugfix: ability to place any structure
  • Loading branch information
stefanhendriks committed Aug 4, 2017
2 parents 82dfee5 + f0b72f5 commit 5761b89
Show file tree
Hide file tree
Showing 34 changed files with 258 additions and 90 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 @@ -3,12 +3,12 @@

import com.fundynamic.d2tm.game.controls.Mouse;
import com.fundynamic.d2tm.game.entities.Entity;
import com.fundynamic.d2tm.game.entities.superpowers.SuperPower;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.game.entities.EntityRepository;
import com.fundynamic.d2tm.game.entities.entitybuilders.PlacementBuildableEntity;
import com.fundynamic.d2tm.game.entities.superpowers.SuperPower;
import com.fundynamic.d2tm.game.map.Cell;
import com.fundynamic.d2tm.game.rendering.gui.battlefield.BattleField;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.math.Coordinate;
import org.newdawn.slick.Graphics;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;

import static com.fundynamic.d2tm.game.map.Cell.TILE_SIZE;
import static java.util.stream.Collectors.toList;

/**
* 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
9 changes: 6 additions & 3 deletions src/main/java/com/fundynamic/d2tm/game/entities/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.state.StateBasedGame;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -228,11 +231,11 @@ public List<Coordinate> getAllCellsAsCoordinates() {
}

public List<MapCoordinate> getAllSurroundingCellsAsCoordinates() {
return getMapCoordinates(coordinate.toMapCoordinate()); // coordinate == top left
return getAllSurroundingCellsAsMapCoordinatesStartingFromTopLeft(coordinate.toMapCoordinate()); // coordinate == top left
}

// this basically goes 'around' the entity, starting from top-left
public List<MapCoordinate> getMapCoordinates(MapCoordinate topLeftMapCoordinate) {
private List<MapCoordinate> getAllSurroundingCellsAsMapCoordinatesStartingFromTopLeft(MapCoordinate topLeftMapCoordinate) {
int currentX = topLeftMapCoordinate.getXAsInt();
int currentY = topLeftMapCoordinate.getYAsInt();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


import com.fundynamic.d2tm.game.behaviors.Updateable;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.game.entities.Player;
import com.fundynamic.d2tm.game.entities.sidebar.BuildableState;
import com.fundynamic.d2tm.game.types.EntityData;
import org.newdawn.slick.Image;

/**
Expand Down 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 @@ -2,9 +2,9 @@


import com.fundynamic.d2tm.game.entities.Entity;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.game.entities.Player;
import com.fundynamic.d2tm.game.entities.sidebar.BuildableState;
import com.fundynamic.d2tm.game.types.EntityData;

/**
* <h1>General</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import com.fundynamic.d2tm.game.behaviors.EntityBuilder;
import com.fundynamic.d2tm.game.entities.Entity;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.game.entities.Player;
import com.fundynamic.d2tm.game.types.EntityData;

import java.util.ArrayList;
import java.util.List;
Expand Down 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
@@ -1,8 +1,8 @@
package com.fundynamic.d2tm.game.entities.entitybuilders;


import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.game.entities.Player;
import com.fundynamic.d2tm.game.types.EntityData;

/**
* <h1>General</h1>
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 @@ -12,9 +12,7 @@
import java.util.HashSet;
import java.util.Set;

import static com.fundynamic.d2tm.game.entities.superpowers.SuperPower.SuperPowerState.DONE;
import static com.fundynamic.d2tm.game.entities.superpowers.SuperPower.SuperPowerState.EXPLODING;
import static com.fundynamic.d2tm.game.entities.superpowers.SuperPower.SuperPowerState.LAUNCHED;
import static com.fundynamic.d2tm.game.entities.superpowers.SuperPower.SuperPowerState.*;
import static com.fundynamic.d2tm.game.map.Cell.HALF_TILE;
import static com.fundynamic.d2tm.game.map.Cell.TILE_SIZE;

Expand All @@ -41,7 +39,7 @@ enum SuperPowerState {

public SuperPower(Coordinate coordinate, EntityData entityData, Player player, EntityRepository entityRepository) {
super(coordinate, null, entityData, player, entityRepository);
state = SuperPowerState.INITIAL;
state = INITIAL;
}

// TODO: onCreate!?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.fundynamic.d2tm.game.entities.units;


import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.game.rendering.gui.battlefield.RenderQueue;
import com.fundynamic.d2tm.game.types.EntityData;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.fundynamic.d2tm.game.behaviors.EnrichableAbsoluteRenderable;
import com.fundynamic.d2tm.game.behaviors.Updateable;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.game.rendering.gui.battlefield.RenderQueue;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.math.Random;
import com.fundynamic.d2tm.math.Vector2D;
import org.newdawn.slick.Graphics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public boolean isCellPassableForMe(Coordinate intendedMapCoordinatesToMoveTo) {
}

public Coordinate getNextIntendedCellToMoveToTarget() {
List<MapCoordinate> allSurroundingCellsAsCoordinates = getMapCoordinates(coordinate.toMapCoordinate());
List<MapCoordinate> allSurroundingCellsAsCoordinates = getAllSurroundingCellsAsCoordinates();

float distanceToTarget = distanceTo(target);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.fundynamic.d2tm.game.entities.units.states;


import com.fundynamic.d2tm.game.map.Cell;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.game.entities.EntityRepository;
import com.fundynamic.d2tm.game.entities.units.Unit;
import com.fundynamic.d2tm.game.map.Cell;
import com.fundynamic.d2tm.game.map.Map;
import com.fundynamic.d2tm.game.types.EntityData;
import com.fundynamic.d2tm.math.Coordinate;
import com.fundynamic.d2tm.math.Vector2D;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
import org.newdawn.slick.Input;

/**
* Listens to mouse events and passes them to our mouse class.
* Listens to (Slick) mouse events and passes them to the {@link Mouse} class.
*/
public class MouseListener extends AbstractMouseListener {

private final Mouse mouse;
private Vector2D mouseScreenPosition;

public MouseListener(Mouse mouse) {
this.mouse = mouse;
this.mouseScreenPosition = Vector2D.zero();
}

@Override
Expand Down Expand Up @@ -46,12 +48,12 @@ public void mouseReleased(int button, int x, int y) {

@Override
public void mouseMoved(int oldx, int oldy, int newx, int newy) {
mouse.movedTo(Vector2D.create(newx, newy));
mouse.movedTo(mouseScreenPosition.update(newx, newy));
}

@Override
public void mouseDragged(int oldx, int oldy, int newx, int newy) {
mouse.draggedToCoordinates(Vector2D.create(newx, newy));
mouse.draggedToCoordinates(mouseScreenPosition.update(newx, newy));
}

}
4 changes: 2 additions & 2 deletions src/main/java/com/fundynamic/d2tm/game/map/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.ArrayList;
import java.util.List;

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

/**
*
* This class represents the game map data.
Expand All @@ -21,8 +23,6 @@
*/
public class Map {

private static final int TILE_SIZE = 32; // If possible, get rid of this!

private Shroud shroud;
private final int height, width;
private final int heightWithInvisibleBorder, widthWithInvisibleBorder;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/fundynamic/d2tm/game/map/MapEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import com.fundynamic.d2tm.math.Vector2D;
import org.newdawn.slick.SlickException;

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

public class MapEditor {

private static final int BIT_MASK_NONE = 0;
Expand Down Expand Up @@ -128,13 +131,10 @@ public void putTerrainOnCell(Map map, int x, int y, int terrainType) {
public void createCircularField(Map map, Vector2D centerPosition, int terrainType, int size) {
if (size < 1) return;

int TILE_SIZE = 32;
float halfATile = TILE_SIZE / 2;

// convert to absolute pixel coordinates
Vector2D asPixelsCentered = map.getCellCoordinatesInAbsolutePixels(
centerPosition.getXAsInt(), centerPosition.getYAsInt()).
add(Vector2D.create(halfATile, halfATile));
add(Vector2D.create(HALF_TILE, HALF_TILE));

double centerX = asPixelsCentered.getX();
double centerY = asPixelsCentered.getY();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
/**
* <h1>General</h1>
* <p>
* This class represents all GUI elements in the game.
* This class represents all GUI elements in the game. It can be considered the 'root
* GUI object.
* </p>
* <h2>Rendering</h2>
* <p>
Expand All @@ -32,14 +33,17 @@
* <p>
* This updates all gui elements in the order they have been added (first in, first updated)
* </p>
* <b>{@link MouseBehavior} events</b>
* <h2>{@link MouseBehavior} events</h2>
* <p>
* Mouse events are propagated from the {@link com.fundynamic.d2tm.game.controls.Mouse} class.
* The {@link com.fundynamic.d2tm.game.controls.Mouse} class will propagate any MouseBehavior to this class.
* </p>
* <p>
* This class listens to mouse events. Based on {@link #movedTo(Vector2D)} it will determine
* which guiElement is focussed on, stored in {@link #activeGuiElement}. All other mouse events
* are propagated to {@link #activeGuiElement}.
* <b>Example:</b>
* Suppose Mouse recieves a Move event, based on {@link #movedTo(Vector2D)}. This will help tell the guiComposite
* which guiElement is focussed on. This is in turn stored in {@link #activeGuiElement}.
* </p>
* <p>
* When an active gui element is present, all mouse events are also propagated to {@link #activeGuiElement}.
* </p>
*/
public class GuiComposite implements Renderable, Updateable, MouseBehavior, BattleFieldInteractable {
Expand Down Expand Up @@ -157,15 +161,16 @@ public void assignBattlefieldPropertyIfApplicable(GuiElement guiElement) {
}

/**
* Event: An entity builder is selected
* @param entityBuilder
* Event: An entity is selected
* @param entity
*/
public void entityBuilderSelected(Entity entityBuilder) {
if (!entityBuilder.isEntityBuilder()) {
public void entityBuilderSelected(Entity entity) {
if (!entity.isEntityBuilder()) {
throw new IllegalArgumentException("Can only select entities which implement entity builder from here on");
}
if (entityBuilder.isEntityTypeStructure()) {
sidebar.showEntityBuilderGuiFor((EntityBuilder) entityBuilder);

if (entity.isEntityTypeStructure()) {
sidebar.showEntityBuilderGuiFor((EntityBuilder) entity);
}
}

Expand Down
Loading

0 comments on commit 5761b89

Please sign in to comment.