/// <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> /// Checks If building can be placedm, also changes colour accordingly /// </summary> /// <param name="screenPosition"></param> /// <returns> If building ca be placed</returns> private bool CanPlaceTower(Vector3 screenPosition) { if (TestPosition(screenPosition)) { if (city.CanBuyByCost(objectToPlace.GetCost())) { ChangeSilhouetteColour(Color.green); return(true); } else { ChangeSilhouetteColour(Color.yellow); return(false); } } else { ChangeSilhouetteColour(Color.red); return(false); } }
/// <summary> /// Sell building and add part of its cost to city /// </summary> /// <param name="targetPosition"></param> private void SellTurret(Vector3 targetPosition) { if (!(targetPosition == Vector3.zero)) { for (int i = 0; i < playerBuildings.Count; i++) { if (playerBuildings[i].transform.position == targetPosition) { IPlaceable <Building> buildingToSell = playerBuildings[i].GetComponent <IPlaceable <Building> >(); city.AddResource(buildingToSell.GetCost(), refundOnSell); buildingToSell.PrepareRemoval(); GameObject buildingToRemove = playerBuildings[i]; playerBuildings.Remove(buildingToRemove); Destroy(buildingToRemove.gameObject); break; } } } }