public void LoadLevel( string levelName ) { if( !_levelUnloaded ) UnloadLevel( ); LevelLoading( ); string path = Path.Combine( _gameManager.levelsFolder, levelName + GameManager.LEVEL_EXTENSION ); using( BinaryReader file = new BinaryReader( File.OpenRead( path ) ) ) { //Check the header of the file for validation if( file.ReadString( ) != GameManager.FILE_ID ) { Console.WriteLine( Path.GetFileName( path ) + " is invalid or corrupted!" ); return; } //Get the file version int levelVersion = file.ReadInt32( ); Level lvl = null; //Get level script's name and immediately loads the script string scriptName = file.ReadString( ); if( scriptName != GameManager.NO_SCRIPT ) { Type type = _gameManager.GetClassType( scriptName ); if( type != null ) lvl = Activator.CreateInstance( type ) as Level; } if( lvl == null ) lvl = new Level( ); // ---- Start reading the sprite references for this level //Get the number of sprite references int spriteCount = file.ReadInt32( ); //Create a local array to hold the sprite paths string[] tmpPaths = new string[spriteCount]; for( int i = 0; i < spriteCount; i++ ) { //Get the sprite's name string line = file.ReadString( ); tmpPaths[i] = Path.Combine( _gameManager.spritesFolder, line + ".xnb" ); } //Pass the array of sprite paths and load the sprites LoadSprite( tmpPaths ); //Checks for the level file version. Font data is only supported in version 2 and above if( levelVersion >= 2 ) { // ---- Start reading the font references for this level //Get the number of font references int fontCount = file.ReadInt32( ); //Create a local array to hold the font paths tmpPaths = new string[fontCount]; for( int i = 0; i < fontCount; i++ ) { //Get the font's name string line = file.ReadString( ); tmpPaths[i] = Path.Combine( _gameManager.fontsFolder, line + ".xnb" ); } //Pass the array of font paths and load the fonts LoadFont( tmpPaths ); } // ---- Start reading the layers data //Get the number of layers int layerCount = file.ReadInt32( ); for( int i = 0; i < layerCount; i++ ) { // ---- Read the layer data //Get the layer's name string line = file.ReadString( ); //Create a new layer instance Layer layer = new Layer( line ); //Get the layer Visible value layer.Visible = file.ReadBoolean( ); //Get the layer Scale value layer.Scale = file.ReadSingle( ); //Get the number of actors on this layer int actorCount = file.ReadInt32( ); for( int j = 0; j < actorCount; j++ ) { // ---- Read the actor data //Create a new Actor instance Actor actor = null; //Get the actor's ID int ID = file.ReadInt32( ); //Get the sprite name string spriteName = file.ReadString( ); //Get the script name scriptName = file.ReadString( ); if( scriptName != GameManager.NO_SCRIPT ) { Type type = _gameManager.GetClassType( scriptName ); if( type != null ) actor = Activator.CreateInstance( type ) as Actor; } if( actor == null ) actor = new Actor( ); actor.ID = ID; //Assign the correct sprite to the actor if a match is found if( Sprites.ContainsKey( spriteName ) ) actor.Sprite = Sprites[spriteName]; //Get and assign the name of the actor actor.Name = file.ReadString( ); //Get and assign the actor's position actor.Position = new Vector2( file.ReadSingle( ), file.ReadSingle( ) ); //Get and assign the actor's origin actor.Origin = new Vector2( file.ReadSingle( ), file.ReadSingle( ) ); //Get and assign the actor's scale actor.Scale = file.ReadSingle( ); //Get and assign the actor's rotation angle actor.Rotation = file.ReadSingle( ); //Get and assign the actor's visibility actor.Visible = file.ReadBoolean( ); if( levelVersion >= 3 ) { int tagCount = file.ReadInt32( ); for( int k = 0; k < tagCount; k++ ) { actor.AddTag( file.ReadString( ) ); } bool isTextActor = file.ReadBoolean( ); if( isTextActor ) { if( ( actor as TextActor ) == null ) { TextActor txtActor = new TextActor( ); actor.CopyValues( txtActor ); actor = txtActor; } string fontName = file.ReadString( ); if( Fonts.ContainsKey( fontName ) ) ( actor as TextActor ).Font = Fonts[fontName]; ( actor as TextActor ).Text = file.ReadString( ); ( actor as TextActor ).TextColor = new Color( file.ReadByte( ), file.ReadByte( ), file.ReadByte( ), file.ReadByte( ) ); } } //Add the actor to the layer layer.AddActor( actor ); } //Add the layer instance to the level lvl.NewLayer( layer ); } file.Close( ); lvl.Game = this; ActiveLevel = lvl; } LevelLoaded( ); ActiveLevel.Init( ); foreach( Layer layer in ActiveLevel.Layers.Values ) { foreach( Actor actor in layer.Actors ) { actor.Init( ); } } }
private Actor NewActor( ActorType actorType ) { Actor actor = null; if( actorType.ScriptName != GameManager.NO_SCRIPT ) { Type type = _game.GameManager.GetClassType( actorType.ScriptName ); if( type != null ) actor = Activator.CreateInstance( type ) as Actor; } if( actor == null ) { if( actorType.IsTextActor ) actor = new TextActor( ); else actor = new Actor( ); } actor.Name = actorType.Name; actor.Scale = actorType.Scale; actor.Rotation = actorType.Rotation; actor.Visible = true; if( actorType.IsTextActor ) { TextActor textActor = actor as TextActor; if( _game.LoadFont( actorType.FontName ) ) { textActor.Font = _game.Fonts[actorType.FontName]; textActor.Text = actorType.Text; textActor.TextColor = actorType.TextColor; } } else { if( _game.LoadSprite( actorType.SpriteName ) ) { actor.Sprite = _game.Sprites[actorType.SpriteName]; actor.Origin = new Vector2( actor.Sprite.Texture.Width * 0.5f, actor.Sprite.Texture.Height * 0.5f ); } } return actor; }