Esempio n. 1
0
        public override bool TakeDamage(Vector2 force, Vector2 source, int damage, int from, Weapon weapon)
        {
            Core.Velocity += force;

            if (GameContext.GameController.IsFriendlyFire(Player.ClientId, from))
            {
                return(false);
            }

            if (from == Player.ClientId)
            {
                damage = Math.Max(1, damage / 2);
            }

            var prevHealth = Health;
            var prevArmor  = Armor;

            if (damage > 0)
            {
                if (Armor > 0)
                {
                    if (damage > 1)
                    {
                        Health--;
                        damage--;
                    }

                    if (damage > Armor)
                    {
                        damage -= Armor;
                        Armor   = 0;
                    }
                    else
                    {
                        Armor -= damage;
                        damage = 0;
                    }
                }

                Health -= damage;
            }

            GameContext.CreateDamageIndicator(Position, source, Player.ClientId,
                                              prevHealth - Health,
                                              prevArmor - Armor, from == Player.ClientId);

            bool AttackerExist() => from >= 0 && from != Player.ClientId && GameContext.Players[from] != null;

            if (AttackerExist())
            {
                var mask = BaseGameContext.MaskOne(from);
                for (var i = 0; i < GameContext.Players.Length; i++)
                {
                    if (GameContext.Players[i] != null && (
                            GameContext.Players[i].Team == Team.Spectators ||
                            GameContext.Players[i].DeadSpectatorMode) &&
                        GameContext.Players[i].SpectatorId == from)
                    {
                        mask |= BaseGameContext.MaskOne(i);
                    }
                }

                GameContext.CreateSound(GameContext.Players[from].ViewPos, Sound.Hit, mask);
            }

            if (Health <= 0)
            {
                Die(from, weapon);

                if (AttackerExist())
                {
                    GameContext.Players[from]
                    .GetCharacter()?
                    .SetEmote(Emote.Happy, Server.Tick + Server.TickSpeed);
                }

                return(false);
            }

            GameContext.CreateSound(Position, damage > 2
                ? Sound.PlayerPainLong
                : Sound.PlayerPainShort);

            SetEmote(Emote.Pain, Server.Tick + Server.TickSpeed / 2);
            return(true);
        }