Spellbook.getIncName C# (CSharp) Method

getIncName() public method

public getIncName ( string name ) : string
name string
return string
    public string getIncName(string name)
    {
        string root = name;
            Regex r = new Regex(@"[0-9]+");
        Match m = r.Match(name);
        if (m.Success) {
            Capture c = m.Groups[0].Captures[0];
            int thisnum = int.Parse(c.ToString());
            root = name.Replace(c.ToString(), "");
        }
            if (!copied_spells.ContainsKey(root))
                copied_spells.Add(root, 0);
            copied_spells[root]++;
            return root + copied_spells[root];
    }

Usage Example

示例#1
0
    void setupSpecialEvents()
    {
        //Remove a spell from the inventory if the user blanks out the file contents.
        //  This is how we'll delete spells (for now).
        IDE.IDEClosed += (file_name, contents) => {
            string   newName  = getSpellName(contents);
            string[] segs     = file_name.Split('/');
            string   prevName = segs[segs.Length - 1].Replace(".java", "");

            Inventory i = GameObject.Find("Inventory").GetComponent <Inventory>();

            //previous name
            List <GameObject> matching_items = i.getMatching(prevName);

            if (Regex.Match(contents.Replace("\n", ""), "^\\s*$").Success&& matching_items.Count > 0)
            {
                Debug.Log("About to remove item");
                i.removeItem(matching_items[0]);
                return;
            }
            if (!prevName.Equals(newName))
            {
                // this spell already exists in the inventory so we need to automatically rename it
                Spellbook spellbook       = GameObject.Find("Spellbook").GetComponent <Spellbook>();
                string    newNameAdjusted = spellbook.getIncName(newName);
                contents = contents.Replace(newName, newNameAdjusted);
                newName  = newNameAdjusted;
                //matching_items[0].GetComponent<Item>().item_name = newName;
                matching_items[0].GetComponent <CodeScrollItem>().setCurrentFile(newName + ".java");
                matching_items[0].GetComponent <CodeScrollItem>().getIDEInput().SetCode(contents);
                ProgramLogger.LogKVtime("rename", prevName + ", " + newName);
                SpellLogger.Log("rename: " + prevName + ", " + newName);
            }
            matching_items[0].GetComponent <CodeScrollItem>().SetCompilable();
            ProgramLogger.LogKV("compilable", newName + ", " + matching_items[0].GetComponent <CodeScrollItem>().IsCompilable());
        };


        AudioClip monster_clip = Resources.Load("Growls") as AudioClip;

        //Setup sounds
        Monster.AttackStarted += (monster) => {
            monster.audio.PlayOneShot(monster_clip);
            Popup.mainPopup.popup("Monster awoken!  Hide in the swamp!");
        };

        Monster.AttackEnded += (monster) => {
            monster.audio.PlayOneShot(monster_clip);
            Popup.mainPopup.popup("You lost him!");
        };

        AudioSource main_audio = GameObject.Find("Voice").audio;

        AudioClip spellbook_clip = Resources.Load("PageTurn") as AudioClip;

        Spellbook.PageTurnedForward += (page) => {
            main_audio.audio.PlayOneShot(spellbook_clip);
        };
        Spellbook.PageTurnedBackward += (page) => {
            main_audio.audio.PlayOneShot(spellbook_clip);
        };
        Spellbook.SpellCopied += (page) => {
            main_audio.audio.PlayOneShot(spellbook_clip);
        };

        AudioClip drop_item_clip = Resources.Load("DropItem") as AudioClip;

        Inventory.DroppedOff += (target) => {
            main_audio.audio.PlayOneShot(drop_item_clip);
        };


        AudioClip badge_clip = Resources.Load("BadgeUnlocked") as AudioClip;

        Badgebook.BadgeUnlocked += (target) => {
            main_audio.audio.PlayOneShot(badge_clip);
        };

        ConversationDisplayer.ConversationStarted += (target) => {
            int i = Random.Range(1, 7);

            AudioClip hi_clip = Resources.Load("GnomeHi" + i) as AudioClip;

            main_audio.audio.PlayOneShot(hi_clip);
        };

        ConversationDisplayer.ConversationStopped += (target) => {
            int       i        = Random.Range(1, 3);
            AudioClip bye_clip = Resources.Load("GnomeBye" + i) as AudioClip;

            main_audio.audio.PlayOneShot(bye_clip);
        };
    }