Ejemplo n.º 1
0
        public void SetBuildingType(int newType)
        {
            if (AvailableBuildingTypes.Count <= newType)
            {
                return;
            }
            currentBuildingIndex = newType;
            SimpleBuildingType buildingType = AvailableBuildingTypes[currentBuildingIndex];

            SetBuildingType(buildingType);
        }
Ejemplo n.º 2
0
        public void Setup(SimpleBuildingType buildingType, IPlayerController player, Text txtTooltip)
        {
            this.buildingType = buildingType;
            this.player       = player;

            gameObject.name = buildingType.name;
            GetComponentInChildren <Text>().text = buildingType.name;

            UIButtonTooltip tooltip = GetComponent <UIButtonTooltip>();

            tooltip.tooltipObject = buildingType;
            tooltip.txtTooltip    = txtTooltip;

            GetComponent <Button>().onClick.AddListener(HandleClick);
        }
Ejemplo n.º 3
0
        public void SetBuildingType(SimpleBuildingType buildingType)
        {
            if (AvailableBuildingTypes.Contains(buildingType))
            {
                currentBuildingIndex = AvailableBuildingTypes.IndexOf(buildingType);
            }
            else
            {
                return;
            }

            SetTerraformType((int)TerraFormType.BUILD);
            currentTerraform             = TerraFormType.BUILD;
            playerDisplay.tmpBuildObject = (GameObject)GameObject.Instantiate(buildingType.prefab, transform.position, transform.rotation);
            playerDisplay.tmpBuildObject.GetComponent <SimpleBuildingBehaviour>().enabled = false;
            playerDisplay.tmpBuildObject.transform.SetParent(transform);
            playerDisplay.tmpBuildObject.transform.localScale = buildingType.scale;
        }
Ejemplo n.º 4
0
 private bool CanAfford(SimpleBuildingType building, bool doShow = false)
 {
     if (resourceManager != null)
     {
         if (!resourceManager.CanAfford(building.constructionCost))
         {
             if (doShow)
             {
                 FloatingTextManager.Instance.AddText(transform.position, "Can't afford\n" + resourceManager.GetCostString(building.constructionCost), 2 * Vector3.up, 3f, Color.yellow);
             }
             return(false);
         }
         else
         {
             return(true);
         }
     }
     else
     {
         return(true);
     }
 }
Ejemplo n.º 5
0
 protected virtual void PayConstructionCost(SimpleBuildingType building, bool doShow = true)
 {
     PayCost(building.constructionCost, doShow);
 }
Ejemplo n.º 6
0
        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;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Instantiates the building by actually creating the gameobject and assigning it to the world
        /// </summary>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        /// <param name="buildingInstance">Building instance.</param>
        protected void InstantiateBuilding(int x, int y, SimpleBuildingInstance buildingInstance)
        {
            SimpleBuildingType buildingType = buildingTypes[buildingInstance.buildingTypeIndex];

            int width  = buildingType.buildArea.width;
            int length = buildingType.buildArea.length;

            //If we turned the building -90 or 90 degrees then switch width and length
            if (buildingInstance.direction % 2 == 1)
            {
                width  = buildingType.buildArea.length;
                length = buildingType.buildArea.width;
            }


            int startX = x;
            int stopX  = x + width - 1;
            int startY = y;
            int stopY  = y + length - 1;

            buildingInstance.x = x;
            buildingInstance.y = y;

            //Calculate dead center of building area to place the actual game object
            Vector3 pos = (world.GetWorldPositionFromTile(startX, startY, true) + world.GetWorldPositionFromTile(stopX, stopY, true)) / 2;

            GameObject building = (GameObject)GameObject.Instantiate(buildingType.prefab, pos, Quaternion.identity);

            building.name = buildingType.name;

            Collider buildingCollider = building.GetComponent <Collider>();

            if (buildingCollider != null)
            {
                buildingCollider.enabled = true;
            }
            Vector3 scale = building.transform.localScale;

            scale = Vector3.Scale(buildingType.scale, scale);
            building.transform.localScale = scale;

            Vector3 rot = building.transform.eulerAngles;

            rot.y = 90 * buildingInstance.direction;
            building.transform.rotation = Quaternion.Euler(rot);

            if (buildingType.doLeanWithTerrain)
            {
                if (world.isTileLeaningDownX(x, y))
                {
                    building.transform.Rotate(45, 0, 0);
                }
                if (world.isTileLeaningUpX(x, y))
                {
                    building.transform.Rotate(-45, 0, 0);
                }
                if (world.isTileLeaningDownY(x, y))
                {
                    building.transform.Rotate(-45, 0, 0);
                }
                if (world.isTileLeaningUpY(x, y))
                {
                    building.transform.Rotate(45, 0, 0);
                }
            }

            SimpleBuildingBehaviour buildingBehaviour = building.GetComponent <SimpleBuildingBehaviour>();

            buildingBehaviour.positionKey          = world.GetTileCoordKey(x, y);
            buildingBehaviour.buildingType         = buildingType;
            buildingBehaviour.buildingInstanceData = buildingInstance;
            buildingBehaviour.width  = width;
            buildingBehaviour.length = length;
            buildingBehaviour.Setup();

            //Add construction utilities if applicable
            if (buildingBehaviour.underConstructionBuilding != null &&
                constructionUtilitiesPrefab != null)
            {
                GameObject constructionUtilities = Instantiate <GameObject>(constructionUtilitiesPrefab);
                constructionUtilities.transform.SetParent(buildingBehaviour.underConstructionBuilding.transform);
                constructionUtilities.transform.localPosition = Vector3.zero;
            }

            //We add a reference to this building in every tile it occupies
            for (int i = x; i <= stopX; i++)
            {
                for (int j = y; j <= stopY; j++)
                {
                    int key = world.GetTileCoordKey(i, j);
                    buildingTiles.Add(key, buildingBehaviour);
                }
            }

            if (OnBuildingsChanged != null)
            {
                OnBuildingsChanged();
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Determines whether this building position is allowed at the specified x, y and direction. The rules set for this building
        /// (eg can it be placed on a slope) are applied.
        /// </summary>
        /// <returns><c>true</c> if this building position is allowed otherwise, <c>false</c>.</returns>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        /// <param name="direction">Direction.</param>
        /// <param name="building">Building.</param>
        public bool IsBuildingPositionAllowed(int x, int y, int direction, SimpleBuildingType building)
        {
            int stopX = x + building.buildArea.width - 1;
            int stopY = y + building.buildArea.length - 1;

            if (x <= 0 || y <= 0 || stopX >= world.tileMapSize - 1 || stopY >= world.tileMapSize - 1)
            {
                return(false);
            }

            //If we turned the building -90 or 90 degrees then switch width and length
            bool isTurned = direction % 2 == 1;

            if (isTurned)
            {
                stopX = x + building.buildArea.length - 1;
                stopY = y + building.buildArea.width - 1;
            }

            for (int xT = x; xT <= stopX; xT++)
            {
                for (int yT = y; yT <= stopY; yT++)
                {
                    MapTile tile = new MapTile(xT, yT);
                    if (DoTileHaveBuilding(xT, yT))
                    {
                        return(false);
                    }

                    //Compare this tile type to the correct corresponding one on building

                    //First find local coordinates for building tiles
                    int xLoc;
                    int yLoc;
                    switch (direction)
                    {
                    case 0:
                        xLoc = xT - x;
                        yLoc = yT - y;
                        break;

                    case 1:
                        xLoc = yT - y;
                        yLoc = xT - x;
                        break;

                    case 2:
                        xLoc = building.buildArea.width - (xT - x) - 1;
                        yLoc = building.buildArea.length - (yT - y) - 1;
                        break;

                    case  3:
                        xLoc = building.buildArea.width - (yT - y) - 1;
                        yLoc = building.buildArea.length - (xT - x) - 1;
                        break;

                    default:
                        xLoc = -1;
                        yLoc = -1;
                        break;
                    }

                    //now that we have the local coordinates of the specified tile we can compare it to the building allowed tile for said coordinate
                    //Get the tile type
                    MapTile.MapTileType worldTileType = world.GetTileType(tile);
                    //Get the tile type
                    MapTile.MapTileType buildingTileType = building.buildArea.GetTileType(xLoc, yLoc);

                    //Check if this tile is ok
                    if (MapTile.IsTileOk(buildingTileType, worldTileType))
                    {
                        continue;
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            if (!world.IsAreaEmpty(x, y, stopX, stopY) && this.IsAreaEmpty(x, y, stopX, stopY))
            {
                Debug.Log("Area not empty");
                return(false);
            }

            /* Redundant now?
             * if(!world.IsAreaAboveWater(x, y, stopX, stopY)){
             *      Debug.Log("Area not above water");
             *      return false;
             * }*/

            return(true);
        }
Ejemplo n.º 9
0
        public void IsBuildingPositionAllowed(int x, int y, int direction, SimpleBuildingType building, out bool[,] tilesResults)
        {
            int stopX = x + building.buildArea.width - 1;
            int stopY = y + building.buildArea.length - 1;

            //If we turned the building -90 or 90 degrees then switch width and length
            bool isTurned = direction % 2 == 1;

            if (isTurned)
            {
                stopX        = x + building.buildArea.length - 1;
                stopY        = y + building.buildArea.width - 1;
                tilesResults = new bool[building.buildArea.length, building.buildArea.width];
            }
            else
            {
                tilesResults = new bool[building.buildArea.width, building.buildArea.length];
            }

            if (x <= 0 || y <= 0 || stopX >= world.tileMapSize - 1 || stopY >= world.tileMapSize - 1)
            {
                return;
            }

            for (int xT = x; xT <= stopX; xT++)
            {
                for (int yT = y; yT <= stopY; yT++)
                {
                    MapTile tile = new MapTile(xT, yT);
                    if (DoTileHaveBuilding(xT, yT))
                    {
                        continue;
                    }

                    //Compare this tile type to the correct corresponding one on building

                    //First find local coordinates for building tiles
                    int xLoc;
                    int yLoc;
                    switch (direction)
                    {
                    case 0:
                        xLoc = xT - x;
                        yLoc = yT - y;
                        break;

                    case 1:
                        xLoc = yT - y;
                        yLoc = xT - x;
                        break;

                    case 2:
                        xLoc = building.buildArea.width - (xT - x) - 1;
                        yLoc = building.buildArea.length - (yT - y) - 1;
                        break;

                    case  3:
                        xLoc = building.buildArea.width - (yT - y) - 1;
                        yLoc = building.buildArea.length - (xT - x) - 1;
                        break;

                    default:
                        xLoc = -1;
                        yLoc = -1;
                        break;
                    }

                    //now that we have the local coordinates of the specified tile we can compare it to the building allowed tile for said coordinate
                    //Get the tile type
                    MapTile.MapTileType worldTileType = world.GetTileType(tile);
                    //Get the tile type
                    MapTile.MapTileType buildingTileType = building.buildArea.GetTileType(xLoc, yLoc);

                    //Check if this tile is ok
                    if (MapTile.IsTileOk(buildingTileType, worldTileType) && world.IsTileEmpty(tile))
                    {
                        tilesResults[xT - x, yT - y] = true;
                    }
                    else
                    {
                        tilesResults[xT - x, yT - y] = false;
                    }
                }
            }
        }