Пример #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);
            }
        }
    }
Пример #2
0
 private Vector3?GetPositionFromMouseClick()
 {
     if (RaycastHandler.Update())
     {
         if (ExtendLayerMask.NotGroundMask(RaycastHandler.HitInfo.transform.gameObject.layer))
         {
             return(RaycastHandler.HitInfo.point);
         }
     }
     return(null);
 }
Пример #3
0
 private bool setPositionFromMouseClick()
 {
     //If the ray hits an object
     if (RaycastHandler.Update())
     {
         if (ExtendLayerMask.NotGroundMask(RaycastHandler.HitInfo.transform.gameObject.layer))
         {
             targetPosition = RaycastHandler.HitInfo.point;
             return(true);
         }
     }
     return(false);
 }
Пример #4
0
 private bool IsPickable()
 {
     if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo))
     {
         if (hitInfo.transform.gameObject.layer == ExtendLayerMask.UI())
         {
             return(false);
         }
         if (hitInfo.transform.gameObject == gameObject)
         {
             return(Physics.Raycast(hitInfo.point, Vector3.down, out hitInfo, 100.0f));
         }
         return(onHold);
     }
     return(false);
 }
Пример #5
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);
    }
Пример #6
0
    private static int selectedOwner = 0;             //static since it is not updated over network and can be controlled locally.

    //private GameObject relatedPlayerConnectionObject;
    private void Awake()
    {
        ExtendLayerMask.Update();
    }
Пример #7
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;
                    }
                }
            }
        }
    }