所以,我现在正在SDL2中开展一个简单的项目,而且我很难在代码中找到对话树的代码。除了这个,我几乎完成了所有工作。那么让我们从对话树开始:
因此,对话树基本上是一个分支故事,用户能够响应并影响要求获得不同结果的问题。这在游戏中越来越受欢迎。
接下来我会解释我做了什么。我将通过向您展示我的代码摘录来实现此目的:
switch(faze){
case 1:
if(button1Press){
button1Press = false;
textToAdd += "That's nice, I'm glad to hear it.\n";
answer1TextToAdd = "As am I";
answer2TextToAdd = "How are you holding up?";
answer3TextToAdd = "Wassup with you?";
}else if(button2Press){
button2Press = false;
textToAdd += "What do you mean Meh?\n";
answer1TextToAdd = "Meh";
answer2TextToAdd = "Just; Meh...";
answer3TextToAdd = "I've been up and down recently.";
}else if(button3Press){
button3Press = false;
textToAdd += "I'm sorry, you wanna talk about it?.\n";
answer1TextToAdd = "No, I'm fine.";
answer2TextToAdd = "I'll live";
answer3TextToAdd = "Yeah...";
}
break;
case 2:
if(button1Press){
button1Press = false;
textToAdd += "Cool.\n";
answer1TextToAdd = "I am.";
answer2TextToAdd = "Totally.";
answer3TextToAdd = "Yup.";
}else if(button2Press){
button2Press = false;
textToAdd += "Wow.\n";
answer1TextToAdd = ":S";
answer2TextToAdd = "Yup.";
answer3TextToAdd = "I know.";
}else if(button3Press){
button3Press = false;
textToAdd += "Go on...\n";
answer1TextToAdd = "It doesn't matter.";
answer2TextToAdd = "I'm fine";
answer3TextToAdd = "Yeah...";
}
break;
所以,让我解释一下; faze是定义我们所处游戏点的变量,当按下其中一个按钮时会增加,“TextToAdd”不重要,但是回答1,回答2和回答3是用户的3个可能答案可以给。
正如您所看到的,它只响应按钮按下,用户说的不重要,响应完全基于哪个按钮。这导致对话树看起来更像这样:
正如您所看到的,它忽略了所提出的问题,甚至是答案。我的引擎唯一要做的就是按下哪个按钮。
答案 0 :(得分:2)
我实际上将它组织得更“复杂”。
对话框中的每一步都应该有一个指向下一步的指针,所以你可以 选择,对话框的一部分是下一个。 这使一切变得更容易,你甚至可以回到你的树上。
所以Dialogpart类持有:
*指向下一部分的指针
*可行的选择
* func选择
* func绘制选项按钮
像你这样的实现对于一个小应用程序是可以的,但是可怕的 更大的游戏或更长的拨号。