检查某些内容是否增加了 1

本文关键字:增加 是否 检查 | 更新日期: 2023-09-27 18:25:54

    public int LevelReward()
    {
        int oldlevel = Client.Player.Level;
        if (Client.Player.Level == oldlevel + 1)
        {
            for (int i = 0; i < Client.Player.Inventory.Length; i++)
                if (Client.Player.Inventory[i] == null)
                {
                    Client.Player.Inventory[i] = Client.Player.Manager.GameData.Items[0x7016];
                    Client.Player.UpdateCount++;
                    Client.Player.SaveToCharacter();
                    Client.Player.SendInfo("You've been given 10 gold for leveling up!");
                }
        }
        return 0;
    }

我想要的是,每当玩家升级时,此代码都会给他们物品"10 金币"(这就是0x7016(但是看看代码的顶部,我很难让它检查/意识到玩家何时升级。下面的代码只是它的其余部分,在库存点等中生成项目,但我只需要顶部的帮助

        int oldlevel = Client.Player.Level;
        if (Client.Player.Level == oldlevel + 1)

检查某些内容是否增加了 1

最好的方法是在实际发生的事件上启动 10 金币。你什么时候更新玩家等级计数?在这一点上给黄金。

我会在我的播放器类上使用INotifyPropertyChanged。然后,您可以在不同的属性更改时收到通知,并将事件逻辑置于事件发生 x 时应该发生的情况。

下面是一个示例。你可以将其复制并粘贴到 LinqPad 应用中,并在将概念应用于项目之前使用它。

属性更改通知的示例代码:

public static void Main()
{
    var player = new Player { Level = 1 };
    player.PropertyChanged += (sender, pcEventArgs) =>
    {
          var self = (Player)sender;
          self.Gold += 10;
          Console.WriteLine($"Player has leveled up to level {self.Level} and given 10 gold, gold is now at {self.Gold}.");
    };
    for(var i = 0; i < 10; i++)
    {
        player.Level++;
    }
}
public class Player : INotifyPropertyChanged
{
    public int Gold { get; set; }
    private int level;
    public int Level
    {
        get
        { 
            return level;
        }
        set 
        {
            level = value;
            OnPropertyChanged(nameof(Level));
        } 
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

结果:

每次级别更改时,都会触发事件。

控制台输出为:

Player has leveled up to level 2 and given 10 gold, gold is now at 10.
Player has leveled up to level 3 and given 10 gold, gold is now at 20.
Player has leveled up to level 4 and given 10 gold, gold is now at 30.
Player has leveled up to level 5 and given 10 gold, gold is now at 40.
Player has leveled up to level 6 and given 10 gold, gold is now at 50.
Player has leveled up to level 7 and given 10 gold, gold is now at 60.
Player has leveled up to level 8 and given 10 gold, gold is now at 70.
Player has leveled up to level 9 and given 10 gold, gold is now at 80.
Player has leveled up to level 10 and given 10 gold, gold is now at 90.
Player has leveled up to level 11 and given 10 gold, gold is now at 100.

在将 INotifyPropertyChange 添加到您的播放器类后,您将在第一次创建播放器后立即将更改的事件放在代码中的某个位置的播放器上。

Client.Player.PropertyChanged += (sender, pcEventArgs) =>
{
    var player= (Player)sender;
    if(pcEventArgs.PropertyName == nameof(player.Level))
    {
        for (int i = 0; i < player.Inventory.Length; i++)
        { 
            if (player.Inventory[i] == null)
            {
                player.Inventory[i] = player.Manager.GameData.Items[0x7016];
                player.UpdateCount++;
                player.SaveToCharacter();
                player.SendInfo("You've been given 10 gold for leveling up!");
            }
        }
    }
}

现在,只要您拥有Client.Player.Level++或更改该值,它就会触发您的逻辑。

注意:虽然你的循环看起来可能会给他们库存中的每件物品提供 10 个金币。不确定这是否是您想要的,但是您要求知道他们的水平何时发生变化的概念得到了回答。