LoadNewLevel
Level_Index = LoadNewLevel(Filename$, Map_Index)
 
Parameters:

    Filename$ = The file name of the level
    Map_Index = The Map this level should be loaded into
Returns:

    Level_Index = The index inside the map of this level
 

     LoadNewLevel reads a previously saved PlayBASIC Level file into a created map. Unlike the LoadLevel command, LoadNewLevel allocates a free level index within the map for you. Beyond that it functions the same as LoadLevel command.


     Bellow we're provided you with a very basic bare bones map editing example. Enough though it's simple, it's well worth looking over. Hopefully it'll give you some ideas in building your own helper tools to aid in your games development. You can import data from most common place map systems, but of course given the number of the formats that's largely going to be up to you.




FACTS:


      * LoadNewLevel is part of the Maps library, which includes various map Loading/Saving functions with compression. To use these function you'll need to include the maps library at the top of your code. Eg #include "Maps"




Mini Tutorial #1:


      This example imports the Maps loader/saver functions, creates a map, loads a block set from the help files as well as pre-existing (known to exist) level from the help files. So we can get a map set up ready for display pretty quickly if the data already exists.

  
; Include the maps load/saver functions
  #Include "maps"
  
  
; create a map with space for 5 levels
  ThisMap=NewMap(5)
  
  
; compute the location of the help files
  Help_Path$ =ProgramDir$()+"Help\Commands\"
  
; get the filename of the blocks in the help files
  BLocks$=Help_Path$+"Media\Grass-Tiles.bmp"
  
; load the blocks into our map
  LoadMapGFX  Blocks$,ThisMap,32,32,RGB(0,0,0),2
  
  
; compute the location of a level from the
; LoadLevel example in the help files
  Level_file_name$=Help_Path$+"Maps\LoadLevel\TestLevel.Level"
  
  ThisLevel=LoadNewLevel(Level_File_Name$,ThisMap)
  
  If ThisLevel>-1
     DrawMap THisMap,ThisLevel,0,0
     
  Else
     Print "Error - Couldn't find the level.."
     Print Level_File_Name$
     
  EndIf
  
  Sync
  WaitKey
  
  
  





Mini Tutorial #2: Simple Map Editor


      The attached emample is very simple map editing tool. It lets the user select blocks, draw them to the map with basic map loading and saving functionality.




 
Example Source: Download This Example
; *=-------------------------------------------------------------------=*
;                      >> Simple Map Editor Example <<
; *=-------------------------------------------------------------------=*
;
;  This is basic map editor that includes Load and Save functions.
; The editor only supports a single map layer, but you can expand
; on those yourself.
;
;
;  Controls:
;
;           Mouse Left Button: Write Current block to map
;                 Mouse Wheel: Change Current block
;                  Arrow Keys: Scroll View
;                            F1 Key: Load Previously Saved Level
;                            F2 Key: Save current level
;
; *=-------------------------------------------------------------------=*
  
  // -----------------------------------------------------
  // --[INCLUDE MAP]-------------------------------------
  // -----------------------------------------------------
  
  #Include "maps"
  
; Make the APP limit itself to 30 frames per second
; or less
  SetFPS 40
  
; set the screen title
  TitleScreen "Simple Map Editor"
  
; --------------------------------------
; CReate a map and level to edit
; --------------------------------------
  
; Create a new map with provision for 5 levels
  ThisMap=NewMap(5)
  
; Load common grass blocks from the help files
  Blocks$ =ProgramDir$()+"Help\Commands/Media/"
  BLocks$+="Grass-Tiles.bmp"
  LoadMapGFX  Blocks$,ThisMap,32,32,RGB(0,0,0),2
  
; set the name of the test level we'll
  Level_file_name$="TestLevel.Level"
  
; Either load the existing level or
; create a level with the map that's 50 by 50 blocks
  If FileExist(Level_file_name$)
     ThisLevel=LoadNewLevel(Level_File_Name$,ThisMap)
  Else
     ThisLevel=NewLevel(ThisMap, 50,50)
  EndIf
  
; The current block the user is drawing
  CurrentDrawingBlock     = 13
  
  
  // -----------------------------------------------------
  // --[MAIN LOOP]----------------------------------------
  // -----------------------------------------------------
  Do
     
     // Render Scene
     Cls 0
     DrawMap THisMap,ThisLevel,-ScrollX,-ScrollY
     
     // handle all the mouse controls
     Gosub Handle_Mouse
     Gosub Handle_Key_Board
     
     // Display the map info
     Gosub Display_Map_Info
     
     
     Sync
  Loop EscKey()
  
  End
  
  
  
  //-------------------------------------------------------
Handle_Mouse:
  //-------------------------------------------------------
  
  // Render the current block at mouse position
  Mx=MouseX()
  My=MouseY()
  Mz=MouseMoveZ()
  Mb=MouseButton()
  
  If MZ<>0
     CurrentDrawingBlock=WrapValue(CurrentDrawingBlock+Mz,0GetMapBlocks(THisMap))
  EndIf
  
  // Convert Mouse Screen coordinates to Map Tile Coords
  MapTileX  = (ScrollX+MX)/GetMapBlockWidth(ThisMap)
  MapTileY  = (ScrollY+MY)/GetMapBlockHeight(ThisMap)
  
  // compute the cords of the hower box
  x1=(MapTileX *     GetMapBlockWidth(ThisMap)) - ScrollX
  y1=(MapTileY *     GetMapBlockHeight(ThisMap)) - ScrollY
  x2=X1+GetMapBlockWidth(ThisMap)
  y2=Y1+GetMapBlockHeight(ThisMap)
  
  // display a hover box, so where the mouse is over
  BoxC x1,y1,x2,y2,false$ff0000
  
  // display the current block at the mouses position
  DrawMapBlk ThisMap,CurrentDrawingBLock,MX,MY,0
  
  // check if thne left mouse button is down
  If MB And 1
     // if the button is press, we write the current drawing block
     // to the map at this position
     If MapTileX>=0 And MapTileY>=0
        If MapTileX<= GetLevelWidth(ThisMap,ThisLevel)
           If MapTileY<= GetLevelHeight(ThisMap,ThisLevel)
              PokeLevelTile ThisMap,ThisLevel,MapTileX,MapTileY,CurrentDrawingBlock
           EndIf
        EndIf
     EndIf
  EndIf
  
  Return
  
  
  
  
  //-------------------------------------------------------
Handle_Key_Board:
  //-------------------------------------------------------
  
  // ------------------------------------------
  // Load Pre existing Level
  // ------------------------------------------
  
  If FunctionKeys(1)=true
     If FileExist(Level_file_name$)
        LoadLevel Level_file_name$,ThisMap,ThisLevel
     EndIf
  EndIf
  
  
  // ------------------------------------------
  // Save Level existing Level
  // ------------------------------------------
  
  If FunctionKeys(2)=true
     SaveLevel Level_file_name$,ThisMap,ThisLevel
  EndIf
  
  
  
  // ------------------------------------------
  // ARROW KEY scrolling
  // ------------------------------------------
  If ScrollX>0 And LeftKey()
     ScrollX-=GetMapBlockWidth(ThisMap)
  EndIf
  
  If ScrollX< GetLevelABSWidth(ThisMap,ThisLevel)
     If RightKey()
        ScrollX+=GetMapBlockWidth(ThisMap)
     EndIf
  EndIf
  
  If ScrollY>0 And UpKey()
     ScrollY-=GetMapBlockHeight(ThisMap)
  EndIf
  
  If ScrollY< GetLevelABSHeight(ThisMap,ThisLevel)
     If DownKey()
        ScrollY+=GetMapBlockHeight(ThisMap)
     EndIf
  EndIf
  
  Return
  
  
  
  
  //-------------------------------------------------------
Display_Map_Info:
  //-------------------------------------------------------
  
  // -----------------------------------------------
  // render current map information
  // -----------------------------------------------
  s$ =" Editing Map:"+Digits$(ThisMap,2)
  s$+=" Level:"+Digits$(THisLevel,2)
  
  // Render current position
  s$+=" Position:"+Str$(ScrollX)
  s$+="X, "+Str$(ScrollY)+"Y"
  
  s$+="  Current Drawing Block:"
  s$+=Str$(CurrentDrawingBlock)
  Print s$
  
  Print " (F1) = Load Level"
  Print " (F2) = Save Level"
  
  Return
  
  
  
 
Related Info: LoadMapGfx | SaveLevel :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com