Skip to content

Making Realtime Minimaps

Bear Thorne edited this page Nov 5, 2017 · 9 revisions

PAGE UNDER CONSTRUCTION

For one of my projects, (a dungeon crawler) I decided I wanted a minimap to help the player navigate the levels.

Here's a screenshot of what I wanted:
dungeon_minimap

The levels are generated using the Drunk Walk algorithm so I wasn't going to be able to draw them beforehand.
I needed a way to read the map and draw a minimap based on the layout.
Immediately I thought of the pix() function.

HOW TO USE PIX()

this function is very straightforward.
It takes two or three arguments, based on the function you want:
pix( x_coor, y_coor, color_index)
This will take the pixel at the given (x,y) coordinates and make it the color you specified.
If you leave off the color_index, it will return the current color index of that pixel.

DRAWING THE MAP

The idea of drawing the map is very similar to the section "Building Levels From Blueprints" in my tutorial on Using mset() and mget().
Again we will be using nested for loops to iterate over the entire map area.
From there we will read each cell and decide what color pixel to draw based on the cell sprite's property.
Here's the basic version:

--first some spite
SPACE=0
WALL=17

MAXX=30
MAXY=17

function minimap_simple()
 for x=0,MAXX-1 do
  for y=0,MAXY-1 do
   if mget(x,y)>=WALL then pix(x,y,3) end
   if mget(x,y)<WALL then pix(x,y,10) end
   if x==p.x and y==p.y then pix(x,y,6) end
  end
 end
end
-- title:  mini-map
-- author: Bear Thorne
-- desc:   a dynamically drawn minimap
-- script: lua

--sprite constants
SPACE=0
WALL=17

TILE_GREEN=1
TILE_BROWN=2
TILE_BLUE =3

WALL_GREEN=17
WALL_BROWN=18
WALL_BLUE =19

DUDE=33

MAXX=30
MAXY=17

U={x= 0,y=-1}
D={x= 0,y= 1}
L={x=-1,y= 0}
R={x= 1,y= 0}

DIRS={U,D,L,R}

MOX=1 --minimap offset x
MOY=15*8-3 --minimap offset y

p={x=1,y=1}

function mini_map()
 for x=-1,30 do
  for y=-1,17 do

   if mget(x,y)==TILE_GREEN then
    pix(x+MOX,y+MOY,11)

   elseif mget(x,y)==TILE_BROWN then
    pix(x+MOX,y+MOY,9)	

   elseif mget(x,y)==TILE_BLUE then
    pix(x+MOX,y+MOY,8)
	
   elseif mget(x,y)==WALL_GREEN then
    pix(x+MOX,y+MOY,5)

   elseif mget(x,y)==WALL_BROWN then
    pix(x+MOX,y+MOY,4)
	
   elseif mget(x,y)==WALL_BLUE then
    pix(x+MOX,y+MOY,2)

   else
    pix(x+MOX,y+MOY,7) --space color
   end

   if x==p.x and y==p.y then
    pix(x+MOX,y+MOY,6) --player color
   end
  end
 end
end

function minimap_simple()
 for x=0,MAXX-1 do
  for y=0,MAXY-1 do
   if mget(x,y)>=WALL then pix(x,y,3) end
   if mget(x,y)<WALL then pix(x,y,10) end
   if x==p.x and y==p.y then pix(x,y,6) end
  end
 end
end

function clear_ahead(dir)
 if mget(p.x+dir.x,p.y+dir.y)<WALL_GREEN then
  return true
 else
  return false
 end
end

function draw()
 cls()
 map(p.x-14,p.y-8,MAXX,MAXY)
 minimap_simple()
 mini_map()
 spr(DUDE,14*8,8*8,0)
end

function TIC()

 if btnp(0,6,10)and clear_ahead(U) then p.y=p.y-1 end
 if btnp(1,6,10)and clear_ahead(D) then p.y=p.y+1 end
 if btnp(2,6,10)and clear_ahead(L) then p.x=p.x-1 end
 if btnp(3,6,10)and clear_ahead(R) then p.x=p.x+1 end

 draw()
end
Clone this wiki locally