Wrote this in DBP awhile back. Thought I'd copy it over to AGK. Turns out I already did (at least in my project folder) but couldn't find it posted here yet. I updated a few things and so here it is.
Arrow keys to scroll map. Mouse wheel to cycle through the tiles. The tiles are all cloned from a single animated sprite, using the frames as the different tiles. 'G' will toggle the grid. Nothing fancy, just a demo to get people started. Attached is the sample image I used for the demo.
rem ====================
rem Isometric tile map
rem Author: Phaelax
rem Date: 18 Dec 2015
rem ====================
setVirtualResolution(800,600)
// Load the tile map
tm = loadImage("isometric_grass_and_water.png")
// Set image filter so image is sharp and tiles match/line up better
SetImageMagFilter(tm, 0)
// Create the base sprite and 'animate' it so we can use the frames as tiles
_Spr_Tiles = createSprite(tm)
setSpriteAnimation(_Spr_Tiles, 64, 64, 24)
setSpritePosition(_Spr_Tiles, 736, 0)
// Map dimensions
Global _MapWidth = 20
Global _MapHeight = 10
// for map scrolling
Global _MapOffsetX = 320
Global _MapOffsetY = 100
#CONSTANT TILESIZE = 64 ` actual image size
#CONSTANT TILE_X = 32 ` typically half of TILESIZE
#CONSTANT TILE_Y = 16 ` typically half of TILE_X
#CONSTANT BASEOFFSET = 0 ` if graphic starts at bottom of image, this would be the same value as TILE_Y
#CONSTANT BASE_DEPTH = 10 ` Lowest sprite z-depth
#CONSTANT TILE_COUNT = _MapHeight*_MapWIDTH ` how many tiles make up this map (necessary for setting z-depth)
// Map array
dim map[_MapWidth, _MapHeight]
// Initialize map data
for i = 1 to _MapWidth
for j = 1 to _MapHeight
map[i,j] = 0
next j
next i
showGrid = 1 : `simple true or false to show iso grid
scrollSpeed = 4 : `map scroll speed
do
// Map scroll controls
if getRawKeyState(37) = 1 then positionMap(_MapOffsetX+scrollSpeed, _MapOffsetY)
if getRawKeyState(38) = 1 then positionMap(_MapOffsetX, _MapOffsetY+scrollSpeed)
if getRawKeyState(39) = 1 then positionMap(_MapOffsetX-scrollSpeed, _MapOffsetY)
if getRawKeyState(40) = 1 then positionMap(_MapOffsetX, _MapOffsetY-scrollSpeed)
// Scroll wheel cycles through tiles
wheel = GetRawMouseWheelDelta()
if wheel < 0
f = GetSpriteCurrentFrame(_Spr_Tiles)
if f < GetSpriteFrameCount(_Spr_Tiles)
setSpriteFrame(_Spr_Tiles, f+1)
else
setSpriteFrame(_Spr_Tiles, 1)
endif
endif
if wheel > 0
f = GetSpriteCurrentFrame(_Spr_Tiles)
if f > 1
setSpriteFrame(_Spr_Tiles, f-1)
else
setSpriteFrame(_Spr_Tiles, GetSpriteFrameCount(_Spr_Tiles))
endif
endif
// Mouse position relative to map
mx = GetRawMouseX() - _MapOffsetX
my = GetRawMouseY() - _MapOffsetY
// 'g' to toggle grid
if getRawKeyPressed(71) = 1
showGrid = 1 - showGrid
endif
// Draws an isometric grid
if showGrid = 1
drawGrid()
endif
// Translate mouse coordinates into tile map coordinates [ix,iy]
mx = GetRawMouseX()-_MapOffsetX
my = GetRawMouseY()-_MapOffsetY
ix = (TILESIZE*my + TILE_X*mx) / (TILESIZE*TILE_X)
iy = (TILESIZE*my - TILE_X*mx) / (TILESIZE*TILE_X)
// Place tile on map
if GetRawMouseLeftPressed() = 1
if ix >= 0 and ix < _MapWidth and iy >= 0 and iy < _MapHeight
if map[ix+1, iy+1] > 0 then deleteSprite(map[ix+1, iy+1])
map[ix+1, iy+1] = cloneSprite(_Spr_Tiles)
tileDepth = (TILE_COUNT) - (ix*_MapHeight + iy) + BASE_DEPTH
setSpriteDepth(map[ix+1, iy+1], tileDepth)
tx = (ix-iy-1)*TILE_X
ty = (ix+iy-1)*TILE_Y - BASEOFFSET
setSpritePosition(map[ix+1, iy+1], _MapOffsetX + tx , _MapOffsetY + ty)
endif
endif
print("FPS: "+str(screenfps()))
print(str(ix)+":"+str(iy))
print("Tile:# "+str(ix*_MapHeight + iy))
sync()
loop
// ===============================================================
// Returns true if mouse is within the specified boundary
// ===============================================================
function mouseWithin(x1, y1, x2, y2)
if GetRawMouseX() > x1 and GetRawMouseX() < x2 and GetRawMouseY() > y1 and GetRawMouseY() < y2 then exitfunction 1
endfunction 0
// ===============================================================
// Positions the sprites making up the map
// ===============================================================
function positionMap(x, y)
_MapOffsetX = x
_MapOffsetY = y
for i = 0 to _MapWidth-1
for j = 0 to _MapHeight-1
if map[i+1,j+1] > 0
tx = x + (i-j-1)*TILE_X
ty = y + (i+j-1)*TILE_Y - BASEOFFSET
setSpritePosition(map[i+1,j+1], tx, ty)
endif
next j
next i
endfunction
// ===============================================================
// Draws an isometric grid
// ===============================================================
function drawGrid()
height = _MapHeight*TILE_Y
for x = 0 to _MapWidth
lx = _MapOffsetX + x*TILE_X
ly = _MapOffsetY + x*TILE_Y
drawLine(lx, ly, lx-TILE_X*_MapHeight, ly+height, 48,48,48)
next x
width = _MapWidth*TILE_X
for y = 0 to _MapHeight
lx = _MapOffsetX - y*TILE_X
ly = _MapOffsetY + y*TILE_Y
drawLine(lx, ly, lx+width, ly+TILE_Y*_MapWidth, 48,48,48)
next y
endfunction
(why does the code tags not support AppGameKit yet?)