protected virtual void PayCost(WorldBehaviour.TerraformCostCalculation cost, bool doShow = false) { if (resourceManager) { resourceManager.Pay(cost); if (doShow) { FloatingTextManager.Instance.AddText(transform.position, "-" + resourceManager.GetCostString(cost), 2 * Vector3.up, 3f, Color.yellow); } } }
private ResourceInstance CalculateCost(WorldBehaviour.TerraformCostCalculation cost) { if (cost.heightChangedTiles.Count > 0) { ResourceInstance resources = changeTerrainTileHeightCost; resources.amount *= cost.heightChangedTiles.Count; return(resources); } else { ResourceInstance resources = clearTerrainTileCost; resources.amount *= cost.bulldozedTiles.Count; return(resources); } }
protected virtual bool CanAfford(WorldBehaviour.TerraformCostCalculation cost, bool doShow = false) { if (resourceManager != null) { bool canAfford = resourceManager.CanAfford(cost); if (doShow && !canAfford) { FloatingTextManager.Instance.AddText(transform.position, "Can't afford\n" + resourceManager.GetCostString(cost), 2 * Vector3.up, 3f, Color.yellow); } return(canAfford); } else { return(true); } }
public string GetCostString(WorldBehaviour.TerraformCostCalculation cost) { return(CalculateCost(cost).ToString()); }
public void Pay(WorldBehaviour.TerraformCostCalculation cost) { Pay(CalculateCost(cost)); }
public bool CanAfford(WorldBehaviour.TerraformCostCalculation cost) { return(CanAfford(CalculateCost(cost))); }
void HandleTerraforming() { tileOverlay.isVisible = currentTerraform == TerraFormType.BUILD; //Make sure that start and stop are actually in correct corners MapTile startTile = dragStartTile; MapTile stopTile = dragStartTile; MapTile.SetStartStop(ref startTile, ref stopTile); switch (currentTerraform) { case TerraFormType.BULLDOZE: transform.position = world.GetWorldPositionFromTile(currentTile); gridOverlay.isVisible = true; gridOverlay.start = transform.position - HalfTileSize(); gridOverlay.stop = transform.position + HalfTileSize(); if (Input.GetMouseButtonDown(0)) { if (doDebug) { Debug.Log("Bulldoze " + transform.position + " at " + currentTile); } WorldBehaviour.TerraformCostCalculation cost = world.GetBullDozeTileCost(currentTile); if (CanAfford(cost, true)) { world.BullDozeTile(currentTile); buildingManager.Bulldoze(currentTile.x, currentTile.y); PayCost(cost); GetComponent <AudioSource>().PlayOneShot(explosionClip); } } break; case TerraFormType.BULLDOZE_AREA: transform.position = GetHeightMapCoords(currentPointerPos); if (isDragging) { gridOverlay.isVisible = true; gridOverlay.start = startPos; gridOverlay.stop = currentPointerPos; } else { gridOverlay.isVisible = false; } if (didRelease) { if (doDebug) { Debug.Log("BULLDOZE AREA between " + startPos + " and " + currentPointerPos); } WorldBehaviour.TerraformCostCalculation cost = world.GetBulldozeAreaCost(dragStartTile, dragStopTile); if (CanAfford(cost, true)) { PayCost(cost); world.BulldozeArea(dragStartTile, dragStopTile); buildingManager.BulldozeArea(dragStartTile, dragStopTile); GetComponent <AudioSource>().PlayOneShot(explosionClip); } gridOverlay.isVisible = false; } break; case TerraFormType.RAISE_TERRAIN: transform.position = GetHeightMapCoords(currentPointerPos) + world.terrain.transform.position; gridOverlay.isVisible = true; gridOverlay.start = currentPointerPos - HalfTileSize(); gridOverlay.stop = currentPointerPos + HalfTileSize(); if (didPress) { if (doDebug) { Debug.Log("RAISE tile" + currentTile); } WorldBehaviour.TerraformCostCalculation cost = world.GetRaiseTerrainCost(currentTile); if (buildingManager.DoTileHaveBuilding(cost.bulldozedTiles)) { FloatingTextManager.Instance.AddText(transform.position, "Can't raise terrain, building in the way", 2 * Vector3.up, 3f, Color.red); break; } else { if (CanAfford(cost, true)) { PayCost(cost); world.RaiseTerrain(currentTile); GetComponent <AudioSource>().PlayOneShot(explosionClip); } } } break; case TerraFormType.LOWER_TERRAIN: transform.position = GetHeightMapCoords(currentPointerPos) + world.terrain.transform.position; gridOverlay.isVisible = true; gridOverlay.start = currentPointerPos - HalfTileSize(); gridOverlay.stop = currentPointerPos + HalfTileSize(); if (didPress) { if (doDebug) { Debug.Log("RAISE tile" + currentTile); } WorldBehaviour.TerraformCostCalculation cost = world.GetLowerTerrainCost(currentTile); if (buildingManager.DoTileHaveBuilding(cost.bulldozedTiles)) { FloatingTextManager.Instance.AddText(transform.position, "Can't lower terrain, building in the way", 2 * Vector3.up, 3f, Color.red); break; } else { if (CanAfford(cost, true)) { PayCost(cost); world.LowerTerrain(currentTile); GetComponent <AudioSource>().PlayOneShot(explosionClip); } } } break; case TerraFormType.LEVEL_TERRAIN: transform.position = GetHeightMapCoords(currentPointerPos); if (isDragging) { gridOverlay.isVisible = true; gridOverlay.start = startPos; gridOverlay.stop = currentPointerPos; } else { gridOverlay.isVisible = false; } if (didRelease) { if (doDebug) { Debug.Log("LEVEL between " + dragStart + " and " + dragEnd); } WorldBehaviour.TerraformCostCalculation cost = world.GetLevelTerrainCost(dragStartTile, dragStopTile); //First check so that no buildings would be affected (just so that user does not unintentionally destroy buildings) if (buildingManager.DoTileHaveBuilding(cost.bulldozedTiles)) { FloatingTextManager.Instance.AddText(transform.position, "Can't level terrain, building in the way", 2 * Vector3.up, 3f, Color.red); } else { if (CanAfford(cost, true)) { PayCost(cost); world.LevelTerrain(dragStartTile, dragStopTile); GetComponent <AudioSource>().PlayOneShot(explosionClip); } } gridOverlay.isVisible = false; } break; case TerraFormType.BUILD: SimpleBuildingType buildingType = AvailableBuildingTypes[currentBuildingIndex]; gridOverlay.isVisible = true; int width = buildingType.buildArea.width; int length = buildingType.buildArea.length; if (direction % 2 == 1) { width = buildingType.buildArea.length; length = buildingType.buildArea.width; } //If we have an even number as width or length then we want the center to be moved by one step Vector3 pos = currentPointerPos + new Vector3(width % 2 - 1, 0, length % 2 - 1) * world.worldData.tileWidth / 2f; //Make sure we are never below water surface pos.y = Mathf.Max(pos.y, world.GetWaterLevelHeight()); //Calc tile coords int startX = (int)((pos.x - width * 0.5f) / world.worldData.tileWidth); //((int)pos.x - width); int startY = (int)((pos.z - length * 0.5f) / world.worldData.tileWidth); //(int)pos.z - length; int stopX = startX + width; int stopY = startY + length; //Mult by tile width gridOverlay.start = new Vector3(world.worldData.tileWidth * startX, pos.y, world.worldData.tileWidth * startY); gridOverlay.stop = new Vector3(world.worldData.tileWidth * stopX, pos.y, world.worldData.tileWidth * stopY); // pos + new Vector3(width,0,length); tileOverlay.xCoord = startX; tileOverlay.yCoord = startY; transform.position = (gridOverlay.start + gridOverlay.stop) / 2 + world.terrain.transform.position; transform.rotation = Quaternion.Euler(new Vector3(0, direction * 90, 0)); buildingManager.IsBuildingPositionAllowed(startX, startY, direction, buildingType, out tileOverlay.tiles); if (didPress) { if (doDebug) { Debug.Log("BUILD " + pos + "Coords x:" + (pos.x - 1) / 2 + " y:" + (pos.z - 1) / 2); } if (!buildingManager.IsBuildingPositionAllowed(startX, startY, direction, buildingType)) { FloatingTextManager.Instance.AddText(transform.position, "Can't build there", 2 * Vector3.up, 3f, Color.red); break; } //If we can afford then build if (CanAfford(buildingType, true)) { PayConstructionCost(buildingType); buildingManager.ConstructBuildingInTile(startX, startY, currentBuildingIndex, direction); GetComponent <AudioSource>().PlayOneShot(explosionClip); } } break; default: break; } }