Example #1
0
        private static void LoadScriptBlock(Level level, ScriptDef targetSD, XmlNode node, ScriptBlock root, string firstChildName = "")
        {
            if (node == null || root == null) {
                return;
            }

            var child = node.FirstChild;
            if (child == null) {
                return;
            }

            do {
                if (!firstChildName.Equals("")) {
                    if (!child.Name.Equals(firstChildName, StringComparison.InvariantCultureIgnoreCase)) {
                        Log.WriteInfo("Attention ! Impossible d'ajouter le scriptBlock : " + child.Name + " alors que le scriptBlock obligatoire est : " + firstChildName);
                        continue;
                    }
                }

                ScriptBlock scriptBlock = null;

                switch (child.Name.ToLower()) {
                    case "action": {
                            var a = child.Attributes?["a"];
                            if (a == null)
                                throw new Exception(" le scriptBlock <action> ne contient pas d'action \"a\" !");
                            var p = child.Attributes?["p"];
                            if (p == null)
                                throw new Exception(" le scriptBlock <action> ne contient pas de paramètres ! \"p\" ");

                            var parameters = new List<string>();

                            var i = 1;

                            do {
                                parameters.Add(p.Value);
                                ++i;
                            } while ((p = child.Attributes?["p" + i]) != null);

                            scriptBlock = new ScriptAction(level, a.Value, parameters);
                        }
                        break;

                    case "update": {
                            var delayA = child.Attributes?["delay"];
                            if (delayA == null)
                                throw new Exception(" le scriptBlock <update> ne contient pas de delay");

                            double delay = 0.0;
                            if (delayA.Value.Equals("$FRAME_RATE")) {
                                delay = 1.0 / 60.0;
                            }
                            else {
                                if (!double.TryParse(delayA.Value, out delay))
                                    throw new Exception(" le scriptBlock <update> ne contient pas de delay valide !");
                                if (delay <= 0)
                                    throw new Exception(" le scriptBlock <update> ne contient pas un delay valide ! Il est plus petit ou égal à 0 !");
                            }
                            scriptBlock = new ScriptUpdate(delay);
                            LoadScriptBlock(level, targetSD, child, scriptBlock);
                        }
                        break;
                    case "condition": {
                            var varRefA = child.Attributes?["ref"];
                            var valueConditionA = child.Attributes?["value"];

                            if (varRefA == null || valueConditionA == null)
                                throw new Exception(" le scriptBlock <condition> ne contient pas la référence vers la variable ou sa condition \"ref\" ou \"value\" !");

                            if (!targetSD.Variables.ContainsKey(varRefA.Value))
                                throw new Exception(" le scriptBlock <condition> utilise la variable : " + varRefA.Value + " alors qu'elle n'est pas définie dans <variables> !");

                            var variable = targetSD.Variables[varRefA.Value];

                            if (variable is int) {
                                if (!int.TryParse(valueConditionA.Value, out var condition)) {
                                    throw new Exception(" dans le scriptBlock <condition>, la condition utilisée ne peut pas être convertie en nombre (int) !");
                                }
                                scriptBlock = new ScriptCondition<int>(varRefA.Value, condition);
                            }
                            else if (variable is bool) {
                                if (!bool.TryParse(valueConditionA.Value, out var condition)) {
                                    throw new Exception(" dans le scriptBlock <condition>, la condition utilisée ne peut pas être convertie en booléen (bool) !");
                                }
                                scriptBlock = new ScriptCondition<bool>(varRefA.Value, condition);
                            }
                            else
                                throw new Exception(" le scriptBlock <condition> utilise une variable dont le type est inconnu !");

                            LoadScriptBlock(level, targetSD, child, scriptBlock);
                        }
                        break;
                    case "collision": {
                            var dirSide = child.Attributes?["side"];
                            var typeRef = child.Attributes?["typeRef"];
                            if (dirSide == null || typeRef == null)
                                throw new Exception(" le scriptBlock <collision> ne contient pas la direction \"side\" ou la référence \"typeRef\" !");

                            var sides = new List<PhysicsBody.CollisionSide>();

                            var rawSides = dirSide.Value.Split('|');

                            foreach (var rawSide in rawSides) {
                                if (!Enum.TryParse<PhysicsBody.CollisionSide>(rawSide, out var side))
                                    throw new Exception(" le scriptBlock <collision> contient une erreur ! Impossible de convertir : " + dirSide.Value + " en direction !");
                                sides.Add(side);
                            }

                            scriptBlock = new ScriptCollisionEvent(sides, typeRef.Value);
                            LoadScriptBlock(level, targetSD, child, scriptBlock);
                        }
                        break;
                    case "key": {
                            var codeA = child.Attributes?["code"];
                            var justPressedA = child.Attributes?["justPressed"];
                            var upA = child.Attributes?["up"];

                            if (codeA == null)
                                throw new Exception(" le scriptBlock <key> ne contient pas la touche \"code\"");

                            if (!Enum.TryParse<Keyboard.Key>(codeA.Value, true, out var key)) {
                                throw new Exception(" le scriptBlock <key> contient une touche qui n'existe pas ! : " + codeA.Value);
                            }

                            bool justPressed = false, up = false;

                            if (justPressedA != null) {
                                if (!bool.TryParse(justPressedA.Value, out justPressed))
                                    throw new Exception(" le scriptBlock <key> contient des erreurs ! Impossible de convertir : " + justPressedA.Value + " en valeur booléenne !");
                            }

                            if (upA != null) {
                                if (!bool.TryParse(upA.Value, out up))
                                    throw new Exception(" le scriptBlock <key> contient des erreurs ! Impossible de convertir : " + upA.Value + " en valeur booléenne !");
                            }

                            scriptBlock = new ScriptInput(key, justPressed, up);
                            LoadScriptBlock(level, targetSD, child, scriptBlock);
                        }
                        break;
                }

                if (scriptBlock == null)
                    throw new Exception(" le script contient un scriptBlock inconnu !");

                root.AddChild(scriptBlock);

            } while ((child = child.NextSibling) != null);
        }