/// <summary> /// Create an empty world. /// </summary> /// <param name="parentScreen">The screen this world will be updated in.</param> public World(Screen parentScreen, Player player) : base(parentScreen) { this.worldObjects = new List<WorldObject>(); this.player = player; ParentScreen.Components.Add(player); this.interactiveLayers = new Dictionary<int, TileMapLayer>(); this.parallaxLayers = new Dictionary<int, TileMapLayer>(); batchService = (ISpriteBatchService)this.Game.Services.GetService(typeof(ISpriteBatchService)); otherMaps = new List<TileMap>(); //Set up Sound systems audioEngine = new AudioEngine("Content\\System\\Sounds\\Win\\SoundFX.xgs"); soundBank = new SoundBank(audioEngine, "Content\\System\\Sounds\\Win\\GameSoundBank.xsb"); waveBank = new WaveBank(audioEngine, "Content\\System\\Sounds\\Win\\GameWavs.xwb"); // Set up our collision systems: spriteCollisionManager = new SpriteSpriteCollisionManager(this.Game, batchService, 40, 40); ParentScreen.Components.Add(spriteCollisionManager); bgm = this.Game.Content.Load<Song>("System\\Music\\DesertBGM"); MediaPlayer.IsRepeating = true; MediaPlayer.Play(bgm); LoadCtorInfos(); }
// TODO: add proper support for parallax layers. This will require a new class that derives from // TileMapLayer and overrides Draw() private void LoadWorldXmlFile(string fileName) { XDocument fileContents = ScreenManager.Instance.Content.Load<XDocument>(fileName); #region LOAD INTERACTIVE MAP LAYERS // Load the interactive TileMap: map = new TileMap(this, fileContents.Root.Attribute("InteractiveMapFileName").Value); for (int i = 0; i < map.Width; ++i) { for (int j = 0; j < map.Height; ++j) { if (map[i, j] != null && map[i, j] is ISpriteCollideable) { spriteCollisionManager.addObjectToRegisteredObjectList((ISpriteCollideable)map[i, j]); } } } IEnumerable<XElement> allMapLayers = fileContents.Descendants("MapLayer"); foreach (XElement mapLayer in allMapLayers) { string LayerName; int LayerIndex, ZIndex; LayerName = mapLayer.Attribute("LayerName").Value; LayerIndex = Int32.Parse(mapLayer.Attribute("LayerIndex").Value); ZIndex = Int32.Parse(mapLayer.Attribute("ZIndex").Value); interactiveLayers[ZIndex] = new TileMapLayer(ParentScreen, batchService.GetSpriteBatch(TileMapLayer.SpriteBatchName), map, LayerIndex); interactiveLayers[ZIndex].DrawOrder = ZIndex; ParentScreen.Components.Add(interactiveLayers[ZIndex]); } #endregion #region LOAD CHARACTERS // Add the player: Vector2 playerPosition = new Vector2(float.Parse(fileContents.Root.Attribute("PlayerPositionX").Value), float.Parse(fileContents.Root.Attribute("PlayerPositionY").Value)); player = new Player(this, spriteBatch, playerPosition); player.UpdateOrder = 3; player.DrawOrder = PLAYER_DRAW_ORDER; ParentScreen.Components.Add(player); spriteCollisionManager.addObjectToRegisteredObjectList(player); foreach (XElement woElement in fileContents.Descendants("o")) { string name = woElement.Attribute("n").Value; Vector2 position = new Vector2(); position.X = float.Parse(woElement.Attribute("x").Value); position.Y = float.Parse(woElement.Attribute("y").Value); ConstructorInfo ci = (from CI in WorldObjectCtorInfos where CI.DeclaringType.Name == name select CI).First<ConstructorInfo>(); AddWorldObject((WorldObject)ci.Invoke(new object[]{this, SpriteBatch, position})); } #endregion #region LOAD PARALLAX LAYERS IEnumerable<XElement> allParallaxMaps = fileContents.Descendants("Parallax"); // A parallaxMap is made up of a tileMap just like the interactive map, // so it can have multiple layers in and of itself. This might not be common // but the functionality is there. foreach (XElement parallaxMap in allParallaxMaps) { IEnumerable<XElement> allParallaxLayers = parallaxMap.Descendants("Layer"); float ScrollSpeed; TileMap tileMap; tileMap = new TileMap(this, parallaxMap.Attribute("MapFileName").Value); otherMaps.Add(tileMap); ScrollSpeed = Single.Parse(parallaxMap.Attribute("ScrollSpeed").Value); foreach (XElement parallaxLayer in allParallaxLayers) { string LayerName; int LayerIndex, ZIndex; LayerName = parallaxLayer.Attribute("LayerName").Value; LayerIndex = Int32.Parse(parallaxLayer.Attribute("LayerIndex").Value); ZIndex = Int32.Parse(parallaxLayer.Attribute("ZIndex").Value); parallaxLayers[ZIndex] = new TileMapLayer(ParentScreen, batchService.GetSpriteBatch(TileMapLayer.SpriteBatchName), tileMap, LayerIndex, ScrollSpeed); parallaxLayers[ZIndex].DrawOrder = ZIndex; ParentScreen.Components.Add(parallaxLayers[ZIndex]); } } #endregion }