public static ConstructionBehaviour CreateConstructionBehaviour(ConstructionData constructionData) { ConstructionBehaviour constructionBehaviour = Instantiate(constructionData.Prefab).AddComponent <ConstructionBehaviour>(); constructionBehaviour.Initialize(constructionData, constructionBehaviour.GetComponent <IConstructable>()); return(constructionBehaviour); }
void OnConstructionCancelation() { ReturnConstructionToPool(_currentStructureBehaviour.gameObject); _currentStructureBehaviour = null; _gameManager.Interaction = GameManager.InteractionMode.NoSelection; _gameManager.Input.SetInGameLayout(); }
public void StartPlacement(ConstructionData constructionData) { if (!IsInvoking()) { _constructionBehaviour = CreateConstructionBehaviour(constructionData); InvokeRepeating(nameof(Routine), 0, Constant.DEFAULT_REFRESH_RATE); } }
void SetBlueprintedCurrentConstrution(GameObject gameObject) { _currentStructurePlacement = gameObject; ConstructionBehaviour constructionBehaviour = gameObject.GetComponent <ConstructionBehaviour>(); constructionBehaviour.ResetToBlueprint(); _currentStructureBehaviour = constructionBehaviour; }
// Callbacks void OnConstructionConfirmation() { ConstructConfirmationCount(_currentStructureBehaviour); _currentStructureBehaviour.Activate(OnStructureClick); _currentStructureBehaviour = null; _gameManager.Interaction = GameManager.InteractionMode.NoSelection; _gameManager.Input.SetInGameLayout(); _gameManager.Enemies.MapChanged(); }
void OnDestroyConfirmation(GameObject gameObject) { Type constructionType = ReturnConstructionToPool(gameObject); DestructionConfirmationCount(constructionType); _currentStructureBehaviour = null; _gameManager.Interaction = GameManager.InteractionMode.NoSelection; _gameManager.Input.SetInGameLayout(); _gameManager.Enemies.MapChanged(); }
void ConstructConfirmationCount(ConstructionBehaviour script) { if (script.GetType() == typeof(TowerBehaviour)) { if (_towerPool.Available <= 0) { _gameManager.Input.SetVisibilityTowerButton(false); } } else // WallBehaviour { if (_wallPool.Available <= 0) { _gameManager.Input.SetVisibilityWallButton(false); } } }
/// <summary> /// Place an object unto the map based on the available areas. /// </summary> /// <param name="constructionData"></param> public void Construct(ConstructionData constructionData) { ConstructionBehaviour constructionBehaviour = Foreman.CreateConstructionBehaviour(constructionData); ObjectBehaviour objectBehaviour = constructionBehaviour.GetComponent <ObjectBehaviour>(); objectBehaviour.Initialize(); bool placed = false; List <Area> splitArea = new List <Area>(); int count = _availableAreas.Count; while (count > 0) { // Generate a random index to check if the areas is ok. int i = Random.Range(0, _availableAreas.Count); if (_availableAreas[i].ObstacleCanFitInTheArea(objectBehaviour.Obstacle, out bool rotate)) { Debug.Log("The obstacle " + objectBehaviour.Obstacle.Size + " can fit in: " + _availableAreas[i] + " with rotation: " + rotate); // If the Obstacle fits if the area is rotated, rotate it. if (rotate) { constructionBehaviour.Rotate(); } // Remove the obstacle from the area. if (Area.RemoveObstacleFromArea(_availableAreas[i], objectBehaviour.Obstacle, ref splitArea)) { Vector3 origin = Map.GetMapOriginInWorldPos(_encampment.Map, _encampment.transform.position); Vector3 objectPos = new Vector3(_availableAreas[i].Origin.x, 0, _availableAreas[i].Origin.y); Vector3 pos = objectPos + origin + (new Vector3(_availableAreas[i].Size.x, 0, _availableAreas[i].Size.y) / 2); // TODO get height at position on terrain. constructionBehaviour.transform.position = pos; if (!_encampment.Map.IsPositionValid(objectBehaviour.Obstacle, _encampment)) { break; } objectBehaviour.RegisterZone(_encampment); constructionBehaviour.StartConstruction(); _availableAreas.Remove(_availableAreas[i]); RegisterAreas(splitArea); placed = true; break; } else { throw new UnityException("It should work."); } } else { Debug.Log("The obstacle " + objectBehaviour.Obstacle.Size + " cannot fit in: " + _availableAreas[i]); } count--; } if (!placed) { Destroy(constructionBehaviour.gameObject); } }
public ConstructArguments(ConstructionBehaviour constructionBehaviour) { ConstructionBehaviour = constructionBehaviour; }
void UpdateCurrentStructure(GameManager.InteractionMode currentState, GameManager.InteractionMode nextState) { if (nextState == GameManager.InteractionMode.NoSelection) { if (_currentStructurePlacement != null) { switch (currentState) { case GameManager.InteractionMode.TowerSelection: _towerPool.ReturnInstance(_currentStructurePlacement); _currentStructurePlacement = null; _currentStructureBehaviour = null; break; case GameManager.InteractionMode.WallSelection: _wallPool.ReturnInstance(_currentStructurePlacement); _currentStructurePlacement = null; _currentStructureBehaviour = null; break; } } } else { // Return old switch (currentState) // Important - there is a validation in GameManager Interaction setter that checks if the current and next are the same { case GameManager.InteractionMode.TowerSelection: if (nextState != GameManager.InteractionMode.ConstructionConfirmation) { _towerPool.ReturnInstance(_currentStructurePlacement); } break; case GameManager.InteractionMode.WallSelection: if (nextState != GameManager.InteractionMode.ConstructionConfirmation) { _wallPool.ReturnInstance(_currentStructurePlacement); } break; case GameManager.InteractionMode.ConstructionConfirmation: ReturnConstructionToPool(_currentStructureBehaviour.gameObject); _gameManager.Input.SetInGameLayout(); break; case GameManager.InteractionMode.DestructionConfirmation: _gameManager.Input.SetInGameLayout(); break; } // Get new switch (nextState) { case GameManager.InteractionMode.TowerSelection: SetBlueprintedCurrentConstrution(_towerPool.GetInstance()); break; case GameManager.InteractionMode.WallSelection: SetBlueprintedCurrentConstrution(_wallPool.GetInstance()); break; } } _mode = nextState; }