// factory function,according to name it creates model. public override IScrollBuildingModel CreateScrollBuildingModel(BuildingEventTypes eventType) { switch (eventType) { case BuildingEventTypes.Barrack: return(new ScrollBarrackModel()); case BuildingEventTypes.PowerPlant: return(new ScrollPowerPlantModel()); default: return(null); } }
private void AddBuildingToLists(BuildingEventTypes buildingDragEventType) { BuildingFactory factory = new ScrollBuildingModelFactory(); IScrollBuildingModel newBuilding = null; newBuilding = factory.CreateScrollBuildingModel(buildingDragEventType); if (_activeCellPositionX != null) { newBuilding.XIndex = (int)_activeCellPositionX; } if (_activeCellPositionY != null) { newBuilding.YIndex = (int)_activeCellPositionY; } newBuilding.BuildingNumber = _buildingsList[buildingDragEventType].Count + 1; _buildingsList[buildingDragEventType].Add(newBuilding); }
public abstract IScrollBuildingModel CreateScrollBuildingModel(BuildingEventTypes eventType);
// On drag finish for barracks, this checks it crashed with other buildings // set the color of cells after collision on drag finish private void CheckCollidedBuildingOnDragFinish(BuildingEventTypes eventType) { BuildingEventTypes buildingCollisionChecker = BuildingEventTypes.None; int indexChecker = 0; Color gridCellColor = Color.yellow; if (eventType == BuildingEventTypes.Barrack) { indexChecker = 1; gridCellColor = Color.blue; } for (int i = (int)_activeCellPositionX - 1; i <= (int)_activeCellPositionX + indexChecker; i++) { for (int j = (int)_activeCellPositionY - 1; j <= (int)_activeCellPositionY + 1; j++) { if (_gridCellGameObjectArray[i, j].gameObject.GetComponent <Image>().color == Color.red) { if (indexChecker == 0) { buildingCollisionChecker = BuildingEventTypes.Barrack; } else { buildingCollisionChecker = BuildingEventTypes.PowerPlant; } } } } if (buildingCollisionChecker != BuildingEventTypes.None) { for (int i = (int)_activeCellPositionX - 1; i <= (int)_activeCellPositionX + indexChecker; i++) { for (int j = (int)_activeCellPositionY - 1; j <= (int)_activeCellPositionY + 1; j++) { if (_gridCellGameObjectArray[i, j].gameObject.GetComponent <Image>().color == gridCellColor) { _gridCellGameObjectArray[i, j].gameObject.GetComponent <Image>().color = Color.green; _gridCellCellArray[i, j].GridCellType = GridCellTypes.Empty; } if (_gridCellGameObjectArray[i, j].gameObject.GetComponent <Image>().color == Color.red) { if (_gridCellCellArray[i, j].GridCellType == GridCellTypes.Barrack) { _gridCellGameObjectArray[i, j].gameObject.GetComponent <Image>().color = Color.blue; } else if (_gridCellCellArray[i, j].GridCellType == GridCellTypes.PowerPlant) { _gridCellGameObjectArray[i, j].gameObject.GetComponent <Image>().color = Color.yellow; } } if (_gridCellGameObjectArray[i, j].gameObject.GetComponent <Image>().color == Color.green) { _gridCellCellArray[i, j].GridCellType = GridCellTypes.Empty; } } } } if (buildingCollisionChecker == BuildingEventTypes.None && !_soldierChecker) { AddBuildingToLists(ScrollController.Instance().ScrollDragEventChecker.BuildingEventType); } else { buildingCollisionChecker = BuildingEventTypes.None; } }
// On drag finish it checks if soldier is in building area. // Set the cell colors on grid according to collision. private void CheckSoldierInBuildingArea(BuildingEventTypes eventType) { int indexSetterWithBuildingType = 0; switch (eventType) { case BuildingEventTypes.Barrack: indexSetterWithBuildingType = 1; break; case BuildingEventTypes.PowerPlant: indexSetterWithBuildingType = 0; break; case BuildingEventTypes.None: break; default: throw new ArgumentOutOfRangeException("eventType", eventType, null); } if (_activeCellPositionX != null && _activeCellPositionY != null) { for (int i = (int)_activeCellPositionX - 1; i <= (int)_activeCellPositionX + indexSetterWithBuildingType; i++) { for (int j = (int)_activeCellPositionY - 1; j <= (int)_activeCellPositionY + 1; j++) { if (_gridCellCellArray[i, j].GridCellType == GridCellTypes.Soldier) { _soldierChecker = true; } } } if (_soldierChecker) { for (int i = (int)_activeCellPositionX - 1; i <= (int)_activeCellPositionX + indexSetterWithBuildingType; i++) { for (int j = (int)_activeCellPositionY - 1; j <= (int)_activeCellPositionY + 1; j++) { if (_gridCellCellArray[i, j].GridCellType != GridCellTypes.Soldier) { _gridCellCellArray[i, j].GridCellType = GridCellTypes.Empty; _gridCellGameObjectArray[i, j].GetComponent <Image>().color = Color.green; } else { _gridCellCellArray[i, j].GridCellType = GridCellTypes.Soldier; _gridCellGameObjectArray[i, j].GetComponent <Image>().color = Color.green; } } } } else { for (int i = (int)_activeCellPositionX - 1; i <= (int)_activeCellPositionX + indexSetterWithBuildingType; i++) { for (int j = (int)_activeCellPositionY - 1; j <= (int)_activeCellPositionY + 1; j++) { if (_gridCellCellArray[i, j].GridCellType == GridCellTypes.Empty) { if (indexSetterWithBuildingType == 0) { _gridCellCellArray[i, j].GridCellType = GridCellTypes.PowerPlant; } else if (indexSetterWithBuildingType == 1) { _gridCellCellArray[i, j].GridCellType = GridCellTypes.Barrack; } } } } } } }