/// <summary> /// Updates when currently in placement mode. /// </summary> private void UpdatePlacing() { var pos = CoordinateConverter.GetCurrentMouseTilePosition(cam); instance.transform.position = CoordinateConverter.ToVector3Int(pos); // Update color of instance. var color = buildingsData.InMap(CoordinateConverter.WorldToArray(pos, worldData.worldSize)) && buildingsData.IsPlaceable(placeable, pos) ? placeableColor : notPlaceableColor; color = city.CanBuyByCost(placeable.GetCost()) ? color : cantBuyColor; for (int i = 0; i < spriteRenderers.Length; i++) { spriteRenderers[i].color = color; } // Place building if possible. if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject() && buildingsData.InMap(CoordinateConverter.WorldToArray(pos, worldData.worldSize)) && buildingsData.IsPlaceable(placeable, pos) && city.Buy(placeable.GetCost())) { GameObject gameObject = Instantiate(placeable.GetObject(), instance.transform.position, Quaternion.identity, parent); if (!buildingsData.TrySet(gameObject.GetComponent <IPlaceable <Building> >(), pos)) { throw new InvalidOperationException("This shouldn't be possible!"); } } }
/// <summary> /// Creates building Silhouette and starts placement /// </summary> /// <param name="toPlaceObject"></param> public void StartTowerPlacement(IPlaceable <Building> toPlaceObject) { if (inBuildPhase) { CancelTowerPlacement(); placingActive = true; SellingActive = false; objectToPlace = toPlaceObject; buildingSilhouette = Instantiate(toPlaceObject.GetObject(), Vector3.zero, Quaternion.identity); } }
/// <summary> /// Places building into map /// </summary> private void PlaceBuilding() { Vector3 newPos = new Vector3(buildingSilhouette.transform.position.x, buildingSilhouette.transform.position.y, 0); GameObject newObject = Instantiate(objectToPlace.GetObject(), newPos, Quaternion.identity); if (newObject.GetComponent <LineRenderer>() != null) { newObject.GetComponent <LineRenderer>().enabled = false; } //objectToPlace.FinishPlacement(newObject); city.Buy(objectToPlace.GetCost()); }
/// <summary> /// Initiates the placement of placeables. /// </summary> /// <param name="placeable"> Placeable to use for placement </param> public void StartPlacement(IPlaceable <Building> placeable) { CleanUp(); this.placeable = placeable; state = ConstructionState.Placing; var pos = CoordinateConverter.ToVector3Int(CoordinateConverter.GetCurrentMouseTilePosition(cam)); instance = Instantiate(placeable.GetObject(), pos, Quaternion.identity, transform); instance.GetComponent <Building>().enabled = false; spriteRenderers = instance.GetComponentsInChildren <SpriteRenderer>(); // Make instance be in the foreground. for (int i = 0; i < spriteRenderers.Length; i++) { spriteRenderers[i].sortingOrder += 20; } onStateChanged?.Invoke(); }
/// <summary> /// Removes a placeable from the map. /// </summary> /// <param name="placeable"> Placeable to remove </param> public void RemoveBuilding(IPlaceable <Building> placeable) { buildingsData.Remove(placeable); placeable.PrepareRemoval(); Destroy(placeable.GetObject()); }