Exemplo n.º 1
0
    private void Build(string name) // only to be invoked by BuildObject()
    {
        if (!placingObject && objMap[name] != null)
        {
            currPlacingObject      = Instantiate(objMap[name]);
            currPlacingObject.name = name;
            CommonProperties common = currPlacingObject.GetComponent <CommonProperties>();
            common.SetTransparency(0.2f, BlendMode.Transparent);
            common.SetColliderEnabled(false);
            common.InitMats();

            if (debugCubeEnabled)
            {
                int diff = debugCubeCount - common.GetFootprint().Count;
                if (diff > 0)
                {
                    for (int i = 0; i < diff; i++)
                    {
                        GameObject g = debugCubeContainer.transform.GetChild(0).gameObject;
                        g.SetActive(false);
                        Destroy(g);
                        debugCubeCount--;
                    }
                }
                else if (diff < 0)
                {
                    for (int i = 0; i < -diff; i++)
                    {
                        GameObject g = Instantiate(debugCube, Vector3.zero, Quaternion.identity, debugCubeContainer.transform);
                        debugCubeCount++;
                    }
                }
            }


            placingObject = true;
        }
    }
Exemplo n.º 2
0
    public void OnLMB(InputAction.CallbackContext context)
    {
        LMB = context.ReadValue <float>() == 1;
        if (snapped && LMB && context.started && placingObject && !(world.GridSpaceExists(placingObjPos)))
        {
            if (currPlacingObject.GetComponents <IActivatable>().Length != 0)
            {
                currPlacingObject.GetComponents <IActivatable>()[0].Activate();
            }

            placingObject                      = false;
            currPlacingObject.layer            = 7;
            currPlacingObject.transform.parent = blockContainer.transform;

            CommonProperties common = currPlacingObject.GetComponent <CommonProperties>();

            foreach (Vector3 relPos in AdjustFootprint(common.GetFootprint(), common.GetFacing())) //get adjusted footprint of placing object and add world positions to worldGrid
            {
                world.AddSpace(new GridSpace(placingObjPos + 10 * relPos, currPlacingObject));
            }

            common.SetTransparency(1f, BlendMode.Opaque);
            common.ResetTintColor();
            common.SetColliderEnabled(true);

            if (streakPlaceName == "")
            {
                currPlacingObject = null;
            }
            else
            {
                Direction dir = common.GetFacing();
                currPlacingObject = null;
                BuildObject(streakPlaceName, true, dir);
            }
        }
    }
Exemplo n.º 3
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;
                    }
                }
            }
        }
    }