Beispiel #1
0
    public void STATE_Update(CivillianController agent, StateMachine_CIV stateMachine, float deltaTime)
    {
        if (agent.enableWander == true)
        {
            //Just set a random destination
            if (agent.navAgent.hasPath == false && agent.navAgent.enabled == true)
            {
                //Debug.Log("PickNewWander() called from " + agent.name);
                if (!agent.navAgent.SetDestination(PickNewWanderPoint()))
                {
                    Debug.Log("SetDest failed: " + agent.name);
                }

                //agent.currentDest = agent.navAgent.destination;
            }

            //if (agent.navAgent.isPathStale == true)
            //    Debug.Log(agent.name + " is stale");

            //if (agent.navAgent.remainingDistance > agent.navAgent.stoppingDistance)
            //    agent.Move(agent.navAgent.desiredVelocity, false, false);
            //else
            //    agent.Move(Vector3.zero, false, false);
        }
    }
 //Inherited from IBehaviour
 public void Update(CivillianController pAgent, float deltaTime)
 {
     if (currentState != null)
     {
         currentState.STATE_Update(pAgent, this, deltaTime); //Pass in agent and current statemachine
     }
 }
    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;
        }
    }
Beispiel #4
0
    [HideInInspector] public static Vector3 dirAwayFromObject; //Set in OnCollisionEnter

    public void OnEnter(CivillianController agent)
    {
        //Reset path to prevent errors when switching
        if (agent.navAgent.hasPath)
        {
            agent.navAgent.ResetPath();
        }

        //Update the priority so we push through other agents when blocked
        agent.navAgent.avoidancePriority = 30;

        currentAgent = agent;                            //Storing reference
        currentAgent.currentState = State.State_Retreat; //Setting currentState - kinda temporary i hope

        if (agent.initialSpawn)
        {
            agent.initialSpawn = false;
        }

        //Update icon
        agent.civIconStateScript.myState = script_civilianIconState.gameState.retreat;

        //Update animator
        if (currentAgent.m_Animator.enabled == false) //This is here temporarilly until we get a intriged animation going, this just unfreezes anim
        {
            currentAgent.m_Animator.enabled = true;
        }

        //Play scared sound clip
        agent.PlayScaredSound();

        currentAgent.navAgent.speed = 5.0f; //Add some additional speed to make them feel really spooked
        currentAgent.m_Animator.SetBool("Scared", true);

        //Debug.Log(currentAgent.gameObject.name + " State: RETREAT");
        agent.txtState.text = "RETREAT";

        if (agent.TRIGGERED_floating)   //Was scared by a floating object - multiply base scariness by x 1
        {
            CheckScaryRating();
            ectoplasm.GetComponent <ectoplasmController>().modifier = Camera.main.GetComponent <valueController>().EctoSidSeenScareValue;
            currentAgent.currentScareValue += (currentAgent.sid.GetComponent <playerPossession>().itemThrown.GetComponent <ItemController>().baseScariness);
        }
        else if (agent.TRIGGERED_hit)   //Was hit by an item - multiply base scariness by x2
        {
            CheckScaryRating();
            ectoplasm.GetComponent <ectoplasmController>().modifier = Camera.main.GetComponent <valueController>().EctoThrowScareValue;
            currentAgent.currentScareValue += (currentAgent.sid.GetComponent <playerPossession>().itemThrown.GetComponent <ItemController>().baseScariness);
        }
        else if (agent.TRIGGERED_repel) //Was lured to an item and spooked - multiply by x5??
        {
            CheckScaryRating();
            ectoplasm.GetComponent <ectoplasmController>().modifier = Camera.main.GetComponent <valueController>().EctoRepelledScareValue;
            currentAgent.currentScareValue += (currentAgent.sid.GetComponent <playerPossession>().itemThrown.GetComponent <ItemController>().baseScariness);
        }

        currentAgent.testParticle = ectoplasm;
    }
 public void OnEnter(CivillianController agent)
 {
     currentAgent = agent;
     currentAgent.currentState        = State.State_Alert;
     agent.civIconStateScript.myState = script_civilianIconState.gameState.alerted;
     currentAgent.txtState.text       = "ALERT";
     itemPos = currentAgent.itemPosition;
     currentAgent.m_Animator.SetBool("lured", true);
 }
Beispiel #6
0
    public void OnExit(CivillianController agent)
    {
        //Once the agent has fully finished the retreat state, reset the TRIGGERED flags so we can once again gain ecto for scaring them
        agent.TRIGGERED_floating    = false;
        agent.TRIGGERED_hit         = false;
        agent.TRIGGERED_repel       = false;
        agent.hasDroppedEcto        = false;
        currentAgent.navAgent.speed = 1.5f;
        currentAgent.m_Animator.SetBool("Scared", false);

        //Reset back to old priority
        currentAgent.navAgent.avoidancePriority = 50;

        //Reset target item that hit us
        //agent.target = null; //Trying to prevent multiple hits from the same object, but seems to be working ok so far
    }
    public void ChangeState(CivillianController agent, State_CIV state)
    {
        //Exit the currentState
        if (currentState != null)
        {
            currentState.OnExit(agent);
        }

        //Calls OnEnter from the new state
        if (state != null)
        {
            state.OnEnter(agent);
        }

        previousState = currentState;
        currentState  = state;
    }
Beispiel #8
0
    public void OnEnter(CivillianController agent)
    {
        currentAgent = agent;
        currentAgent.currentState        = State.State_Wander;
        agent.civIconStateScript.myState = script_civilianIconState.gameState.normal;
        agent.txtState.text = "WANDER";


        //Testing
        //Vector3 dir = agent.transform.forward * 15f;
        //dir += agent.transform.position; //Update direction based on Agent's current position

        //NavMeshHit navHit; //Stores the result of a NavMesh query
        //NavMesh.SamplePosition(dir, out navHit, 15f, -1); //Returns the closest point where randDirection is situated on the NavMesh
        //agent.navAgent.SetDestination(navHit.position);


        //agent.navAgent.SetDestination(agent.testTarget.position);
    }
Beispiel #9
0
    private void Update()
    {
        timer += Time.deltaTime;

        if (timer >= (float)Random.Range(1f, 5f) && currentSpawned < numberOfAgents) //Spawn a civ every x seconds
        {
            timer = 0;

            Vector2 initialSpherePos = Random.insideUnitCircle * spawnRadius;
            Vector3 adjustedSpawnPos = new Vector3(transform.position.x + spherePos.x + initialSpherePos.x, transform.position.y, transform.position.z + spherePos.z + initialSpherePos.y);

            NavMeshHit hit;
            NavMesh.SamplePosition(adjustedSpawnPos, out hit, spawnRadius, 1);
            adjustedSpawnPos = hit.position;


            //Instantiate the civ at the spawners inital position, because i want them to "walk" into the shop as if its realistic ish
            GameObject o = Instantiate(civPrefab, transform.position, transform.rotation);
            o.transform.parent = gameObject.transform;

            //Randomise Colours
            CivillianController civ = o.GetComponent <CivillianController>();
            civ.civilianPantsColour = Random.ColorHSV(0, 1, .5f, .7f, .5f, 1, 1, 1);
            civ.civilianTop1Colour  = Random.ColorHSV(0, 1, .3f, .7f, .5f, 1, 1, 1);
            civ.civilianTop2Colour  = Random.ColorHSV(0, 1, .3f, .7f, .5f, 1, 1, 1);
            civ.firstSpawnDest      = adjustedSpawnPos;
            civ.initialSpawn        = true;

            if (Time.time > .1f) //Delay the NavAgent component otherwise would bug out and not find the navmesh intime because it is still baking
            {
                o.GetComponent <NavMeshAgent>().enabled = true;
                o.GetComponent <NavMeshAgent>().SetDestination(adjustedSpawnPos); //Path towards the random point found within the spherePos(should be inside the mall)
            }


            //Debug.DrawLine(adjustedSpawnPos, new Vector3(adjustedSpawnPos.x, 20f, adjustedSpawnPos.z), Color.green, 99f);

            currentSpawned++;
        }
    }
Beispiel #10
0
 public void OnExit(CivillianController agent)
 {
     agent.navAgent.ResetPath();
 }
Beispiel #11
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
    }
    //#OPTIMISE - These two methods both run physics.overlap, possibly merge that call into one list for use in repel, and if repel detects that list empty, then run its own?
    //Gotta think about how id do it, what happens if list has 1, but then 4 AI walk in during a repel...
    void Lure() //Written by Jak
    {
        //Change UI
        GameManager.Instance.EnableHideScary(false);
        GameManager.Instance.EnableHideScaryLure(true);

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

        //Create particle effect
        lureEffect = Instantiate(GameObject.Find("PrefabController").GetComponent <PrefabController>().lureEffect, this.gameObject.transform);
        lureEffect.transform.localPosition = new Vector3(0, 0, 0);

        //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);

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

        //Lure Enemies to us
        //#OPTIMISE //Refactor this so it finds tags first instead of all colliders
        //Or maybe have an already populated list of civs on startup that we loop through and check if in range?
        Collider[] civillians = Physics.OverlapSphere(transform.position, lureRange);

        //Sample this object position before sending it to the ai
        NavMeshHit navHit;

        NavMesh.SamplePosition(this.gameObject.transform.position, out navHit, lureRange, -1);

        //Check for any civs in the radius and "lure" them to us
        foreach (Collider civ in civillians)
        {
            if (civ.tag == "Civillian")
            {
                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.itemPosition  = navHit.position;
                    civillian.alertedByItem = true;
                }
            }
        }

        Destroy(lureEffect, 2.0f);

        lureUsed = true;
    }
 public void OnExit(CivillianController agent)
 {
     stopped = false;
     currentAgent.m_Animator.SetBool("lured", false);
     currentAgent.m_Animator.SetBool("idle", false);
 }