コード例 #1
0
    // Instantiate a new building at the same place as the hover building and disable it
    public static void PlaceBuilding()
    {
        // Check if we have enough coins
        //if (myVillage.GetCoins() < placingBuilding.GetCost()) canBuild = false;

        int gx = Instance.rotation % 2 == 0 ? placingBuilding.gridWidth : placingBuilding.gridHeight;
        int gy = Instance.rotation % 2 == 1 ? placingBuilding.gridWidth : placingBuilding.gridHeight;

        if (CanBuild())
        {
            // Disable old occupation temporary
            for (int dx = 0; dx < gx; dx++)
            {
                for (int dy = 0; dy < gy; dy++)
                {
                    if (!Grid.ValidNode(Instance.hoverGridX + dx, Instance.hoverGridY + dy))
                    {
                        continue;
                    }
                    Node checkNode = Grid.GetNode(Instance.hoverGridX + dx, Instance.hoverGridY + dy);
                    checkNode.SetTempOccupied(false, false);
                }
            }

            BuildingScript mb = Instance.movingBuilding;
            if (mb) // moving an existing building
            {
                int oldGx = mb.RotWidth();
                int oldGy = mb.RotHeight();
                for (int dx = 0; dx < oldGx; dx++)
                {
                    for (int dy = 0; dy < oldGy; dy++)
                    {
                        if (!Grid.ValidNode(mb.GridX + dx, mb.GridY + dy))
                        {
                            continue;
                        }
                        Node n = Grid.GetNode(mb.GridX + dx, mb.GridY + dy);
                        n.SetNodeObject(null);
                        if (!mb.Walkable)
                        {
                            n.objectWalkable = true;
                        }
                    }
                }

                mb.RemoveTerrainGround();

                mb.transform.position = Instance.hoverBuilding.position;
                mb.transform.rotation = Instance.hoverBuilding.rotation;
                mb.SetPosRot(Instance.hoverGridX, Instance.hoverGridY, Instance.rotation);

                mb.MoveBuilding();

                for (int dx = 0; dx < gx; dx++)
                {
                    for (int dy = 0; dy < gy; dy++)
                    {
                        if (!Grid.ValidNode(Instance.hoverGridX + dx, Instance.hoverGridY + dy))
                        {
                            continue;
                        }
                        Node n = Grid.GetNode(Instance.hoverGridX + dx, Instance.hoverGridY + dy);
                        n.SetNodeObject(mb.transform);
                        if (!mb.Walkable)
                        {
                            n.objectWalkable = false;
                        }
                    }
                }
            }
            else
            {
                GameBuilding toSpawn = new GameBuilding(placingBuilding, Instance.hoverGridX, Instance.hoverGridY, Instance.rotation);
                if (toSpawn.building.stayInRangeOfParent)
                {
                    toSpawn.parentBuildingNr = UIManager.Instance.GetSelectedBuilding().Nr;
                }
                toSpawn.SetPosition(Instance.hoverBuilding.position);
                toSpawn.SetRotation(Instance.hoverBuilding.rotation);
                toSpawn.blueprint = true;
                /*BuildingScript bs = */ SpawnBuilding(toSpawn);
            }

            // Take cost for coins
            //myVillage.Purchase(b);

            // If shift is pressed, don't exit the placing mode
            if (mb || !Input.GetKey(KeyCode.LeftShift) || !placingBuilding.multipleBuildings)
            {
                EndPlacing();
            }

            InputManager.LeftClickHandled = true;
        }
    }