`Back to the future? NO WAY!
`
`2D is not a thing of the past, and it never will be. Sure 3D is more common in the games
`you'd buy in shelves, but in the shareware world, 2D is booming! Not to mention the fact
`that 2D will keep you from getting a few more headaches! Not to say that 2D is anymore
`simplier than 3D, but with 1 less dimension to worry about, 2D is perfect for beginners.
`
`So what exactly are we making this month? Well, those of you who've always wanted to make
`MARIO, Metroid Prime, or a Castlevania game are in luck! We'll begin working on a 2D
`scrolling engine, and eventually building a full blown 2D side scroller!
`
`Lets go ahead and get started!
`
`
`
`Here is our routine setup.
`First we flush the video memory to make sure we get the best usage out of our
`programs. This is especially important for graphics intensive programs, and generally
`good practice.
`After that, we set the sync rate on(refresh rate of 60), and then turn the backdrop on.
`Finally, we setup our window, the resolution, and the title.

flush video memory
sync on : sync rate 60
backdrop on
set display mode 640,480,32
set window size 640,480

set window title "Scrolling tiles!"

`Here are some important constants that will determine the size of our map.
`Note that constants are exactly like variables, except they're not dynamic. Once
`a constant is assigned, it cannot be changed.

#Constant Dimensions 17
#Constant TileSize 32
#Constant MapSize 150

`Here we're just clearing the screen to a specific color, and then grabbing a
`little section to use as an image for this program.

cls rgb(0,0,192)
get image 1,0,0,32,32
cls rgb(0,128,0)
get image 2,0,0,31,31
cls rgb(160,96,0)
get image 3,0,0,31,31
cls rgb(255,0,0)
get image 4,0,0,31,31
cls rgb(255,192,0)
get image 5,0,0,31,31

`Next we create two arrays.
`The first array(GRID), will hold the data of which tile is where on the x,y coordinates.
`The next array(DISPLAY), works as a sort of temporary array. It's values will constantly
`change, and it will only display what I will refer to as "relative" tiles. These relative
`tiles are the only tiles that the user will actually see at once. As we move around our
`screen, it will appear as though we're actually scrolling, when in fact, we're just shift
`the tiles left,right,up, and down.

dim Grid(MapSize,MapSize)
dim Display(Dimensions,Dimensions)

`Assign a random value to the Grid array.
for x = 0 to MapSize
for y = 0 to MapSize
  Grid(x,y)=1
next x
next y


`Our main loop
do
cls `clear the screen.

`Here is where the actual shifting values occur.
`We shift on the x and y axis accordingly with the variables "currentx" and "currenty".
`We of course put restrictions on these variables(currentx>0 for example), to insure
`that we won't go out of bounds in our array later.

if leftkey()=1 and currentx>0
  currentx=currentx-1
endif

if rightkey()=1 and currentx<MapSize
  currentx=currentx+1
endif

if downkey()=1 and currenty<(MapSize-1)
  currenty=currenty+1
endif

if upkey()=1 and currenty>0
  currenty=currenty-1
endif

`Here is the simple for-next loop that breaks everything down.
`For the current dimensions, the GRID array data is applied to our DISPLAY array.
`This is then the array that we use to paste the various images to the screen with.
`

for x = 0 to Dimensions
for y = 0 to Dimensions
  if currentx+x>MapSize or currenty+y>MapSize `Ensure we're not out of bounds on our array.
    Display(x,y)=Grid(150,150)                `If we are, just assign the last GRID value.
  else                                        `If we're not out of bounds, shift values
                                              `to the DISPLAY array.
    Display(x,y)=Grid(x+currentx,y+currenty)
  endif
  if Display(x,y)=1 then paste image 1,x*TileSize,y*TileSize
  if Display(x,y)=2 then paste image 2,x*TileSize,y*TileSize
  if Display(x,y)=3 then paste image 3,x*TileSize,y*TileSize
  if Display(x,y)=4 then paste image 4,x*TileSize,y*TileSize
  if Display(x,y)=5 then paste image 5,x*TileSize,y*TileSize
next x
next y

`This is just a simple text statement to help show us the values of each tile in the
`DISPLAY array.
if spacekey()=0
for x=0 to Dimensions
for y=0 to Dimensions
 text x*TileSize,y*TileSize,str$(Display(x,y))
next x
next y
endif

`Now that the array is pasted, we can now start to change the tiles around. This is a
`great start to a little tile map editor.
`So now we have the variables TileX and TileY that we must assign.
`We do this by getting the mouse coordinates, and then dividing it by the tile size.
`We of course must also take into consideration if there's been a shift, so we use our
`"currentx" and "currenty" tile variables to ensure that the TileX/TileY values shift as
`well.
TileX=((mousex()+(currentx*32))/TileSize)*1.0
TileY=((mousey()+(currenty*32))/TileSize)*1.0

`Remember earlier when I mentioned relative tile coordinates in the DISPLAY array?
`Well, here they are so we can use them for general debugging perposes if necessary.
relativeTileX=(mousex()/TileSize)*1.0
relativeTileY=(mousey()/TileSize)*1.0


`Now that we know what tiles are what value, and which tile we'er on, we can start editing
`them. We'll do this by left and right clicking to change the tile values.
`Once we click them, then all we have to do is make the change in our GRID array, and
`voila, easy as pie!
if mouseclick()=1 and TileX<=MapSize and TileY<=MapSize
 inc Grid(TileX,TileY),1
 wait 100
endif

if mouseclick()=2 and TileX<=MapSize and TileY<=MapSize
 dec Grid(TileX,TileY),1
 wait 100
endif

`Here's a little bit of debugging, showing the tiles our mouse is over.
text 580,0,str$(currentx+relativeTileX)
text 580,20,str$(currenty+relativeTileY)
text 580,40,str$(relativeTileX)
text 580,60,str$(relativeTileY)


if shiftkey()=1 then SaveLevel(Dimensions, TileSize, MapSize)
if controlkey()=1 then LoadLevel()

sync
loop ` Go back to our do, and start all over!

`Here is our save function, that will save our level.
`We start out by writting each file
function SaveLevel( _Dimensions, _TileSize, _MapSize)
input "",filename$
if file exist(filename$) then delete file filename$
open to write 1,filename$
  write string 1,str$(_Dimensions)
  write string 1,str$(_TileSize)
  write string 1,str$(_MapSize)

  for x = 0 to MapSize
  for y = 0 to MapSize
    write string 1,str$(Grid(x,y))
  next x
  next y

close file 1

endfunction

`Our load level function.
function LoadLevel()
input "",filename$
if file exist(filename$) then open to read 1,filename$
  read string 1,str$(_Dimensions)
  read string 1,str$(_TileSize)
  read string 1,str$(_MapSize)

`Re-assign our constants, in case they've changed.
#Constant Dimensions _Dimensions
#Constant TileSize _TileSize
#Constant MapSize _MapSize

`Read our values from the text file, then convert the values from a string
`variable, to a integer. Then, once it's an integer, we assign it back to our
`array.
  for x = 0 to MapSize
  for y = 0 to MapSize
    read string 1,str$(Grid(x,y))
    Grid(x,y)=val(str$(Grid(x,y)))
  next x
  next y

close file 1

endfunction
