コード例 #1
0
    private void UpdateBlock(Vector3 position, bool currPlacingAtPosition, bool ignoreCurrPlacing)
    {
        if (ignoreCurrPlacing)
        {
            currPlacingAtPosition = false;
        }

        if (world.GridSpaceExists(position) || currPlacingAtPosition)
        {
            Dictionary <string, GridSpace> adjBlocks = world.FindAdjacentOccupied(position);

            GridSpace  targetSpace = world.FindNearestGridSpace(position); // would be null if currplacingatposition
            GameObject targetBlock;

            if (currPlacingAtPosition)
            {
                targetBlock = currPlacingObject;
            }
            else
            {
                targetBlock = targetSpace.GetObject();
            }

            if (targetBlock.tag == "convVariant")
            {
                GameObject northBlock = adjBlocks.ContainsKey("north") ? adjBlocks["north"].GetObject() : null;
                GameObject eastBlock  = adjBlocks.ContainsKey("east") ? adjBlocks["east"].GetObject() : null;
                GameObject southBlock = adjBlocks.ContainsKey("south") ? adjBlocks["south"].GetObject() : null;
                GameObject westBlock  = adjBlocks.ContainsKey("west") ? adjBlocks["west"].GetObject() : null;

                if (!currPlacingAtPosition && !ignoreCurrPlacing)
                {
                    Vector3 relative = position - placingObjPos;
                    if (relative.x == -10)
                    {
                        eastBlock = currPlacingObject;
                    }
                    else if (relative.x == 10)
                    {
                        westBlock = currPlacingObject;
                    }

                    if (relative.z == -10)
                    {
                        northBlock = currPlacingObject;
                    }
                    else if (relative.z == 10)
                    {
                        southBlock = currPlacingObject;
                    }
                }

                bool northExists = northBlock != null && northBlock.tag == "convVariant";
                bool eastExists  = eastBlock != null && eastBlock.tag == "convVariant";
                bool southExists = southBlock != null && southBlock.tag == "convVariant";
                bool westExists  = westBlock != null && westBlock.tag == "convVariant";

                bool north = northExists && northBlock.GetComponent <CommonProperties>().GetFacing() == Direction.SOUTH;
                bool east  = eastExists && eastBlock.GetComponent <CommonProperties>().GetFacing() == Direction.WEST;
                bool south = southExists && southBlock.GetComponent <CommonProperties>().GetFacing() == Direction.NORTH;
                bool west  = westExists && westBlock.GetComponent <CommonProperties>().GetFacing() == Direction.EAST;

                string prefabName = Utils.GetDirection(targetBlock.GetComponent <CommonProperties>().GetFacing(), north, east, south, west);

                if (targetBlock.name != prefabName)
                {
                    Direction objDir   = targetBlock.GetComponent <CommonProperties>().GetFacing();
                    int       objLayer = targetBlock.layer;

                    if (currPlacingAtPosition)
                    {
                        Destroy(currPlacingObject);
                        currPlacingObject = Instantiate(objMap[prefabName], new Vector3(0, 0, 100), Quaternion.Euler(0, 180, 0));
                        currPlacingObject.GetComponent <CommonProperties>().SetDirection(objDir);
                        currPlacingObject.layer = objLayer;
                        currPlacingObject.name  = prefabName;
                        CommonProperties common = currPlacingObject.GetComponent <CommonProperties>();
                        common.SetTransparency(0.2f, BlendMode.Transparent);
                        common.SetColliderEnabled(false);
                        common.InitMats();
                    }
                    else
                    {
                        world.DeleteSpace(targetSpace);
                        GameObject replObj = Instantiate(objMap[prefabName]);
                        replObj.transform.position = position;
                        replObj.name = prefabName;

                        GridSpace replacement = new GridSpace(position, replObj);
                        world.AddSpace(replacement);
                        replacement.GetObject().GetComponent <CommonProperties>().SetDirection(objDir);
                        replacement.GetObject().layer = objLayer;
                    }
                }
            }
        }
    }