首页 文章

港口线性游戏到Unity [关闭]

提问于
浏览
1

我正在编写桌面游戏“Get Bit!” . 首先我创建了一个控制台版本,现在我正在尝试将它移植到Unity .

我的问题:

在控制台中,整个游戏都是线性的 . 因此,PlayCards()向玩家询问他想要播放的牌,然后等待用户输入其值 .

在团结中,我试图通过按钮获取输入,但这不等待用户输入和与MoveSwimmer()等对比 . 我试图通过while(inupt == -1)得到值,但这冻结了整个游戏 . 目前Run()方法属于Game:MonoBehaviour类附加到相机 .

根据McAden的建议,这是有问题的函数的代码:

void Game::Update ()
{
    this.GameRun = true;
    Debug.Log(string.Format("Round: {0}", (round - 1)));
    Debug.Log("POSITION: " + PlayerPositionString());

    while (GameRun)
    {
        PlayCards();
        MoveSwimmers();
        GetBit();
        EndTurn();
    }

    Debug.Log(string.Format("Congrats! Player {0} won!", playerPosition.First()));
}

private void Game::PlayCards()
{
    for (int i = 0; i < playerScripts.Count; i++)
    {
        if (playerScripts [i].IsAlive())
            cardsPlayed.Add(playerScripts [i].PlaceCard());
    }
}

override public Card Player::PlaceCard()
{
    // a MonoBehaviour that shows for each card a Button that sets the value member ChoosenCardValue (on default -1).
    GUICardChooser chooser = Camera.main.GetComponent<GUICardChooser>();

    while (chooser.ChoosenCardValue == -1)
        ;

    int cardIndex = cards.FindIndex(c => c.Value == chooser.ChoosenCardValue);
    Card theChoosenOne = cards [cardIndex];
    cards.RemoveAt(cardIndex);

    return theChoosenOne;
}

我是否专注于错误的模式?我该如何解决这个问题?

感谢帮助 .

2 回答

  • 1

    首先,我认为您应该查看Unity文档和Unity示例 .

    但是如果你想实现它,其中一种可能性就是创建一个状态mashine . 在Update()方法中,您将根据当前状态进行操作 . 但这只是一种可能性 . 这一切都取决于你的游戏玩法......

  • 0

    在Unity3D中,您无需定义自己的游戏循环 . Unity already does that for you . 创建 Update 函数 .

    Update 函数内 - 检查输入并对其作出反应 . 由于它处于循环中,因此请使用 if 而不是while . Update 已经在一个循环内,但你最终可能会查找它与 FixedUpdate 之间的差异 .

    就像是:

    void Update
    {
      if (Input.GetKeyUp (KeyCode.LeftArrow))
      {
        DoSomething();
      }
    }
    

    你想研究different ways of dealing with input . 你不会总是想使用 GetKeyUp . 例如,您可以使用 GetAxisGetButtonUp .

    正如@Didier在他的回答中建议的那样,从长远来看,你可能希望最终实现某种状态机 .

相关问题