コード例 #1
0
            protected override Map Read(ContentReader input, Map existingInstance)
            {
                Map map = new Map();

                map.Name           = input.ReadString();
                map.Dimensions     = input.ReadObject <Point>();
                map.TileDimensions = input.ReadObject <Point>();
                map.Description    = input.ReadString();
                map.ThumbnailAsset = input.ReadString();
                if (string.IsNullOrEmpty(map.ThumbnailAsset))
                {
                    map.ThumbnailAsset = "nothumb";
                    map.Thumbnail      = input.ContentManager.Load <Texture2D>(String.Format("Textures\\Maps\\{0}", map.ThumbnailAsset));
                }
                else
                {
                    map.Thumbnail = input.ContentManager.Load <Texture2D>(String.Format("Textures\\Maps\\{0}\\{1}", map.Name, map.ThumbnailAsset));
                }

                map.TileSheetAsset = input.ReadString();
                if (string.IsNullOrEmpty(map.TileSheetAsset))
                {
                    throw new Exception("You need a tile sheet in order to create a map.");
                }
                map.TileSheet            = input.ContentManager.Load <Texture2D>(String.Format("Textures\\Maps\\{0}\\{1}", map.Name, map.TileSheetAsset));
                map.NumberOfTilesInSheet = new Point(map.TileSheet.Width / map.TileDimensions.X, map.TileSheet.Height / map.TileDimensions.Y);

                map.GroundTiles = input.ReadObject <int[]>();
                map.PathTiles   = input.ReadObject <int[]>();

                map.SpawnLocation  = input.ReadObject <Point>();
                map.SpawnAssetName = input.ReadString();
                if (string.IsNullOrEmpty(map.SpawnAssetName))
                {
                    throw new Exception("You need a spawn asset name in the XML.");
                }
                map.Spawn          = new GameplayObject();
                map.Spawn.Texture  = input.ContentManager.Load <Texture2D>(String.Format("Textures\\Maps\\{0}\\{1}", map.Name, map.SpawnAssetName));
                map.Spawn.Position = new Vector2((map.SpawnLocation.X * map.TileDimensions.X) + map.Spawn.Origin.X, (map.SpawnLocation.Y * map.TileDimensions.Y) + map.Spawn.Origin.Y);



                map.CastleLocation  = input.ReadObject <Point>();
                map.CastleAssetName = input.ReadString();
                if (string.IsNullOrEmpty(map.CastleAssetName))
                {
                    throw new Exception("You need a castle asset name in the XML.");
                }
                map.Castle          = new GameplayObject();
                map.Castle.Texture  = input.ContentManager.Load <Texture2D>(String.Format("Textures\\Maps\\{0}\\{1}", map.Name, map.CastleAssetName));
                map.Castle.Position = new Vector2((map.CastleLocation.X * map.TileDimensions.X) + map.Castle.Origin.X, (map.CastleLocation.Y * map.TileDimensions.Y) + map.Castle.Origin.Y);


                GenerateActiveTilePath(map);
                map.Spawn.Rotation  = GetAngleBetweenTwoMapLocations(map.SpawnLocation, GetAdjacentTileLocation(map, map.SpawnLocation));
                map.Castle.Rotation = GetAngleBetweenTwoMapLocations(map.CastleLocation, GetAdjacentTileLocation(map, map.CastleLocation));

                map.Endless = input.ReadBoolean();

                map.TowersAvailable.AddRange(input.ReadObject <List <string> >());
                map.TowersInfo = String.Format("Available Towers: {0}", map.TowersAvailable.Count);

                map.TowerList = new List <Tower>(map.TowersAvailable.Count);
                foreach (string s in map.TowersAvailable)
                {
                    Tower t = input.ContentManager.Load <Tower>(String.Format("Towers\\{0}", s)).Clone();
                    t.Initialize(map, input.ContentManager);
                    map.TowerList.Add(t);
                }

                map.WavesAvailable.AddRange(input.ReadObject <List <string> >());
                map.WavesInfo = String.Format("Waves: {0}", map.WavesAvailable.Count);

                map.WaveList = new List <Wave>(map.WavesAvailable.Count);
                foreach (string s in map.WavesAvailable)
                {
                    Wave w = input.ContentManager.Load <Wave>(String.Format("Wave\\{0}", s)).Clone();
                    w.Initialize(map);
                    map.WaveList.Add(w);
                }

                map.ActiveWave = map.WaveList[0];
                map.WaveIndex  = 0;

                map.WaveDelay = input.ReadSingle();

                map.Difficulty     = input.ReadInt32();
                map.DifficultyInfo = String.Format("Difficulty: {0}", map.Difficulty);

                map.Money          = input.ReadInt32();
                map.ForeColorArray = input.ReadObject <int[]>();
                if (map.ForeColorArray.Length == 4)
                {
                    map.ForeColor = new Color(map.ForeColorArray[0], map.ForeColorArray[1], map.ForeColorArray[2], map.ForeColorArray[3]);
                }
                else
                {
                    throw new Exception(input.AssetName + ".xml must have 4 integers to represent RGBA color data in the ForeColorArray tag.");
                }

                map.ErrorColorArray = input.ReadObject <int[]>();
                if (map.ErrorColorArray.Length == 4)
                {
                    map.ErrorColor = new Color(map.ErrorColorArray[0], map.ErrorColorArray[1], map.ErrorColorArray[2], map.ErrorColorArray[3]);
                }
                else
                {
                    throw new Exception(input.AssetName + ".xml must have 4 integers to represent RGBA color data in the ErrorColorArray tag.");
                }

                map.InfoBarBackgroundAsset = input.ReadString();
                if (!string.IsNullOrEmpty(map.InfoBarBackgroundAsset))
                {
                    map.InfoBarBackground = input.ContentManager.Load <Texture2D>(String.Format("Textures\\Maps\\{0}\\{1}", map.Name, map.InfoBarBackgroundAsset));
                }

                map.BorderColorArray = input.ReadObject <int[]>();
                if (map.BorderColorArray.Length == 4)
                {
                    map.BorderColor = new Color(map.BorderColorArray[0], map.BorderColorArray[1], map.BorderColorArray[2], map.BorderColorArray[3]);
                }

                map.BorderTextureAsset = input.ReadString();
                if (!string.IsNullOrEmpty(map.BorderTextureAsset))
                {
                    map.BorderTexture = input.ContentManager.Load <Texture2D>(String.Format("Textures\\Maps\\{0}\\{1}", map.Name, map.BorderTextureAsset));
                }

                map.MouseTextureAsset = input.ReadString();
                if (!string.IsNullOrEmpty(map.MouseTextureAsset))
                {
                    map.MouseTexture = input.ContentManager.Load <Texture2D>(String.Format("Textures\\Maps\\{0}\\{1}", map.Name, map.MouseTextureAsset));
                }

                map.PlaceableArray = GeneratePlaceableArray(map);

                map.SmallErrorButtonTextureAsset = input.ReadString();
                if (!string.IsNullOrEmpty(map.SmallErrorButtonTextureAsset))
                {
                    map.SmallErrorButtonTexture = input.ContentManager.Load <Texture2D>(String.Format("Textures\\Maps\\{0}\\{1}", map.Name, map.SmallErrorButtonTextureAsset));
                }

                map.SmallNormalButtonTextureAsset = input.ReadString();
                if (!string.IsNullOrEmpty(map.SmallNormalButtonTextureAsset))
                {
                    map.SmallNormalButtonTexture = input.ContentManager.Load <Texture2D>(String.Format("Textures\\Maps\\{0}\\{1}", map.Name, map.SmallNormalButtonTextureAsset));
                }

                map.LargeErrorButtonTextureAsset = input.ReadString();
                if (!string.IsNullOrEmpty(map.LargeErrorButtonTextureAsset))
                {
                    map.LargeErrorButtonTexture = input.ContentManager.Load <Texture2D>(String.Format("Textures\\Maps\\{0}\\{1}", map.Name, map.LargeErrorButtonTextureAsset));
                }

                map.LargeNormalButtonTextureAsset = input.ReadString();
                if (!string.IsNullOrEmpty(map.LargeNormalButtonTextureAsset))
                {
                    map.LargeNormalButtonTexture = input.ContentManager.Load <Texture2D>(String.Format("Textures\\Maps\\{0}\\{1}", map.Name, map.LargeNormalButtonTextureAsset));
                }

                if (map.SmallNormalButtonTexture.Bounds != map.SmallErrorButtonTexture.Bounds)
                {
                    throw new Exception("The SmallNormalButtonTexture and the SmallErrorButtonTexture must be the same size");
                }
                if (map.LargeNormalButtonTexture.Bounds != map.LargeErrorButtonTexture.Bounds)
                {
                    throw new Exception("The LargeNormalButtonTexture and the LargeErrorButtonTexture must be the same size");
                }

                if (map.WaveDelay > 0)
                {
                    map.State = MapState.WaveDelay;
                    map.Timer = map.WaveDelay;
                }

                map.SongCueName = input.ReadString();

                return(map);
            }