Minimap using SetSpriteScissor by Zwarteziel8th Feb 2014 7:41
|
---|
Summary Minimap using SetSpriteScissor: Generates a map from file-data and uses the SetSpriteScissor-command to only display a portion of it Description Minimap using SetSpriteScissor: Generate a map from file-data using a single sprite, convert it to a large sprite and use the SetSpriteScissor-command to only display a portion of it, thus creating a mini-map that can be used in a game. The code needs a map-file (in \media\maps) which is included in the CodeBase as a .ZIP-file. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com // // Minimap using SetSpriteScissor: Generate a map from file-data using a single sprite, // convert it to a large sprite and use the SetSpriteScissor-command to only display a // portion of it, thus creating a mini-map that can be used in a game. The code needs a // map-file (in \media\maps) which is included in the CodeBase as a .ZIP-file. // // Zerotown, AGK v1.821 // `====================================================================================== ` Set variables and assign values `-------------------------------------------------------------------------------------- #constant KeyLeft 37 #constant KeyUp 38 #constant KeyRight 39 #constant KeyDown 40 #constant KeyEsc 27 // variables for the map global MapLength as integer : MapLength = 64 global MapWidth as integer : MapWidth = 64 type MapUDT Char$ as string endtype dim Map[64, 64] as MapUDT // variables for the sprites global SprId as integer global SprSize as integer : SprSize = 12 global SprPlayer as integer // variables for debug information global DebugInfo as integer : DebugInfo = 1 // variables for setting the screen global ScreenAspect# as float global ScreenHeight# as float global ScreenMax# as float global ScreenMin# as float global ScreenWidth# as float // variables for minimap global MMImage as integer global MMPosX as integer global MMPosZ as integer global MMSizeX as integer global MMSizeZ as integer global MMRange# as float : MMRange# = 21 // variables to test the minimap global CropStartX as integer global CropStartZ as integer global CropEndX as integer global CropEndZ as integer global PlayerPosX as integer global PlayerPosZ as integer `====================================================================================== ` Load data and create sprite `-------------------------------------------------------------------------------------- LoadMap("maps/Dungeon02_64.map") SprId = CreateSprite(0) SetSpriteSize(SprId, SprSize, SprSize) SetSpriteTransparency(SprId, 0) SprPlayer = CreateSprite(0) SetSpriteSize(SprPlayer, SprSize, SprSize) SetSpriteTransparency(SprId, 0) SetSpriteColor(SprPlayer, 86, 117, 226, 255) SetSpriteVisible(SprPlayer, 0) `====================================================================================== ` Set the screen `-------------------------------------------------------------------------------------- SetOrientationAllowed(0, 0, 1, 1) SetVirtualResolution(1024, 786) SetResolutionMode(0) SetPrintSize(18) SetClearColor(0, 0, 0) SetScissor(0, 0, 0, 0) SetSortTextures(1) SetSyncRate(0, 0) ScreenWidth# = GetVirtualWidth() ScreenHeight# = GetVirtualHeight() if ScreenWidth# > ScreenHeight# ScreenMin# = ScreenHeight# ScreenMax# = ScreenWidth# else ScreenMin# = ScreenWidth# ScreenMax# = ScreenHeight# endif ScreenAspect# = ScreenMax# / ScreenMin# `====================================================================================== ` Prepare the map in the backbuffer `-------------------------------------------------------------------------------------- // Generate the minimap - parameters allow you to generate only a part of it, should that be desirable GenerateMiniMap(1, 1, MapWidth, MapLength) // update and render the map in the backbuffer update(0) render() // calculate the size of the map in pixels and grab an image it MMSizeX = MapWidth * SprSize MMSizeZ = MapLength * SprSize MMImage = GetImage(MMPosX, MMPosZ, (MMPosX + MMSizeX), (MMPosZ + MMSizeZ)) // delete the sprite originally used to generate the map and create a new one, containing the whole map DeleteSprite(SprId) SprId = CreateSprite(MMImage) // clear the screen and swap the backbuffer to the frontbuffer ClearScreen() Swap() `====================================================================================== ` Prepare the execution of the main program loop `-------------------------------------------------------------------------------------- // set coordinates to display the mini-map (can be any position one likes) MMPosX = 0 MMPosZ = GetVirtualHeight() / 2 // place the sprite at the desired coordinates SetSpritePosition(SprId, MMPosX, MMPosZ) // find a valid position for the player repeat PlayerPosX = Random(1, MapWidth) PlayerPosZ = Random(1, MapLength) until Map[PlayerPosX, PlayerPosZ].Char$ = ":" // make player sprite visible SetSpriteVisible(SprPlayer, 1) SetSpriteDepth(SprPlayer, 1) // calculate initial crop for sprite, based on set range CalcCropRange(PlayerPosX, PlayerPosZ, MMRange#) SetSpriteScissor(SprId, MMPosX, MMPosZ, MMPosX + (MMRange# * SprSize), MMPosZ + (MMRange# * SprSize)) `====================================================================================== ` Execute main program loop `-------------------------------------------------------------------------------------- do // display some debug-information, should one desire so if DebugInfo = 1 print("FPS : " + str(ScreenFPS())) print("Use arrow keys to move") print("") print("Map Size : " + str(MapWidth) + ", " + str(MapLength) + " (" + str(round(GetSpriteWidth(SprId))) + ", " + str(round(GetSpriteHeight(SprId))) + ")") print("Player X/Y : " + str(PlayerPosX) + ", " + str(PlayerPosZ) + " (" + str(PlayerPosX * SprSize) + ", " + str(PlayerPosZ * SprSize) +")") print("Crop StrX/Y: " + str(CropStartX) + ", " + str(CropStartZ) + " (" + str(CropStartX * SprSize) + ", " + str(CropStartZ * SprSize) +")") print("Crop EndX/Y: " + str(CropEndX) + ", " + str(CropEndZ) + " (" + str(CropEndX * SprSize) + ", " + str(CropEndZ * SprSize) +")") print("") print("Map X/Y : " + str(round(GetSpriteX(SprId))) + ", " + str(round(GetSpriteY(SprId)))) endif // check for user-input CheckInput() // calculate crop for mini-map sprite, based on player's position and range CalcCropRange(PlayerPosX, PlayerPosZ, MMRange#) // place sprites SetSpritePosition(SprPlayer, MMPosX - (CropStartX * SprSize) + (PlayerPosX * SprSize), MMPosZ - (CropStartZ * SprSize) + (PlayerPosZ * SprSize)) SetSpritePosition(SprId, MMPosX - (CropStartX * SprSize), MMPosZ - (CropStartZ * SprSize)) Sync() loop `====================================================================================== ` Function to load map-data from file `-------------------------------------------------------------------------------------- function LoadMap(FileName$) if GetFileExists(FileName$) = 1 FileId = OpenToRead(FileName$) for z = 1 to MapLength for x = 1 to MapWidth Char$ = chr(ReadByte(FileId)) Map[x, z].Char$ = Char$ next x next z CloseFile(FileId) endif endfunction `====================================================================================== ` Function to generate the minimap [StartX, StartZ, EndX, EndZ] `-------------------------------------------------------------------------------------- function GenerateMiniMap(StartX, StartZ, EndX, EndZ) // display the minimap-sprites for z = StartZ to EndZ for x = StartX to EndX if z => 1 and z <= MapLength if x => 1 and x <= MapWidth Char$ = Map[x, z].Char$ colR = 20 colG = 20 colB = 20 // set sprite color if Char$ = "D" or Char$ = "O" or Char$ = "U" colR = 132 colG = 128 colB = 112 endif if Char$ = " " or Char$ = ":" colR = 244 colG = 226 colB = 152 endif if Char$ = "#" or Char$ = "S" colR = 166 colG = 113 colB = 22 endif // draw sprite setSpritePosition(SprId, x * SprSize, z * SprSize) setSpriteColor(SprId, colR, colG, colB, 255) drawSprite(SprId) endif endif next x next z endfunction `====================================================================================== ` Function to calculate the coordinates the map-sprite should be displayed if cropped `-------------------------------------------------------------------------------------- function CalcCropRange(PosX, PosZ, Range) // find radius of range RadRange = round(MMRange# / 2) // find positions for crop e.g. SetSpriteScissor-function CropStartX = PosX - RadRange CropStartZ = PosZ - RadRange CropEndX = PosX + RadRange CropEndZ = PosZ + RadRange endfunction `====================================================================================== ` Function to check user input `-------------------------------------------------------------------------------------- function CheckInput() if GetRawKeyState(KeyUp) = 1 PlayerPosZ = PlayerPosZ - 1 if PlayerPosZ <= 1 then PlayerPosZ = 1 endif if GetRawKeyState(KeyDown) = 1 PlayerPosZ = PlayerPosZ + 1 if PlayerPosZ => MapLength - 1 then PlayerPosZ = MapLength - 1 endif if GetRawKeyState(KeyLeft) = 1 PlayerPosX = PlayerPosX - 1 if PlayerPosX <= 1 then PlayerPosX = 1 endif if GetRawKeyState(KeyRight) = 1 PlayerPosX = PlayerPosX + 1 if PlayerPosX => MapWidth - 1 then PlayerPosX = MapWidth - 1 endif endfunction |