Exemplo n.º 1
0
        /// <summary>
        /// Parses the provided strings into a LevelContainer
        /// </summary>
        /// <param name="levelNode">The to parse form LevelNode</param>
        /// <returns>The ready to initialize LevelContainer</returns>
        /// <exception cref="Exception">If something could not be parsed properly</exception>
        public LevelContainer parseLevelFromXmlString(XmlNode levelNode)
        {
            var gravityStringX = ParseHelper.getAttributeValueByName(levelNode, "gravity_x");

            if (!float.TryParse(gravityStringX, out var gravityX))
            {
                throw new Exception("Could not parse gravityScaleX argument: " + gravityStringX);
            }

            var gravityStringY = ParseHelper.getAttributeValueByName(levelNode, "gravity_y");

            if (!float.TryParse(gravityStringY, out var gravityY))
            {
                throw new Exception("Could not parse gravityScaleY argument: " + gravityStringY);
            }

            var level = new LevelContainer(parseLevelHeadFromXmlString(levelNode), new Vector2(gravityX, gravityY));

            var elements = levelNode.SelectSingleNode("Elements");

            if (elements != null)
            {
                // Then goes through each element
                foreach (XmlNode element in elements.ChildNodes)
                {
                    // Parses position and rotation
                    var position = ParseHelper.parsePositionFromNode(element);
                    var angle    = ParseHelper.parseRotationFromNode(element);

                    // Its ElementType
                    if (!Enum.TryParse(element.Name, out ElementType elementType))
                    {
                        throw new Exception($"Could not parse the name ElementType {element.Name}");
                    }

                    var idString = ParseHelper.getAttributeValueByName(element, "id");
                    if (!int.TryParse(idString, out var id))
                    {
                        throw new Exception($"Could not parse the id {idString}");
                    }

                    // And if its Wall, get its scale and create a WallInitializer
                    if (elementType == ElementType.Wall)
                    {
                        var scale = ParseHelper.parseScaleFromNode(element);
                        level.addInitializer(new WallInitializer(scale, elementType, id, position, angle));
                        continue;
                    }

                    // If it is a Effector parses the Parameters
                    var parameters     = new Dictionary <string, string>();
                    var parameterNodes = element.SelectNodes("Parameter");
                    foreach (XmlNode parameterNode in parameterNodes)
                    {
                        var attributeName = parameterNode.Attributes[0].Value;
                        parameters.Add(attributeName, parameterNode.InnerText);
                    }

                    // And if its ColliderBody, create a ColliderBodyInitializer with its parameter
                    if (elementType == ElementType.ColliderBody)
                    {
                        level.addInitializer(new ColliderBodyInitializer(parameters, elementType, id, position, angle));
                        continue;
                    }


                    if (elementType == ElementType.Effector)
                    {
                        // And its EffectorType
                        string effectorTypeString = ParseHelper.getAttributeValueByName(element, "type");
                        if (!Enum.TryParse(effectorTypeString, out EffectorType effectorType))
                        {
                            throw new Exception($"Could parse {effectorTypeString} to a EffectorType");
                        }

                        // Finally add the EffectorInitializer
                        level.addInitializer(new EffectorInitializer(effectorType, parameters, elementType, id, position,
                                                                     angle));
                    }

                    if (elementType == ElementType.Trigger)
                    {
                        // And its TriggerType
                        string triggerTypeString = ParseHelper.getAttributeValueByName(element, "type");
                        if (!Enum.TryParse(triggerTypeString, out TriggerType triggerType))
                        {
                            throw new Exception($"Could parse {triggerTypeString} to a EffectorType");
                        }

                        // Finally add the EffectorInitializer
                        level.addInitializer(new TriggerInitializer(triggerType, parameters, elementType, id, position,
                                                                    angle));
                    }
                }
            }

            return(level);
        }
Exemplo n.º 2
0
        public TutorialContainer parseTutorialFromXmlString(XmlNode xmlDocument)
        {
            var parts = xmlDocument.SelectSingleNode("TutorialSequence");

            var tutorialContainer = new TutorialContainer();

            if (parts != null)
            {
                // Then goes through each element
                foreach (XmlNode part in parts.ChildNodes)
                {
                    var partIdString = ParseHelper.getAttributeValueByName(part, "id");
                    if (!int.TryParse(partIdString, out var partId))
                    {
                        throw new Exception($"Could not parse part ID: {partIdString}");
                    }

                    var partContainer = new PartContainer(partId);
                    tutorialContainer.addPart(partContainer);

                    var checkEvents  = part.SelectNodes("CheckEvent");
                    var helpDisplays = part.SelectNodes("HelpDisplay");
                    var uiMasks      = part.SelectNodes("TutorialUiMask");

                    if (checkEvents != null)
                    {
                        foreach (XmlNode checkEvent in checkEvents)
                        {
                            var typeString = ParseHelper.getAttributeValueByName(checkEvent, "type");
                            if (!Enum.TryParse(typeString, out CheckEventType checkEventType))
                            {
                                throw new Exception($"Could not parse EventType: {typeString}");
                            }

                            var idString = ParseHelper.getAttributeValueByName(checkEvent, "id");
                            if (!int.TryParse(idString, out var id))
                            {
                                throw new Exception($"Could not parse id: {idString}");
                            }

                            if (checkEventType == CheckEventType.ElementEvent || checkEventType == CheckEventType.PartElementEvent)
                            {
                                var elementIdString = ParseHelper.getAttributeValueByName(checkEvent, "elementId");
                                if (!int.TryParse(elementIdString, out var elementId))
                                {
                                    throw new Exception($"Could not parse elementId: {elementIdString}");
                                }
                                if (checkEventType == CheckEventType.ElementEvent)
                                {
                                    partContainer.addElement(new ElementCheckEvent(checkEvent.InnerText, elementId),
                                                             id);
                                }
                                else
                                {
                                    partIdString = ParseHelper.getAttributeValueByName(checkEvent, "partId");
                                    if (!int.TryParse(partIdString, out partId))
                                    {
                                        throw new Exception($"Could not parse partId: {partIdString}");
                                    }
                                    partContainer.addElement(new PartElementCheckEvent(checkEvent.InnerText,
                                                                                       partId, elementId), id);
                                }
                            }
                            else if (checkEventType == CheckEventType.GameObjectEvent)
                            {
                                var gameObjectName = ParseHelper.getAttributeValueByName(checkEvent, "name");
                                partContainer.addElement(new GameObjectCheckEvent(checkEvent.InnerText,
                                                                                  gameObjectName), id);
                            }
                        }
                    }

                    if (helpDisplays != null)
                    {
                        foreach (XmlNode helpDisplay in helpDisplays)
                        {
                            var typeString = ParseHelper.getAttributeValueByName(helpDisplay, "type");
                            if (!Enum.TryParse(typeString, out HelpDisplayType helpDisplayType))
                            {
                                throw new Exception($"Could not parse EventType: {typeString}");
                            }

                            var idString = ParseHelper.getAttributeValueByName(helpDisplay, "id");
                            if (!int.TryParse(idString, out var id))
                            {
                                throw new Exception($"Could not parse id: {idString}");
                            }

                            var parameters     = new Dictionary <string, string>();
                            var parameterNodes = helpDisplay.SelectNodes("Parameter");
                            foreach (XmlNode parameterNode in parameterNodes)
                            {
                                var attributeName = parameterNode.Attributes[0].Value;
                                parameters.Add(attributeName, parameterNode.InnerText);
                            }

                            partContainer.addElement(new HelpDisplayInitializer(helpDisplayType, parameters), id);
                        }
                    }

                    if (uiMasks != null)
                    {
                        foreach (XmlNode uiMask in uiMasks)
                        {
                            var idString = ParseHelper.getAttributeValueByName(uiMask, "id");
                            if (!int.TryParse(idString, out var id))
                            {
                                throw new Exception($"Could not parse id: {idString}");
                            }

                            var parameters     = new Dictionary <string, string>();
                            var parameterNodes = uiMask.SelectNodes("Parameter");
                            foreach (XmlNode parameterNode in parameterNodes)
                            {
                                var attributeName = parameterNode.Attributes[0].Value;
                                parameters.Add(attributeName, parameterNode.InnerText);
                            }

                            partContainer.addElement(new TutorialUiMaskInitializer(parameters), id);
                        }
                    }
                }
            }

            return(tutorialContainer);
        }