简体   繁体   English

如何在 Unity 中对动作和动画进行排队以进行回合制战斗?

[英]How to queue up actions and animations in Unity for turn-based combat?

I'm working on a turn-based combat sequence in Unity on C# and I currently have turn switching working and inputs and AI.我正在 C# 上的 Unity 中进行基于回合的战斗序列,目前我有回合切换工作和输入和 AI。 But I can't quite figure out how to queue it up with animations anf stuff, so that one happens after another in ExecuteTurnActions() ;但我不太清楚如何将它与动画和其他东西一起排队,以便在ExecuteTurnActions()中一个接一个地发生;

So my question is basically, how do I structure ExecuteTurnActions() ?所以我的问题基本上是,我如何构建ExecuteTurnActions()

I need it to go through all Entities and play their animations according to what's going on, like:我需要它遍历所有实体并根据正在发生的事情播放它们的动画,例如:

// PlayAnimation is simply Animator.SetTrigger(string)
Entities[0].PlayAnimation("Attack") or Entities[0].PlayAnimation("ReceiveDamage")

I need to make sure that when one Entity attacks, right when the attack happens, the other Entity receives damage and plays its animation at that moment.我需要确保当一个实体攻击时,就在攻击发生时,另一个实体受到伤害并在那一刻播放其动画。 I know there are animation events, but I'm not sure how to make it flow from one place and make sure each entity knows which entity to trigger for animation next (when attacked for example).我知道有动画事件,但我不确定如何让它从一个地方流动并确保每个实体都知道接下来要触发哪个实体进行动画(例如,当受到攻击时)。 Any suggestions?有什么建议么?

My structure looks something like this in pseudocode :我的结构在伪代码中看起来像这样:

ENTITIES_TO_CREATE = MatchMaker.GetNumberOfPlayersAndEnemies();

void Start() {

    InitializeAllThatsNeeded();
    Entities[] = new Entity[ENTITIES_TO_CREATE];  

    // In a for loop, load up stuff and populate Entities
    Entities[0].Name = MatchMaker.GetName(0);
    Entities[0].Strength = MatchMaker.GetStrength(0); // etc etc etc

}

void Update() {

    bool HaveAllEntitiesSelectedTurnAction = new bool[Entities.Length];

    // Check every frame if all Entities have selected a turn action
    for (int i = 0; i <= Entities.Length - 1; i++) {

            HaveAllEntitiesSelectedTurnAction[i] = Entities[i].isTurnActionSelected;

        }

    // If the Entity that goes first has its action selected, then let the rest select theirs
    if(Entities[EntityHasFirstTurn].TurnActionSelected) {

        //All logic happens here, if Entity happens to be player then the player
        //is allowed input, if enity is NPC then generate hit value etc.

        // isTurnActionSelected set in this part

        // Now if all have selected their action, execute them in order
        if(HaveAllEntitiesSelectedTurnAction.All(x => x) {

            ExecuteTurnActions();

        }

    }   

}

To prioritize events I would suggest using a priority queue data structure.为了对事件进行优先级排序,我建议使用优先级队列数据结构。

As for ExecuteTurnActions() , it should only set appropriate flags of an Animator Component (based on the queue, most likely).至于ExecuteTurnActions() ,它应该只设置动画组件的适当标志(最有可能基于队列)。 You might want each entity to have its own event queue which interacts with its own Animator.您可能希望每个实体都有自己的事件队列,该队列与自己的 Animator 交互。

Building a state machine for all your animations is what you need to do first.首先需要为所有动画构建状态机。 Setting certain states will allow Unity to handle all the transitions.设置某些状态将允许 Unity 处理所有转换。 To learn more about the Animator and handling its states look here .要了解有关 Animator 和处理其状态的更多信息,请查看此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM