Esempio n. 1
0
        public static void Draw( SpriteBatch spriteBatch, LevelEd level )
        {
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle( bounds.X, bounds.Y, bounds.Width, bounds.Height );
            int gridSpacing;
            if( Global.Tools.DragSnapAmount < 16 )
                gridSpacing = 16;
            else
                gridSpacing = Global.Tools.DragSnapAmount;

            line.Width = GRID_LINE_WIDTH;
            line.Y = rect.Y;
            line.Height = rect.Height;

            for( line.X = rect.X; line.X < rect.Right; line.X += gridSpacing )
                spriteBatch.Draw( Global.Pixel, line, GRID_COLOR );

            line.X = rect.X;
            line.Width = rect.Width;
            line.Height = GRID_LINE_WIDTH;

            for( line.Y = rect.Y; line.Y < rect.Bottom; line.Y += gridSpacing )
                spriteBatch.Draw( Global.Pixel, line, GRID_COLOR );

            bounds.Draw( spriteBatch, 2 );
        }
 protected virtual void DisposeResources( )
 {
     _viewport.Dispose( );
     _level = null;
 }
        /// <summary>
        /// Opens a Level file given its path and returns a Level object with the data.
        /// </summary>
        public static LevelEd LoadLevel( string filePath )
        {
            LevelEd level = new LevelEd( );

            //Sets the name of the level
            level.Name = Path.GetFileNameWithoutExtension( filePath );

            using( BinaryReader reader = new BinaryReader( File.OpenRead( filePath ) ) )
            {
                // ---- Gets the header data

                if( reader.ReadString( ) != Consts.Editor.FILE_HEADER_ID )
                {
                    reader.Close( );
                    return null;
                }

                //Gets the version of the file
                int levelVersion = reader.ReadInt32( );

                // ---- Read and assign the level's script

                //Get the level's script name
                string scriptName = reader.ReadString( );

                //First check if the script name is a valid one
                if( scriptName != NO_SCRIPT )
                {
                    //Check if the assets list of scripts contains the given script name
                    if( Global.AssetsBrowser.ScriptItems.ContainsKey( scriptName ) )
                        level.Script = Global.AssetsBrowser.ScriptItems[scriptName].Script;
                    else
                        MessageBox.Show( scriptName + " could not be found!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning );
                }

                // ---- Reads through the layers data

                //Gets the number of layers
                int layersCount = reader.ReadInt32( );

                for( int i = 0; i < layersCount; i++ )
                {
                    LevelLayerReadData( level, reader, levelVersion );
                }

                reader.Close( );
            }

            level.FilePath = filePath;
            level.LevelAdded = true;

            return level;
        }
        /// <summary>
        /// Creates a Level file given a Level object.
        /// </summary>
        public static void CreateLevelFile( LevelEd level )
        {
            using( BinaryWriter writer = new BinaryWriter( File.Create( level.FilePath ) ) )
            {
                // ---- Writes the file header

                writer.Write( Consts.Editor.FILE_HEADER_ID );

                //Writes the level file version
                writer.Write( LEVEL_VERSION );

                //Writes the level's script name, if any
                if( level.Script == null )
                    writer.Write( NO_SCRIPT );
                else
                    writer.Write( level.Script.Name );

                //Writes the number of layers
                writer.Write( level.Layers.Count );

                //Iterates through each layer and saves its data
                foreach( LayerEd layer in level.Layers )
                {
                    LevelLayerWriteData( layer, writer );
                }

                writer.Close( );
            }
        }
        public static void CreateGameLevelFile( LevelEd level, string filePath )
        {
            using( BinaryWriter file = new BinaryWriter( File.Create( filePath ) ) )
            {
                // ---- Write the header data

                file.Write( Consts.Editor.FILE_HEADER_ID );

                //Writes the file version
                file.Write( GAME_LEVEL_VERSION );

                //Write the script name reference
                if( level.Script == null )
                    file.Write( NO_SCRIPT );
                else
                    file.Write( level.Script.Name );

                // ---- Start to write the sprite and font references for this level

                //Create a local list of string values to store the sprites used in this level
                List<string> spriteNames = new List<string>( );
                //Create a local list of string values to store the fonts used in this level
                List<string> fontNames = new List<string>( );

                //iterate through each layer
                foreach( LayerEd layer in level.Layers )
                {
                    //then iterate through each actor of the current layer
                    foreach( ActorEd actor in layer.Actors )
                    {
                        if( !actor.IsTextActor )
                        {
                            //check if list contains the name of the sprite used by the current actor
                            if( !string.IsNullOrEmpty( actor.Sprite.Name ) && !spriteNames.Contains( actor.Sprite.Name ) )
                                //if not, then add the sprite name to the list
                                spriteNames.Add( actor.Sprite.Name );
                        }
                        else
                        {
                            //check if list contains the name of the font used by the current actor
                            if( actor.FontAsset != null && !fontNames.Contains( actor.FontAsset.AssetName ) )
                                //if not, then add the font name to the list
                                fontNames.Add( actor.FontAsset.AssetName );
                        }
                    }
                }

                //Write the number of sprite names
                file.Write( spriteNames.Count );

                //Iterate through each name in the list
                foreach( string spriteName in spriteNames )
                {
                    //and write the name to the file
                    file.Write( spriteName );
                }

                //Write the number of font names
                file.Write( fontNames.Count );

                //Iterate through each name in the list
                foreach( string fontName in fontNames )
                {
                    //and write the name to the file
                    file.Write( fontName );
                }

                // ---- Start to write the layers data

                //Write the number of layers
                file.Write( level.Layers.Count );
                foreach( LayerEd layer in level.Layers )
                {
                    //Write the layer data
                    GameLevelLayerWriteData( layer, file );
                }
                file.Close( );
            }
        }
        private static void LevelLayerReadData( LevelEd level, BinaryReader file, int version )
        {
            LayerEd layer = new LayerEd( file.ReadString( ) );
            layer.Visible = file.ReadBoolean( );
            layer.Locked = file.ReadBoolean( );
            layer.Scale = file.ReadSingle( );

            //Gets the number of actors
            int actorsCount = file.ReadInt32( );

            for( int i = 0; i < actorsCount; i++ )
            {
                LevelActorReadData( layer, file, version );
            }

            level.NewLayer( layer );
        }