コード例 #1
0
ファイル: Deer.cs プロジェクト: jamesleakos/artemis
 void SwitchToAlert()
 {
     velocity.x       = 0;
     deerState        = DeerState.alert;
     alertActionState = AlertActionState.idle;
     endAlertTime     = Time.time + alertAttentionSpan;
 }
コード例 #2
0
ファイル: Deer.cs プロジェクト: Toruitas/awolfalone_code
    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
ファイル: Deer.cs プロジェクト: jamesleakos/artemis
    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
ファイル: Deer.cs プロジェクト: jamesleakos/artemis
 void SwitchToGrazing()
 {
     velocity.x          = 0;
     deerState           = DeerState.grazing;
     grazingState        = GrazingState.lookingDown;
     grazingCycleCounter = 0;
 }
コード例 #5
0
ファイル: Deer.cs プロジェクト: Toruitas/awolfalone_code
 private void StopFleeing()
 {
     anim.SetBool("running", false);
     anim.SetBool("moving", false);
     currentState = DeerState.idle;
     FindClosestPlant();
 }
コード例 #6
0
ファイル: Deer.cs プロジェクト: Toruitas/awolfalone_code
    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
ファイル: Deer.cs プロジェクト: Toruitas/awolfalone_code
 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
ファイル: Deer.cs プロジェクト: Toruitas/awolfalone_code
 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
ファイル: Deer.cs プロジェクト: Toruitas/awolfalone_code
    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
ファイル: Deer.cs プロジェクト: Toruitas/awolfalone_code
    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
ファイル: Deer.cs プロジェクト: jamesleakos/artemis
 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
ファイル: Deer.cs プロジェクト: jamesleakos/artemis
 public void SwitchToJumping()
 {
     velocity.x = -1 * jumpSpeed * faceDirX;
     deerState  = DeerState.jumping;
 }
コード例 #21
0
ファイル: DeerAI.cs プロジェクト: keve118/Evolution
    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
ファイル: Deer.cs プロジェクト: jamesleakos/artemis
 void SwitchToScared()
 {
     velocity.x = 0;
     deerState  = DeerState.scared;
 }