Esempio n. 1
0
        /// <summary>
        /// Gets a name from the input spawn frequency table
        /// </summary>
        public static string GetRandomFrom(SpawnFrequency[] table, TCODRandom rng)
        {
            float randVal = rng.getFloat(0.0f, 1.0f);

            float totalProb = 0.0f;

            for (int i = 0; i < table.Length; i++)
            {
                totalProb += table[i].chance;

                if (randVal < totalProb)
                {
                    return(table[i].name);
                }
            }

            return("");
        }
Esempio n. 2
0
        /// <summary>
        /// Cleans up the current level and regenerates a new one
        /// </summary>
        public void InitializeNewLevel()
        {
            // TODO: much more generic way of setting up the map data
            currentMapInfo = XMLObjectLoader.LoadXMLObject <MapInfo>("data\\xml\\DefaultMapInfo.xml", MapInfo.PostLoadInitialization);

            if (currentMap != null)
            {
                if (mapCache == null)
                {
                    mapCache = new List <AreaMap>();
                }

                mapCache.Add(currentMap);
                mapIdx += 1;

                CleanUpMap(currentMap);
                currentMap = null;
            }

            // Choose which generator to use
            Generator mapGenerator = null;

            switch (currentMapInfo.generatorType)
            {
            case MapInfo.EGeneratorType.Dungeon:
                if (rng.getFloat(0.0f, 1.0f) > 0.5f)
                {
                    mapGenerator = new GenCaveDungeon(rng, currentMapInfo.terrainSet, currentMapInfo);
                }
                else
                {
                    mapGenerator = new GenRoomDungeon(rng, currentMapInfo.terrainSet, currentMapInfo);
                }
                break;

            case MapInfo.EGeneratorType.Overworld:
                // TODO: new generator class
                break;

            default:
                mapGenerator = new GenSimpleDungeon(rng, currentMapInfo.terrainSet, currentMapInfo);
                break;
            }

            // Create the area map from the selected generator
            currentMap = new AreaMap(this, currentMapInfo.width, currentMapInfo.height, mapGenerator);

            // spawn the entities requested from the generation algorithm, stuff like doors and other static objects
            if (mapGenerator.objectSpawns != null)
            {
                foreach (var spawn in mapGenerator.objectSpawns)
                {
                    // special case handles player spawn, need to cache the reference
                    if (spawn.entity == "Player")
                    {
                        SpawnPlayer(spawn.location);
                        continue;
                    }

                    Entity spawned = SpawnEntity(spawn.entity, spawn.location);

                    // handle a few other special cases
                    if (spawn.entity == "Chest")
                    {
                        PopulateLootChest(spawned);
                    }
                    if (spawn.entity == "Entrance")
                    {
                        currentMap.entranceLoc = spawn.location;
                    }
                    if (spawn.entity == "Exit")
                    {
                        currentMap.exitLoc = spawn.location;
                    }
                }
            }

            // give the map a first LOS update
            currentMap.UpdateLOSFrom(player.position, 10);
        }