コード例 #1
0
        // Does the same thing as InterpretString(), but accepts and returns multiple responses.
        public static bool InterpretStringMC(string[] acceptedAnswers, out string[] result)
        {
            result = null;
            List <string> results = new List <string>();

            if (!AskYesNo("Would you like to choose any of these?"))
            {
                return(true);
            }
            do
            {
                Output.WriteLineTagged("Here are your options:", Output.Tag.List);
                for (int answerIndex = 0; answerIndex < acceptedAnswers.Length; answerIndex++)
                {
                    string answer = acceptedAnswers[answerIndex];
                    Output.WriteLineToConsole(answerIndex + ". " + answer);
                }
                string response = GetUserResponse("Which do you choose?");
                if (InterpretInt(response, 0, acceptedAnswers.Length - 1, out int intIndex))
                {
                    results.Add(acceptedAnswers[intIndex]);
                }
                else
                {
                    if (!InterpretString(response, acceptedAnswers, out string stringResponse))
                    {
                        return(false);
                    }
                    results.Add(stringResponse);
                }
            } while (CommandInterpretation.AskYesNo("Would you like to add any more options?"));

            result = results.ToArray();
            return(true);
        }
コード例 #2
0
        // This takes in a string as input and evaluates whether it matches any commands in CommandList.
        // If it does, it runs that command, passing in any parameters that were also entered
        public bool EvaluateCommand(string userCommand)
        {
            string[] splitCommand = userCommand.Split(' ');

            foreach (Command commandItem in CommandList)
            {
                if (splitCommand[0].Equals(commandItem.Identifier, StringComparison.OrdinalIgnoreCase))
                {
                    List <string> responses = new List <string>();
                    int           commandParameter;
                    for (commandParameter = 1; commandParameter < splitCommand.Length; commandParameter++)
                    {
                        responses.Add(splitCommand[commandParameter]);
                    }
                    for (int helpLineIndex = commandParameter; helpLineIndex < commandItem.HelpLines.Length + 1; helpLineIndex++)
                    {
                        string helpLine = commandItem.HelpLines[helpLineIndex - 1];
                        Output.WriteLineTagged(helpLine, Output.Tag.Prompt);
                        responses.Add(CommandInterpretation.GetUserResponse());
                    }
                    commandItem.CustomCommand(responses.ToArray());
                    return(commandItem.TakesTime);
                }
            }
            return(false);
        }
コード例 #3
0
        // This does none of the loading; it just lists all the loadable files and prompts the user to pick one. It then redirects to the function below.
        public static bool LoadFromFile()
        {
            string fileMouth;

            if (!TryGetMapsFolder(out fileMouth))
            {
                return(false);
            }

            DirectoryInfo directory = new DirectoryInfo(fileMouth);

            Dictionary <string, FileInfo> files = new Dictionary <string, FileInfo>();

            foreach (FileInfo file in directory.GetFiles("*.xml"))
            {
                files.Add(file.Name, file);
            }

            if (files.Count == 0)
            {
                Output.WriteLineTagged("It appears there are no files to load. Either download a world file, or try designing one in the level editor", Output.Tag.Error);
                return(false);
            }

            string[] fileNames = files.Keys.ToArray();

            Output.WriteLineTagged("Choose a file from the options below.", Output.Tag.Prompt);
            if (CommandInterpretation.InterpretString(fileNames, out string result))
            {
                Game.FilePath = files[result].FullName;
                LoadFromFile(files[result].FullName);
            }
            else
            {
                if (CommandInterpretation.AskYesNo("No file found with that name. Would you like to try again?"))
                {
                    return(LoadFromFile());
                }
                return(false);
            }
            return(true);
        }
コード例 #4
0
        // This saves the world map into a file
        // WorldMap -> <worldFile>.xml
        public static void SaveToFile(string filePath)
        {
            XmlWriter         xw;
            XmlWriterSettings xmlSettings = new XmlWriterSettings();

            xmlSettings.Indent = true;
            xmlSettings.NewLineOnAttributes = true;
            xmlSettings.IndentChars         = "\t";

            try
            {
                xw = XmlWriter.Create(filePath, xmlSettings);
            }
            catch (Exception e)
            {
                Output.WriteLineTagged(e.Message, Output.Tag.Error);
                if (CommandInterpretation.AskYesNo("File path is invalid. Would you like to try again?"))
                {
                    SaveToFile(CommandInterpretation.GetUserResponse("Enter file path"));
                }
                return;
            }
            Game.FilePath = filePath;

            Level[,] levelMap = WorldMap.LevelMap;
            xw.WriteComment("RPG-Engine XML world file");
            xw.WriteComment("Version No. " + Game.Version);
            xw.WriteComment(" - - - - - - - - - - - - - - - - - - - - ");

            xw.WriteWhitespace("\n\n");
            xw.WriteStartElement("World");
            xw.WriteStartElement("Map");
            xw.WriteAttributeString("Name", WorldMap.Name);
            for (int levelY = 0; levelY < levelMap.GetLength(1); levelY++)
            {
                xw.WriteStartElement("Row");
                xw.WriteAttributeString("Number", levelY.ToString());
                for (int levelX = 0; levelX < levelMap.GetLength(0); levelX++)
                {
                    Level level = levelMap[levelX, levelY];
                    if (level == null)
                    {
                        xw.WriteStartElement("Level");
                        xw.WriteEndElement();
                        continue;
                    }
                    xw.WriteStartElement("Level");

                    xw.WriteAttributeString("Name", level.Name);
                    xw.WriteAttributeString("VisualChar", level.VisualChar.ToString());
                    xw.WriteAttributeString("LoadedLevel", level.Equals(LoadedLevel).ToString());

                    xw.WriteStartElement("LevelCoord", null);

                    xw.WriteAttributeString("X", level.LevelCoord.X.ToString());
                    xw.WriteAttributeString("Y", level.LevelCoord.Y.ToString());

                    xw.WriteEndElement();

                    #region entry points
                    xw.WriteStartElement("NorthEntry");

                    if (level.NorthEntry != null)
                    {
                        xw.WriteAttributeString("X", level.NorthEntry.X.ToString());
                        xw.WriteAttributeString("Y", level.NorthEntry.Y.ToString());
                    }

                    xw.WriteEndElement();

                    xw.WriteStartElement("EastEntry");
                    if (level.EastEntry != null)
                    {
                        xw.WriteAttributeString("X", level.EastEntry.X.ToString());
                        xw.WriteAttributeString("Y", level.EastEntry.Y.ToString());
                    }

                    xw.WriteEndElement();

                    xw.WriteStartElement("SouthEntry");
                    if (level.SouthEntry != null)
                    {
                        xw.WriteAttributeString("X", level.SouthEntry.X.ToString());
                        xw.WriteAttributeString("Y", level.SouthEntry.Y.ToString());
                    }

                    xw.WriteEndElement();

                    xw.WriteStartElement("WestEntry");
                    if (level.WestEntry != null)
                    {
                        xw.WriteAttributeString("X", level.WestEntry.X.ToString());
                        xw.WriteAttributeString("Y", level.WestEntry.Y.ToString());
                    }

                    xw.WriteEndElement();
                    #endregion

                    xw.WriteStartElement("Grid");

                    for (int y = 0; y < level.Grid.TileGrid.GetLength(1); y++)
                    {
                        xw.WriteStartElement("Row");
                        xw.WriteAttributeString("Number", y.ToString());
                        for (int x = 0; x < level.Grid.TileGrid.GetLength(0); x++)
                        {
                            Tile tileAtCoords = level.Grid.TileGrid[x, y];
                            xw.WriteStartElement("Tile");

                            xw.WriteStartElement("TileCoords");

                            xw.WriteAttributeString("X", x.ToString());
                            xw.WriteAttributeString("Y", y.ToString());

                            xw.WriteEndElement();

                            xw.WriteStartElement("Floor");

                            xw.WriteAttributeString("Name", tileAtCoords.Floor.Name);
                            xw.WriteAttributeString("VisualChar", tileAtCoords.Floor.VisualChar.ToString());

                            xw.WriteEndElement();

                            ListAllContents(tileAtCoords.Contents, xw);

                            xw.WriteEndElement();
                        }
                        xw.WriteEndElement();
                    }
                    xw.WriteEndElement();
                    xw.WriteEndElement();
                }
                xw.WriteEndElement();
            }
            xw.WriteEndElement();

            xw.WriteStartElement("Player");

            xw.WriteAttributeString("Strength", Player.Strength.ToString());

            xw.WriteStartElement("Coordinates");

            xw.WriteAttributeString("X", Player.Contents.Coordinates.X.ToString());
            xw.WriteAttributeString("Y", Player.Contents.Coordinates.Y.ToString());

            xw.WriteEndElement();

            xw.WriteStartElement("Holding");

            ListAllContents(Player.Holding, xw);

            xw.WriteEndElement();

            xw.WriteEndElement();

            xw.WriteStartElement("TileIndex");

            foreach (Contents contents in ContentsIndex)
            {
                if (contents == null)
                {
                    continue;
                }
                contents.Container = false;
                ListAllContents(contents, xw);
            }

            xw.WriteEndElement();

            xw.WriteStartElement("Dialogue");

            foreach (int key in Dialogue.Keys)
            {
                xw.WriteStartElement("Line");

                xw.WriteAttributeString("ID", key.ToString());
                xw.WriteAttributeString("Dialogue", Dialogue[key]);

                xw.WriteEndElement();
            }

            xw.WriteEndElement();

            xw.WriteStartElement("Connections");

            foreach (string key in EventHandler.IdentifierEventMapping.Keys)
            {
                xw.WriteStartElement(key);

                foreach (Connection connection in EventHandler.IdentifierEventMapping[key].ConnectionList)
                {
                    xw.WriteStartElement("Connection");
                    xw.WriteAttributeString("TriggerContentsID", connection.TriggerContentsID.ToString());
                    xw.WriteAttributeString("ResultContentsID", connection.ResultContentsID.ToString());
                    xw.WriteAttributeString("ResultType", connection.ResultType);
                    xw.WriteAttributeString("ResultInformation", connection.ResultInformation);
                    xw.WriteEndElement();
                }

                xw.WriteEndElement();
            }

            xw.WriteEndElement();

            xw.Close();

            Output.WriteLineToConsole("World file saved successfully as " + Game.FilePath);
        }
コード例 #5
0
        // Used in InterpretTile() and other cases. Prompts user for all aspects of Contents
        public static bool InterpretContents(out Contents result)
        {
            Output.WriteLineToConsole("\nContents");
            result = null;
            int uniqueID = Contents.UniqueID();

            Dictionary <string, string> preContainerParamMap = new Dictionary <string, string>()
            {
                { "Name", GetUserResponse("Name (unique identifier)<string>:") },
                { "VisualChar", GetUserResponse("Visual character (to represent on the grid)<character>:") },
                { "Transparent", GetUserResponse("Would you like this tile to be transparent? (visible and targetable through)<boolean>:") },
                { "Durability", GetUserResponse("Durability (health points)<integer>:") },
                { "Size", GetUserResponse("Size (space it takes up in containers)<integer>:") },
                { "Weight", GetUserResponse("Weight (Depending on player strength, they may or may not be able to pick this up)<float>:\nCurrent player strength is " + World.Player.Strength) },
                { "Action", string.Empty },
                { "Behavior", string.Empty },
                { "Tags", string.Empty }
            };

            Output.WriteLineTagged("Choose an action that this contents takes", Output.Tag.Prompt);
            if (!CommandInterpretation.InterpretString(UseActions.GetIdentifiers(), out string actionString))
            {
                Output.WriteLineTagged("Action was not a valid response", Output.Tag.Error);
                return(false);
            }
            preContainerParamMap["Action"] = actionString;

            if (preContainerParamMap["Action"] == "Dialogue")
            {
                string dialogue = CommandInterpretation.GetUserResponse("Please enter the line of dialogue for this contents");
                World.Dialogue.Add(uniqueID, dialogue);
            }

            Output.WriteLineTagged("Choose a behavior for this contents", Output.Tag.Prompt);
            do
            {
                if (!CommandInterpretation.InterpretString(Behavior.GetIdentifiers(), out string behaviorString))
                {
                    Output.WriteLineTagged("Behavior was not a valid response", Output.Tag.Error);
                    return(false);
                }
                preContainerParamMap["Behavior"] += behaviorString + ",";
            } while (CommandInterpretation.AskYesNo("Would you like to add any more behavior?"));

            string tempBehavior = preContainerParamMap["Behavior"];

            tempBehavior = tempBehavior.Substring(0, tempBehavior.Length - 1);

            #region Checking params
            if (!InterpretInt(preContainerParamMap["Durability"], out int durability))
            {
                Output.WriteLineTagged("Durability was not in integer format", Output.Tag.Error);
                return(false);
            }
            if (!InterpretInt(preContainerParamMap["Size"], out int size))
            {
                Output.WriteLineTagged("Size was not in integer format", Output.Tag.Error);
                return(false);
            }
            if (!InterpretFloat(preContainerParamMap["Weight"], out float weight))
            {
                Output.WriteLineTagged("Weight was not in float format", Output.Tag.Error);
                return(false);
            }

            if (!UseActions.TryGetAction(preContainerParamMap["Action"], out Action <string[], Contents> action))
            {
                Output.WriteLineTagged("Action was not found", Output.Tag.Error);
                return(false);
            }

            if (!Behavior.TryGetBehaviors(tempBehavior.Split(","), out Action <Contents>[] behavior))