Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0) || (onHold && !Input.GetMouseButtonUp(0)))
        {
            if (IsPickable())
            {
                if (!onHold) // first lift, clean the grid at objects location
                {
                    int tmpMask = gameObject.layer;
                    gameObject.layer = ExtendLayerMask.GhostMask();
                    grid.UpdateGrid(gameObject);
                    gameObject.layer    = tmpMask;
                    zOffset             = (transform.position - hitInfo.transform.position).y;
                    onHold              = true;
                    gameObject.isStatic = false;
                }
                PickupObject(Vector3.up * liftHeight);
            }
        }
        else if (onHold) // Object is no longer under control, put it down
        {
            if (IsPickable())
            {
                PickupObject(Vector3.up * zOffset);
                onHold = false;

                gameObject.isStatic = true;
                zOffset             = 0;

                grid.UpdateGrid(gameObject);
            }
        }
    }
Example #2
0
    private bool CheckIfNodeIsCollidingWithObject(ref bool isItself, Node n, GameObject gameObject)
    {
        LayerMask mask = unWalkableMask | 1 << gameObject.layer;

        RaycastHit[] hitInfos = Physics.SphereCastAll(n.worldPosition, safeRadius,
                                                      Vector3.up, safeRadius, mask);

        foreach (RaycastHit hitInfo in hitInfos)
        {
            if (hitInfo.collider.gameObject == gameObject)
            {
                isItself = true;
                // if there was only a collision with Ghost, it is not a collision
                if (gameObject.layer == ExtendLayerMask.GhostMask())
                {
                    return(hitInfos.Length > 1);
                }
            }
        }
        return(hitInfos.Length > 0);
    }
Example #3
0
    public void UpdateGrid(GameObject movedObject)
    {
        //Do not need to update grid if the object is walkable
        if ((1 << movedObject.layer & unWalkableMask) == 0 &&
            movedObject.layer != ExtendLayerMask.GhostMask()
            )
        {
            return;
        }
        gridUpdated = true;

        Node n = NodeFromWorldPoint(movedObject.transform.position);
        int  Xmin = n.gridX + 1;
        int  Xmax = n.gridX;
        int  Ymin = n.gridY;
        int  Ymax = n.gridY;
        int  x, y;

        bool isItSelf = false;
        bool nNonWalkable;
        int  left   = 0;
        int  right  = 0;
        int  top    = 0;
        int  bottom = 0;

        if (runCase != 0)
        {
            right  = treshhold;
            top    = treshhold;
            bottom = treshhold;
            left   = treshhold;
        }
        if ((runCase & 1) > 0)
        {
            right = 0;
        }
        if ((runCase & 2) > 0)
        {
            left = 0;
        }
        if ((runCase & 4) > 0)
        {
            top = 0;
        }
        if ((runCase & 8) > 0)
        {
            bottom = 0;
        }

        while (left < treshhold || right < treshhold || top < treshhold || bottom < treshhold)
        {
            //decrese Y, travers down on left side
            if (left < treshhold)
            {
                if (0 == Xmin)
                {
                    left = treshhold;
                    x    = Xmin;
                }
                else
                {
                    x = --Xmin;
                }
                isItSelf = false;
                for (y = Ymin; y <= Ymax; y++)
                {
                    nNonWalkable        = CheckIfNodeIsCollidingWithObject(ref isItSelf, grid[x, y], movedObject);
                    grid[x, y].walkable = !nNonWalkable;
                }
                if (!isItSelf)
                {
                    left++;
                }
                else
                {
                    left = 0;
                }
            }

            //increase X, travers left on bottom side
            if (bottom < treshhold)
            {
                if (0 == Ymin)
                {
                    top = treshhold;
                    y   = Ymin;
                }
                else
                {
                    y = --Ymin;
                }

                isItSelf = false;
                for (x = Xmin; x <= Xmax; x++)
                {
                    nNonWalkable        = CheckIfNodeIsCollidingWithObject(ref isItSelf, grid[x, y], movedObject);
                    grid[x, y].walkable = !nNonWalkable;
                }
                if (!isItSelf)
                {
                    bottom++;
                }
                else
                {
                    bottom = 0;
                }
            }

            //increase Y, travers up on right side
            if (right < treshhold)
            {
                if (gridSizeX == Xmax)
                {
                    right = treshhold;
                }
                else
                {
                    x = ++Xmax;

                    isItSelf = false;
                    for (y = Ymin; y <= Ymax; y++)
                    {
                        nNonWalkable        = CheckIfNodeIsCollidingWithObject(ref isItSelf, grid[x, y], movedObject);
                        grid[x, y].walkable = !nNonWalkable;
                    }
                    if (!isItSelf)
                    {
                        right++;
                    }
                    else
                    {
                        right = 0;
                    }
                }
            }

            //decrese X, travers left on top side
            if (top < treshhold)
            {
                if (gridSizeY == Ymax)
                {
                    top = treshhold;
                }
                else
                {
                    y        = ++Ymax;
                    isItSelf = false;
                    for (x = Xmin; x <= Xmax; x++)
                    {
                        nNonWalkable        = CheckIfNodeIsCollidingWithObject(ref isItSelf, grid[x, y], movedObject);
                        grid[x, y].walkable = !nNonWalkable;
                    }
                    if (!isItSelf)
                    {
                        top++;
                    }
                    else
                    {
                        top = 0;
                    }
                }
            }
        }
    }