Example #1
0
        public void AttackLogic(InputComponent input, GameTime gameTime)
        {
            if (input.Bindings.TryGetValue(typeof(AttackBehaviorComponent), out var attackBehaviorBinding))
            {
                AttackBehaviorComponent attackBehavior = attackBehaviorBinding as AttackBehaviorComponent;
                SetAttackStates(attackBehavior);

                if (HasDelayedAttack(attackBehavior))
                {
                    input.MovementDirection = Vector2.Zero;
                    CountdownRemainingAttackDelay(attackBehavior, gameTime);
                    TrySpawnDelayedAttack(input, attackBehavior, gameTime);
                    return;
                }
                if (IsInAttackCooldown(attackBehavior))
                {
                    CountdownRemainingAttackCooldown(attackBehavior, gameTime);
                }

                if (!(input.PrimaryAttack || input.SecondaryAttack))
                {
                    return;
                }
                if (attackBehavior.RemainingAttackCooldownMilliseconds > 0)
                {
                    return;
                }

                FindAndStartTriggeredAttack(input, attackBehavior, gameTime);
            }
        }
Example #2
0
 private void TrySpawnDelayedAttack(InputComponent input, AttackBehaviorComponent attackBehavior, GameTime gameTime)
 {
     if (attackBehavior.DelayedAttack.RemainingAttackDelayMilliseconds <= 0)
     {
         SpawnAttackProjectile(input, attackBehavior.DelayedAttack.Cursor, attackBehavior, attackBehavior.DelayedAttack.Attack, gameTime);
     }
 }
Example #3
0
        private void SpawnAttackProjectile(InputComponent input, Point cursor, AttackBehaviorComponent attackBehavior, AttackComponent attack, GameTime gameTime)
        {
            Vector2 cursorDelta;

            if (attack.CursorType == CursorType.Absolute)
            {
                cursorDelta = new Vector2(cursor.X, cursor.Y) - attack.SourcePos.Vector2;
            }
            else //Relative
            {
                cursorDelta = new Vector2(cursor.X, cursor.Y);
            }
            _ecs.RegisterEntity(_ecs.CreateEntity(attack.Projectile,
                                                  position: new Vector2Ref(attack.SourcePos.Vector2 + new Coord2(cursorDelta).ChangePolarLength(attack.PosOffsetInDirection).Cartesian),
                                                  speed: new Coord2(cursorDelta).ChangePolarLength(attack.StartSpeed),
                                                  gameTime: gameTime, allegiance: attack.Allegiance));


            attackBehavior.RemainingAttackCooldownMilliseconds = attack.AttackCooldownMilliseconds;

            if (attack.BlockInput)
            {
                BlockInput(input, gameTime, attack.BlockInputDurationMilliseconds);
            }
            if (attack.SelfKnockback != 0)
            {
                ApplySelfKnockback(input, attack.SelfKnockback, cursorDelta);
            }

            attackBehavior.DelayedAttack = null;
        }
Example #4
0
        private static bool ExecuteAttackBlink(BlinkComponent blink, InputComponent input)
        {
            if (blink.Bindings.TryGetValue(typeof(AttackBehaviorComponent), out var attackBinding))
            {
                AttackBehaviorComponent attackBehavior = attackBinding as AttackBehaviorComponent;
            }

            return(false);
        }
Example #5
0
 private void FindAndStartTriggeredAttack(InputComponent input, AttackBehaviorComponent attackBehavior, GameTime gameTime)
 {
     foreach (var attack in attackBehavior.AttackComponents)
     {
         if (AttackTriggered(input, attack)) // && same attackState
         {
             StartAttack(input, attackBehavior, attack, gameTime);
             break;
         }
     }
 }
Example #6
0
 private void PlaceCursorInBehavior(InputComponent input, AttackBehaviorComponent attackBehavior, AttackComponent attack)
 {
     if (attack.CursorType == CursorType.Absolute)
     {
         attackBehavior.Cursor = input.CursorPoint;
     }
     else //Relative
     {
         Point cursor = input.CursorPoint;
         Point source = new Point((int)attack.SourcePos.Vector2.X, (int)attack.SourcePos.Vector2.Y);
         Point delta  = cursor - source;
         attackBehavior.Cursor = delta;
     }
 }
Example #7
0
        private void StartAttack(InputComponent input, AttackBehaviorComponent attackBehavior, AttackComponent attack, GameTime gameTime)
        {
            PlaceCursorInBehavior(input, attackBehavior, attack);

            if (attack.IsDelayedAttack)
            {
                attackBehavior.DelayedAttack = new AttackBehaviorComponent.DelayedAttackClass(attack.AttackDelayMilliseconds, attack, attackBehavior.Cursor);
                if (attack.BlockInput)
                {
                    BlockInput(input, gameTime, attack.BlockInputDurationMilliseconds);
                }
            }
            else
            {
                SpawnAttackProjectile(input, attackBehavior.Cursor, attackBehavior, attack, gameTime);
            }
        }
Example #8
0
 private void CountdownRemainingAttackCooldown(AttackBehaviorComponent attackBehavior, GameTime gameTime)
 {
     attackBehavior.RemainingAttackCooldownMilliseconds -= gameTime.ElapsedGameTime.TotalMilliseconds;
 }
Example #9
0
 private bool IsInAttackCooldown(AttackBehaviorComponent attackBehavior)
 {
     return(attackBehavior.RemainingAttackCooldownMilliseconds > 0);
 }
Example #10
0
 private void CountdownRemainingAttackDelay(AttackBehaviorComponent attackBehavior, GameTime gameTime)
 {
     attackBehavior.DelayedAttack.RemainingAttackDelayMilliseconds -= gameTime.ElapsedGameTime.TotalMilliseconds;
 }
Example #11
0
 private bool HasDelayedAttack(AttackBehaviorComponent attackBehavior)
 {
     return(attackBehavior.DelayedAttack != null);
 }
Example #12
0
        private void SetAttackStates(AttackBehaviorComponent attack)
        {
            if (attack.CurrentEntityState == null || !attack.CurrentEntityState.ReadyToChange)
            {
                return;
            }
            StateComponent stateComponent = attack.CurrentEntityState;

            if (attack.RemainingAttackCooldownMilliseconds > 0 || (attack.DelayedAttack != null && attack.DelayedAttack.RemainingAttackDelayMilliseconds > 0))
            {
                stateComponent.TryToSetState(EntityState.Attacking);

                if (stateComponent.CurrentState == EntityState.Attacking)
                {
                    double  PI          = Math.PI;
                    Vector2 cursorDelta = new Vector2(attack.Cursor.X, attack.Cursor.Y) - attack.SourcePos.Vector2;
                    double  Angle       = new Coord2(cursorDelta).Polar.Angle;

                    if (Angle < PI / 2 && Angle > -PI / 2)
                    {
                        stateComponent.Orientation = Direction.Right;
                    }
                    else if (Angle >= PI / 2 && Angle <= -PI / 2)
                    {
                        stateComponent.Orientation = Direction.Left;
                    }
                }
            }
            else
            {
                if (stateComponent.CurrentState == EntityState.Attacking)
                {
                    stateComponent.Clear();
                }
            }

            /*
             * if(movement.CurrentEntityState == null || !movement.CurrentEntityState.ReadyToChange) return;
             * StateComponent stateComponent = movement.CurrentEntityState;
             * if (movement.IsMoving)
             * {
             *  if (stateComponent.CurrentStatePriority < 1)
             *  {
             *      stateComponent.CurrentState = EntityState.Moving;
             *  }
             *
             *  if (stateComponent.CurrentState == EntityState.Moving)
             *  {
             *      double PI = Math.PI;
             *      double Angle = movement.Speed.Polar.Angle;
             *
             *      if (Angle < PI / 2 && Angle > -PI / 2) stateComponent.Orientation = Direction.Right;
             *      if (Angle >= PI / 2 || Angle <= -PI / 2) stateComponent.Orientation = Direction.Left;
             *
             *
             *      if (Angle <= PI / 4 && Angle >= -PI / 4) stateComponent.Direction = Direction.Right;
             *      if (Angle > PI / 4 && Angle < 3 * PI / 4) stateComponent.Direction = Direction.Down;
             *      if (Angle < -PI / 4 && Angle > 3 * -PI / 4) stateComponent.Direction = Direction.Up;
             *      if (Angle >= 3 * PI / 4 || Angle <= 3 * -PI / 4) stateComponent.Direction = Direction.Left;
             *
             *  }
             * }
             * else
             * {
             *  if (stateComponent.CurrentState == EntityState.Moving)
             *  {
             *      stateComponent.CurrentState = EntityState.Idle;
             *  }
             * }
             */
        }