Пример #1
0
    // Update is called once per frame
    void Update()
    {
        //Check for game over.
        if (Vector3.Distance(transform.position, player.transform.position) < 1.5f && !(stunTime > 0.0f))
        {
            //Debug.Log("PLAYER CAUGHT");
            player.GetComponent <RobotController>().GameOver();
            return;
        }

        //Be stunned.
        if (stunTime > 0.0f)
        {
            // handle stun
            stunTime -= Time.deltaTime;
            if (stunTime <= 0.0f)
            {
                //TODO: recover from stun animation
                transform.rotation = Quaternion.identity;
                transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
            }
            return;
        }
        //else transform.rotation = Quaternion.Euler();

        //Check for player detection.
        if (Sense.player(gameObject, player))
        {
            state = "ChasePlayer";
            //Debug.Log ("Hunting Player!");
            rotation = 4 * baseRotation;
        }
        else
        {
            rotation = baseRotation;
        }

        //Reset the StepsInRoom counter if you go from reset node to reset node.
        if (targetNode != null && lastNode != null)
        {
            if (targetNode.GetComponent <NodeScript>().canReset&& lastNode.GetComponent <NodeScript>().canReset)
            {
                stepsInRoom = 0;
            }
        }

        //Main Switch Control
        switch (state)
        {
        case ("FindNode"):
            targetNode = Utility.selectNode(gameObject, nodeDistance, stepsInRoom, lastNode);
            state      = "MoveToNode";
            stepsInRoom++;
            break;

        case ("MoveToNode"):
            if (targetNode == null)
            {
                state = "FindNode";
            }
            else
            {
                guardController.Move(Vector3.Normalize(targetNode.transform.position - transform.position) * Time.deltaTime * moveSpeed);

                Quaternion newRotation = Quaternion.LookRotation(targetNode.transform.position - transform.position);
                newRotation.x      = 0f;
                newRotation.z      = 0f;
                transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * rotation);
            }
            break;

        case ("ChasePlayer"):
            audio.PlayOneShot(alarmSound, OptionsMenu.sfx * 0.2f);
            if (!Sense.player(gameObject, player))
            {
                state = "FindNode";
                audio.Stop();
                audio.PlayOneShot(hoverSound, OptionsMenu.sfx);


                //Debug.Log ("Lost sight of Player.");
            }
            else
            {
                if (!player.GetComponent <RobotController>().isWin)
                {
                    // don't move if game is already won
                    guardController.Move(Vector3.Normalize(player.transform.position - transform.position) * Time.deltaTime * moveSpeed * 1.5f);
                }

                Quaternion newRotation = Quaternion.LookRotation(player.transform.position - transform.position);
                newRotation.x      = 0f;
                newRotation.z      = 0f;
                transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * rotation);
            }
            break;
        }
    }