Example #1
0
    private void inUpdateControlCommand(string command)
    {
        string[] cmds = command.Split(':');
        string[] args = new string[0];
        if (cmds.Length == 2)
        {
            args    = cmds[1].Split(',');
            cmds[1] = args[0];
        }
        switch (cmds[0].ToLower())
        {
        case "w":
            letterTimer = timePerLetter - (singleFrameTiming * ParseUtil.getInt(cmds[1]));
            break;

        case "waitall":
            timePerLetter = singleFrameTiming * ParseUtil.getInt(cmds[1]);
            break;

        case "voice":
            if (cmds[1].ToLower() == "default")
            {
                letterSound.clip = SpriteFontRegistry.Get(SpriteFontRegistry.UI_DEFAULT_NAME).Sound;
            }
            else
            {
                letterSound.clip = AudioClipRegistry.GetVoice(cmds[1].ToLower());
            }
            break;

        case "font":
            letterSound.clip = SpriteFontRegistry.Get(cmds[1].ToLower()).Sound;
            break;

        case "novoice":
            letterSound.clip = null;
            break;

        case "next":
            autoSkip = true;
            break;

        case "func":
            if (caller == null)
            {
                UnitaleUtil.displayLuaError("???", "Func called but no script to reference. This is the engine's fault, not yours.");
            }
            if (args.Length > 1)
            {
                caller.Call(args[0], DynValue.NewString(args[1]));
            }
            else
            {
                caller.Call(cmds[1]);
            }
            break;
        }
    }
Example #2
0
 public void ResizeImmediate(int w, int h)
 {
     if (UIController.instance.getState() == UIController.UIState.DEFENDING)
     {
         ArenaSizer.instance.ResizeImmediate(w, h);
     }
     else
     {
         UnitaleUtil.displayLuaError("NOT THE WAVE SCRIPT", "sorry but pls don't");
     }
 }
Example #3
0
    private void Start()
    {
        try
        {
            string scriptText = ScriptRegistry.Get(ScriptRegistry.MONSTER_PREFIX + scriptName);
            if (scriptText == null)
            {
                UnitaleUtil.displayLuaError(StaticInits.ENCOUNTER, "Tried to load monster script " + scriptName + ".lua but it didn't exist. Is it misspelled?");
                return;
            }

            script.scriptname = scriptName;
            script.Bind("SetSprite", (Action <string>)SetSprite);
            script.Bind("SetActive", (Action <bool>)SetActive);
            script.Bind("Kill", (Action)DoKill);
            script.Bind("Spare", (Action)DoSpare);
            script.DoString(scriptText);

            string spriteFile = script.GetVar("sprite").String;
            if (spriteFile != null)
            {
                SetSprite(spriteFile);
            }
            else
            {
                throw new InvalidOperationException("missing sprite");
            }

            ui               = FindObjectOfType <UIController>();
            maxHP            = HP;
            currentHP        = HP;
            textBubbleSprite = Resources.Load <Sprite>("Sprites/UI/SpeechBubbles/right");

            /*if (script.GetVar("canspare") == null)
             * {
             *  CanSpare = false;
             * }
             * if (script.GetVar("cancheck") == null)
             * {
             *  CanCheck = true;
             * }*/
        }
        catch (InterpreterException ex)
        {
            UnitaleUtil.displayLuaError(scriptName, ex.DecoratedMessage);
        }
        catch (Exception ex)
        {
            UnitaleUtil.displayLuaError(scriptName, "Unknown error. Usually means you're missing a sprite.\nSee documentation for details.\nStacktrace below in case you wanna notify a dev.\n" + ex.StackTrace);
        }
    }
    public override void updateWave()
    {
        string currentScript = "";

        try
        {
            for (int i = 0; i < waves.Length; i++)
            {
                currentScript = waveNames[i];
                waves[i].Call(waves[i].Globals["Update"]);
            }
        }
        catch (InterpreterException ex)
        {
            UnitaleUtil.displayLuaError(currentScript, ex.DecoratedMessage);
            return;
        }
    }
Example #5
0
 public override void OnProjectileHit()
 {
     if (owner.Globals["OnHit"] != null && owner.Globals.Get("OnHit") != null)
     {
         try
         {
             owner.Call(owner.Globals["OnHit"], this.ctrl);
         }
         catch (ScriptRuntimeException ex)
         {
             UnitaleUtil.displayLuaError("[wave script filename here]\n(should be a filename, sorry! missing feature)", ex.DecoratedMessage);
         }
     }
     else
     {
         PlayerController.instance.Hurt(3);
     }
 }
Example #6
0
    /// <summary>
    /// Returns the path to the given file in the mod folder if it exists, otherwise the default folder. If it doesn't exist, returns null and optionally shows the error screen.
    /// </summary>
    /// <param name="filename">Filename to require, relative to either mod or default folder root</param>
    /// <param name="errorOnFailure">Defines whether the error screen should be displayed if the file isn't in either folder.</param>
    /// <returns>File path if it exists, null otherwise (closely followed by error screen)</returns>
    public static string requireFile(string filename, bool errorOnFailure = true)
    {
        FileInfo fi = new FileInfo(pathToModFile(filename));

        if (!fi.Exists)
        {
            fi = new FileInfo(pathToDefaultFile(filename));
        }

        if (!fi.Exists)
        {
            if (errorOnFailure)
            {
                UnitaleUtil.displayLuaError("???", "Attempted to load " + filename + " from either a mod or default directory, but it was missing in both.");
            }
            return(null);
        }

        return(fi.FullName);
    }
    /// <summary>
    /// Attempts to initialize the encounter's script file and bind encounter-specific functions to it.
    /// </summary>
    /// <returns>True if initialization succeeded, false if there was an error.</returns>
    private bool initScript()
    {
        script            = new ScriptWrapper();
        script.scriptname = StaticInits.ENCOUNTER;
        string scriptText = ScriptRegistry.Get(ScriptRegistry.ENCOUNTER_PREFIX + StaticInits.ENCOUNTER);

        try
        {
            script.DoString(scriptText);
        }
        catch (InterpreterException ex)
        {
            UnitaleUtil.displayLuaError(StaticInits.ENCOUNTER, ex.DecoratedMessage);
            return(false);
        }
        script.Bind("RandomEncounterText", (Func <string>)RandomEncounterText);
        script.Bind("CreateProjectile", (Func <Script, string, float, float, DynValue>)CreateProjectile);
        script.Bind("CreateProjectileAbs", (Func <Script, string, float, float, DynValue>)CreateProjectileAbs);
        script_ref = script;
        return(true);
    }
Example #8
0
    public static Sprite[] atlasFromXml(XmlNode sheetNode, Sprite source)
    {
        try
        {
            List <Sprite> tempSprites = new List <Sprite>();
            foreach (XmlNode child in sheetNode.ChildNodes)
            {
                if (child.Name.Equals("sprite"))
                {
                    //Sprite s = Sprite.Create(source.texture,
                    Sprite s = spriteWithXml(child, source);
                    tempSprites.Add(s);
                }
            }

            return(tempSprites.ToArray());
        }
        catch (Exception ex)
        {
            UnitaleUtil.displayLuaError("[XML document]", "One of the sprites' XML documents was invalid. This could be a corrupt or edited file.\n\n" + ex.Message);
            return(null);
        }
    }
    private void prepareWave()
    {
        DynValue nextWaves = script.GetVar("nextwaves");

        waves     = new Script[nextWaves.Table.Length];
        waveNames = new string[waves.Length];
        int currentWaveScript = 0;

        try
        {
            for (int i = 0; i < waves.Length; i++)
            {
                currentWaveScript = i;
                waves[i]          = LuaScriptBinder.boundScript();
                DynValue ArenaStatus = UserData.Create(ArenaSizer.luaStatus);
                waves[i].Globals.Set("Arena", ArenaStatus);
                waves[i].Globals["State"]               = (Action <string>)UIController.instance.SwitchStateOnString;
                waves[i].Globals["CreateProjectile"]    = (Func <Script, string, float, float, DynValue>)CreateProjectile;
                waves[i].Globals["CreateProjectileAbs"] = (Func <Script, string, float, float, DynValue>)CreateProjectileAbs;
                waves[i].Globals["EndWave"]             = (Action)endWaveTimer;
                if (nextWaves.Table.Get(i + 1).Type != DataType.String)
                {
                    UnitaleUtil.displayLuaError(StaticInits.ENCOUNTER, "Non-string value encountered in nextwaves table");
                    return;
                }
                else
                {
                    waveNames[i] = nextWaves.Table.Get(i + 1).String;
                }
                waves[i].DoString(ScriptRegistry.Get(ScriptRegistry.WAVE_PREFIX + nextWaves.Table.Get(i + 1).String));
            }
        }
        catch (InterpreterException ex)
        {
            UnitaleUtil.displayLuaError(nextWaves.Table.Get(currentWaveScript + 1).String + ".lua", ex.DecoratedMessage);
        }
    }
 public bool TryCall(string func, DynValue[] param = null)
 {
     try
     {
         if (script.GetVar(func) == null)
         {
             return(false);
         }
         if (param != null)
         {
             script.Call(func, param);
         }
         else
         {
             script.Call(func);
         }
         return(true);
     }
     catch (InterpreterException ex)
     {
         UnitaleUtil.displayLuaError(StaticInits.ENCOUNTER, ex.DecoratedMessage);
         return(true);
     }
 }
    protected override void loadEnemiesAndPositions()
    {
        AudioSource musicSource = Camera.main.GetComponent <AudioSource>();

        EncounterText = script.GetVar("encountertext").String;
        DynValue enemyScriptsLua   = script.GetVar("enemies");
        DynValue enemyPositionsLua = script.GetVar("enemypositions");
        string   musicFile         = script.GetVar("music").String;

        try
        {
            enemies = new LuaEnemyController[enemyScriptsLua.Table.Length]; // dangerously assumes enemies is defined
        }
        catch (Exception)
        {
            UnitaleUtil.displayLuaError(StaticInits.ENCOUNTER, "There's no enemies table in your encounter. Is this a pre-0.1.2 encounter? It's easy to fix!\n\n"
                                        + "1. Create a Monsters folder in the mod's Lua folder\n"
                                        + "2. Add the monster script (custom.lua) to this new folder\n"
                                        + "3. Add the following line to the beginning of this encounter script, located in the mod folder/Lua/Encounters:\nenemies = {\"custom\"}\n"
                                        + "4. You're done! Starting from 0.1.2, you can name your monster and encounter scripts anything.");
            return;
        }
        if (enemyPositionsLua != null && enemyPositionsLua.Table != null)
        {
            enemyPositions = new Vector2[enemyPositionsLua.Table.Length];
            for (int i = 0; i < enemyPositionsLua.Table.Length; i++)
            {
                Table posTable = enemyPositionsLua.Table.Get(i + 1).Table;
                if (i >= enemies.Length)
                {
                    break;
                }

                enemyPositions[i] = new Vector2((float)posTable.Get(1).Number, (float)posTable.Get(2).Number);
            }
        }

        if (musicFile != null)
        {
            try
            {
                AudioClip music = AudioClipRegistry.GetMusic(musicFile);
                musicSource.clip = music;
            }
            catch (Exception)
            {
                Debug.Log("Loading custom music failed.");
            }
        }
        else
        {
            musicSource.clip = AudioClipRegistry.GetMusic("mus_battle1");
        }

        // Instantiate all the enemy objects
        if (enemies.Length > enemyPositions.Length)
        {
            UnitaleUtil.displayLuaError(StaticInits.ENCOUNTER, "All enemies in an encounter must have a screen position defined. Either your enemypositions table is missing, "
                                        + "or there are more enemies than available positions. Refer to the documentation's Basic Setup section on how to do this.");
        }
        enemyInstances = new GameObject[enemies.Length];
        for (int i = 0; i < enemies.Length; i++)
        {
            enemyInstances[i] = Instantiate(Resources.Load <GameObject>("Prefabs/LUAEnemy"));
            enemyInstances[i].transform.SetParent(gameObject.transform);
            enemyInstances[i].transform.localScale = new Vector3(1, 1, 1); // apparently this was suddenly required or the scale would be (0,0,0)
            enemies[i]            = enemyInstances[i].GetComponent <LuaEnemyController>();
            enemies[i].scriptName = enemyScriptsLua.Table.Get(i + 1).String;
            if (i < enemyPositions.Length)
            {
                enemies[i].GetComponent <RectTransform>().anchoredPosition = enemyPositions[i];
            }
            else
            {
                enemies[i].GetComponent <RectTransform>().anchoredPosition = new Vector2(0, 1);
            }
        }

        // Attach the controllers to the encounter's enemies table
        DynValue[] enemyStatusCtrl = new DynValue[enemies.Length];
        Table      luaEnemyTable   = script.GetVar("enemies").Table;

        for (int i = 0; i < enemyStatusCtrl.Length; i++)
        {
            //enemies[i].luaStatus = new LuaEnemyStatus(enemies[i]);
            enemies[i].script = new ScriptWrapper();
            luaEnemyTable.Set(i + 1, UserData.Create(enemies[i].script));
        }
        script.SetVar("enemies", DynValue.NewTable(luaEnemyTable));
        musicSource.Play(); // play that funky music
    }