Exemplo n.º 1
0
    protected override void Act()
    {
        CM.AddSequentialTask(new MeleeAITask()
        {
            AI = this, StartClock = EntityClock, Priority = (int)entity.Type, Act = () => {
                if (!entity.IsActive)
                {
                    return;
                }
                memoryTime++;

                if (memoryTime > 3)
                {
                    IsTargetSeen = false;
                    Target       = null;
                }
                //Debug.Log(state.ToString());
                emotion = EntityEmotion.None;
                switch (state)
                {
                case EntityAIState.Attack:
                    if (Target == null || !Target.enabled)
                    {
                        Target  = null;
                        state   = EntityAIState.Wander;
                        emotion = EntityEmotion.Confused;
                        break;
                    }
                    if (!EW.CanAttack(Target))
                    {
                        if (IsTargetSeen)
                        {
                            state = EntityAIState.Chase;
                        }
                        else
                        {
                            state   = EntityAIState.Wander;
                            emotion = EntityEmotion.Confused;
                            Target  = null;
                        }
                    }
                    break;

                case EntityAIState.Chase:
                    if (Target == null || !Target.enabled)
                    {
                        Target = null;
                        state  = EntityAIState.Wander;
                        break;
                    }

                    if (EW.CanAttack(Target))
                    {
                        state = EntityAIState.Attack;
                    }
                    if (entity.IdealPosition == LastTargetPositionSeen && !ES.IsVisible(Target))
                    {
                        state   = EntityAIState.Wander;
                        emotion = EntityEmotion.Confused;
                        Target  = null;
                    }
                    break;

                case EntityAIState.Wander:
                    if (IsRecentlyDamaged)
                    {
                        //Target = EM.FindEntityInLine(DamagedDirection, entity, 6);

                        state   = EntityAIState.Confused;
                        emotion = EntityEmotion.Angry;
                        break;
                    }
                    if (ES.IsVisible(EM.Player))
                    {
                        IsTargetSeen = true;
                        memoryTime   = 0;
                        Target       = EM.Player;
                        state        = EntityAIState.Chase;
                        emotion      = EntityEmotion.Surprised;
                        break;
                    }
                    break;

                case EntityAIState.Confused:
                    if (Util.LHQToFace(entity.IdealRotation) == DamagedDirection)
                    {
                        Target = EM.FindEntityInLine(DamagedDirection, entity);
                        if (Target != null)
                        {
                            IsTargetSeen = true;
                            memoryTime   = 0;
                            state        = EntityAIState.Chase;
                            emotion      = EntityEmotion.Surprised;
                        }
                        else
                        {
                            state   = EntityAIState.Wander;
                            emotion = EntityEmotion.Confused;
                        }
                    }
                    break;
                }

                switch (state)
                {
                case EntityAIState.Attack:
                    EW.TryAttack(Target, EntityClock, 1, 0, 0);//Attempts melee attack
                    IsTargetSeen = true;
                    memoryTime   = 0;
                    break;

                case EntityAIState.Chase:

                    if (ES.IsVisible(Target))
                    {
                        IsTargetSeen           = true;
                        memoryTime             = 0;
                        LastTargetPositionSeen = Target.IdealPosition;
                        move.MoveToward(Target.IdealPosition, EntityClock);
                    }
                    else
                    {
                        move.MoveToward(LastTargetPositionSeen, EntityClock);
                    }
                    break;

                case EntityAIState.Wander:
                    IsTargetSeen = false;
                    RandomWalk();
                    break;

                case EntityAIState.Confused:
                    move.Face(DamagedDirection, EntityClock);
                    break;
                }
                IsRecentlyDamaged = false;
                Act();
                void RandomWalk()
                {
                    if (UnityEngine.Random.Range(0f, 1f) < 0.5f)
                    {
                        emotion = EntityEmotion.None; move.MoveToward(Util.LHQToLHUnitVector(entity.IdealRotation), EntityClock);
                    }
                    else
                    {
                        emotion = EntityEmotion.None; move.Face(Util.LHQToFace(TurnRandom()), EntityClock);
                    }
                }
            }
        }