コード例 #1
0
ファイル: MapLoader.cs プロジェクト: Nostritius/OpenSAGE
        protected override Scene LoadEntry(FileSystemEntry entry, ContentManager contentManager, LoadOptions loadOptions)
        {
            contentManager.IniDataContext.LoadIniFile(@"Data\INI\Terrain.ini");

            var mapFile = MapFile.FromFileSystemEntry(entry);

            var result = new Scene();

            result.MapFile = mapFile;

            result.Settings.LightingConfigurations = mapFile.GlobalLighting.LightingConfigurations.ToLightSettingsDictionary();
            result.Settings.TimeOfDay = mapFile.GlobalLighting.Time;

            var heightMap = new HeightMap(mapFile.HeightMapData);

            result.HeightMap = heightMap;

            var indexBufferCache = AddDisposable(new TerrainPatchIndexBufferCache(contentManager.GraphicsDevice));

            var tileDataTexture = AddDisposable(CreateTileDataTexture(
                                                    contentManager.GraphicsDevice,
                                                    mapFile,
                                                    heightMap));

            var cliffDetailsBuffer = AddDisposable(CreateCliffDetails(
                                                       contentManager.GraphicsDevice,
                                                       mapFile));

            CreateTextures(
                contentManager,
                mapFile.BlendTileData,
                out var textureArray,
                out var textureDetails);

            var textureDetailsBuffer = AddDisposable(Buffer <TextureInfo> .CreateStatic(
                                                         contentManager.GraphicsDevice,
                                                         textureDetails,
                                                         BufferBindFlags.ShaderResource));

            var terrainMaterial = new TerrainMaterial(contentManager.EffectLibrary.Terrain);

            terrainMaterial.SetTileData(tileDataTexture);
            terrainMaterial.SetCliffDetails(cliffDetailsBuffer);
            terrainMaterial.SetTextureDetails(textureDetailsBuffer);
            terrainMaterial.SetTextureArray(textureArray);

            var terrainPatches = CreatePatches(
                contentManager.GraphicsDevice,
                heightMap,
                mapFile.BlendTileData,
                terrainMaterial,
                indexBufferCache);

            var terrain = new Terrain.Terrain(heightMap, terrainPatches);

            var world = new World(terrain);

            result.Scene3D = new Scene3D(world);

            var objectsEntity = new Entity();

            result.Entities.Add(objectsEntity);
            LoadObjects(
                contentManager,
                objectsEntity,
                heightMap,
                mapFile.ObjectsList.Objects,
                result.Settings);

            foreach (var team in mapFile.SidesList.Teams ?? mapFile.Teams.Items)
            {
                var name        = (string)team.Properties["teamName"].Value;
                var owner       = (string)team.Properties["teamOwner"].Value;
                var isSingleton = (bool)team.Properties["teamIsSingleton"].Value;

                // TODO
            }

            var waypointPaths = mapFile.WaypointsList.WaypointPaths.Select(path =>
            {
                var start = result.Settings.Waypoints[path.StartWaypointID];
                var end   = result.Settings.Waypoints[path.EndWaypointID];
                return(new Settings.WaypointPath(start, end));
            }).ToList();

            result.Settings.WaypointPaths = new WaypointPathCollection(waypointPaths);

            var scriptsEntity = new Entity();

            result.Entities.Add(scriptsEntity);

            // TODO: Don't hardcode this.
            // Perhaps add one ScriptComponent for the neutral player,
            // and one for the active player.
            var scriptList = (mapFile.SidesList.PlayerScripts ?? mapFile.PlayerScriptsList).ScriptLists[0];

            AddScripts(scriptsEntity, scriptList, result.Settings);

            return(result);
        }