开发者

Collection of criteria that a bool CriteriaAreMet method checks

开发者 https://www.devze.com 2023-03-24 21:12 出处:网络
I\'m trying to design a simple skill system for a game. In each game loop I want to iterate through all the player\'s skills and determine whether any should advance.

I'm trying to design a simple skill system for a game. In each game loop I want to iterate through all the player's skills and determine whether any should advance.

I thought I'd construct something like below. I don't know what type of component to use for the list of advancement criteria in the Skill class.

I'm not married to this approach but I'm hoping to avoid writing a separate concrete class for each skill; there will be dozens, and I plan to use this as a library.

public class Player
{
    public List<Skill> Skills { get; }
}
public class Skill
{
    public List<???> AdvancementCriteria { get; }

    bool AdvancementCriteriaMet()
    {
        // Iterate through AdvancementCriteria
        // if any return FALSE then return FALSE
    }

    void Advance()
    {
        // Advance the skill
    }

    public void Update()
    {
        if(AdvancementCriteriaMet) Advance();
    }
}
public class GameLoop
{
    Player player = new Player();

    void Loop()
    {
        foreach(Skill s in player.Skills) s.Update();
    }
}

EDIT for clarification: I want to be able to assert things as criteria like:

  • Athletics skill: Player is jumping || Player is running || Player is swimming
  • Sword skill: Player.InCombat == true &&a开发者_开发知识库mp; Player.Wield.WieldType == Sword
  • First Aid skill: BandagesUsedCurrent > BandagesUsedPrevious


You can use a delegate, with a set of lambda expressions.

public delegate bool AdvancementCriterion(Player player);
public List<AdvancementCriterion> AdvancementCriteria { get; }

AdvancementCriteria.Add(p => p.Thingy > x);

if (AdvancementCriteria.All(c => c(Player))


public List<Func<bool>> AdvancementCriteria { get; }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号