/// <summary> /// Give selected units a move command /// </summary> /// <param name="point">Destination</param> public void MoveSelectedUnitsToPoint(PointF point) { List<ModelComponent> selectedEntities = ((XnaUITestGame)Game).Model.GetSelectionState().SelectedEntities; // Ensure each component is a Unit. bool allAreUnits = true; bool playerEntities = false; foreach (ModelComponent component in selectedEntities) { if (entityBelongsToPlayer(component)) { playerEntities = true; if (!(component is UnitComponent)) { allAreUnits = false; break; } } } if (allAreUnits && playerEntities) { foreach (UnitComponent unit in selectedEntities) { MoveAction moveAction = new MoveAction(point.X, point.Y,getMap() , unit); ActionQueue aq = unit.GetActionQueue(); aq.GetChildren().Clear(); aq.AddChild(moveAction); } } }
/// <summary> /// This function will perform the Attack Action. /// </summary> /// <returns>true if the action is completed, false if it is not.</returns> public override bool Work() { if (target == null) { return true; } // target is dead, return true. if (target.CurrentHealth <=0) { return true; } // unit cannot attack, return true. if (!unit.CanAttack) { return true; } // Target is in range attack target. if (targetIsInRange()) { // Create a SimpleAttackAction if it is needed. if (this.attackAction == null) { attackAction = new SimpleAttackUnitAction(unit, target); } // Call the SimpleAttackAction attackAction.Work(); // Set the MoveAction to null. moveAction = null; } // Target is not in range, move to it. else { if(moveAction != null && !targetIsInRange(target.PointLocation.X, target.PointLocation.Y, moveAction.targetX, moveAction.targetY)) { moveAction = null; } // Create a MoveAction if it is needed. if (moveAction == null) { if (target is UnitComponent) { UnitComponent temp = (UnitComponent)target; moveAction = new MoveAction(temp.PointLocation.X, temp.PointLocation.Y, gw.GetMap(), unit); } else if (target is Building) { //StaticEntity temp = (StaticEntity)target; //moveAction = new MoveAction(temp.orginCell.Xcoord, temp.orginCell.Ycoord, gw, unit); } } // Set the SimpleAttackAction to null. attackAction = null; // Call the MoveAction. moveAction.Work(); } return false; }
/// <summary> /// This function will perform a building cycle if the number of ticks since the last cycle is equal to TICKS_PER_CYCLE. /// </summary> /// <returns>true if the building is complete and the action is finished, false otherwise.</returns> public override bool Work() { if (!building.Completed) { if (curTicks % TICKS_PER_CYCLE == 0) { // Check if unit is adjacent to building. if (isUnitNextToBuilding()) { UnitComponent unit = (UnitComponent)Parent.Parent; unit.State = UnitComponent.UnitState.BUILDING; UnitComponent worker = (UnitComponent)Parent.Parent; // Add the building to the model if we have not done so yet. if (building.Parent == null) { // TODO: Ensure that the spaces are cleared. Perhaps wait/give up, as with move? PlayerComponent player = Parent.Parent.Parent.Parent as PlayerComponent; if (!map.addBuildingToMap(building)) // add building to the map { return false; } player.addBuilding(building); // add building to player's building list } updateBuildingProgress(building, worker); } else { // Move towards the building. Insert a move action into the Unit's action queue. CellComponent targetCell = findClosestCell(((UnitComponent)Parent.Parent).PointLocation); MoveAction moveAction = new MoveAction(targetCell.X, targetCell.Y, map, ((UnitComponent)Parent.Parent)); Parent.AddChildAt(moveAction, 0); } } } curTicks++; return building.Completed; }
public void TestUnitReactingToEnemyMovingWithinRange() { setupModel(); // Add a unit to each player. UnitList list = ((PlayerComponent)model.GetScenario().GetGameWorld().GetPlayerList().GetChildren()[0]).GetUnitList(); UnitList list2 = ((PlayerComponent)model.GetScenario().GetGameWorld().GetPlayerList().GetChildren()[1]).GetUnitList(); // Create a Unit for Player 1 UnitComponent unit1 = new UnitComponent(); list.AddChild(unit1); unit1.PointLocation = new PointF(0.5f, 0.5f); unit1.AttackStance = UnitComponent.UnitAttackStance.Aggressive; unit1.VisibilityRange = 4.0f; // Create a Unit for Player 2 UnitComponent unit2 = new UnitComponent(); list2.AddChild(unit2); unit2.PointLocation = new PointF(9.5f, 9.5f); // outisde unit1's visibility range (4.0f). // Check to make sure that unit1 did not notice unit2 being added. bool output = false; if (unit1.GetActionQueue().GetChildren().Count > 0) { if (unit1.GetActionQueue().GetChildren()[0] is AttackAction) { AttackAction action = (AttackAction)unit1.GetActionQueue().GetChildren()[0]; output = action.Target == unit2; } } Assert.IsFalse(output); // Have unit2 move into unit1's visibility range. MoveAction move = new MoveAction(2.0f, 2.0f, model.GetScenario().GetGameWorld().GetMap(), unit2); //Have unit2 move until the move action is completed. while (!move.Work()) { } // Test that unit1 has been given an AttackAction with unit2 as the target. output = false; if (unit1.GetActionQueue().GetChildren().Count > 0) { if (unit1.GetActionQueue().GetChildren()[0] is AttackAction) { AttackAction action = (AttackAction)unit1.GetActionQueue().GetChildren()[0]; output = action.Target == unit2; } } Assert.IsTrue(output); }