示例#1
0
 void SwitchToAlert()
 {
     velocity.x       = 0;
     deerState        = DeerState.alert;
     alertActionState = AlertActionState.idle;
     endAlertTime     = Time.time + alertAttentionSpan;
 }
示例#2
0
    public void FindClosestPlant()
    {
        // altered from https://www.youtube.com/watch?v=YLE3v8bBnck

        closestPlant = null;  // clear

        // FIRST if there's food in the physics circle, go to that one.
        // Approach from https://forum.unity.com/threads/find-object-with-tag-in-range.190938/
        SniffOutFood();

        // SECOND if there aren't any bits of food in the physics circle, go through all the plants to find one nearby.
        if (!closestPlant)
        {
            Debug.Log("No nearby food, searching for closest.");
            SniffOutFoodFar();
        }

        agent.speed = speed;
        agent.SetDestination(closestPlant.transform.position);
        DebugDrawPath(agent.path.corners);
        currentState = DeerState.walk;
        anim.SetBool("moving", true);


        // ChangeState(DeerState.walk);

        // Debug.Log("Deer position " + transform.position);
        // Debug.Log("Closest plant position "+ closestPlant.transform.position);
        Debug.DrawLine(transform.position, closestPlant.transform.position);
    }
示例#3
0
    void Start()
    {
        controller   = GetComponent <Controller2D>();
        thisCollider = GetComponent <Collider2D>();
        animator     = GetComponent <Animator>();
        audioManager = GameObject.FindGameObjectWithTag("AudioManager").GetComponent <AudioManager>();

        gm = GameObject.FindGameObjectWithTag("GM").GetComponent <GameMaster>();

        cam = Camera.main;

        headBone  = TransformDeepChildExtension.FindDeepChild(gameObject.transform, "HeadBone");
        winCanvas = TransformDeepChildExtension.FindDeepChild(gameObject.transform, "WinCanvas").GetComponent <DeerWinCanvas>();

        gravity = -155f;

        globalMaxInhabitRange = localMaxInhabitRange + transform.position;
        globalMinInhabitRange = localMinInhabitRange + transform.position;

        globalMaxSightRange = localMaxSightRange + transform.position;
        globalMinSightRange = localMinSightRange + transform.position;

        globalMaxFriendRange = localMaxFriendRange + transform.position;
        globalMinFriendRange = localMinFriendRange + transform.position;

        globalSpawnPoint = localSpawnPoint + transform.position;

        deerState = DeerState.grazing;
        faceDirX  = -1;
    }
示例#4
0
 void SwitchToGrazing()
 {
     velocity.x          = 0;
     deerState           = DeerState.grazing;
     grazingState        = GrazingState.lookingDown;
     grazingCycleCounter = 0;
 }
示例#5
0
 private void StopFleeing()
 {
     anim.SetBool("running", false);
     anim.SetBool("moving", false);
     currentState = DeerState.idle;
     FindClosestPlant();
 }
示例#6
0
    private IEnumerator GetHungryCoroutine()
    {
        currentState = DeerState.idle;
        yield return(new WaitForSeconds(timeToHunger));

        FindClosestPlant();
    }
示例#7
0
 void CalmDeer()
 {
     // Debug.Log("Becoming Calm");
     deerState           = DeerState.calm;
     nextTimeForCalmLook = Time.time + calmLookIntervalAverage + Random.Range(-1, 1);
     lastStateTime       = Time.time;
 }
示例#8
0
    void Start()
    {
        controller   = GetComponent <Controller2D>();
        thisCollider = GetComponent <Collider2D>();

        gravity      = -(2 * jumpHeight) / Mathf.Pow(timeToJumpApex, 2);
        jumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
        print("Gravity: " + gravity + "  Jump Velocity: " + jumpVelocity);

        deerState = DeerState.calm;
    }
示例#9
0
 public void OnTriggerEnter2D(Collider2D other)
 {
     if (other.CompareTag("Player") && !other.isTrigger)
     {
         currentState = DeerState.dying;
         agent.ResetPath();
         agent.enabled = false;
         // flash the sprite a couple times and play the death sound
         StartCoroutine(FlashDie(flashSpeed));
     }
 }
示例#10
0
 public void StartEatingClosestPlant()
 {
     // And if the deer has arrived at its destination, it starts eating.
     // if(Vector3.Distance(agent.path.corners[0],transform.position) < eatRadius){
     if (agent.path.corners.Length >= 1)
     {
         if (Vector3.Distance(transform.position, closestPlant.transform.position) < eatRadius)
         {
             agent.ResetPath();
             currentState = DeerState.eat;
             anim.SetBool("moving", false);
         }
     }
 }
示例#11
0
    private IEnumerator FleeCoroutine(Vector3 moveDirection)
    {
        yield return(new WaitForSeconds(reflexTime));

        currentState = DeerState.flee;
        // flee distance
        float fleeDistance = Random.Range(1.0f, 7.0f);

        agent.speed = speed * runSpeedMultiplier;
        agent.SetDestination(moveDirection * fleeDistance);
        anim.SetBool("moving", true);
        anim.SetBool("running", true);

        // now set a reasonable time-out, just in case the deer gets stuck running somewhere it can't get
        yield return(new WaitForSeconds(deerStuckFleeingMaxTime));

        StopFleeing();
    }
示例#12
0
    public void Start()
    {
        myRigidbody = GetComponent <Rigidbody2D>();
        anim        = GetComponent <Animator>();
        agent       = GetComponent <NavMeshAgent>();

        wolf        = GameObject.FindWithTag("Player").transform;
        speed       = Random.Range(0.5f, 2.5f);
        agent.speed = speed;
        spriteR     = gameObject.GetComponent <SpriteRenderer>();

        agent.updateRotation = false;
        agent.updateUpAxis   = false;
        currentState         = DeerState.idle;

        // randomly generate hunger and reflex times
        // TODO re-factor random numbers to have a normal distribution (bell curve), rather than just this simple random
        timeToHunger = Random.Range(0.0f, 3.0f);
        reflexTime   = Random.Range(0.0f, 0.2f);
        // InvokeRepeating("FindClosestPlant", timeToHunger, timeToHunger);
        FindClosestPlant();
    }
示例#13
0
    public void setState(DeerState newState)
    {
        if (newState != currentState)
        {
            currentState = newState;
            findNewDestination();
            switch (currentState)
            {
            case DeerState.Normal:
                agent.speed = normalSpeed;
                break;

            case DeerState.OnEdge:
                agent.speed = onEdgeSpeed;
                break;

            case DeerState.Scared:
                agent.speed = scaredSpeed;
                break;
            }
            print(currentState);
        }
    }
示例#14
0
 void SwitchToFriendly()
 {
     velocity.x = 0;
     deerState  = DeerState.friendly;
 }
示例#15
0
 void ScareDeer()
 {
     // Debug.Log("Becoming Scared");
     deerState     = DeerState.scared;
     lastStateTime = Time.time;
 }
示例#16
0
 void FriendDeer()
 {
     // Debug.Log("Becoming Friends");
     deerState     = DeerState.friendly;
     lastStateTime = Time.time;
 }
示例#17
0
    /// <summary>
    /// The particular calcSteeringForces used by
    /// deer.
    /// </summary>
    protected override void calcSteeringForces()
    {
        Vector3 temp = Vector3.zero; //for prioritizing
        steeringForce = Vector3.zero;
        switch(state)
        {
            case DeerState.GRAZE:
                {
                    //if (anim.GetCurrentAnimatorStateInfo(0).nameHash == runHash)
                        //anim.SetTrigger("walkTrigger");
                    maxSpeed = walkMaxSpeed;

                    if ((this.transform.position - flock.seekpoints[flock.seekindex]).sqrMagnitude < 4)
                    {
                        int currIndex = flock.seekindex;
                        do
                            flock.seekindex = Random.Range(0, flock.seekpoints.Length);
                        while (flock.seekindex == currIndex);
                    }

                    steeringForce += wander() * wanderWeight;
                    steeringForce += seek(flock.seekpoints[flock.seekindex]) * seekWeight;
                    steeringForce += this.transform.forward;

                    temp += cohesion(flock.Centroid) * cohesionWeight; //only non-zero conditionally
                    temp += separation(separateDistance) * separationWeight; //also only non-zero conditionally
                    steeringForce += seek(flock.seekpoints[flock.seekindex]) * seekWeight;
                    temp += alignment(flock.FlockDirection) * alignmentWeight; //will always apply a force if called

                    foreach (GameObject obstacle in gm.Obstacles)
                    {
                        temp += avoid(obstacle) * avoidWeight;
                    }

                    steeringForce += temp;

                    steeringForce += flowFollow() * flowWeight;
                    int index = getNearest(gm.Wolves.Flockers);
                    if (index > -1)
                    {
                        //Debug.Log("checking for wolves");
                        Flocker nearestWolf = gm.Wolves.Flockers[index];
                        if ((this.transform.position - nearestWolf.transform.position).sqrMagnitude < fleedistance * fleedistance)
                        {
                            Debug.Log("Change State to flee");
                            foreach (Flocker f in flock.Flockers)
                                if (f != null)
                                    f.GetComponent<deerScript>().state = DeerState.FLEE;
                            if (gm.herds.Contains(flock))
                                gm.herds.Remove(flock); //should get garbage collected if we remove our way of getting it
                            flock = null;
                        }
                    }
                    break;
                }
        //fleeing
            case DeerState.FLEE:
            {
                //if (anim.GetCurrentAnimatorStateInfo(0).nameHash == walkHash)
                    //anim.SetTrigger("runTrigger");
                maxSpeed = runMaxSpeed;
                bool safe = true;
                foreach(Flocker wolf in gm.Wolves.Flockers)
                {
                    steeringForce += flee(wolf.transform.position);
                    if ((wolf.transform.position - this.transform.position).sqrMagnitude < fleedistance * fleedistance)
                        safe = false;
                }
                steeringForce += separation(separateDistance);
                if (safe)
                    state = DeerState.SEARCH;
                break;
            }
        //regroup
            case DeerState.SEARCH:
            {
                maxSpeed = walkMaxSpeed;
                temp += regroup();
                if (temp == Vector3.zero) //we found a group!
                    state = DeerState.GRAZE;
                break;
            }
        }
        steeringForce += stayInBounds() * boundsWeight;
        base.calcSteeringForces();
    }
示例#18
0
 void AlertDeer()
 {
     // Debug.Log("Becoming Alert");
     deerState     = DeerState.alert;
     lastStateTime = Time.time;
 }
示例#19
0
    /// <summary>
    /// The particular calcSteeringForces used by
    /// deer.
    /// </summary>


    protected override void calcSteeringForces()
    {
        Vector3 temp = Vector3.zero; //for prioritizing

        steeringForce = Vector3.zero;
        switch (state)
        {
        case DeerState.GRAZE:
        {
            //if (anim.GetCurrentAnimatorStateInfo(0).nameHash == runHash)
            //anim.SetTrigger("walkTrigger");
            maxSpeed = walkMaxSpeed;

            if ((this.transform.position - flock.seekpoints[flock.seekindex]).sqrMagnitude < 4)
            {
                int currIndex = flock.seekindex;
                do
                {
                    flock.seekindex = Random.Range(0, flock.seekpoints.Length);
                }while (flock.seekindex == currIndex);
            }

            steeringForce += wander() * wanderWeight;
            steeringForce += seek(flock.seekpoints[flock.seekindex]) * seekWeight;
            steeringForce += this.transform.forward;

            temp          += cohesion(flock.Centroid) * cohesionWeight;         //only non-zero conditionally
            temp          += separation(separateDistance) * separationWeight;   //also only non-zero conditionally
            steeringForce += seek(flock.seekpoints[flock.seekindex]) * seekWeight;
            temp          += alignment(flock.FlockDirection) * alignmentWeight; //will always apply a force if called

            foreach (GameObject obstacle in gm.Obstacles)
            {
                temp += avoid(obstacle) * avoidWeight;
            }

            steeringForce += temp;

            steeringForce += flowFollow() * flowWeight;
            int index = getNearest(gm.Wolves.Flockers);
            if (index > -1)
            {
                //Debug.Log("checking for wolves");
                Flocker nearestWolf = gm.Wolves.Flockers[index];
                if ((this.transform.position - nearestWolf.transform.position).sqrMagnitude < fleedistance * fleedistance)
                {
                    Debug.Log("Change State to flee");
                    foreach (Flocker f in flock.Flockers)
                    {
                        if (f != null)
                        {
                            f.GetComponent <deerScript>().state = DeerState.FLEE;
                        }
                    }
                    if (gm.herds.Contains(flock))
                    {
                        gm.herds.Remove(flock);         //should get garbage collected if we remove our way of getting it
                    }
                    flock = null;
                }
            }
            break;
        }

        //fleeing
        case DeerState.FLEE:
        {
            //if (anim.GetCurrentAnimatorStateInfo(0).nameHash == walkHash)
            //anim.SetTrigger("runTrigger");
            maxSpeed = runMaxSpeed;
            bool safe = true;
            foreach (Flocker wolf in gm.Wolves.Flockers)
            {
                steeringForce += flee(wolf.transform.position);
                if ((wolf.transform.position - this.transform.position).sqrMagnitude < fleedistance * fleedistance)
                {
                    safe = false;
                }
            }
            steeringForce += separation(separateDistance);
            if (safe)
            {
                state = DeerState.SEARCH;
            }
            break;
        }

        //regroup
        case DeerState.SEARCH:
        {
            maxSpeed = walkMaxSpeed;
            temp    += regroup();
            if (temp == Vector3.zero)     //we found a group!
            {
                state = DeerState.GRAZE;
            }
            break;
        }
        }
        steeringForce += stayInBounds() * boundsWeight;
        base.calcSteeringForces();
    }
示例#20
0
 public void SwitchToJumping()
 {
     velocity.x = -1 * jumpSpeed * faceDirX;
     deerState  = DeerState.jumping;
 }
示例#21
0
    void Update()
    {
        //distance to player from agent (deer)
        distance = Vector3.Distance(player.position, transform.position);

        switch (currentState)
        {
        case DeerState.Eating:
        {
            agent.speed = 0;
            if (isWandering == false)
            {
                StartCoroutine(Wander());
            }

            if (isWalking)
            {
                currentState = DeerState.Walking;
            }
            else
            {
                animator.SetBool("isWalking", false);
                animator.SetBool("isRunning", false);
                animator.SetBool("isEating", true);
            }

            if (distance <= lookRadius && ToolSwitching.huntingToolEquiped)
            {
                currentState = DeerState.Running;
            }

            break;
        }

        case DeerState.Running:
        {
            RunAway();

            if (distance > runAwayRadius)
            {
                currentState = DeerState.Eating;
            }

            Harvest harvestScript = gameObject.GetComponent <Harvest>();
            if (harvestScript.health <= 0)
            {
                currentState = DeerState.Dieing;
            }

            break;
        }

        case DeerState.Walking:
        {
            //decides the rotation / direction to face
            if (isRotRight == true)
            {
                agent.transform.Rotate(transform.up * Time.deltaTime * rotSpeed);
            }

            if (isRotLeft == true)
            {
                agent.transform.Rotate(transform.up * Time.deltaTime * -rotSpeed);
            }

            animator.SetBool("isRunning", false);
            animator.SetBool("isEating", false);
            animator.SetBool("isWalking", true);

            agent.speed = walkSpeed;
            agent.transform.position += transform.forward * walkSpeed * Time.deltaTime;

            //distance = distance between player and the deer
            if (distance <= lookRadius && ToolSwitching.huntingToolEquiped)
            {
                currentState = DeerState.Running;
            }

            if (isWandering == false)
            {
                currentState = DeerState.Eating;
            }

            break;
        }

        case DeerState.Dieing:
        {
            agent.speed = 0;
            animator.SetBool("isRunning", false);
            animator.SetBool("isDead", true);

            break;
        }
        }
    }
示例#22
0
 void SwitchToScared()
 {
     velocity.x = 0;
     deerState  = DeerState.scared;
 }