0 Members and 1 Guest are viewing this topic.
'==============================================================================='===============================================================================' Sample4.bmx By Scott Shaver'' This sample displays a simple one layer LayeredTileMap and allows the user' to move a character around the map with the cursor keys. Use the escape key' to exit the program. The character can't go through the walls. '' Written: 6/23/2005' Update 6/27/2005 -' Added the code for detecting the wall collisions'==============================================================================='===============================================================================Strict Framework BRL.GlMax2DImport BRL.SystemImport BRL.BasicImport BRL.pngloaderImport BRL.RetroImport pub.iglImport SAS.tilemaps?Win32SetGraphicsDriver(GLMax2DDriver())?' init the graphics modeGraphics 800,600,32',85' Load a LayeredTileMap fileGlobal map:LayeredTileMap = LayeredTileMap.Load("maps/Walls.smf")' load the character imageSetMaskColor(255,255,255)Global guy:TImage = LoadImage("maps/circleguy.png",MASKEDIMAGE)Global charWidth:Int=30Global charHeight:Int=30Global halfCW:Int=charWidth/2Global halfCH:Int=charHeight/2' Set the viewport to the entire screenmap.ViewPort(50,50,GraphicsWidth()-100,GraphicsHeight()-100)' some helper varsGlobal halfVPW:Int = map.viewPortPixelWidth/2Global halfVPH:Int = map.viewPortPixelHeight/2Global centerVPX:Int = map.viewPortX + halfVPWGlobal centerVPY:Int = map.viewPortY + halfVPHGlobal scrollSpeed:Int=3Global wallTileIndex=0' set up the starting position of the character by finding a specific cellGlobal sx:Float,sy:Floatmap.MapCellToScreenCoords(1,1,sx,sy,0)' blue backgroundSetClsColor(0,0,255)' the main program loopWhile Not KeyDown(KEY_ESCAPE) Cls Local lastX:Float = map.GetX() Local lastY:Float = map.GetY() Local cx:Float,cy:Float Local temp:Float ' handle moving the map and character around with the cursor keys If KeyDown(KEY_A) Or KeyDown(KEY_LEFT) Then If sx+halfCW > centerVPX Then ' the character is right of the center of the screen sx:-scrollSpeed Else If lastX=0 Then ' edge of the map move the character If sx > map.viewPortX Then sx:-scrollSpeed If sx < map.viewPortX Then sx=map.viewPortX Else ' character is centered move the map map.SetPosition(lastX+scrollSpeed,lastY) lastX = map.GetX() EndIf ' check the characters upper left corner map.ScreenToMapCell(sx,sy,cx,cy,0) If map.GetCell(cx,cy,0)=wallTileIndex Then map.MapCellToScreenCoords(cx+1,cy,sx,temp,0) ' check the characters lower left corner map.ScreenToMapCell(sx,sy+charHeight-1,cx,cy,0) If map.GetCell(cx,cy,0)=wallTileIndex Then map.MapCellToScreenCoords(cx+1,cy,sx,temp,0) EndIf If KeyDown(KEY_D) Or KeyDown(KEY_RIGHT) Then If sx+halfCW < centerVPX Then ' the character is left of the center of the screen sx:+scrollSpeed Else If lastX=-((map.cellPixelWidth*map.mapCellWidth) - map.viewPortPixelWidth) Then ' edge of the map move the character If sx < map.viewPortX+map.viewPortPixelWidth-charWidth Then sx:+scrollSpeed If sx > map.viewPortX+map.viewPortPixelWidth-charWidth Then sx=map.viewPortX+map.viewPortPixelWidth-charWidth Else ' character is centered move the map map.SetPosition(lastX-scrollSpeed,lastY) lastX = map.GetX() EndIf ' check the characters upper right corner map.ScreenToMapCell(sx+charWidth,sy,cx,cy,0) If map.GetCell(cx,cy,0)=wallTileIndex Then map.MapCellToScreenCoords(cx-1,cy,sx,temp,0) sx:+(map.cellPixelWidth-charWidth) EndIf ' check the characters lower right corner map.ScreenToMapCell(sx+charWidth-1,sy+charHeight-1,cx,cy,0) If map.GetCell(cx,cy,0)=wallTileIndex Then map.MapCellToScreenCoords(cx-1,cy,sx,temp,0) sx:+(map.cellPixelWidth-charWidth) EndIf EndIf If KeyDown(KEY_W) Or KeyDown(KEY_UP) Then If sy+halfCH > centerVPY Then ' the character is below of the center of the screen sy:-scrollSpeed Else If lastY=0 Then ' edge of the map move the character If sy > map.viewPortY Then sy:-scrollSpeed If sy < map.viewPortY Then sy=map.viewPortY Else ' character is centered move the map map.SetPosition(lastX,lastY+scrollSpeed) lastY = map.GetY() EndIf ' check the characters upper left corner map.ScreenToMapCell(sx,sy,cx,cy,0) If map.GetCell(cx,cy,0)=wallTileIndex Then map.MapCellToScreenCoords(cx,cy+1,temp,sy,0) ' check the characters upper right corner map.ScreenToMapCell(sx+charWidth-1,sy,cx,cy,0) If map.GetCell(cx,cy,0)=wallTileIndex Then map.MapCellToScreenCoords(cx,cy+1,temp,sy,0) EndIf If KeyDown(KEY_S) Or KeyDown(KEY_DOWN) Then If sy+halfCH < centerVPY Then ' the character is above of the center of the screen sy:+scrollSpeed Else If lastY=-((map.cellPixelHeight*map.mapCellHeight) - map.viewPortPixelHeight) Then ' edge of the map move the character If sy < map.viewPortY+map.viewPortPixelHeight-charHeight Then sy:+scrollSpeed If sy > map.viewPortY+map.viewPortPixelHeight-charHeight Then sy=map.viewPortY+map.viewPortPixelHeight-charHeight Else ' character is centered move the map map.SetPosition(lastX,lastY-scrollSpeed) lastY = map.GetY() EndIf ' check the characters lower left corner map.ScreenToMapCell(sx,sy+charHeight-1,cx,cy,0) If map.GetCell(cx,cy,0)=wallTileIndex Then map.MapCellToScreenCoords(cx,cy-1,temp,sy,0) sy:+(map.cellPixelHeight-charHeight) EndIf ' check the characters lower right corner map.ScreenToMapCell(sx+charWidth-1,sy+charHeight-1,cx,cy,0) If map.GetCell(cx,cy,0)=wallTileIndex Then map.MapCellToScreenCoords(cx,cy-1,temp,sy,0) sy:+(map.cellPixelHeight-charHeight) EndIf EndIf ' clear the background of the map with black SetColor(0,0,0) DrawRect(50,50,GraphicsWidth()-100,GraphicsHeight()-100) ' draw the map map.Draw() ' draw the character shadow SetAlpha(.5) SetColor(0,0,0) DrawImage(guy,sx+3,sy+3) ' draw the character SetAlpha(1) SetColor(255,255,255) DrawImage(guy,sx,sy) Flip' FlushMem()Wend