UnderwareDESIGN

PlayBASIC => Beginners => Topic started by: Scott_Bro on February 07, 2021, 02:52:16 PM

Title: Per-pixel Tile Engine
Post by: Scott_Bro on February 07, 2021, 02:52:16 PM
Here it is so far.

It can now handle .csv comma spaced variables with just a tweak.

I got the x & y offsets for the movement.

The screen is 16 X 12 tiles but world is 48 X 36.

Need help moving there the world while keeping player on screen.

Please help!

Scott B.

p.s. This can be used for a great many games!
Title: Re: Per-pixel Tile Engine
Post by: kevin on February 08, 2021, 06:58:24 AM

  why not just work in pixels and then translated those values back to tiles.
Title: Re: Per-pixel Tile Engine
Post by: kevin on February 09, 2021, 08:57:18 PM
 Some old examples,

  -  Mario Demo / 2D Platform Game (https://www.underwaredesign.com/forums/index.php?topic=3101.0)

  - 2D Platformer Revisited - Parallax Version (https://www.underwaredesign.com/forums/index.php?topic=2906.0)

   - 4 Way Map Movement (Moving through tunnels) (https://www.underwaredesign.com/forums/index.php?topic=3687.0)
Title: Re: Per-pixel Tile Engine
Post by: kevin on February 12, 2021, 10:02:13 AM

   Tweaked this up and added scrolling.. 

[pbcode]
; PROJECT : Per-Pixel Tile Engine - Kevs Edit
; AUTHOR  : Scott_Bro_1@outlook.com
; CREATED : 5/02/2021
; EDITED  : 13/02/2021
; ---------------------------------------------------------------------

X_Screen_Res    = 1024
Y_Screen_Res    =  768
Color_Mode       = 32
Windowed_Mode    = 1

OpenScreen X_Screen_Res,Y_Screen_Res,Color_Mode,Windowed_Mode

TitleScreen "Per-Pixel Tile Engine!"

// Tile size.

constant X_Cell_Size = 64
constant Y_Cell_Size = 64

// Tiles per screen.

X_Number_Of_Cells = X_Screen_Res / X_Cell_Size
Y_Number_Of_Cells = Y_Screen_Res / Y_Cell_Size

// Tiles per level.

X_Scalar = 3
Y_Scalar = 3

X_World_Size = X_Number_Of_Cells * X_Scalar
Y_World_Size = Y_Number_Of_Cells * Y_Scalar

// World map.

Dim World_Map(X_World_Size,Y_World_Size)

LoadWorld("Dungeon_1.txt",X_World_Size,Y_World_Size)



// Player's starting position.

X1_Player =  X_Cell_Size * 4
Y1_Player =  Y_Cell_Size

// Player's current cell location.

X1_Player_Cell = X1_Player / X_Cell_Size
Y1_Player_Cell = Y1_Player / Y_Cell_Size

// Player's speed.

Speed = 1


// -------------------------------------------------------
// Camera Scrolling Position
// -------------------------------------------------------
 
CameraX = 0
CameraY   = 0

Do
   
   Cls
   
   
   // ----------------------------------------------
    //  Scroll the Camera
   // ----------------------------------------------
       CameraX = mod(CameraX-1,-1000)
   
      
   // ----------------------------------------------
   // Render tiles to screen.
   // ----------------------------------------------
   
         DrawWorld(CameraX,CameraY, X_World_Size,Y_World_Size)
         
   // ----------------------------------------------
   //  Draw the Player
   // ----------------------------------------------
            
         // Player one.
         
         Ink RGB(0,0,255)
         ScreenX = CameraX +(X1_Player_Cell * X_Cell_Size + X_Offset)
         ScreenY = Y1_Player_Cell * Y_Cell_Size + Y_Offset      
         Box ScreenX,ScreenY,ScreenX+ X_Cell_Size ,ScreenY+ Y_Cell_Size,1
         

   
      // Display readout.
   
      // Player's current tile postion.
      Text 0,0,"X1_Player_Cell: " + Str$(X1_Player_Cell) + " Y1_Player_Cell: " + Str$(Y1_Player_Cell)
   
      // Player's movement offsets.
      Text 0,16,"X_Offset: " + Str$(X_Offset) + " Y_Offset: " + Str$(Y_Offset)
   
   
      // Camera X/Y positions
      Text 0,32,"Camera X: " + Str$(CameraX) + " CameraY:" + Str$(CameraY)
      
   
   // Bring up the curtains.
   Sync
   
   // Saved player's old position.
   
   X1_Player_Old = X1_Player
   Y1_Player_Old = Y1_Player
   
   // Player's controls.
         
   If Leftkey() = 1 Then X1_Player = X1_Player - Speed
   If Rightkey() = 1 Then X1_Player = X1_Player + Speed
   
   If Upkey() = 1 Then Y1_Player = Y1_Player - Speed
   If Downkey() = 1 Then Y1_Player = Y1_Player + Speed
   
   // Player's cell location.
   X1_Player_Cell = X1_Player / X_Cell_Size
   Y1_Player_Cell = Y1_Player / Y_Cell_Size
   
   // World collision detection.
   
   If World_Map(X1_Player_Cell,Y1_Player_Cell) > 1
      
      // Restore player's last position before collision occured.
                       
      X1_Player = X1_Player_Old
      Y1_Player = Y1_Player_Old
      
      // Place player into safe position.
      
      X1_Player_Cell = X1_Player / X_Cell_Size
      Y1_Player_Cell = Y1_Player / Y_Cell_Size
   
   Else
      
      // Per-pixel movement.
          
      X_Offset = Mod(X1_Player,X_Cell_Size)
      Y_Offset = Mod(Y1_Player,Y_Cell_Size)
         
   EndIf
   
Loop  Spacekey()

End   




   
   
Function DrawWorld(ScrollX,ScrollY,MapWidth,MapHeight)

   Static PaletteStatus
   
   //  Check if the palette has been setup ??
   if PaletteStatus=0

      //  if not, create it and store our colours in it..
      // but only the first time we run the function
      dim Palette(256)   
      Palette(0)    = 0
      Palette(1)    = RGB(63,63,63)      // floor
      palette(2)   =  RGB(127,127,127)   // Walls
      palette(3)   =  RGB(255,0,0)      // Doors
      palette(4)   = RGB(63,63,63)      // BSP node
   
       //  Change this STATIC from it's default of Zero to one
       // so this code isn't executed next frame
      PaletteStatus=1
   endif   


   //  Draw this grid of tiles
   For Y_Cells = 0 To MapHeight
      
      Y1 = ScrollY+(Y_Cells * Y_Cell_Size)
      Y2 = Y1 + Y_Cell_Size
      For X_Cells = 0 To MapWidth
            ThisRGB = Palette(World_Map(X_Cells,Y_Cells))

            X1   = ScrollX+(X_Cells * X_Cell_Size)
            X2   = X1+X_Cell_Size   

            Boxc X1,Y1,X2,Y2,1, ThisRGB
            Boxc X1,Y1,X2,Y2,0, Rgb(255,255,255)

      Next X_Cells      
   Next Y_Cells      
   
EndFunction


function LoadWorld(Filename$,Width,Height)
      if fileexist(Filename$)   
         size=filesize(filename$)
         fh=readnewfile(Filename$)
         if fh
            s$=readchr$(fh,Size)
            closefile fh
            Dim Rows$(Height)
            Dim TileIndexes(Width)
            
            s$=replace$( s$,chr$(13),"")
            rowcount=splittoarray( s$,chr$(10),Rows$(),0,0)
            RowCount=ClipRange(RowCount,0,Height)
            for ylp =0 to RowCount-1
               s$=rows$(ylp)
               s$=replace$(s$,","," , ")
               Count = SplitToArray( s$ ,",",TileIndexes(),0,7)
               Count = Cliprange(Count,0,Width)
               for xlp=0 to Count-1
                     World_Map(xlp,ylp)= TileIndexes(xlp)
               next
                                  
            next
         endif   
      endif
EndFunction

[/pbcode]