Exemplo n.º 1
0
    void handleInput()
    {
        if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
        {
            if (!isMouseOverUI())
            {
                if (tooltip.enabled)
                {
                    tooltip.Deactivate();
                }
            }

            RaycastHit hit2;
            //Layer masks
            LayerMask layerMask    = (1 << 8) | (1 << 11);
            LayerMask layerMaskNpc = (1 << 9) | (1 << 10);
            Ray       ray          = new Ray();
            //for unity editor
#if UNITY_EDITOR || UNITY_STANDALONE
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //for touch device
#elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
            ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
#endif

            /*
             * Prioritize raycast hit on npc, so that when npc is sitting
             * or sleeping you hit the npc instead of the object they are on
             */
            RaycastHit[] rays;
            rays = Physics.RaycastAll(ray, 10000.0f, layerMaskNpc);
            List <RaycastHit> hits = new List <RaycastHit>();
            bool npcwashit         = false;
            movingToTarget = false;
            if (rays.Length > 0)
            {
                if (!isMouseOverUI())
                {
                    anim.StopAll();
                    pickingup = false;
                    foreach (RaycastHit hit in rays)
                    {
                        //object who booked the object.
                        GameObject temp = objManager.isObjectBooked(hit.transform.gameObject);
                        if (hit.transform.tag != "NPC" && temp == null)
                        {
                            hits.Add(hit);
                            continue;
                        }
                        else
                        {
                            npcwashit = true;
                            if ((target == hit.transform.gameObject) || (temp != null && target == temp))
                            {
                                nextDestination = new Vector3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
                                moveTo(nextDestination);
                                followNpc = true;
                            }
                            else
                            {
                                disableTarget();
                                if (temp != null && temp != gameObject)
                                {
                                    target = temp;
                                }
                                else
                                {
                                    target = hit.transform.gameObject;
                                }
                                interaction.setTarget(target);
                                outlineGameObjectRecursive(target.transform, Shader.Find("Outlined/Silhouetted Diffuse"));
                            }
                            if (sitting)
                            {
                                sitting = false;
                                objManager.unbookObject(interaction.getCurrentChair());
                            }
                            if (sleeping)
                            {
                                sleeping = false;
                                objManager.unbookObject(interaction.getBed());
                            }
                        }
                    }
                    //if ther was now npc's hit, take the first hit object
                    if (!npcwashit)
                    {
                        hit2 = hits[0];
                        if (target == hit2.transform.gameObject)
                        {
                            if (sitting)
                            {
                                sitting = false;
                                objManager.unbookObject(interaction.getCurrentChair());
                            }
                            if (sleeping)
                            {
                                sleeping = false;
                                objManager.unbookObject(interaction.getBed());
                            }

                            if (target.tag == "Chair" || target.tag == "QueueChair" || target.tag == "Chair2")
                            {
                                if (objManager.bookTargetObject(target, gameObject))
                                {
                                    interaction.setCurrentChair(interaction.getTarget());
                                    if (target.tag == "Chair2")
                                    {
                                        nextDestination = interaction.getDestToTargetObjectSide(1, 16.0f);
                                        moveTo(nextDestination);
                                    }
                                    else
                                    {
                                        nextDestination = interaction.getDestToTargetObjectSide(0, 16.0f);
                                        moveTo(nextDestination);
                                    }
                                    sitting = true;
                                    disableMoveIndicator();
                                }
                            }
                            else if (target.tag == "Bed")
                            {
                                if (objManager.bookTargetObject(target, gameObject))
                                {
                                    interaction.setBookedBed(interaction.getTarget());
                                    nextDestination = interaction.getDestToTargetObjectSide(1, 16.0f);
                                    moveTo(nextDestination);
                                    sleeping = true;
                                    disableMoveIndicator();
                                }
                            }
                            else if (target.tag == "PickupItemFloor" || target.tag == "PickupItem")
                            {
                                interaction.setTarget(target);
                                pickingup       = true;
                                nextDestination = target.transform.position;
                                moveTo(nextDestination);
                                disableMoveIndicator();
                            }
                            else if (target.tag == "MedCabinet" || target.tag == "Computer" || target.tag == "TrashCan" || target.tag == "CoffeeMachine")
                            {
                                interaction.setTarget(target);
                                nextDestination = target.transform.position;
                                moveTo(nextDestination);
                                disableMoveIndicator();
                                movingToTarget = true;
                            }
                        }
                        else
                        {
                            if (sitting || interaction.getCurrentChair() != null)
                            {
                                sitting = false;
                                objManager.unbookObject(interaction.getCurrentChair());
                            }
                            if (sleeping || interaction.getBed() != null)
                            {
                                sleeping = false;
                                objManager.unbookObject(interaction.getBed());
                            }
                            /*Disable target*/
                            disableTarget();
                            GameObject temp = objManager.isObjectBooked(hit2.transform.gameObject);
                            if (temp != null)
                            {
                                target = temp;
                            }
                            else
                            {
                                //set new target
                                target = hit2.transform.gameObject;
                            }
                            interaction.setTarget(target);
                            //outline the object
                            outlineOnlyParent(target.transform, Shader.Find("Outlined/Silhouetted Diffuse"));
                        }
                    }
                }
            }
            //check if the ray hits floor collider
            else if (Physics.Raycast(ray, out hit2, 10000.0f, layerMask))
            {
                if (!isMouseOverUI())
                {
                    anim.StopAll();
                    pickingup = false;
                    if (interaction.getCurrentChair() != null)
                    {
                        objManager.unbookObject(interaction.getCurrentChair());
                    }
                    if (interaction.getBed() != null)
                    {
                        objManager.unbookObject(interaction.getBed());
                    }
                    //get position of hit and move there
                    Vector3 pos = new Vector3(hit2.point.x, 0, hit2.point.z);
                    enableMoveIndicator(pos);
                    nextDestination = pos;
                    moveTo(nextDestination);
                    if (sitting == true)
                    {
                        sitting = false;
                        objManager.unbookObject(target);
                    }
                    if (sleeping == true)
                    {
                        sleeping = false;
                        objManager.unbookObject(target);
                    }
                    if (followNpc)
                    {
                        followNpc = false;
                    }
                }
            }
            //check if the ray hits targetableobjects collider
        }
    }