TGC Codebase Backup



Snap to Grid: move mouse along a map tile by Zwarteziel

7th Feb 2014 4:25
Summary

Snap to Grid: a small program that generates a map from a file and lets the user navigate along it using a 'Snap to grid' calculation.



Description

Snap to Grid: a small program that generates a 2D map from a file and lets the user navigate along it (with the mouse) tile-by-tile using a 'Snap to grid' calculation. The functionality might come in handy when building level-editors etc. The program needs a map file (placed in \media\maps\) that is included in a ZIP-file.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    //
// Snap to Grid: a program that generates a map from a file and lets the user navigate
// along it (with the mouse) tile-by-tile. 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
`--------------------------------------------------------------------------------------

// variables for the map
global MapLength        as integer  : MapLength =    64
global MapWidth         as integer  : MapWidth =     64

type MapUDT
    Char$       as string
    SpriteId    as integer
endtype
dim Map[64, 64] as MapUDT

// variables for the sprites
global SprMap           as integer
global SprSize          as integer  : SprSize = 12
global SprPlayer        as integer
global SprPointer       as integer
global SprPosX          as integer
global SprPosZ          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 PlayerPosX       as integer
global PlayerPosZ       as integer
global MapSizeX#        as float    : MapSizeX# = MapWidth * SprSize
global MapSizeZ#        as float    : MapSizeZ# = MapLength * SprSize

// variables for the snap-function
global GridX            as integer
global GridZ            as integer
global SnapX#           as float
global SnapZ#           as float

`======================================================================================
` Load data and create sprite
`--------------------------------------------------------------------------------------
LoadMap("maps/Dungeon02_64.map")

// create map-sprite
SprMap = CreateSprite(0)
setSpriteSize(SprMap, SprSize, SprSize)
setSpriteTransparency(SprMap, 0)
SetSpriteDepth(SprMap, 11)

// create player-sprite
SprPlayer = CreateSprite(0)
setSpriteSize(SprPlayer, SprSize, SprSize)
setSpriteColor(SprPlayer, 86, 117, 226, 255)
setSpriteTransparency(SprPlayer, 0)
SetSpriteDepth(SprPlayer, 10)

// create pointer-sprite
SprPointer = CreateSprite(0)
setSpriteSize(SprPointer, SprSize, SprSize)
setSpriteColor(SprPointer, 255, 255, 225, 255)
setSpriteTransparency(SprPointer, 0)
SetSpriteDepth(SprPointer, 9)

`======================================================================================
` Set the screen
`--------------------------------------------------------------------------------------
SetOrientationAllowed(0, 0, 1, 1)
SetVirtualResolution(1024, 768)
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#

// hide the mouse
SetRawMouseVisible(0)

`======================================================================================
` Set position of player
`--------------------------------------------------------------------------------------

// find a valid position for a 'player' which is used as a reference in this example
repeat
    PlayerPosX = Random(1, MapWidth)
    PlayerPosZ = Random(1, MapLength)
until Map[PlayerPosX, PlayerPosZ].Char$ = ":"

`======================================================================================
` Execute main program loop
`--------------------------------------------------------------------------------------

repeat

    // determine snap, based on mouse position and tile-size
    GetSnap(GetPointerX(), GetPointerY(), SprSize)

    // show map
    ShowMiniMap(1, 1, MapWidth, MapLength)

    // show player-sprite and pointer
    SetSpritePosition(SprPlayer, PlayerPosX * SprSize, PlayerPosZ * SprSize)
    SetSpritePosition(SprPointer, SnapX#, SnapZ#)

    // display some debug-information
    if DebugInfo = 1
        print("Mouse X/Y : " + str((GetPointerX()),2) + ", " + str((GetPointerY()),2))
        print("Snap X/Y  : " + str((SnapX#),2) + ", " + str((SnapZ#),2))
        print("Grid X/Y  : " + str(GridX) + ", " + str(GridZ))
    endif

 Sync()
until GetRawKeyPressed(27) = 1

SetRawMouseVisible(1)
end


`======================================================================================
` Function to determine position on grid, which allows us to 'snap' to it's position
`--------------------------------------------------------------------------------------
function GetSnap(PosX#, PosZ#, Size)
    SnapX# = Size * round((PosX# / Size) + 0.5)
    SnapZ# = Size * round((PosZ# / Size) + 0.5)

    GridX = SnapX# / Size
    GridZ = SnapZ# / Size
endfunction

`======================================================================================
` 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)
    else
        WriteDebugFile("Error in function [LoadMap]: file [FileName$] not found.")
    endif
endfunction


`======================================================================================
` Function to show the minimap [StartX, StartZ, EndX, EndZ]
`--------------------------------------------------------------------------------------
function ShowMiniMap(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$

                    // set default sprite color
                    colR = 20
                    colG = 20
                    colB = 20

                    // set sprite color for door
                    if Char$ = "D" or Char$ = "O" or Char$ = "U"
                        colR = 132
                        colG = 128
                        colB = 112
                    endif

                    // set sprite color for floor
                    if Char$ = " " or Char$ = ":" ` or Char$ = "."
                        colR = 244
                        colG = 226
                        colB = 152
                    endif

                    // set sprite color for walls
                    if Char$ = "#" or Char$ = "S"
                        colR = 166
                        colG = 113
                        colB = 22
                    endif

                    // draw sprite
                    SetSpritePosition(SprMap, (x + SprPosX ) * SprSize, (z + SprPosZ) * SprSize)
                    SetSpriteColor(SprMap, colR, colG, colB, 255)
                    DrawSprite(SprMap)

                endif
            endif
        next x
    next z
endfunction


`======================================================================================
` Function to write debug file
`--------------------------------------------------------------------------------------
function WriteDebugFile(Error$)
    FileId = OpenToWrite("debug.txt", 1)
    WriteLine(FileId, Error$)
     CloseFile(FileId)
endfunction