Gw Temp

Menu

Tutorial - 'Minimap in GM6' by Guest

An item about Gamemaker posted on

Blurb

A neat code/tutorial detailing the code necessary for creating a mini-map in Gamemaker 6

Body

Hi. Let us look through a simple minimap script

Here is the code:

-----------------------------------------------------------
// script written by Dimitry "Ripp3r" Solovyov
// for Game Maker 6.x

#define draw_minimap
// argument0 - map size to screen size ratio (float from 0 to 1)
// argument1 - view (if views are not used, specify -1)
// argument2 - display map (0 - in range, 1 - full)
// argument3 - map x
// argument4 - map y
// argument5 - map background color
// argument6 - map alpha transparency (float from 0 to 1)
{
draw_set_color(argument5);
draw_set_alpha(argument6);
if (argument1 >= 0) {
shift_left = argument3+view_xview[argument1];
shift_top = argument4+view_yview[argument1];
} else {
shift_left = argument3;
shift_top = argument4;
}
if (argument2 < 1) {
shift_width = view_wview[argument1];
shift_height = view_hview[argument1];
} else {
shift_width = room_width;
shift_height = room_height;
}
draw_rectangle(shift_left, shift_top, shift_left+(shift_width*argument0), shift_top+(shift_height*argument0), false);
}

#define draw_minimap_object
// argument0 - map size to screen size ratio (float from 0 to 1)
// argument1 - view (if views are not used, specify -1)
// argument2 - display map (0 - in range, 1 - full)
// argument3 - map x
// argument4 - map y
// argument5 - object index
// argument6 - object color
// argument7 - object size (in pixels)
// argument8 - object alpha transparency (float from 0 to 1)
// argument9 - draw sprites (true or false)
// argument10 - sprite to use (if argument9 is set to true)
// argument11 - sprite frame (to draw animated, specify -1)
{
draw_set_color(argument6);
draw_set_alpha(argument8);
if (argument1 >= 0) {
shift_left = argument3+view_xview[argument1];
shift_top = argument4+view_yview[argument1];
} else {
shift_left = argument3;
shift_top = argument4;
}
if (argument2 < 1) {
shift_width = view_wview[argument1];
shift_height = view_hview[argument1];
} else {
shift_width = room_width;
shift_height = room_height;
}
var i;
for (i=0; i if (argument2 < 1) {
obj_x = instance_find(argument5, i).x-shift_left+argument3;
obj_y = instance_find(argument5, i).y-shift_top+argument4;
} else {
obj_x = instance_find(argument5, i).x;
obj_y = instance_find(argument5, i).y;
}
if ((obj_x>=0) and (obj_y>=0) and (obj_x<(shift_width-(argument7/argument0))) and (obj_y<(shift_height-(argument7/argument0)))) {
if (argument9) {
draw_sprite_stretched_ext(argument10, argument11, shift_left+(obj_x*argument0), shift_top+(obj_y*argument0), argument7, argument7, c_white, argument8);
} else {
draw_rectangle(shift_left+(obj_x*argument0), shift_top+(obj_y*argument0), shift_left+(obj_x*argument0)+argument7, shift_top+(obj_y*argument0)+argument7, false);
}
}
}
}
-----------------------------------------------------------

This script has alot of arguments, so it can be nicely configured to your own needs. All possible arguments are explained in comments.

Call these two scripts from the draw event of an object.

Example:
{
draw_minimap(0.10, 0, 0, 10, 10, c_green, 0.25);
draw_minimap_object(0.25, 0, 0, 10, 10, obj_player, c_yellow, 3, 0.5, false, 0, 0);
draw_minimap_object(0.25, 0, 0, 10, 10, obj_enemy, c_red, 2, 0.5, false, 0, 0);
draw_minimap_object(0.25, 0, 0, 10, 10, obj_wall, c_red, 2, 0.5, false, 0, 0);
}
This code will draw a 25% transparent minimap at position (10, 10) and the size will be 10% of the screen (ten times smaller). And the minimap will display all objects of kind "obj_player", "obj_enemy" and "obj_wall" as rectangles (not using object sprites). Player's position on screen will be in yellow, all enemies positions in red. Walls will be black.

break(); ^_^