Ejemplo n.º 1
0
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject == heldObj && holdingObj)
        {
            return;
        }

        if (selectableLayer == (selectableLayer.value | 1 << collision.gameObject.layer))
        {
            if (numberOfCollisionOld == 0)
            {
                return;
            }

            //highlights only the most recent object
            currentHighlightedObj = collision.gameObject;
            if (highlight != null)
            {
                highlightOld = highlight;
                highlightOld.SetAlternateSprite(false);
            }
            highlight = currentHighlightedObj.GetComponent <Highlightable>();
            if (highlight.highlightable)
            {
                highlight.SetAlternateSprite(true);
            }
            else
            {
                highlight             = null;
                currentHighlightedObj = null;
            }
        }
    }
Ejemplo n.º 2
0
    private void OnCollisionStay(Collision collision)
    {
        if (collision.gameObject == heldObj && holdingObj)
        {
            return;
        }

        if (selectableLayer == (selectableLayer.value | 1 << collision.gameObject.layer))
        {
            ++numberOfCollisions;

            if (numberOfCollisions > 1) //checks the y pos, of the objects. then it only highlights the lowest one.
            {
                secondCollYPos = collision.gameObject.transform.position.y;
                if (highlightOld != null)
                {
                    highlightOld.SetAlternateSprite(false);
                }
                if (highlight != null)
                {
                    highlightOld = highlight;
                    highlightOld.SetAlternateSprite(false);
                }

                if (secondCollYPos < firstCollYPos)
                {
                    currentHighlightedObj = collision.gameObject;
                    highlight             = currentHighlightedObj.GetComponent <Highlightable>();
                    if (highlight.highlightable)
                    {
                        highlight.SetAlternateSprite(true);
                    }
                    else
                    {
                        highlight             = null;
                        currentHighlightedObj = null;
                    }
                }
                else
                {
                    currentHighlightedObj = firstCollision;
                    highlight             = currentHighlightedObj.GetComponent <Highlightable>();
                    if (highlight.highlightable)
                    {
                        highlight.SetAlternateSprite(true);
                    }
                    else
                    {
                        highlight             = null;
                        currentHighlightedObj = null;
                    }
                }

                return;
            }
            else
            {
                firstCollYPos  = collision.gameObject.transform.position.y;
                firstCollision = collision.gameObject;
                if (numberOfCollisionOld > 1 || numberOfCollisionOld == 0)
                {
                    return;
                }
            }


            if (collision.gameObject != currentHighlightedObj)
            {
                //highlights only the most recent object
                currentHighlightedObj = collision.gameObject;
                if (highlight != null)
                {
                    highlightOld = highlight;
                    highlightOld.SetAlternateSprite(false);
                }
                highlight = currentHighlightedObj.GetComponent <Highlightable>();
                if (highlight.highlightable)
                {
                    highlight.SetAlternateSprite(true);
                }
                else
                {
                    highlight             = null;
                    currentHighlightedObj = null;
                }
            }
        }
    }
Ejemplo n.º 3
0
    void HandledInteract()
    {
        if (((controller.playerOne && Input.GetAxisRaw("InteractOne") == 1) || (!controller.playerOne && Input.GetAxisRaw("InteractTwo") == 1)) && !performedInteraction)
        {
            if (currentHighlightedObj != null)
            {
                InteractionType type = highlight.interactionType;
                switch (type)
                {
                case InteractionType.PickUp:
                    if (heldObj == null)
                    {
                        performedInteraction = true;
                        holdingObj           = true;
                        heldObj                         = currentHighlightedObj;
                        heldObjHighlight                = highlight;
                        heldObj.transform.parent        = transform;
                        heldObj.transform.localPosition = handsTransform.localPosition;
                        currentHighlightedObj           = null;
                        highlight.SetAlternateSprite(false);
                    }
                    break;

                case InteractionType.Activate:
                    //actions possible if you are holding or not holding a part



                    if (heldObj != null)  //actions only possible while holding parts
                    {
                        performedInteraction = true;
                        //checks to see if you can use a part to repair
                        RecieveParts partsReciver;
                        currentHighlightedObj.TryGetComponent(out partsReciver);
                        if (partsReciver != null)
                        {
                            if (partsReciver.RecivePart(heldObjHighlight.partID))
                            {
                                performedInteraction         = true;
                                holdingObj                   = false;
                                controller.controller.center = new Vector3(0, 0, 0);
                                Destroy(heldObj);

                                if (highlight != null)
                                {
                                    highlight.SetAlternateSprite(false);
                                    currentHighlightedObj = null;
                                    highlight             = null;
                                }
                                heldObjHighlight = null;
                                heldObj          = null;
                                return;
                            }
                        }
                        //checks to see if you can activate a light breaker
                        ChangeLight changeLight;
                        currentHighlightedObj.TryGetComponent(out changeLight);
                        if (changeLight)
                        {
                            performedInteraction = true;
                            changeLight.SwitchLights(transform.position);
                            return;
                        }
                        //checks to see if you can pass a part
                        PassPart partsPasser;
                        currentHighlightedObj.TryGetComponent(out partsPasser);
                        if (partsPasser != null)
                        {
                            if (partsPasser.TransferPart(heldObj, transform.position))
                            {
                                performedInteraction         = true;
                                holdingObj                   = false;
                                controller.controller.center = new Vector3(0, 0, 0);
                                heldObj.transform.parent     = null;
                                heldObjHighlight             = null;
                                heldObj = null;
                                return;
                            }
                        }
                    }
                    else    //actions only possible while not holding parts
                    {
                        //checks to see if you can activate a light breaker
                        ChangeLight changeLight;
                        currentHighlightedObj.TryGetComponent(out changeLight);
                        if (changeLight)
                        {
                            performedInteraction = true;
                            changeLight.SwitchLights(transform.position);
                        }
                    }
                    break;


                default:
                    Debug.Log("error for " + currentHighlightedObj.name);
                    break;
                }
            }

            if (currentHighlightedObj == null && !performedInteraction)//put down an object
            {
                if (heldObj != null)
                {
                    performedInteraction         = true;
                    holdingObj                   = false;
                    controller.controller.center = new Vector3(0, 0, 0);
                    heldObj.transform.parent     = null;
                    heldObjHighlight             = null;
                    heldObj = null;
                }
            }
        }

        if (((controller.playerOne && Input.GetAxisRaw("InteractOne") == 0) || (!controller.playerOne && Input.GetAxis("InteractTwo") == 0)) && performedInteraction)
        {
            performedInteraction = false;
        }
    }