[customwars] r761 committed - Don't make Unit implement Location.

1 view
Skip to first unread message

codesite...@google.com

unread,
Oct 16, 2012, 1:46:43 PM10/16/12
to customwar...@googlegroups.com
Revision: 761
Author: ace.stef
Date: Tue Oct 16 10:46:08 2012
Log: Don't make Unit implement Location.
http://code.google.com/p/customwars/source/detail?r=761

Modified:
/trunk/v2/src/com/customwars/client/action/ActionParser.java
/trunk/v2/src/com/customwars/client/action/unit/LoadAction.java
/trunk/v2/src/com/customwars/client/controller/UnitController.java
/trunk/v2/src/com/customwars/client/controller/UnitMenuBuilder.java
/trunk/v2/src/com/customwars/client/io/loading/map/BinaryCW2MapParser.java
/trunk/v2/src/com/customwars/client/model/CWGameController.java
/trunk/v2/src/com/customwars/client/model/GameController.java
/trunk/v2/src/com/customwars/client/model/fight/Attacker.java
/trunk/v2/src/com/customwars/client/model/gameobject/Unit.java
/trunk/v2/src/com/customwars/client/model/map/Map.java
/trunk/v2/src/com/customwars/client/model/map/path/Mover.java

/trunk/v2/src/com/customwars/client/ui/hud/panel/HorizontalTransportInfoPanel.java

/trunk/v2/src/com/customwars/client/ui/hud/panel/VerticalTransportInfoPanel.java
/trunk/v2/src/com/customwars/client/ui/sprite/UnitSprite.java
/trunk/v2/test/com/customwars/client/action/TestActionCommand.java
/trunk/v2/test/com/customwars/client/io/loading/BinCW2MapParserTest.java

=======================================
--- /trunk/v2/src/com/customwars/client/action/ActionParser.java Sun Mar 20
06:49:26 2011
+++ /trunk/v2/src/com/customwars/client/action/ActionParser.java Tue Oct 16
10:46:08 2012
@@ -104,7 +104,7 @@
int dropCol = scanner.nextInt();
int dropRow = scanner.nextInt();
Location dropLocation = map.getTile(dropCol, dropRow);
- Unit unit = (Unit) transport.getLocatable(transportIndex);
+ Unit unit = transport.getUnitInTransport(transportIndex);
dropQueue.addDropLocation(dropLocation, unit);
}

=======================================
--- /trunk/v2/src/com/customwars/client/action/unit/LoadAction.java Tue Aug
14 03:51:58 2012
+++ /trunk/v2/src/com/customwars/client/action/unit/LoadAction.java Tue Oct
16 10:46:08 2012
@@ -8,7 +8,6 @@
import com.customwars.client.network.MessageSender;
import com.customwars.client.network.NetworkException;
import com.customwars.client.ui.GUI;
-import com.customwars.client.ui.renderer.MapRenderer;
import com.customwars.client.ui.state.InGameContext;
import org.apache.log4j.Logger;

@@ -18,7 +17,6 @@
public class LoadAction extends DirectAction {
private static final Logger logger = Logger.getLogger(LoadAction.class);
private InGameContext inGameContext;
- private MapRenderer mapRenderer;
private GameController gameController;
private MessageSender messageSender;
private final Unit unit, transport;
@@ -31,7 +29,6 @@

protected void init(InGameContext inGameContext) {
this.inGameContext = inGameContext;
- this.mapRenderer = inGameContext.getObj(MapRenderer.class);
this.gameController = inGameContext.getObj(GameController.class);
this.messageSender = inGameContext.getObj(MessageSender.class);
}
@@ -45,7 +42,6 @@
private void load() {
logger.debug("Loading " + unit + " into " + transport);
gameController.load(unit, transport);
- mapRenderer.removeUnit(unit);

if (transport.getStats().canLaunchUnit()) {
// Planes that land on a carrier have to wait 1 turn
=======================================
--- /trunk/v2/src/com/customwars/client/controller/UnitController.java Sun
Mar 20 13:51:30 2011
+++ /trunk/v2/src/com/customwars/client/controller/UnitController.java Tue
Oct 16 10:46:08 2012
@@ -22,8 +22,6 @@
* Handles any input for 1 unit
* This can be surrounding tile information, clicks in a menu, Ai
* Note that the unit is teleported to the select tile in each method.
- *
- * @author stefan
*/
public abstract class UnitController {
Game game;
@@ -189,7 +187,7 @@
if (target.getOwner() == unit.getOwner() && target.getHp() <= 9) {
if (targetStats.canJoin() && unitStats.canJoin()) {
if (targetStats.canTransport()) {
- if (target.getLocatableCount() == 0 &&
unit.getLocatableCount() == 0) {
+ if (!target.hasUnitsInTransport()
&& !unit.hasUnitsInTransport()) {
return true;
}
} else {
=======================================
--- /trunk/v2/src/com/customwars/client/controller/UnitMenuBuilder.java Thu
Mar 24 05:37:03 2011
+++ /trunk/v2/src/com/customwars/client/controller/UnitMenuBuilder.java Tue
Oct 16 10:46:08 2012
@@ -91,8 +91,7 @@
inGameContext.clearUnitsInTransport();

if (controller.canWait(to)) {
- for (int dropIndex = 0; dropIndex < unit.getLocatableCount();
dropIndex++) {
- Unit unitInTransport = (Unit) unit.getLocatable(dropIndex);
+ for (Unit unitInTransport : unit.getUnitsInTransport()) {
boolean unitIsAlreadyDropped =
inGameContext.isUnitDropped(unitInTransport);

// Build start drop actions for each unit in the transport
@@ -294,8 +293,7 @@
inGameContext.clearUnitsInTransport();

if (controller.canWait(to)) {
- for (int unitIndex = 0; unitIndex < unit.getLocatableCount();
unitIndex++) {
- Unit unitInTransport = (Unit) unit.getLocatable(unitIndex);
+ for (Unit unitInTransport : unit.getUnitsInTransport()) {

// Build start launch actions for each unit in the transport
if (controller.canLaunch(from, to, unitInTransport)) {
=======================================
---
/trunk/v2/src/com/customwars/client/io/loading/map/BinaryCW2MapParser.java
Tue Oct 16 09:45:58 2012
+++
/trunk/v2/src/com/customwars/client/io/loading/map/BinaryCW2MapParser.java
Tue Oct 16 10:46:08 2012
@@ -472,13 +472,13 @@
* write 0 as transport count
*/
private void writeUnitsInTransport(Unit unit) throws IOException {
- int unitsInTransport = unit.getLocatableCount();
+ int unitsInTransport = unit.getUnitsInTransportCount();

if (unit.getStats().canTransport() && unitsInTransport > 0) {
out.writeByte(unitsInTransport);

for (int i = 0; i < unitsInTransport; i++) {
- Unit unitInTransport = (Unit) unit.getLocatable(i);
+ Unit unitInTransport = unit.getUnitInTransport(i);
writeUnit(unitInTransport);
}
} else {
=======================================
--- /trunk/v2/src/com/customwars/client/model/CWGameController.java Sun Aug
26 08:33:53 2012
+++ /trunk/v2/src/com/customwars/client/model/CWGameController.java Tue Oct
16 10:46:08 2012
@@ -255,8 +255,17 @@

public void produceUnit(Unit producer, String unitToProduce) {
Unit unit = UnitFactory.getUnit(unitToProduce);
- buildUnit(unit, producer, producer.getOwner());
+ Player player = producer.getOwner();
+ player.addToBudget(-unit.getPrice());
+ player.addUnit(unit);
+ producer.add(unit);
producer.deCreaseConstructionMaterials();
+
+ if (player.isAi()) {
+ controllerManager.addAIUnitController(unit);
+ } else {
+ controllerManager.addHumanUnitController(unit);
+ }
}

@Override
=======================================
--- /trunk/v2/src/com/customwars/client/model/GameController.java Sun Aug
26 08:33:53 2012
+++ /trunk/v2/src/com/customwars/client/model/GameController.java Tue Oct
16 10:46:08 2012
@@ -18,13 +18,17 @@
public interface GameController {
/**
* Drops a unit from within the transport to a drop location
+ *
+ * @param transport The transporter that wants to drop the given unit
+ * @param unit The unit to be dropped on the given drop location
+ * @param dropLocation The location where the unit in the transport
should be dropped on
*/
void drop(Unit transport, Unit unit, Location dropLocation);

void teleport(Location from, Location to);

/**
- * Capture the city with unit
+ * Capture the city with the given unit
*
* @param unit unit that wants to perform the capturing
* @param city The city to capture
@@ -34,34 +38,45 @@

/**
* Remove the unit from the map and add it to the transport
+ *
+ * @param unit The unit that wants to be loaded into the transport
+ * @param transport The transporter that will load the given unit
*/
void load(Unit unit, Unit transport);

/**
- * Supplies units around the apc
+ * Supplies units around the given unit
*
- * @param apc The unit that performs the supply action
+ * @param unit The unit that performs the supply action
* @return The amount of units that have been supplied
*/
- int supply(Unit apc);
+ int supply(Unit unit);

/**
* The unit joins with target. Only the target unit remains on the map.
* If joining causes the target to go over the max HP of a unit then
* translate that excess hp to money and add it to the budget of the
unit owner.
+ *
+ * @param unit The unit that wants to join with the target
+ * @param target The unit that remains in the map after the join
*/
void join(Unit unit, Unit target);

/**
* The attacking unit attacks the city
*
+ * @param attacker The unit that wants to attack the city
+ * @param city The city that will be attacked
* @return the damage percentage done to the city
*/
int attack(Unit attacker, City city);

/**
- * The attacking unit attacks the defending unit
+ * The attacking unit attacks the defending unit.
+ * The defender can perform a counter attack.
*
+ * @param attacker The unit that wants to attack the defender
+ * @param defender The unit that will receive the damage
* @return the damage percentage done to the defender
*/
int attack(Unit attacker, Unit defender);
@@ -81,12 +96,19 @@
/**
* Transform the terrain on the location to the transformToTerrain
terrain.
* Transforming a terrain costs 1 construction point.
+ * @param unit The unit that wants to transform a terrain
+ * @param location The location of the terrain that will be transformed
+ * @param transformToTerrain The new terrain that will replace the
previous terrain
*/
void transformTerrain(Unit unit, Location location, Terrain
transformToTerrain);

/**
* The unit fires a flare revealing flareRange tiles around the
flareCenter.
* The primary weapon ammo is decreased by 1.
+ *
+ * @param unit The unit that wants to fire a flare
+ * @param flareCenter The centre location, where numOfTilesToReveal will
be revealed
+ * @param numOfTilesToReveal The radius of the spiral around the centre
location.
*/
void flare(Unit unit, Location flareCenter, int numOfTilesToReveal);

@@ -108,27 +130,34 @@

/**
* Make the unit IDLE, meaning that it can no longer be controlled.
+ *
+ * @param unit The unit that needs to be IDLE
*/
void makeUnitWait(Unit unit);

/**
* Create a unit and load it into the producer unit.
- * The unit cost is subtracted from the player.
+ * Creating a unit costs the player the price of the produced unit.
*
- * @param producer The unit that wants to create a new unit.
+ * @param producer The producing unit to add the new unit to
* @param unitToProduce The name of the unit to produce.
*/
void produceUnit(Unit producer, String unitToProduce);

/**
* The player creates a new unit. The unit is placed on the location in
the map.
- * Note that the location can be a Tile or a unit!
* Creating a unit costs the player the price of the unit.
+ *
+ * @param unit The unit to be build and placed in the map on the given
location
+ * @param location The location where the unit will be placed on
+ * @param player The player that will control this new unit
*/
void buildUnit(Unit unit, Location location, Player player);

/**
- * Load a CO into a Unit
+ * Load the CO into the given unit
+ *
+ * @param unit The unit that wants to have the co on board
*/
void loadCO(Unit unit);

=======================================
--- /trunk/v2/src/com/customwars/client/model/fight/Attacker.java Sun May
23 08:27:07 2010
+++ /trunk/v2/src/com/customwars/client/model/fight/Attacker.java Tue Oct
16 10:46:08 2012
@@ -29,4 +29,6 @@
* @return A list of locations where this unit can move in
*/
List<Location> getMoveZone();
+
+ boolean isInTransport();
}
=======================================
--- /trunk/v2/src/com/customwars/client/model/gameobject/Unit.java Tue Sep
4 12:18:45 2012
+++ /trunk/v2/src/com/customwars/client/model/gameobject/Unit.java Tue Oct
16 10:46:08 2012
@@ -21,7 +21,12 @@

/**
* Is a Mover meaning it has the ability to be put on and removed from a
Location.
- * Is a Location, meaning it can transport Locatables
+ * It can transport other units:
+ * If a unit is within a transport the location will be null. The
transport field will
+ * be set to the transporting unit. A unit in a transport can be idle or
active:
+ * Active means that the unit can be loaded and dropped in the same turn.
+ * Idle means that the unit needs to wait 1 turn in the transport.
+ * <p/>
* It has a Facing direction, this is one of the compass Direction(N,S,E,W)
* Units have 2 weapons(both are optional): 1 Primary and 1 Secondary
* <p/>
@@ -31,7 +36,7 @@
* hp=95, maxhp=100 getHP() = 10 -5% damage
* hp=90, maxhp=100 getHP() = 9
*/
-public class Unit extends GameObject implements Mover, Location,
TurnHandler, Attacker, Defender {
+public class Unit extends GameObject implements Mover, TurnHandler,
Attacker, Defender {
public static final Direction DEFAULT_ORIENTATION = Direction.EAST;
private static final int LOW_AMMO_PERCENTAGE = 50;
private static final int LOW_SUPPLIES = 50;
@@ -44,7 +49,7 @@
private int experience; // Each time a unit wins a fight his
Experience rises(starting from 0)
private UnitState unitState; // Current Unitstate (submerged, capturing
a city,...)
private Player owner; // Player owning this unit
- private Location location; // Current Location
+ private Location location; // Current Location, null if in a transport
private boolean coOnBoard; // Is a CO loaded within this unit
private List<Location> moveZone; // A zone where this unit can move in
private List<Location> attZone; // A zone where this unit can attack in
@@ -52,7 +57,8 @@
private boolean hidden; // In fog of war, is this unit hidden
within enemy los

private Weapon primaryWeapon, secondaryWeapon;
- private List<Locatable> transport; // Locatables that are within this
Transport
+ private Unit transport; // The unit that is transporting
this unit
+ private List<Unit> unitsInTransport;// Units that are within this
transport
private MoveStrategy moveStrategy; // A MoveStrategy instance for this
unit

public Unit(UnitStats unitStats) {
@@ -64,7 +70,7 @@

void init() {
stats.init();
- transport = new LinkedList<Locatable>();
+ unitsInTransport = new LinkedList<Unit>();
if (stats.canDive) dive();
unitState = UnitState.IDLE;
moveStrategy = stats.moveStrategy.newInstance(this);
@@ -92,14 +98,13 @@
coOnBoard = otherUnit.coOnBoard;
owner = otherUnit.owner;
moveStrategy = otherUnit.stats.moveStrategy.newInstance(this);
- copyUnitsInTheTransport(otherUnit.transport);
+ copyUnitsInTheTransport(otherUnit.unitsInTransport);
}

- private void copyUnitsInTheTransport(List<Locatable> unitsInTransport) {
- this.transport = new LinkedList<Locatable>();
- for (Locatable locatable : unitsInTransport) {
- Unit unit = (Unit) locatable;
- this.transport.add(new Unit(unit));
+ private void copyUnitsInTheTransport(List<Unit> unitsInTransport) {
+ this.unitsInTransport = new LinkedList<Unit>();
+ for (Unit unit : unitsInTransport) {
+ this.unitsInTransport.add(new Unit(unit));
}
}

@@ -134,8 +139,14 @@
clearTransport();
owner.removeUnit(this);
owner = null;
- location.remove(this);
- location = null;
+
+ if (isInTransport()) {
+ transport.remove(this);
+ transport = null;
+ } else {
+ location.remove(this);
+ location = null;
+ }

if (fireEvent) {
setState(GameObjectState.DESTROYED);
@@ -146,13 +157,9 @@
* Destroy each unit in the transport
*/
private void clearTransport() {
- while (!transport.isEmpty()) {
- Locatable locatable = transport.get(transport.size() - 1);
-
- if (locatable instanceof Unit) {
- Unit unit = (Unit) locatable;
- unit.destroy(false);
- }
+ while (hasUnitsInTransport()) {
+ Unit unit = unitsInTransport.get(unitsInTransport.size() - 1);
+ unit.destroy(false);
}
}

@@ -166,9 +173,8 @@
* then supply and heal the units in the transport.
*/
private void supplyUnitsInTransport() {
- if (stats.canTransport() && !transport.isEmpty()) {
- for (Locatable locatable : transport) {
- Unit unit = (Unit) locatable;
+ if (stats.canTransport()) {
+ for (Unit unit : unitsInTransport) {
if (stats.canSupplyUnitInTransport(unit)) {
supplyInTransport(unit);
unit.heal(stats.healRate);
@@ -182,8 +188,7 @@
* so they can be dropped/launched in this turn.
*/
private void activateUnitsInTransport() {
- for (Locatable locatable : transport) {
- Unit unitInTransport = (Unit) locatable;
+ for (Unit unitInTransport : unitsInTransport) {
unitInTransport.setActive(true);
}
}
@@ -301,56 +306,41 @@
// Actions :: Transport
//
---------------------------------------------------------------------------

- public int getCol() {
- return location.getCol();
- }
-
- public int getRow() {
- return location.getRow();
- }
-
- public String getLocationString() {
- return location.getLocationString();
- }
-
- public Locatable getLastLocatable() {
- return getLocatable(transport.size() - 1);
+ public Unit getLastUnitInTransport() {
+ return getUnitInTransport(unitsInTransport.size() - 1);
}

- public Locatable getLocatable(int index) {
- return !transport.isEmpty() ? transport.get(index) : null;
+ public Unit getUnitInTransport(int index) {
+ return !unitsInTransport.isEmpty() ? unitsInTransport.get(index) :
null;
}

- public boolean remove(Locatable locatable) {
- if (!contains(locatable) || locatable == null) {
+ public boolean remove(Unit unit) {
+ if (!contains(unit) || unit == null) {
return false;
}

- locatable.setLocation(null); // Keep locatable and tile in sync
- transport.remove(locatable);
- firePropertyChange("transport", locatable, null);
+ unit.setLocation(null);
+ unitsInTransport.remove(unit);
+ unit.transport = null;
+ firePropertyChange("transport", unit, null);
return true;
}

- public boolean contains(Locatable locatable) {
- return transport.contains(locatable);
+ public boolean contains(Unit unit) {
+ return unitsInTransport.contains(unit);
}

- public void add(Locatable locatable) {
- if (canAdd(locatable)) {
- transport.add(locatable);
- locatable.setLocation(this); // Keep locatable and tile in sync
+ public void add(Unit unit) {
+ if (canAdd(unit)) {
+ unitsInTransport.add(unit);
+ unit.setLocation(null);
+ unit.transport = this;
}
- firePropertyChange("transport", null, locatable);
+ firePropertyChange("transport", null, unit);
}

- public boolean canAdd(Locatable locatable) {
- if (locatable instanceof Unit) {
- Unit unit = (Unit) locatable;
- return canTransport(unit.stats.name);
- } else {
- return false;
- }
+ public boolean canAdd(Unit unit) {
+ return canTransport(unit.stats.name);
}

public boolean canTransport(String unitName) {
@@ -358,29 +348,44 @@
}

public boolean isTransportFull() {
- return transport.size() >= stats.getMaxTransportCount();
+ return unitsInTransport.size() >= stats.getMaxTransportCount();
+ }
+
+ public boolean isInTransport() {
+ return transport != null;
}

/**
* Check if this transport is transporting at least 1 active unit of the
given armybranch.
*/
public boolean isTransportingUnitType(ArmyBranch armyBranch) {
- if (transport.isEmpty()) return false;
-
- for (Locatable locatable : transport) {
- Unit unitInTransport = (Unit) locatable;
+ for (Unit unitInTransport : unitsInTransport) {
if (unitInTransport.getArmyBranch() == armyBranch &&
unitInTransport.isActive()) {
return true;
}
}
return false;
}
+
+ /**
+ * @return true if there is at least 1 unit in this transport.
+ */
+ public boolean hasUnitsInTransport() {
+ return !unitsInTransport.isEmpty();
+ }

/**
* @return Amount of units in the transport
*/
- public int getLocatableCount() {
- return transport.size();
+ public int getUnitsInTransportCount() {
+ return unitsInTransport.size();
+ }
+
+ /**
+ * @return all the units in this transport, to be used in a for each
loop.
+ */
+ public Iterable<Unit> getUnitsInTransport() {
+ return unitsInTransport;
}

/**
@@ -390,14 +395,7 @@
* @return the index or -1 when the unit is not in this transport
*/
public int indexOf(Unit unit) {
- for (int i = 0; i < transport.size(); i++) {
- Unit unitInTransport = (Unit) getLocatable(i);
-
- if (unitInTransport == unit) {
- return i;
- }
- }
- return -1;
+ return unitsInTransport.indexOf(unit);
}

//
----------------------------------------------------------------------------
@@ -537,7 +535,7 @@
//
---------------------------------------------------------------------------

public void deCreaseConstructionMaterials() {
- setConstructionMaterials(constructionMaterials-1);
+ setConstructionMaterials(constructionMaterials - 1);
}

private void setConstructionMaterials(int newVal) {
@@ -647,7 +645,7 @@
}

public void setMoveStrategy(MoveStrategy moveStrategy) {
- MoveStrategy oldVal = moveStrategy;
+ MoveStrategy oldVal = this.moveStrategy;
this.moveStrategy = moveStrategy;
firePropertyChange("moveStrategy", oldVal, moveStrategy);
}
@@ -870,8 +868,16 @@
// Getters :: Moving
//
---------------------------------------------------------------------------

+ public String getLocationString() {
+ return getLocation().getLocationString();
+ }
+
public Location getLocation() {
- return location;
+ if (transport != null) {
+ return transport.getLocation();
+ } else {
+ return location;
+ }
}

public int getMovementType() {
@@ -972,10 +978,6 @@
return owner.getCO().captureRateHook(getHp());
}
}
-
- public boolean isInTransport() {
- return location instanceof Unit;
- }

/**
* @return Can this unit fire only on adjacent enemies
@@ -1050,7 +1052,15 @@
}

private String getLocationText() {
- return location == null ? "Not located" : location.getLocationString();
+ if(location == null) {
+ if(transport == null) {
+ return "Not located";
+ } else {
+ return "in transport";
+ }
+ } else {
+ return location.getLocationString();
+ }
}

private String getOwnerText() {
@@ -1059,6 +1069,9 @@

private String getStatsText() {
return String.format("hp=%s/%s supplies=%s/%s exp=%s/%s
transport=%s/%s",
- hp, stats.getMaxHp(), supplies, stats.getMaxSupplies(), experience,
stats.getMaxExperience(), transport.size(), stats.getMaxTransportCount());
+ hp, stats.getMaxHp(),
+ supplies, stats.getMaxSupplies(),
+ experience, stats.getMaxExperience(),
+ unitsInTransport.size(), stats.getMaxTransportCount());
}
}
=======================================
--- /trunk/v2/src/com/customwars/client/model/map/Map.java Fri Mar 25
08:50:39 2011
+++ /trunk/v2/src/com/customwars/client/model/map/Map.java Tue Oct 16
10:46:08 2012
@@ -43,8 +43,6 @@
* Players in a map are called 'map players'
* name and funds unknown they are used to link units and cities to a
player.
* They hold the units, cities, hq location, the default color and the
player ID.
- *
- * @author stefan
*/
public class Map extends TileMap<Tile> implements TurnHandler {
private static final Logger logger = Logger.getLogger(Map.class);
@@ -364,13 +362,15 @@

/**
* Build a zone in which the mover can make a move and set it to the
mover
- * If the mover is within a transport then the movezone is null
+ * If the mover is within a transport then the move zone is null
* If the mover cannot move then the current mover location is set as
the moveZone
+ *
+ * @param mover The mover to build a move zone for
*/
public void buildMovementZone(Mover mover) {
List<Location> moveZone;

- if (mover.getLocation() instanceof Unit) {
+ if (mover.isInTransport()) {
moveZone = null;
} else {
if (mover.canMove()) {
@@ -384,7 +384,7 @@

/**
* Build a zone in which the Attacker can attack and set it to the
attacker
- * If the attacker is within a transport then the attackzone is null
+ * If the attacker is within a transport then the attack zone is null
*
* @param attacker The attacker to build the attack zone for
*/
@@ -392,7 +392,7 @@
List<Location> attackZone = new ArrayList<Location>(30);
Range attackRange = attacker.getAttackRange();

- if (attacker.getLocation() instanceof Unit) {
+ if (attacker.isInTransport()) {
attackZone = null;
} else {
for (Tile t : getAllTiles()) {
@@ -623,8 +623,7 @@
private void replaceUnitOwner(Player newPlayer, Unit unit) {
newPlayer.addUnit(unit);

- for (int i = 0; i < unit.getLocatableCount(); i++) {
- Unit unitInTransport = (Unit) unit.getLocatable(i);
+ for (Unit unitInTransport : unit.getUnitsInTransport()) {
newPlayer.addUnit(unitInTransport);
}
}
@@ -734,8 +733,11 @@

/**
* Retrieve a list of drop locations where all units can be dropped on.
+ * Using the current location of the transport.
*
+ * @param transport The transport that wants to start dropping units
* @see #getFreeDropLocations(Unit, Location)
+ * @return If all units in the transport can be dropped
*/
public List<Location> getFreeDropLocations(Unit transport) {
return getFreeDropLocations(transport, transport.getLocation());
@@ -754,7 +756,7 @@
* @see #canDropAtLeast1Unit(Unit, Tile)
*/
public List<Location> getFreeDropLocations(Unit transport, Location
center) {
- if (transport.getLocatableCount() == 0) return Collections.emptyList();
+ if (!transport.hasUnitsInTransport()) return Collections.emptyList();

List<Location> freeDropLocations = new ArrayList<Location>(4);
for (Tile tile : getSurroundingTiles(center, 1, 1)) {
@@ -772,13 +774,14 @@
* The center parameter allows a transport to find out if any units can
be dropped
* around a given center without actually moving to that location.
*
- * @param transport The transport where we want to find the free drop
locations for.
- * @param center The center where the drop locations are located
around.
- * @return a list of adjacent locations where units inside the transport
can be dropped on.
+ * @param transport The transport that wants to drop the given unit
+ * @param unitToBeDropped The unit that wants to be dropped out of the
transport into the map
+ * @param center The location to search for free adjacent drop
locations
+ * @return a list of adjacent locations where units inside the transport
can be dropped on
* @see #isFreeDropLocation(Unit, Unit, Tile)
*/
public List<Location> getFreeDropLocations(Unit transport, Unit
unitToBeDropped, Location center) {
- if (transport.getLocatableCount() == 0) return Collections.emptyList();
+ if (!transport.hasUnitsInTransport()) return Collections.emptyList();

List<Location> freeDropLocations = new ArrayList<Location>(4);
for (Tile tile : getSurroundingTiles(center, 1, 1)) {
@@ -799,10 +802,9 @@
* @see #isFreeDropLocation(Unit, Unit, Tile)
*/
boolean canDropAtLeast1Unit(Unit transporter, Tile dropLocation) {
- if (transporter.getLocatableCount() == 0) return false;
+ if (!transporter.hasUnitsInTransport()) return false;

- for (int i = 0; i < transporter.getLocatableCount(); i++) {
- Unit unit = (Unit) transporter.getLocatable(i);
+ for (Unit unit : transporter.getUnitsInTransport()) {
if (!isFreeDropLocation(transporter, unit, dropLocation)) {
return false;
}
@@ -821,6 +823,7 @@
* <li>At least 1 of the units in the transport can move over the drop
location</li>
* </ul>
*
+ * @param unit The unit to be dropped
* @param dropLocation The tile that a unit wants to be dropped on
* @param transporter The transport unit that attempts to drop a unit
to the dropLocation
* @return Can a unit be dropped to the given drop location
@@ -842,6 +845,10 @@
/**
* Create a collection of tiles that covers the co zone area around the
given unit.
* When the zone range is 0 an empty collection is returned.
+ *
+ * @param unit The unit to retrieve the co zone from
+ * @param zoneRange the radius of the spiral that makes the co zone
+ * @return The locations that cover the co zone
*/
public Collection<Location> buildCOZone(Unit unit, int zoneRange) {
Collection<Location> coZone = new ArrayList<Location>();
@@ -976,11 +983,14 @@
/**
* Check if each adjacent tile around the unit is occupied by an enemy
unit.
* Fog and hidden units have no influence since adjacent tiles are
always visible.
+ *
+ * @param unit the unit to check for enemies on each adjacent tile
+ * @return If the unit is surrounded by enemies
*/
public boolean isSurroundedByEnemyUnits(Unit unit) {
int enemies = 0;
for (Tile t : getSurroundingTiles(unit.getLocation(), 1, 1)) {
- Unit adjacentUnit = (Unit) t.getLastLocatable();
+ Unit adjacentUnit = getUnitOn(t);
if (adjacentUnit != null
&& !adjacentUnit.isAlliedWith(unit.getOwner())) {
enemies++;
}
=======================================
--- /trunk/v2/src/com/customwars/client/model/map/path/Mover.java Sun Apr
19 11:05:38 2009
+++ /trunk/v2/src/com/customwars/client/model/map/path/Mover.java Tue Oct
16 10:46:08 2012
@@ -8,8 +8,6 @@

/**
* An Objects that can move between tiles
- *
- * @author stefan
*/
public interface Mover extends Locatable {
/**
@@ -52,4 +50,6 @@
* @return if the location contains a trapper
*/
boolean hasTrapperOn(Location location);
+
+ boolean isInTransport();
}
=======================================
---
/trunk/v2/src/com/customwars/client/ui/hud/panel/HorizontalTransportInfoPanel.java
Mon Feb 21 13:36:48 2011
+++
/trunk/v2/src/com/customwars/client/ui/hud/panel/HorizontalTransportInfoPanel.java
Tue Oct 16 10:46:08 2012
@@ -54,7 +54,7 @@
}

public void setUnit(Unit unit) {
- if (unit != null && !unit.isDestroyed() && unit.getLocatableCount() >
0) {
+ if (unit != null && !unit.isDestroyed() && unit.hasUnitsInTransport())
{
this.unit = unit;
initBoxes();
} else {
@@ -65,8 +65,7 @@

private void initBoxes() {
transportBoxes.clear();
- for (int i = 0; i < unit.getLocatableCount(); i++) {
- Unit unitInTransport = (Unit) unit.getLocatable(i);
+ for (Unit unitInTransport : unit.getUnitsInTransport()) {
Image unitImg = getEastFacingUnitImg(unitInTransport);
transportBoxes.add(new ImageBox(unitImg));
}
=======================================
---
/trunk/v2/src/com/customwars/client/ui/hud/panel/VerticalTransportInfoPanel.java
Mon Feb 21 13:36:48 2011
+++
/trunk/v2/src/com/customwars/client/ui/hud/panel/VerticalTransportInfoPanel.java
Tue Oct 16 10:46:08 2012
@@ -46,7 +46,7 @@
}

public void setUnit(Unit unit) {
- if (unit != null && !unit.isDestroyed() && unit.getLocatableCount() >
0) {
+ if (unit != null && !unit.isDestroyed() && unit.hasUnitsInTransport())
{
this.unit = unit;
initBoxes();
} else {
@@ -57,8 +57,8 @@

private void initBoxes() {
transportBoxes.clear();
- for (int i = 0; i < unit.getLocatableCount(); i++) {
- Unit unitInTransport = (Unit) unit.getLocatable(i);
+ for (int i = 0; i < unit.getUnitsInTransportCount(); i++) {
+ Unit unitInTransport = unit.getUnitInTransport(i);
Image unitImg = getEastFacingUnitImg(unitInTransport);
transportBoxes.add(i, new ImageBox(unitImg));
}
=======================================
--- /trunk/v2/src/com/customwars/client/ui/sprite/UnitSprite.java Tue Sep
4 12:18:45 2012
+++ /trunk/v2/src/com/customwars/client/ui/sprite/UnitSprite.java Tue Oct
16 10:46:08 2012
@@ -18,8 +18,6 @@

/**
* A 2D unit
- *
- * @author stefan
*/
public class UnitSprite extends TileSprite implements
PropertyChangeListener {
private static final int UNIT_STATUS_FRAME_DURATION = 2000;
@@ -59,7 +57,7 @@
lowHp = unit.hasLowHp();
statusRotator.setShowFrame(LOW_AMMO, unit.hasLowAmmo());
statusRotator.setShowFrame(LOW_SUPPLIES, unit.hasLowSupplies());
- statusRotator.setShowFrame(LOAD, unit.getLocatableCount() > 0);
+ statusRotator.setShowFrame(LOAD, unit.hasUnitsInTransport());
constructionMaterialsChanged(unit.getCurrentConstructionMaterials());
experienceChange(unit.getExperience());

@@ -225,7 +223,7 @@
} else if (propertyName.equals("location")) {
setLocation((Location) evt.getNewValue());
} else if (propertyName.equals("transport")) {
- statusRotator.setShowFrame(LOAD, unit.getLocatableCount() > 0);
+ statusRotator.setShowFrame(LOAD, unit.hasUnitsInTransport());
} else if (propertyName.equals("unitState")) {
unitStateChange((UnitState) evt.getNewValue());
} else if (propertyName.equals("experience")) {
=======================================
--- /trunk/v2/test/com/customwars/client/action/TestActionCommand.java Sun
Mar 20 06:49:26 2011
+++ /trunk/v2/test/com/customwars/client/action/TestActionCommand.java Tue
Oct 16 10:46:08 2012
@@ -57,7 +57,7 @@
Unit apc = game.getMap().getUnitOn(6, 2);
Location apcLocation = apc.getLocation();
DropLocationsQueue dropQueue = new DropLocationsQueue();
- dropQueue.addDropLocation(new Location2D(6, 1), (Unit)
apc.getLastLocatable());
+ dropQueue.addDropLocation(new Location2D(6, 1),
apc.getLastUnitInTransport());
CWAction loadAction = ActionFactory.buildDropAction(apc, apcLocation,
apcLocation, dropQueue);

// From - To - unit in transport index - drop location
=======================================
---
/trunk/v2/test/com/customwars/client/io/loading/BinCW2MapParserTest.java
Fri Feb 18 15:15:52 2011
+++
/trunk/v2/test/com/customwars/client/io/loading/BinCW2MapParserTest.java
Tue Oct 16 10:46:08 2012
@@ -115,9 +115,9 @@
}

private void validateUnits(Unit hardCodedUnit, Unit loadedUnit) {
- if (hardCodedUnit.getStats().canTransport() &&
hardCodedUnit.getLocatableCount() > 0) {
- int unitsInTransport = hardCodedUnit.getLocatableCount();
- int loadedUnitsInTransport = loadedUnit.getLocatableCount();
+ if (hardCodedUnit.getStats().canTransport() &&
hardCodedUnit.hasUnitsInTransport()) {
+ int unitsInTransport = hardCodedUnit.getUnitsInTransportCount();
+ int loadedUnitsInTransport = loadedUnit.getUnitsInTransportCount();
Assert.assertEquals(unitsInTransport, loadedUnitsInTransport);
}

Reply all
Reply to author
Forward
0 new messages