[customwars] r748 committed - launch planes from a carrier.

1 view
Skip to first unread message

codesite...@google.com

unread,
Mar 20, 2011, 4:52:05 PM3/20/11
to customwar...@googlegroups.com
Revision: 748
Author: ace.stef
Date: Sun Mar 20 13:51:30 2011
Log: launch planes from a carrier.
http://code.google.com/p/customwars/source/detail?r=748

Added:
/trunk/v2/src/com/customwars/client/action/StartUnitLaunchAction.java
Modified:
/trunk/v2/src/com/customwars/client/action/ClearInGameStateAction.java
/trunk/v2/src/com/customwars/client/action/unit/LoadAction.java
/trunk/v2/src/com/customwars/client/action/unit/MoveAnimatedAction.java
/trunk/v2/src/com/customwars/client/action/unit/StartDropAction.java
/trunk/v2/src/com/customwars/client/controller/HumanUnitController.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/model/CWGameController.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/ui/state/InGameContext.java
/trunk/v2/test/com/customwars/client/model/map/MapTest.java

=======================================
--- /dev/null
+++ /trunk/v2/src/com/customwars/client/action/StartUnitLaunchAction.java
Sun Mar 20 13:51:30 2011
@@ -0,0 +1,68 @@
+package com.customwars.client.action;
+
+import com.customwars.client.controller.CursorController;
+import com.customwars.client.model.game.Game;
+import com.customwars.client.model.gameobject.Unit;
+import com.customwars.client.model.map.Location;
+import com.customwars.client.ui.renderer.MapRenderer;
+import com.customwars.client.ui.state.InGameContext;
+import org.apache.log4j.Logger;
+
+/**
+ * Allows a plane on a carrier to be launched.
+ * This action does not actually perform the launch but instead allows the
user to choose
+ * a launch destination tile within the move zone. The plane is put on to
the carrier location.
+ * <p/>
+ * There are 2 locations mentioned:
+ * transport.getLocation() -> The location of the transport before it
moved.
+ * move destination -> The destination of the move, where the plane is
launched from.
+ */
+public class StartUnitLaunchAction extends DirectAction {
+ private static final Logger logger =
Logger.getLogger(StartUnitLaunchAction.class);
+ private InGameContext inGameContext;
+ private CursorController cursorControl;
+ private MapRenderer mapRenderer;
+ private final Unit carrier;
+ private final Unit unitToBeLaunched;
+ private Game game;
+
+ public StartUnitLaunchAction(Unit carrier, Unit unitToBeLaunched) {
+ super("Start Launch");
+ this.carrier = carrier;
+ this.unitToBeLaunched = unitToBeLaunched;
+ }
+
+ protected void init(InGameContext inGameContext) {
+ this.inGameContext = inGameContext;
+ this.game = inGameContext.getObj(Game.class);
+ this.cursorControl = inGameContext.getObj(CursorController.class);
+ this.mapRenderer = inGameContext.getObj(MapRenderer.class);
+ }
+
+ protected void invokeAction() {
+ logger.debug("Preparing to launch from: " +
carrier.getLocationString());
+ mapRenderer.removeZones();
+
+ // Put the plane on the carrier location in the map.
+ Location carrierLocation = carrier.getLocation();
+ carrier.remove(unitToBeLaunched);
+ carrierLocation.add(unitToBeLaunched);
+
+ unitToBeLaunched.setActive(true);
+ game.setActiveUnit(unitToBeLaunched);
+ game.getMap().buildMovementZone(unitToBeLaunched);
+ mapRenderer.showMoveZone();
+
+ inGameContext.setLaunchingUnit(true);
+ }
+
+ public void undo() {
+ Location carrierLocation = carrier.getLocation();
+ carrierLocation.remove(unitToBeLaunched);
+ carrier.add(unitToBeLaunched);
+ carrierLocation.add(carrier);
+ game.setActiveUnit(carrier);
+ cursorControl.moveCursor(carrier.getLocation());
+ new ClearInGameStateAction().invoke(inGameContext);
+ }
+}
=======================================
--- /trunk/v2/src/com/customwars/client/action/ClearInGameStateAction.java
Fri Feb 18 15:53:39 2011
+++ /trunk/v2/src/com/customwars/client/action/ClearInGameStateAction.java
Sun Mar 20 13:51:30 2011
@@ -36,6 +36,7 @@
inGameContext.clearUndoHistory();
inGameContext.clearClickHistory();
inGameContext.setTrapped(false);
+ inGameContext.setLaunchingUnit(false);
inGameContext.clearDropHistory();
inGameContext.getObj(GUIContext.class).getInput().resume();

=======================================
--- /trunk/v2/src/com/customwars/client/action/unit/LoadAction.java Thu
Apr 1 07:20:09 2010
+++ /trunk/v2/src/com/customwars/client/action/unit/LoadAction.java Sun Mar
20 13:51:30 2011
@@ -14,12 +14,9 @@

/**
* Load the unit in the transport
- * and remove the unit sprite
- *
- * @author stefan
*/
public class LoadAction extends DirectAction {
- private Logger logger = Logger.getLogger(LoadAction.class);
+ private static final Logger logger = Logger.getLogger(LoadAction.class);
private InGameContext inGameContext;
private MapRenderer mapRenderer;
private GameController gameController;
@@ -49,6 +46,15 @@
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
+ unit.setActive(false);
+ } else {
+ // Other units can be dropped in the same turn
+ unit.setActive(true);
+ }
+
SFX.playSound("load");
if (App.isMultiplayer()) sendLoad();
}
=======================================
--- /trunk/v2/src/com/customwars/client/action/unit/MoveAnimatedAction.java
Sun Mar 20 06:49:26 2011
+++ /trunk/v2/src/com/customwars/client/action/unit/MoveAnimatedAction.java
Sun Mar 20 13:51:30 2011
@@ -7,6 +7,7 @@
import com.customwars.client.controller.CursorController;
import com.customwars.client.model.GameController;
import com.customwars.client.model.game.Game;
+import com.customwars.client.model.gameobject.Locatable;
import com.customwars.client.model.gameobject.Unit;
import com.customwars.client.model.gameobject.UnitState;
import com.customwars.client.model.map.Location;
@@ -28,6 +29,7 @@
private CursorController cursorControl;
private GameController gameController;
private MessageSender messageSender;
+ private boolean alreadyRevealed;
InGameContext inGameContext;
MoveTraverse moveTraverse;
Game game;
@@ -82,6 +84,10 @@
setActionCompleted(true);
} else {
moveTraverse.update();
+
+ if (inGameContext.isLaunchingUnit()) {
+ revealUnit();
+ }
}
}

@@ -96,6 +102,24 @@
cursorControl.setCursorLocked(false);
if (App.isMultiplayer()) sendMove();
}
+
+ private void revealUnit() {
+ // At first the from tile contains 2 units the plane and the carrier.
+ // Only the plane sprite is visible, if the plane moves then we need
to reveal the carrier.
+ if (!alreadyRevealed && !from.contains(unit)) {
+ revealCarrier(from);
+ }
+ }
+
+ private void revealCarrier(Location carrierLocation) {
+ // Remove and add the carrier to the tile
+ // This triggers a show unit event and will reveal the carrier sprite.
+ Locatable carrier = carrierLocation.getLastLocatable();
+ carrierLocation.remove(carrier);
+ carrierLocation.add(carrier);
+ logger.debug("Revealed " + carrier);
+ alreadyRevealed = true;
+ }

public void undo() {
unit.setDefaultOrientation();
=======================================
--- /trunk/v2/src/com/customwars/client/action/unit/StartDropAction.java
Tue Feb 22 12:32:43 2011
+++ /trunk/v2/src/com/customwars/client/action/unit/StartDropAction.java
Sun Mar 20 13:51:30 2011
@@ -16,7 +16,7 @@
import java.util.List;

/**
- * Allow a unit in a transport to be dropped on a free drop tile around
the center location.
+ * Allow the given unit to be dropped on a free drop tile around the
center location.
* This action does not actually performs the drop but instead allows the
user to choose from
* all available drop locations indicated by a move zone.
* <p/>
@@ -72,14 +72,19 @@
* Excluding drop tiles that are invalid or already taken by
another unit.
*/
private List<Location> getEmptyDropTiles(Location transportLocation) {
- List<Location> availableDropLocations = new ArrayList<Location>();
- List<Location> freeDropLocations = map.getFreeDropLocations(transport,
transportLocation);
+ List<Location> freeDropLocations = map.getFreeDropLocations(transport,
unitToBeDropped, transportLocation);
+ List<Location> availableDropLocations =
removeAlreadyUsedTiles(freeDropLocations);
+
+ return availableDropLocations;
+ }
+
+ private List<Location> removeAlreadyUsedTiles(List<Location>
freeDropLocations) {
+ List<Location> availableDropLocations = new
ArrayList<Location>(freeDropLocations.size());
+
for (Location dropLocation : freeDropLocations) {
Tile dropTile = (Tile) dropLocation;
if (!inGameContext.isDropLocationTaken(dropTile)) {
- if (map.isFreeDropLocation(transport, unitToBeDropped, dropTile)) {
- availableDropLocations.add(dropLocation);
- }
+ availableDropLocations.add(dropLocation);
}
}
return availableDropLocations;
=======================================
--- /trunk/v2/src/com/customwars/client/controller/HumanUnitController.java
Wed Feb 23 09:46:07 2011
+++ /trunk/v2/src/com/customwars/client/controller/HumanUnitController.java
Sun Mar 20 13:51:30 2011
@@ -58,15 +58,10 @@
private void dropUnit(Tile selected) {
// The menu option clicked on is the index of the unit in the
transport List
int unitInTransportIndex = menu.getCurrentItem();
-
- if (canDrop(selected, unitInTransportIndex)) {
- Unit unitInTransport =
inGameContext.getUnitInTransport(unitInTransportIndex);
- inGameContext.addDropLocation(selected, unitInTransport);
- this.menu = new UnitMenuBuilder(this, unit, inGameContext,
selected).getMenu();
- showMenu(selected);
- } else {
- logger.warn("Trying to drop unit on " + selected + " failed");
- }
+ Unit unitInTransport =
inGameContext.getUnitInTransport(unitInTransportIndex);
+ inGameContext.addDropLocation(selected, unitInTransport);
+ this.menu = new UnitMenuBuilder(this, unit, inGameContext,
selected).getMenu();
+ showMenu(selected);
}

private void attack(Tile selected, Location to) {
=======================================
--- /trunk/v2/src/com/customwars/client/controller/UnitController.java Sun
Mar 20 06:49:26 2011
+++ /trunk/v2/src/com/customwars/client/controller/UnitController.java Sun
Mar 20 13:51:30 2011
@@ -1,5 +1,6 @@
package com.customwars.client.controller;

+import com.customwars.client.model.ArmyBranch;
import com.customwars.client.model.fight.Defender;
import com.customwars.client.model.game.Game;
import com.customwars.client.model.game.Player;
@@ -101,7 +102,7 @@
* Can the transport drop any units
* #1 This controller controls a unit with transport abilities
* #2 There is at least 1 free tile within the drop range
- * #3 There is at least 1 unit in the transport
+ * #3 There is at least 1 ground unit in the transport
* #4 At least 1 unit in the transport can be unloaded on an adjacent
tile.
*
* @return true if the transport can drop at least 1 unit
@@ -111,26 +112,40 @@

boolean isTransportingUnit = unit.getStats().canTransport();
boolean atleast1FreeDropTile = !freeDropLocations.isEmpty();
- boolean atLeast1UnitToDrop = unit.getLocatableCount() > 0;
-
- return isTransportingUnit && atleast1FreeDropTile &&
atLeast1UnitToDrop;
+ boolean atLeast1GroundUnitToDrop =
unit.isTransportingUnitType(ArmyBranch.LAND);
+
+ return isTransportingUnit && atleast1FreeDropTile &&
atLeast1GroundUnitToDrop;
}

/**
- * Can this transporting unit drop a unit at dropIndex on the given tile
+ * Can this unit launch planes:
+ * #1 This controller controls a unit with transport abilities
+ * #2 There is at least 1 unit in the transport
+ * #3 The plane can fly at least 1 tile away.
+ * #4 The plane did not land on the carrier in this turn.
+ * Note that carrier is just an example any unit can have launch
abilities.
+ * Only planes can be launched all other units are dropped.
+ */
+ boolean canStartLaunch(Tile from, Tile to) {
+ boolean isCarrier = unit.getStats().canTransport();
+ boolean surroundedByEnemyUnits = map.isSurroundedByEnemyUnits(unit);
+ boolean atLeast1ActiveAirUnitLoaded =
unit.isTransportingUnitType(ArmyBranch.AIR);
+ boolean moved = !from.equals(to);
+
+ return isCarrier && !surroundedByEnemyUnits &&
atLeast1ActiveAirUnitLoaded &&
+ !moved && unit.isActive();
+ }
+
+ /**
+ * Can the unit be launched from the carrier:
+ * #1 The unit is an air unit.
*
- * @param tile The tile the unit is going to be dropped to
- * @param dropIndex The index of the unit in the transport(0 based)
- * @return true If this transport can drop the unit in the transport at
the given index
- * on the given tile
+ * @see #canStartLaunch(Tile, Tile)
*/
- boolean canDrop(Tile tile, int dropIndex) {
- if (unit.getStats().canTransport() && unit.getLocatableCount() >
dropIndex) {
- Unit unitToDrop = (Unit) unit.getLocatable(dropIndex);
- return map.isFreeDropLocation(unit, unitToDrop, tile);
- } else {
- return false;
- }
+ public boolean canLaunch(Tile from, Tile to, Unit unitToLaunch) {
+ boolean canStartLaunch = canStartLaunch(from, to);
+
+ return canStartLaunch && unitToLaunch.isAir();
}

/**
=======================================
--- /trunk/v2/src/com/customwars/client/controller/UnitMenuBuilder.java Sun
Mar 20 06:49:26 2011
+++ /trunk/v2/src/com/customwars/client/controller/UnitMenuBuilder.java Sun
Mar 20 13:51:30 2011
@@ -3,6 +3,7 @@
import com.customwars.client.App;
import com.customwars.client.action.ActionFactory;
import com.customwars.client.action.CWAction;
+import com.customwars.client.action.StartUnitLaunchAction;
import com.customwars.client.action.city.StartLaunchRocketAction;
import com.customwars.client.action.unit.StartAttackAction;
import com.customwars.client.action.unit.StartDropAction;
@@ -35,7 +36,7 @@
public class UnitMenuBuilder {
private boolean canDropUnit, canCapture, canSupply, canStartAttack,
canWait, canJoin, canLoad;
private boolean canLaunchRocketFromCity, canTransformTerrain;
- private boolean canFireFlare;
+ private boolean canFireFlare, canStartLaunch;
private boolean canBuildCity, canProduceUnit;
private boolean canDive, canSurface;
private final InGameContext inGameContext;
@@ -160,6 +161,7 @@
canLoadCO = controller.canLoadCO(from);
canDoPower = controller.canDoPower();
canDoSuperPower = controller.canDoSuperPower();
+ canStartLaunch = controller.canStartLaunch(from, selected);
} else {
// Actions where the active and selected unit are on the same tile.
canJoin = controller.canJoin(selected);
@@ -181,6 +183,10 @@
buildDropMenu(from, to);
map.teleport(to, from, unit);
}
+
+ if (canStartLaunch) {
+ buildLaunchMenu(from, to);
+ }

if (canProduceUnit) {
for (String unitID : unit.getStats().getUnitsThatCanBeProduced()) {
@@ -282,6 +288,31 @@
String cityID =
unit.getStats().getCityToBuildOnTerrain(tile.getTerrain());
return CityFactory.getCity(cityID);
}
+
+ private PopupMenu buildLaunchMenu(Tile from, Tile to) {
+ menu = new PopupMenu(inGameContext.getObj(GUIContext.class), "Unit
launch menu");
+ inGameContext.clearUnitsInTransport();
+
+ if (controller.canWait(to)) {
+ for (int unitIndex = 0; unitIndex < unit.getLocatableCount();
unitIndex++) {
+ Unit unitInTransport = (Unit) unit.getLocatable(unitIndex);
+
+ // Build start launch actions for each unit in the transport
+ if (controller.canLaunch(from, to, unitInTransport)) {
+ CWAction launchAction = new StartUnitLaunchAction(unit,
unitInTransport);
+ String menuText = getLaunchMenuItemText(unitInTransport);
+ addToMenu(launchAction, menuText);
+ }
+ }
+ }
+ return menu;
+ }
+
+ private String getLaunchMenuItemText(Unit unitInTransport) {
+ String unitName = App.translate(unitInTransport.getStats().getName());
+ String menuText = App.translate("launch") + " - " + unitName;
+ return menuText;
+ }

/**
* Add a menu item to the menu backed by a CWAction
=======================================
--- /trunk/v2/src/com/customwars/client/model/CWGameController.java Sun Mar
20 06:49:26 2011
+++ /trunk/v2/src/com/customwars/client/model/CWGameController.java Sun Mar
20 13:51:30 2011
@@ -248,7 +248,12 @@
public void makeUnitWait(Unit unit) {
if (!unit.isDestroyed()) {
unit.setDefaultOrientation();
- unit.setActive(false);
+
+ if (!unit.isInTransport()) {
+ // Deactivate the unit
+ // so that it cannot be controlled anymore.
+ unit.setActive(false);
+ }

// The unit moved
// Reveal the los and check if we detected any hidden units.
=======================================
--- /trunk/v2/src/com/customwars/client/model/gameobject/Unit.java Sun Mar
20 06:49:26 2011
+++ /trunk/v2/src/com/customwars/client/model/gameobject/Unit.java Sun Mar
20 13:51:30 2011
@@ -30,8 +30,6 @@
* hp=100, maxhp=100 getHP() = 10 -5% damage
* hp=95, maxhp=100 getHP() = 10 -5% damage
* hp=90, maxhp=100 getHP() = 9
- *
- * @author Stefan
*/
public class Unit extends GameObject implements Mover, Location,
TurnHandler, Attacker, Defender {
public static final Direction DEFAULT_ORIENTATION = Direction.EAST;
@@ -160,11 +158,12 @@

public void startTurn(Player currentPlayer) {
supplyUnitsInTransport();
+ activateUnitsInTransport();
}

/**
- * If this unit is transporting units and it can supply the unit in the
transport
- * then supply and heal the units in the transport
+ * If this unit is transporting units and it can supply the units.
+ * then supply and heal the units in the transport.
*/
private void supplyUnitsInTransport() {
if (stats.canTransport() && !transport.isEmpty()) {
@@ -177,12 +176,29 @@
}
}
}
+
+ /**
+ * Make all units in the transport active
+ * so they can be dropped/launched in this turn.
+ */
+ private void activateUnitsInTransport() {
+ for (Locatable locatable : transport) {
+ Unit unitInTransport = (Unit) locatable;
+ unitInTransport.setActive(true);
+ }
+ }

public void endTurn(Player currentPlayer) {
- if (unitState == UnitState.SUBMERGED || hidden) {
+ subtractSupplies();
+ }
+
+ private void subtractSupplies() {
+ if (isSubmerged() || hidden) {
addSupplies(-stats.getSuppliesPerTurnWhenHidden());
} else {
- addSupplies(-stats.suppliesPerTurn);
+ if (!isInTransport()) {
+ addSupplies(-stats.suppliesPerTurn);
+ }
}
}

@@ -344,6 +360,21 @@
public boolean isTransportFull() {
return transport.size() >= stats.getMaxTransportCount();
}
+
+ /**
+ * 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;
+ if (unitInTransport.getArmyBranch() == armyBranch &&
unitInTransport.isActive()) {
+ return true;
+ }
+ }
+ return false;
+ }

/**
* @return Amount of units in the transport
@@ -999,6 +1030,10 @@
public boolean willBeDestroyedAfterTakingDamage(int dmg) {
return hp - dmg * 10 <= 0;
}
+
+ public int getCurrentConstructionMaterials() {
+ return constructionMaterials;
+ }

@Override
public String toString() {
=======================================
--- /trunk/v2/src/com/customwars/client/model/map/Map.java Mon Mar 14
07:18:14 2011
+++ /trunk/v2/src/com/customwars/client/model/map/Map.java Sun Mar 20
13:51:30 2011
@@ -356,19 +356,6 @@
return false;
}
}
-
- /**
- * Init the move and attack zone of each unit owned by the given player
- * excluding units within transports
- */
- public void initUnitZonesForPlayer(Player player) {
- for (Unit unit : player.getArmy()) {
- if (!unit.isInTransport()) {
- buildMovementZone(unit);
- buildAttackZone(unit);
- }
- }
- }

/**
* Build a zone in which the mover can make a move and set it to the
mover
@@ -696,7 +683,7 @@
}

/**
- * Retrieve a list of drop locations where a unit can be dropped on.
+ * Retrieve a list of drop locations where all units can be dropped on.
*
* @see #getFreeDropLocations(Unit, Location)
*/
@@ -705,7 +692,7 @@
}

/**
- * Retrieve a list of drop locations where a unit can be dropped on.
+ * Retrieve a list of drop locations where all units can be dropped on.
* Drop locations are always adjacent.
* <p/>
* The center parameter allows a transport to find out if any units can
be dropped
@@ -727,6 +714,30 @@
}
return freeDropLocations;
}
+
+ /**
+ * Retrieve a list of drop locations where the given unit can be dropped
on.
+ * Drop locations are always adjacent.
+ * <p/>
+ * 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.
+ * @see #isFreeDropLocation(Unit, Unit, Tile)
+ */
+ public List<Location> getFreeDropLocations(Unit transport, Unit
unitToBeDropped, Location center) {
+ if (transport.getLocatableCount() == 0) return Collections.emptyList();
+
+ List<Location> freeDropLocations = new ArrayList<Location>(4);
+ for (Tile tile : getSurroundingTiles(center, 1, 1)) {
+ if (isFreeDropLocation(transport, unitToBeDropped, tile)) {
+ freeDropLocations.add(tile);
+ }
+ }
+ return freeDropLocations;
+ }

/**
* Determines if at least 1 unit in the transport can be dropped on the
drop location.
@@ -737,7 +748,7 @@
* @return Can a unit in the transport be dropped to the given drop
location
* @see #isFreeDropLocation(Unit, Unit, Tile)
*/
- public boolean canDropAtLeast1Unit(Unit transporter, Tile dropLocation) {
+ boolean canDropAtLeast1Unit(Unit transporter, Tile dropLocation) {
if (transporter.getLocatableCount() == 0) return false;

for (int i = 0; i < transporter.getLocatableCount(); i++) {
@@ -911,6 +922,21 @@
public int getCityCount(String baseCityName) {
return getCityCount(baseCityName, null);
}
+
+ /**
+ * 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.
+ */
+ public boolean isSurroundedByEnemyUnits(Unit unit) {
+ int enemies = 0;
+ for (Tile t : getSurroundingTiles(unit.getLocation(), 1, 1)) {
+ Unit adjacentUnit = (Unit) t.getLastLocatable();
+ if (adjacentUnit != null
&& !adjacentUnit.isAlliedWith(unit.getOwner())) {
+ enemies++;
+ }
+ }
+ return enemies == 4;
+ }

/**
* Return a map player from this map for the given color
=======================================
--- /trunk/v2/src/com/customwars/client/ui/state/InGameContext.java Thu
Apr 1 07:20:09 2010
+++ /trunk/v2/src/com/customwars/client/ui/state/InGameContext.java Sun Mar
20 13:51:30 2011
@@ -19,29 +19,27 @@
* This object uses the Service locator pattern.
* To lookup objects that are shared while playing a Game
* Execution and undo of CWActions are delegated to the ActionManager.
- *
+ * <p/>
* Objects that are stored:
* A history of clicks in a tileMap
* The drop locations and units to be dropped on those locations.
* The current input mode, this changes the way a click on the map is
interpreted
- * ControllerManager, Game, MoveTraverse, MapRenderer, Hud
- *
- * @author stefan
*/
public class InGameContext {
public enum INPUT_MODE {
- DEFAULT, // Clicking shows a Menu or selects a unit
- GUI, // Input is handled by the GUI
- UNIT_SELECT, // Clicking on a unit will select it
- UNIT_ATTACK, // Clicking on a unit will attack it
- UNIT_DROP, // Clicking on empty space drops the unit
- LAUNCH_ROCKET, // Clicking on a tile fires the rocket
- UNIT_FLARE, // Clicking on a tile fires a flare
- UNIT_CYCLE // Start Iterating between units
+ DEFAULT, // Clicking shows a Menu or selects a unit
+ GUI, // Input is handled by the GUI
+ UNIT_SELECT, // Clicking on a unit will select it
+ UNIT_ATTACK, // Clicking on a unit will attack it
+ UNIT_DROP, // Clicking on empty space drops the unit
+ LAUNCH_ROCKET, // Clicking on a tile fires the rocket
+ UNIT_FLARE, // Clicking on a tile fires a flare
+ UNIT_CYCLE // Start Iterating between units
}

private INPUT_MODE inputMode;
private boolean trapped;
+ private boolean launching;
private final List<Unit> unitsInTransport;
private final DropLocationsQueue dropQueue;
private final ActionManager actionManager;
@@ -129,6 +127,10 @@
public void setTrapped(boolean trapped) {
this.trapped = trapped;
}
+
+ public void setLaunchingUnit(boolean launching) {
+ this.launching = launching;
+ }

public List<CWAction> getExecutedActions() {
return actionManager.getExecutedActions();
@@ -137,6 +139,10 @@
public boolean isTrapped() {
return trapped;
}
+
+ public boolean isLaunchingUnit() {
+ return launching;
+ }

public boolean isActionCompleted() {
return actionManager.isActionCompleted();
@@ -155,7 +161,8 @@
}

public boolean isInUnitMode() {
- return isUnitSelectMode() || isUnitAttackMode() || isUnitDropMode() ||
isRocketLaunchMode();
+ return isUnitSelectMode() || isUnitAttackMode() || isUnitDropMode() ||
+ isRocketLaunchMode();
}

public boolean isDefaultMode() {
=======================================
--- /trunk/v2/test/com/customwars/client/model/map/MapTest.java Tue Feb 22
12:32:43 2011
+++ /trunk/v2/test/com/customwars/client/model/map/MapTest.java Sun Mar 20
13:51:30 2011
@@ -49,7 +49,7 @@

// Create and add a transport in the map
Unit apc = UnitFactory.getUnit(TestData.APC);
- Player p_blue = new Player(0, java.awt.Color.blue);
+ Player p_blue = new Player(0, Color.blue);
MapUtil.addUnitToMap(map, apcLocation, apc, p_blue);

// Add inf to APC
@@ -99,7 +99,7 @@

// Create and add a transport in the map (We allows 2 units in the
transport this time)
Unit apc = UnitFactory.getUnit(TestData.APC);
- Player p_blue = new Player(0, java.awt.Color.blue);
+ Player p_blue = new Player(0, Color.blue);
MapUtil.addUnitToMap(map, apcLocation, apc, p_blue);

// Add artillery into APC
@@ -168,4 +168,46 @@
Assert.assertTrue(pGreen.getId() < 3);
Assert.assertEquals(1, pGreen.getArmyCount());
}
-}
+
+ @Test
+ public void testSurroundedByEnemies() {
+ Tile unitLocation = map.getTile(1, 1);
+
+ // Get the adjacent tiles around the inf
+ Tile northTile = map.getTile(1, 0);
+ Tile eastTile = map.getTile(2, 1);
+ Tile southTile = map.getTile(1, 2);
+ Tile westTile = map.getTile(0, 1);
+
+ // 2 players with different teams
+ Player p_blue = new Player(0, Color.blue, "blue", 0, 0, false);
+ Player p_red = new Player(1, Color.red, "red", 0, 1, false);
+
+ // Create and add an inf to the map
+ Unit inf = UnitFactory.getUnit(TestData.INF);
+ MapUtil.addUnitToMap(map, unitLocation, inf, p_blue);
+ Assert.assertFalse(map.isSurroundedByEnemyUnits(inf));
+
+ // Create and add a tank NORTH of the inf
+ Unit tank = UnitFactory.getUnit(TestData.TANK);
+ MapUtil.addUnitToMap(map, northTile, tank, p_red);
+ Assert.assertFalse(map.isSurroundedByEnemyUnits(inf));
+
+ // Create and add a hidden tank EAST of the apc
+ Unit hiddenTank = UnitFactory.getUnit(TestData.TANK);
+ hiddenTank.setHidden(true);
+ MapUtil.addUnitToMap(map, eastTile, hiddenTank, p_red);
+ Assert.assertFalse(map.isSurroundedByEnemyUnits(inf));
+
+ // Create and add an apc unit SOUTH of the inf
+ Unit apc = UnitFactory.getUnit(TestData.APC);
+ southTile.setFogged(true);
+ MapUtil.addUnitToMap(map, southTile, apc, p_red);
+ Assert.assertFalse(map.isSurroundedByEnemyUnits(inf));
+
+ // Create and add an apc unit WEST of the apc
+ Unit apc2 = UnitFactory.getUnit(TestData.APC);
+ MapUtil.addUnitToMap(map, westTile, apc2, p_red);
+ Assert.assertTrue(map.isSurroundedByEnemyUnits(inf));
+ }
+}

Reply all
Reply to author
Forward
0 new messages