Example #1
0
        public Vector2 GetHandPosition(TerraGuardian guardian, int AnimationFrame, HeldHand hand)
        {
            Vector2 HandPosition = guardian.Position;

            if ((hand == HeldHand.Right || hand == HeldHand.Both) && guardian.Base.DontUseRightHand)
            {
                hand = HeldHand.Left;
            }
            if ((hand == HeldHand.Left || hand == HeldHand.Both) && guardian.Base.DontUseLeftHand)
            {
                hand = HeldHand.Right;
            }
            switch (hand)
            {
            case HeldHand.Left:
                HandPosition += guardian.GetLeftHandPosition(AnimationFrame);
                break;

            case HeldHand.Right:
                HandPosition += guardian.GetRightHandPosition(AnimationFrame);
                break;

            case HeldHand.Both:
                HandPosition += guardian.GetBetweenHandsPosition(AnimationFrame);
                break;
            }
            return(HandPosition);
        }
        public override void Update(TerraGuardian guardian)
        {
            if (guardian.furniturex > -1)
            {
                guardian.LeaveFurniture(false);
            }
            if (guardian.KnockedOut || guardian.IsBeingPulledByPlayer)
            {
                InUse = false;
                return;
            }
            if ((guardian.OwnerPos > -1 && guardian.PlayerMounted && !Main.player[guardian.OwnerPos].GetModPlayer <PlayerMod>().KnockedOut) || guardian.Is2PControlled)
            {
                if (!guardian.MoveDown)
                {
                    InUse = false;
                    return;
                }
            }
            if (guardian.ItemAnimationTime > 0)
            {
                return;
            }
            Vector2 TargetPosition = Vector2.Zero;
            int     TargetWidth = 0, TargetHeight = 0;
            bool    TryReaching               = false;
            bool    IsMountedPlayer           = false;
            bool    AffectedByNegativeHealing = false;

            if (TargetIsPlayer)
            {
                PlayerMod pm = RevivePlayer.GetModPlayer <PlayerMod>();
                AffectedByNegativeHealing = pm.NegativeReviveBoost;
                TargetPosition            = RevivePlayer.position;
                TargetWidth  = RevivePlayer.width;
                TargetHeight = RevivePlayer.height;
                if (RevivePlayer.dead || !RevivePlayer.active || !pm.KnockedOut)
                {
                    InUse = false;
                    return;
                }
                if (guardian.PlayerMounted && RevivePlayer.whoAmI == guardian.OwnerPos)
                {
                    IsMountedPlayer = true;
                }
            }
            else
            {
                TargetPosition = ReviveGuardian.TopLeftPosition;
                TargetWidth    = ReviveGuardian.Width;
                TargetHeight   = ReviveGuardian.Height;
                if (!ReviveGuardian.Active || ReviveGuardian.Downed || !ReviveGuardian.KnockedOut)
                {
                    InUse = false;
                    return;
                }
                if (ReviveGuardian.OwnerPos == guardian.OwnerPos && ReviveGuardian.PlayerControl && guardian.PlayerMounted)
                {
                    IsMountedPlayer = true;
                }
            }
            bool RepelingEnemies = false;

            guardian.MoveLeft = guardian.MoveRight = false;
            if (guardian.TargetID > -1 && !AffectedByNegativeHealing)
            {
                Vector2 EnemyPosition;
                int     EnemyWidth, EnemyHeight;
                guardian.GetTargetInformation(out EnemyPosition, out EnemyWidth, out EnemyHeight);
                EnemyPosition.X += EnemyWidth * 0.5f;
                EnemyPosition.Y += EnemyHeight * 0.5f;
                if (IsMountedPlayer || (Math.Abs(EnemyPosition.X - guardian.Position.X) < 168f + (EnemyWidth + guardian.Width) * 0.5f &&
                                        Math.Abs(EnemyPosition.Y - guardian.CenterY) < 168f + (EnemyHeight + guardian.Height) * 0.5f))
                {
                    RepelingEnemies          = true;
                    IgnoreCombat             = false;
                    guardian.AttackingTarget = true;
                }
                else
                {
                    IgnoreCombat = true;
                }
            }
            else
            {
                IgnoreCombat = false;
            }
            if (!RepelingEnemies)
            {
                bool OffSetToTheLeft = TargetPosition.X + TargetWidth * 0.5f < guardian.Position.X;
                {
                    int Animation    = guardian.Base.StandingFrame;
                    int ArmAnimation = -1;
                    if (IsMountedPlayer)
                    {
                        ArmAnimation = guardian.Base.ItemUseFrames[2];
                    }
                    else
                    {
                        if (guardian.Base.ReviveFrame > -1)
                        {
                            Animation = guardian.Base.ReviveFrame;
                        }
                        else if (guardian.Base.DuckingFrame > -1)
                        {
                            Animation    = guardian.Base.DuckingFrame;
                            ArmAnimation = guardian.Base.DuckingSwingFrames[2];
                        }
                    }
                    if (ArmAnimation == -1)
                    {
                        ArmAnimation = Animation;
                    }
                    guardian.Base.GetBetweenHandsPosition(ArmAnimation, out int x, out int y);
                    float ArmXDistance = (x - guardian.SpriteWidth * 0.5f) * guardian.Scale;
                    if (ArmXDistance > 0)
                    {
                        TargetWidth += (int)ArmXDistance;
                        if (OffSetToTheLeft)
                        {
                            TargetPosition.X -= ArmXDistance;
                        }
                    }
                }
                if (IsMountedPlayer || new Rectangle((int)TargetPosition.X, (int)TargetPosition.Y, TargetWidth, TargetHeight).Intersects(guardian.HitBox))//(MainMod.RectangleIntersects(guardian.TopLeftPosition, guardian.Width, guardian.Height, TargetPosition, TargetWidth, TargetHeight))
                {
                    guardian.Jump = false;
                    float DistanceFromTarget = Math.Abs(guardian.Position.X - (TargetPosition.X + TargetWidth * 0.5f));
                    if (DistanceFromTarget < 8)
                    {
                        if (Math.Abs(guardian.Velocity.X) < 2f)
                        {
                            if (guardian.Position.X < TargetPosition.X + TargetWidth * 0.5f)
                            {
                                guardian.MoveLeft = true;
                            }
                            else
                            {
                                guardian.MoveRight = true;
                            }
                        }
                    }
                    else if (guardian.Velocity.X != 0)
                    {
                        if (Math.Abs(guardian.Position.X + guardian.Velocity.X - (TargetPosition.X + TargetWidth * 0.5f)) < 12)
                        {
                            guardian.MoveLeft    = guardian.MoveRight = false;
                            guardian.Velocity.X *= 0.8f;
                        }
                    }
                    else if (guardian.Velocity.X == 0)
                    {
                        {
                            Vector2 FacingLeftPosition  = guardian.GetLeftHandPosition(guardian.Base.ReviveFrame, true),
                                    FacingRightPosition = FacingLeftPosition;
                            FacingLeftPosition.X  *= -1;
                            FacingLeftPosition.X  += guardian.Position.X - (TargetPosition.X + TargetWidth * 0.5f);
                            FacingRightPosition.X += guardian.Position.X - (TargetPosition.X + TargetWidth * 0.5f);
                            guardian.FaceDirection(Math.Abs(FacingLeftPosition.X) < Math.Abs(FacingRightPosition.X));
                        }
                        byte ReviveBoost = 1;
                        if (!guardian.IsAttackingSomething)
                        {
                            ReviveBoost += 2;
                        }
                        bool IsMounted = guardian.PlayerMounted;
                        if (TargetIsPlayer)
                        {
                            RevivePlayer.GetModPlayer <PlayerMod>().ReviveBoost += ReviveBoost;
                        }
                        else
                        {
                            ReviveGuardian.ReviveBoost += ReviveBoost;
                        }
                        guardian.StuckTimer    = 0;
                        guardian.OffHandAction = false;
                        if (SpeakToFallen)
                        {
                            SpeakToFallen = false;
                            if (MainMod.CompanionsSpeaksWhileReviving)
                            {
                                guardian.SaySomething(guardian.Base.ReviveMessage(guardian, TargetIsPlayer, (TargetIsPlayer ? RevivePlayer : null), (!TargetIsPlayer ? ReviveGuardian : null)));
                            }
                        }
                    }
                }
                else
                {
                    TryReaching = true;
                }
            }
            if (TryReaching)
            {
                if (ResTime >= 5 * 60)
                {
                    guardian.Position.X = TargetPosition.X + Main.rand.Next(TargetWidth);
                    guardian.Position.Y = TargetPosition.Y + TargetHeight - 1;
                    guardian.FallStart  = (int)guardian.Position.Y / 16;
                }
                else if (TargetPosition.X + TargetWidth * 0.5f - guardian.Position.X < 0)
                {
                    guardian.MoveLeft = true;
                }
                else
                {
                    guardian.MoveRight = true;
                }
                guardian.WalkMode = false;
                ResTime++;
            }
            else
            {
                ResTime = 0;
            }
        }