コード例 #1
0
ファイル: CombatComponent.cs プロジェクト: Octanum/Corvus
 private void ResumeAnimation()
 {
     MovementComponent.CombatWalkSpeedModifier = 1f;
     if (MovementComponent.IsWalking)
     {
         MovementComponent.BeginWalking(MovementComponent.CurrentDirection);
     }
     else
     {
         MovementComponent.StopWalking();
     }
 }
コード例 #2
0
 /// <summary>
 /// Moves the player in the specified direction. If it is attacking, the player is slowed.
 /// </summary>
 public void BeginWalking(Direction dir)
 {
     if (!CC.IsAttacking)// && PC.IsGrounded)
     {
         MC.BeginWalking(dir);
     }
     else
     {
         MC.WalkDirection    = dir;
         MC.CurrentDirection = MC.WalkDirection;
         MC.IsWalking        = true;
     }
 }
コード例 #3
0
        protected override void OnUpdate(GameTime Time)
        {
            base.OnUpdate(Time);
            if (AIEnabled)
            {
                //Begin process of death if entity has run out of health.
                if (((AttributesComponent.CurrentHealth / AttributesComponent.MaxHealth) * 100) < 10)
                {
                    if (!DeathStarted)
                    {
                        DeathTime = DateTime.Now;
                    }

                    DeathStarted = true;
                }

                //Begin process of fleeing if entity's health is <25%.
                if (((AttributesComponent.CurrentHealth / AttributesComponent.MaxHealth) * 100) < 25)
                {
                    FleeingStarted = true;
                }

                bool shouldFlee = FleeingStarted && FleeingEnabled;
                bool shouldDie  = DeathStarted && DeathEnabled;

                if (!shouldFlee && !shouldDie) //NORMAL AI
                {
                    bool   foundEntity          = false;
                    bool   projectileFlyingToMe = false;
                    bool   entityAttackingMe    = false;
                    bool   entityAttackable     = false;
                    bool   entityFollowable     = false;
                    Entity entityToFollow       = null;

                    //TODO: This could be very ineffecient.
                    //Foreach entity in this entity's reaction box.
                    foreach (Entity e in PhysicsSystem.GetEntitiesAtLocation(GetReactionBox()))
                    {
                        var clc = e.GetComponent <ClassificationComponent>();
                        var coc = e.GetComponent <CombatComponent>();

                        if (clc.Classification == EntityClassification.Player) //If Player
                        {
                            foundEntity = true;

                            entityFollowable = true;
                            entityToFollow   = e;

                            if (coc.IsAttacking && WithinEntitysAttackRange(e) && EntityFacingMe(e))
                            {
                                entityAttackingMe = true;
                            }

                            if (!MovementComponent.IsWalking && EntityWithinAttackRange(e))
                            {
                                entityAttackable = true;
                            }
                        }
                        else if (clc.Classification == EntityClassification.Projectile) //If Projectile
                        {
                            foundEntity = true;

                            //Basically, projectiles within our rectangle might hit us, so we'll just block.
                            if (EntityGoingToMe(e))
                            {
                                projectileFlyingToMe = true;
                            }
                        }
                    }

                    if (foundEntity) //If we found an entity.
                    {
                        //We're reacting to an entity.
                        IsReacting = true;

                        //If we're following a path and we're aggressive, stop following the path.
                        if (PathComponent.PathingEnabled && Aggressive)
                        {
                            PathComponent.StopFollowing();
                        }

                        //If we're able to follow an entity and we're aggressive, follow the entity.
                        if (entityFollowable && entityToFollow != null && Aggressive)
                        {
                            FollowEntity(entityToFollow, Time);
                        }

                        //If an entity is attackable and if i (the enemy) am not blocking, attack.
                        if (entityAttackable && !CombatComponent.IsBlocking)
                        {
                            CombatComponent.AttackAI();
                        }
                    }
                    else //If we found nothing.
                    {
                        IsReacting = false;

                        //Resume following the path.
                        if (!PathComponent.PathingEnabled)
                        {
                            PathComponent.StartFollowing();
                        }
                    }

                    //If a projectile is coming to us, or an entity is attacking us, block.
                    if (entityAttackingMe || projectileFlyingToMe)
                    {
                        if (!CombatComponent.IsBlocking)
                        {
                            CombatComponent.BeginBlock();
                        }
                    }

                    //If there's no projectile coming to us, or no entity attacking us, end the block.
                    if (!entityAttackingMe && !projectileFlyingToMe)
                    {
                        if (CombatComponent.IsBlocking)
                        {
                            CombatComponent.EndBlock();
                        }
                    }
                }
                else if (shouldDie) //DYING AI
                {
                    //Stop blocking if we were doing so at the moment death occured.
                    if (CombatComponent.IsBlocking)
                    {
                        CombatComponent.EndBlock();
                    }

                    Direction walkDirection = MovementComponent.WalkDirection == Direction.Left ? Direction.Right : Direction.Left;
                    double    walkTime      = 200;

                    if ((DateTime.Now - DeathTime).TotalMilliseconds > walkTime)
                    {
                        MovementComponent.BeginWalking(walkDirection);
                        DeathTime = DateTime.Now;
                    }
                }
                else if (shouldFlee) //FLEEING AI
                {
                    //Stop blocking if we were doing so at the moment fleeing occured.
                    if (CombatComponent.IsBlocking)
                    {
                        CombatComponent.EndBlock();
                    }

                    float checkDistance = PhysicsComponent.VelocityX * Time.GetTimeScalar();

                    //Run back and forth on platform.
                    Vector2 leftPlatformVector  = new Vector2(Parent.Location.Left + checkDistance, Parent.Location.Bottom + 1);
                    Vector2 rightPlatformVector = new Vector2(Parent.Location.Right + checkDistance, Parent.Location.Bottom + 1);
                    Vector2 leftWallVector      = new Vector2(Parent.Location.Left + checkDistance, Parent.Location.Center.Y);
                    Vector2 rightWallVector     = new Vector2(Parent.Location.Right + checkDistance, Parent.Location.Center.Y);
                    bool    leftPossible        = PhysicsSystem.IsLocationSolid(leftPlatformVector) && !PhysicsSystem.IsLocationSolid(leftWallVector);
                    bool    rightPossible       = PhysicsSystem.IsLocationSolid(rightPlatformVector) && !PhysicsSystem.IsLocationSolid(rightWallVector);

                    //Set a location if one hasn't been set (default is Direction.Down). Very unlikely to happen here, but can't say impossible.
                    if (MovementComponent.CurrentDirection == Direction.Down)
                    {
                        MovementComponent.CurrentDirection = Direction.Left;
                    }

                    if (MovementComponent.CurrentDirection == Direction.Left)
                    {
                        if (leftPossible)
                        {
                            MovementComponent.BeginWalking(Direction.Left);
                        }
                        else if (rightPossible)
                        {
                            MovementComponent.BeginWalking(Direction.Right);
                        }
                    }
                    else if (MovementComponent.CurrentDirection == Direction.Right)
                    {
                        if (rightPossible)
                        {
                            MovementComponent.BeginWalking(Direction.Right);
                        }
                        else if (leftPossible)
                        {
                            MovementComponent.BeginWalking(Direction.Left);
                        }
                    }
                }
            }
        }