Ejemplo n.º 1
0
        public override void Dispatch()
        {
            // If player is out of range, chase him
            bool isFarAwayFromPlayer = Vector3.Distance(context.transform.position, context.target.transform.position) > context.safeDistanceFromTarget;

            if (isFarAwayFromPlayer)
            {
                context.SetState(AGENT_STATE.PATROL);
                return;
            }

            timer += Time.deltaTime;

            if (timer >= timeUntilWeCalculateNextDestination)
            {
                timer = 0;

                if (Vector3.Distance(context.transform.position, context.target.transform.position) >= MINIUMUM_DISTANCE_FROM_TARGET)
                {
                    return;
                }

                // If we are still close to player, reset destination to a further away one
                destination = context.transform.position - context.target.transform.position;
                context.MoveTo(destination);
            }
        }
Ejemplo n.º 2
0
        public IEnumerator DefendAction()
        {
            inProgress = true;

            context.battler.Defend();

            // Wait Until We Trigger Attack Animation
            yield return(new WaitUntil(() => context.battler.IsDefending() == true));

            // Now Wait Until Attack Animation Is Over
            yield return(new WaitUntil(() => context.battler.IsDefending() == false));

            inProgress = false;

            context.SetState(AGENT_STATE.FIGHTING);
        }
        public IEnumerator TakeDamageAction()
        {
            inProgress = true;

            context.transform.Translate(new Vector3(0, 0, .5f));

            context.GetComponent <Battler>().TakeDamage();

            yield return(new WaitUntil(() => context.GetComponent <Battler>().IsTakingDamage() == true));

            yield return(new WaitUntil(() => context.GetComponent <Battler>().IsTakingDamage() == false));

            // Chase Player
            context.SetState(AGENT_STATE.CHASE);

            inProgress = false;
        }
Ejemplo n.º 4
0
        public IEnumerator DodgeAction()
        {
            inProgress = true;

            context.transform.Translate(new Vector3(0, 0, -0.5f));

            context.battler.Dodge();

            // Wait Until We Trigger Attack Animation
            yield return(new WaitUntil(() => context.battler.IsDodging() == true));

            // Now Wait Until Attack Animation Is Over
            yield return(new WaitUntil(() => context.battler.IsDodging() == false));

            inProgress = false;

            context.SetState(AGENT_STATE.FIGHTING);
        }
Ejemplo n.º 5
0
        public override void Dispatch()
        {
            if (context.target == null)
            {
                context.SetState(AGENT_STATE.PATROL);
                return;
            }

            if (context.target.GetComponent <Health>().IsDead())
            {
                context.SetState(AGENT_STATE.PATROL);
                return;
            }

            // If my health is low
            if (context.health.IsLowHealth())
            {
                float chanceToFlee = UnityEngine.Random.Range(0f, 1f);

                if (chanceToFlee >= context.minimumChanceToFleeIfLowHealth)
                {
                    context.SetState(AGENT_STATE.FLEE);
                    return;
                }
            }

            // If my target is far away
            if (AI_Helpers.TargetIsFarAway(
                    context.target,
                    context.gameObject,
                    context.gameObject.GetComponent <WeaponManager>()
                    ))
            {
                context.SetState(AGENT_STATE.CHASE);
                return;
            }

            if (inProgress)
            {
                return;
            }


            // Target is attacking, let's receive the attack and decide later if we should dodge or defend
            if (context.target.GetComponent <Battler>().IsAttacking())
            {
                float chanceToNotAttack = UnityEngine.Random.Range(0, 1f);
                if (chanceToNotAttack >= 0.1f)
                {
                    return;
                }
            }

            float chanceToAttack = UnityEngine.Random.Range(0, 1f);

            if (chanceToAttack < context.minimumChanceToAttack)
            {
                Debug.Log("Hit twice");
                context.SetState(AGENT_STATE.CHASE);
                return;
            }

            context.StartCoroutine(Attack());
        }