/// <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()) { // 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; player.BuildingList.AddChild(building); for (int i = (int)building.PointLocation.X; i < building.PointLocation.X + building.Width; i++) { for (int j = (int)building.PointLocation.Y; j < building.PointLocation.Y + building.Height; j++) { building.CellsContainedWithin.Add(map.GetCellAt(i, j)); map.GetCellAt(i, j).AddEntity(building); } } } if (building.MaxHealth - building.CurrentHealth <= ((UnitComponent)Parent.Parent).BuildSpeed) { // Finish the building. building.CurrentHealth = building.MaxHealth; building.Completed = true; } else { // Continue building the building. building.CurrentHealth += ((UnitComponent)Parent.Parent).BuildSpeed; } } 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); Parent.AddChildAt(moveAction, 0); } } } curTicks++; return(building.Completed); }
/// <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); }