Esempio n. 1
0
        /// <summary>
        /// Initialize the manager to start spawning tokens
        /// </summary>
        /// <param name="tokenDict">The tokens to spawn and their frequencies</param>
        public void Init(Dictionary <Enums.Tokens, Enums.Frequency> tokenDict)
        {
            // Get all spawn points
            TokenSpawnPoint[] spawnPoints = FindObjectsOfType <TokenSpawnPoint>();
            if (spawnPoints.Length == 0)
            {
                return;
            }

            // Transfer the tokens from the dict to the list
            tokens = new List <GameObject>();
            foreach (Enums.Tokens key in tokenDict.Keys)
            {
                GameObject go = GameManager.instance.AllTokens.Find(x => x.name.StartsWith(key.ToString()));
                if (go != null)
                {
                    // Spawn a certain number of the same token based on its frequency
                    Enums.Frequency freq = tokenDict[key];
                    for (int i = 0; i < (int)freq; i++)
                    {
                        GameObject spawnToken;
                        spawnToken = Instantiate(go);
                        tokens.Add(spawnToken);
                        spawnToken.SetActive(false);
                    }
                }
                //else Debug.Log("Key: " + key.ToString() + " is null");
            }
            // Initialize all the spawn points
            for (int i = 0; i < spawnPoints.Length; i++)
            {
                spawnPoints[i].Init();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Gets a file of game settings from the file name
        /// </summary>
        /// <param name="extension">Where the file is located</param>
        /// <returns>The game settings from resources</returns>
        public static GameSettings LoadGameSettingsXML(string extension)
        {
            // Get a default settings in case none exists
            GameSettings data = new GameSettings();

            TextAsset xmlFile = (TextAsset)Resources.Load(xmlSettingsDataPath + extension);

            Debug.Log(xmlFile);
            if (xmlFile != null)
            {
                MemoryStream assetStream = new MemoryStream(xmlFile.bytes);
                XmlReader    reader      = XmlReader.Create(assetStream);

                bool endofSettings = false;
                while (reader.Read() && !endofSettings)
                {
                    if (reader.IsStartElement())
                    {
                        switch (reader.LocalName)
                        {
                        case "Type":
                            data.Type = (Enums.GameType)System.Enum.Parse(typeof(Enums.GameType), reader.ReadElementContentAsString());
                            break;

                        case "TimeLimitEnabled":
                            data.TimeLimitEnabled = reader.ReadElementContentAsBoolean();
                            break;

                        case "TimeLimit":
                            data.TimeLimit = reader.ReadElementContentAsFloat();
                            break;

                        case "KillLimit":
                            data.KillLimit = reader.ReadElementContentAsFloat();
                            break;

                        case "StockLimit":
                            data.StockLimit = reader.ReadElementContentAsFloat();
                            break;

                        case "ArrowLimit":
                            data.ArrowLimit = reader.ReadElementContentAsFloat();
                            break;

                        case "DamageModifier":
                            data.DamageModifier = reader.ReadElementContentAsFloat();
                            break;

                        case "GravityModifier":
                            data.GravityModifier = reader.ReadElementContentAsFloat();
                            break;

                        case "SpeedModifier":
                            data.SpeedModifier = reader.ReadElementContentAsFloat();
                            break;

                        case "TokenSpawnFreq":
                            data.TokenSpawnFreq = reader.ReadElementContentAsFloat();
                            break;

                        case "PlayerSpawnFreq":
                            data.PlayerSpawnFreq = reader.ReadElementContentAsFloat();
                            break;

                        case "EnabledTokens":
                            Dictionary <Enums.Tokens, Enums.Frequency> dict = new Dictionary <Enums.Tokens, Enums.Frequency>();
                            XmlReader inner = reader.ReadSubtree();
                            while (inner.Read())
                            {
                                if (inner.IsStartElement())
                                {
                                    if (inner.LocalName.Equals("Token"))
                                    {
                                        Enums.Tokens t = (Enums.Tokens)System.Enum.Parse(typeof(Enums.Tokens), inner.ReadElementContentAsString());
                                        inner.ReadToFollowing("Frequency");
                                        Enums.Frequency f = (Enums.Frequency)System.Enum.Parse(typeof(Enums.Frequency), inner.ReadElementContentAsString());
                                        dict.Add(t, f);
                                    }
                                }
                            }
                            data.EnabledTokens = dict;
                            inner.Close();
                            break;

                        case "DefaultTokens":
                            List <Enums.Tokens> defaultTokens = new List <Enums.Tokens>();
                            XmlReader           tokens        = reader.ReadSubtree();
                            while (tokens.Read())
                            {
                                if (tokens.IsStartElement())
                                {
                                    if (tokens.LocalName.Equals("Token"))
                                    {
                                        Enums.Tokens t = (Enums.Tokens)System.Enum.Parse(typeof(Enums.Tokens), tokens.ReadElementContentAsString());
                                        defaultTokens.Add(t);
                                    }
                                }
                            }
                            data.DefaultTokens = defaultTokens;
                            tokens.Close();
                            break;

                        case "GameSettings":
                            break;

                        default:
                            endofSettings = true;
                            break;
                        }
                    }
                }
                reader.Close();
            }
            return(data);
        }