// TODO. script내용 string을 넘겨 받던, 경로를 넘겨 받던가 해야 한다.
    public void Create()
    {
        interpreter = new Interpreter();

        interpreter.standardOutput = (string s) => Say(s);
        interpreter.implicitOutput = (string s) => Say("<color=#66bb66>" + s + "</color>");
        interpreter.errorOutput    = (string s) =>
        {
            Say("<color=red>" + s + "</color>");
            interpreter.Stop();
        };

        /*
         *      Intrinsic f = Intrinsic.Create("throw");
         *      f.AddParam("param", "");
         *      f.code = (context, partialResult) =>
         *      {
         *          //var rs = context.interpreter.hostData as ReindeerScript;
         *          //Reindeer reindeer = rs.reindeer;
         *          //float eCost = (float)context.GetVar("energy").DoubleValue();
         *          //if (reindeer.Throw(eCost))
         *          //    return Intrinsic.Result.True;
         *
         *          string param = context.GetVar("param").ToString();
         *
         *          Say("throw : " + param);
         *          return Intrinsic.Result.True;
         *      };
         */

        // 일단 최대 파라메터는 2개
        Intrinsic f = Intrinsic.Create("throwCode");

        f.AddParam("codename"); // 코드이름
        f.AddParam("param1");
        f.AddParam("param2");
        f.code = throwCode;


        //         interpreter.SetGlobalValue("_param", new ValString("preaction"));
        //         interpreter.RunUntilDone();
        //
        //
        //         interpreter.Restart();
        //         interpreter.SetGlobalValue("_param", new ValString("discard"));
        //         interpreter.RunUntilDone();
    }
        public static void InitializeIntrinsics()
        {
            if (_hasStaticInit)
            {
                return;
            }
            _hasStaticInit = true;
            // Load the constructor
            Intrinsic ctor = Intrinsic.Create("ExampleCustom");

            ctor.AddParam(NumAValueName, 0.0);
            ctor.AddParam(StringBValueName, "");
            ctor.code = (context, partialResult) =>
            {
                ValNumber numA = context.GetVar(NumAValueName) as ValNumber;
                ValString strB = context.GetVar(StringBValueName) as ValString;

                ExampleCustomVal customVal = new ExampleCustomVal(
                    numA != null ? (float)numA.value : 0,
                    strB != null ? strB.value : "");

                return(new Intrinsic.Result(customVal));
            };

            _isPotatoFunction = Intrinsic.Create(IsPotatoFunction, false);
            _isPotatoFunction.AddParam("item");
            _isPotatoFunction.code = (context, partialResult) =>
            {
                ValString str = context.GetVar("item") as ValString;
                if (str != null && str.value == "potato")
                {
                    return(Intrinsic.Result.True);
                }
                return(Intrinsic.Result.False);
            };
        }
Exemple #3
0
        void Prelude()
        {
            // Create Displays
            int display_counter = 0;

            SpriteDisplay new_display = new SpriteDisplay(spriteBatch);

            new_display.Cartesian = Cartesian;
            displays.Add(new_display);
            var new_display_val = new_display.NewClass();

            displays[display_counter].DisplayRef = new_display_val as ValMap;
            display_counter++;

            new_display           = new SpriteDisplay(spriteBatch);
            new_display.Cartesian = Cartesian;
            displays.Add(new_display);
            new_display_val = new_display.NewClass();
            displays[display_counter].DisplayRef = new_display_val as ValMap;
            display_counter++;

            // std print functions
            interpreter.standardOutput = (string s) => Debug.Print(s);
            interpreter.implicitOutput = (string s) => Debug.Print(s);
            interpreter.errorOutput    = (string s) => { Debug.Print(s); interpreter.Stop(); };

            // globals
            interpreter.SetGlobalValue("dt", ValNumber.zero);
            interpreter.SetGlobalValue("width", new ValNumber(graphicsDeviceManager.PreferredBackBufferWidth));
            interpreter.SetGlobalValue("height", new ValNumber(graphicsDeviceManager.PreferredBackBufferHeight));

            // intrinsics
            Intrinsic f = Intrinsic.Create("display");

            f.AddParam("n");
            f.code = (context, partialResult) => {
                int n = (int)context.GetVar("n").FloatValue();
                if (n >= 0 && n < displays.Count)
                {
                    return(new Intrinsic.Result(displays[n].DisplayRef));
                }
                else
                {
                    Debug.Print("Invalid Display: " + n.ToString());
                    return(Intrinsic.Result.Null);
                }
            };

            f      = Intrinsic.Create("Sprite");
            f.code = (context, partialResult) => {
                var new_sprite = Sprite.SpriteClass();
                //new_sprite.assignOverride = Sprite.SpriteClass().assignOverride;
                //new_sprite["ref"] = new ValWrapper<Sprite>(new Sprite());
                return(new Intrinsic.Result(new_sprite));
            };
            Intrinsic.shortNames[Sprite.SpriteClass()] = "Sprite";

            f = Intrinsic.Create("");
            f.AddParam("path");
            f.code = (context, partialResult) =>
            {
                string  path    = context.GetVar("path").ToString();
                Texture texture = new Texture(graphicsDeviceManager, path);
                return(new Intrinsic.Result(new ValWrapper <Texture>(texture)));
            };
            ValMap file = new ValMap();

            file["loadImage"] = f.GetFunc();
            interpreter.SetGlobalValue("file", file);
        }
Exemple #4
0
        public ValMap NewClass()
        {
            ValMap spriteDisplayClass = new ValMap();

            spriteDisplayClass.assignOverride = (key, value) =>
            {
                switch (key.ToString())
                {
                case "scrollX":
                    (spriteDisplayClass["ref"] as ValWrapper <SpriteDisplay>).UnWrapp().scrollX = value.FloatValue();
                    return(false);

                case "scrollY":
                    (spriteDisplayClass["ref"] as ValWrapper <SpriteDisplay>).UnWrapp().scrollY = value.FloatValue();
                    return(false);
                }
                return(true);
            };

            Intrinsic f = Intrinsic.Create("");

            f.code = (context, partialResult) =>
            {
                var self          = context.GetVar("self") as ValMap;
                var spriteDisplay = (self["ref"] as ValWrapper <SpriteDisplay>).UnWrapp();
                spriteDisplay.Clear();

                return(Intrinsic.Result.Null);
            };
            spriteDisplayClass["clear"] = f.GetFunc();

            f = Intrinsic.Create("");
            f.AddParam("sprite");
            f.code = (context, partialResult) =>
            {
                var spriteVal = context.GetVar("sprite") as ValMap;
                spriteVal.assignOverride = (key, value) =>
                {
                    switch (key.ToString())
                    {
                    case "image":
                        Sprite sprite = (spriteVal["ref"] as ValWrapper <Sprite>).UnWrapp();
                        sprite.texture  = (value as ValWrapper <Texture>).UnWrapp().texture;
                        sprite.origin.X = sprite.texture.Width / 2;
                        sprite.origin.Y = sprite.texture.Height / 2;
                        return(false);

                    case "x":
                        (spriteVal["ref"] as ValWrapper <Sprite>).UnWrapp().position.X = value.FloatValue();
                        return(false);

                    case "y":
                        (spriteVal["ref"] as ValWrapper <Sprite>).UnWrapp().position.Y = Sprite.ReceiveY(value.FloatValue());
                        return(false);

                    case "rotation":
                        (spriteVal["ref"] as ValWrapper <Sprite>).UnWrapp().rotation = value.FloatValue();
                        return(false);
                    }
                    return(false);
                };

                if (spriteVal["x"] == null)
                {
                    spriteVal["x"] = ValNumber.zero;
                }
                if (spriteVal["y"] == null)
                {
                    spriteVal["y"] = ValNumber.zero;
                }
                if (spriteVal["rotation"] == null)
                {
                    spriteVal["rotation"] = ValNumber.zero;
                }
                if (spriteVal["image"] == null)
                {
                    spriteVal["image"] = ValNumber.zero;
                }

                spriteVal["ref"] = new ValWrapper <Sprite>(new Sprite());

                //Sync monomicro_Sprite with Mono_Sprite
                Sprite.SpriteClassSync(spriteVal);

                var self = context.GetVar("self") as ValMap;

                var spriteDisplay = (self["ref"] as ValWrapper <SpriteDisplay>).UnWrapp();
                spriteVal["display"] = new ValWrapper <SpriteDisplay>(spriteDisplay);
                spriteDisplay.Add(spriteVal);

                foreach (ValMap sp in spriteDisplay.sprites)
                {
                    Debug.Print(sp.ToString());
                }

                return(Intrinsic.Result.Null);
            };
            spriteDisplayClass["push"] = f.GetFunc();

            spriteDisplayClass["ref"]     = new ValWrapper <SpriteDisplay>(this);
            spriteDisplayClass["scrollX"] = ValNumber.zero;
            spriteDisplayClass["scrollY"] = ValNumber.zero;

            return(spriteDisplayClass);
        }