public void STATE_Update(CivillianController agent, StateMachine_CIV stateMachine, float deltaTime)
    {
        if (!stopped)
        {
            currentAgent.navAgent.SetDestination(itemPos);
        }

        float stoppingdist = 2.0f;

        if (stoppingdist >= Vector3.Distance(currentAgent.transform.position, currentAgent.target.transform.position) && !stopped)
        {
            stopped = true;
            currentAgent.m_Animator.SetBool("idle", true);
            currentAgent.navAgent.isStopped = true;

            //Create nav obstacle
            if (currentAgent.GetComponent <NavMeshObstacle>() == null)
            {
                NavMeshObstacle obstacle = currentAgent.gameObject.AddComponent <NavMeshObstacle>();
                obstacle.shape   = NavMeshObstacleShape.Capsule;
                obstacle.radius  = 0.3f;
                obstacle.center  = new Vector3(0, 1, 0);
                obstacle.carving = true;
            }
            else //Obstacle has already been added first time around so enable from here on out
            {
                currentAgent.GetComponent <NavMeshObstacle>().enabled = true;
            }

            currentAgent.navAgent.enabled = false;
        }
    }
Example #2
0
    public void STATE_Update(CivillianController agent, StateMachine_CIV stateMachine, float deltaTime)
    {
        //Check if we reached the scare threshold other wise continue with normal retreat state
        RunScaredExit();

        if (agent.hasDroppedEcto == false)
        {
            agent.hasDroppedEcto = true;
            Vector3 dropPos = new Vector3(currentAgent.transform.position.x, currentAgent.transform.position.y + 1f, currentAgent.transform.position.z);
            GameObject.Instantiate(ectoplasm, dropPos, currentAgent.transform.rotation);
        }

        //This is the code that makes the agent run away from whatever target has been set
        if (scared == false)
        {
            Debug.Log("Scared");
            //Get the direction away from the target object that they are trying to flee from
            if (currentAgent.TRIGGERED_hit)
            {
                dirAwayFromObject = currentAgent.transform.position - currentAgent.collidedItemPos;
            }
            else
            {
                dirAwayFromObject = currentAgent.transform.position - currentAgent.target.transform.position;
            }

            //Just minus a default value for now from the scared score
            //Overtime minus 1 from the scared value - possibly change in the future
            if (currentAgent.currentScareValue <= 0)
            {
                currentAgent.currentScareValue = 0;
            }
            else
            {
                scareDecreaseTimer -= Time.deltaTime;

                if (scareDecreaseTimer <= 0f)
                {
                    currentAgent.currentScareValue = currentAgent.currentScareValue - 1;
                    scareDecreaseTimer             = 3.0f;
                }
            }
            //Update the debug text
            currentAgent.txtScaredValue.text = currentAgent.currentScareValue.ToString();

            //If will is not close to the agent then don't flee
            if (dirAwayFromObject.magnitude > scaredRadius)
            {
                currentAgent.navAgent.velocity = Vector3.zero; //Stop the agent from moving

                currentAgent.m_Animator.SetBool("idle", true);

                //Debug.Log(timer);
                timer -= Time.deltaTime; //minus the time
                if (timer <= 0f)
                {
                    currentAgent.m_Animator.SetBool("idle", false);
                    stateMachine.ChangeState(agent, new CIV_Wander()); //Change back to wander
                    timer = 3.0f;                                      //Reset timer
                }
            }
            else //The player has moved close to the agent so move the agent
            {
                timer = 3.0f; //Reset the timer
                currentAgent.m_Animator.SetBool("idle", false);

                if (agent.navAgent.hasPath == false)
                {
                    agent.navAgent.SetDestination(RandomNavSphere(currentAgent.transform.position, 10, -1));
                }
            } //End else
        }     //End Scared if

        //Hack to turn back on the agent because we lost the reference of the otherAgent because they left the scene
        if (currentAgent.isStationary)
        {
            Debug.Log("stationay");
            currentAgent.GetComponent <NavMeshObstacle>().enabled = true;
            currentAgent.m_Animator.SetBool("idle", false);
            currentAgent.navAgent.enabled = true;
            currentAgent.isStationary     = false;
        }
    } //End update
    void Repel()//Written by Jak
    {
        //Change UI
        GameManager.Instance.EnableHideScaryLure(false);
        GameManager.Instance.EnableHideScary(true);

        //Destroy the lureEffect incase it hasnt already.
        if (lureEffect != null)
        {
            Destroy(lureEffect);
        }

        //Spawn scareEffect
        scareEffect = Instantiate(GameObject.Find("PrefabController").GetComponent <PrefabController>().scareEffect, this.gameObject.transform);
        scareEffect.transform.localPosition = new Vector3(0, 0, 0);

        //Play scare animation
        GetComponent <ItemController>().SetAnimScare(true);

        //Sound Effect ----------------------------------
        FMOD.Studio.PLAYBACK_STATE stateLure;
        FMOD.Studio.PLAYBACK_STATE stateScare;
        lureSound.getPlaybackState(out stateLure); //Poll the audio events to see if playback is happening
        scareSound.getPlaybackState(out stateScare);

        //Check if any audio is still playing and stop it to prevent overlap - Then play the required clip
        if (stateLure == FMOD.Studio.PLAYBACK_STATE.PLAYING || stateScare == FMOD.Studio.PLAYBACK_STATE.PLAYING)
        {
            lureSound.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
            scareSound.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);

            scareSound.start();                                                                                                      // Starts the event
            FMODUnity.RuntimeManager.AttachInstanceToGameObject(scareSound, GetComponent <Transform>(), GetComponent <Rigidbody>()); //Setup the 3D audio attributes
        }
        else //If none is playing start immediatly
        {
            scareSound.start(); // Starts the event
            FMODUnity.RuntimeManager.AttachInstanceToGameObject(scareSound, GetComponent <Transform>(), GetComponent <Rigidbody>());
        }
        //End Sound Effect ----------------------------

        //Get all colliders
        //#OPTIMISE
        Collider[] civillians = Physics.OverlapSphere(transform.position, lureRange); //Refactor this so it finds tags first instead of all colliders

        foreach (Collider civ in civillians)
        {
            if (civ.tag == "Civillian")
            {
                //I change the target gameobject in the civillians, so we can easily access the ItemScaryRating
                //The target is also used in CIV_Retreat to know which item to run away from
                CivillianController civillian = civ.GetComponent <CivillianController>();
                if (civillian.currentState != State.State_Retreat) //Only LURE the CIVS if they arent already in a retreat state / Prevents spam
                {
                    civillian.target          = gameObject;
                    civillian.TRIGGERED_repel = true;

                    if (civillian.GetComponent <NavMeshObstacle>() == null)
                    {
                        continue;
                    }
                    else
                    {
                        civillian.GetComponent <NavMeshObstacle>().enabled = false;
                    }

                    civillian.GetComponent <NavMeshAgent>().enabled = true;
                }
            }
        }

        Destroy(scareEffect, 2.0f);

        lureUsed = false;
        //End Repel - Jak
    }