how to create a minimap almost no reduction to frame rate project uploaded for dl
by michael bailey · in Torque Game Builder · 04/06/2010 (9:43 am) · 4 replies
http://www.mediafire.com/file/qwrm2ovew0n/miniMap2.rar first download the project
im am going to explain this very simple minimap creation. everyone wants it no one shares it. in the code i comment how to add enemies to the minimap as well i dont have time to do this in this example. all i ask is if you use this code and implement changes post them SHARE create a real mini map resource for everyone to use.
the first step after creating your level is to remove all enemys and player place them to the side. play and make the camera encompase every thing. screen shot your level. edit it in gimp or photo shop even tracing wall and building outlines in white with black bg or however you want it to look or use how is.
import this map to you main level as an image map. place it on the top most layer resize it to match your level and encompass the whole thing make sure to record the x y width and hieght dont for get this! the process of doing this is the mini map will be rendering 1 png file not several from your seen.
second create a blank level nothing in it just save it.
go back to y our level and create your hud. MAKE sure you add a scene object named scenewindow2d or your level will not even load or render!.
here is my gui code.
game.cs
minmap.cs
entireMapImageMap is what we created in the first part of this tutorial.
.mount mounts the camera to the object
i feel things are well commented if you have questions ask me
the beauty of doing it this way is you are rendering 1 obj i made a 1x1 green block for the player in the minimap playerminimapimagemap
you could also set this to a global in a global.cs file.
run my project inspect the scripting properties for the objects in test.t2d u see i used the downloadable shootercontrols just to make this something everyone has access to. inspect mainscreengui look at all the names that i put it it is very important.
this is a very versitile system setup to add as much as you can imagine.
so lets keep this going adding enemies explaing simsets etc! making up dates this is what everyoen is crying for for tgb dont just dl and not share if you base totally off my code.
imagination 7
michael bailey
im am going to explain this very simple minimap creation. everyone wants it no one shares it. in the code i comment how to add enemies to the minimap as well i dont have time to do this in this example. all i ask is if you use this code and implement changes post them SHARE create a real mini map resource for everyone to use.
the first step after creating your level is to remove all enemys and player place them to the side. play and make the camera encompase every thing. screen shot your level. edit it in gimp or photo shop even tracing wall and building outlines in white with black bg or however you want it to look or use how is.
import this map to you main level as an image map. place it on the top most layer resize it to match your level and encompass the whole thing make sure to record the x y width and hieght dont for get this! the process of doing this is the mini map will be rendering 1 png file not several from your seen.
second create a blank level nothing in it just save it.
go back to y our level and create your hud. MAKE sure you add a scene object named scenewindow2d or your level will not even load or render!.
here is my gui code.
//--- OBJECT WRITE BEGIN ---
%guiContent = new GuiChunkedBitmapCtrl(mainScreenGui) {
canSaveDynamicFields = "0";
isContainer = "1";
Profile = "GuiDefaultProfile";
HorizSizing = "right";
VertSizing = "bottom";
Position = "0 0";
Extent = "1024 768";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
hovertime = "1000";
useVariable = "0";
tile = "0";
new t2dSceneWindow(sceneWindow2D) {
canSaveDynamicFields = "0";
isContainer = "1";
Profile = "GuiDefaultProfile";
HorizSizing = "right";
VertSizing = "bottom";
Position = "0 0";
Extent = "1024 768";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
hovertime = "1000";
lockMouse = "0";
useWindowMouseEvents = "1";
useObjectMouseEvents = "1";
new GuiBackgroundCtrl(minmapGUI) {
canSaveDynamicFields = "0";
isContainer = "1";
Profile = "GuiDefaultProfile";
HorizSizing = "right";
VertSizing = "bottom";
Position = "22 526";
Extent = "159 159";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
hovertime = "1000";
new GuiBitmapCtrl(miniMapBg) {
canSaveDynamicFields = "0";
isContainer = "0";
Profile = "GuiDefaultProfile";
HorizSizing = "right";
VertSizing = "bottom";
Position = "0 1";
Extent = "159 159";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
hovertime = "1000";
bitmap = "~/data/images/minimapbg.png";
wrap = "0";
};
new t2dSceneWindow(minimap) {
canSaveDynamicFields = "0";
isContainer = "0";
Profile = "GuiDefaultProfile";
HorizSizing = "right";
VertSizing = "bottom";
Position = "7 11";
Extent = "137 129";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
hovertime = "1000";
lockMouse = "0";
useWindowMouseEvents = "1";
useObjectMouseEvents = "0";
};
};
};
};
//--- OBJECT WRITE END ---it is very important to name your objects for scripting so do so as i have! game.cs
$minmapscene = 1; //just initializing a global
function startGame(%level)
{
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
exec("./minmap.cs");
exec("./cPlayer.cs");
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
sceneWindow2D.loadLevel(%level);
//load a blank level , just create one with nothing in it at all
minimap.loadLevel(expandFilename("~/data/levels/blank.t2d"));
minimap.setRenderLayers("29 30"); // we only render 2 layers!
$minmapscene = minimap.getSceneGraph(); // hold your scene graph
start_minimap(); // call our startminimap in minmap.cs
}
//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
moveMap.delete();
}cPlayer.csfunction cPlayer::onLevelLoaded(%this){
echo("on level loaded");
%s = player.getPosition();
$playerposx = getWord(%s,0); //set global player position
$playerposy = getWord(%s,1);
sceneWindow2D.mount(player);
}make your player named player or change the line %s = player.getPosition(); accordinglyminmap.cs
//------------------------------
// michael bailey 2010 (c)
//------------------------------
$minmapobj = new SimSet();
$min_bg=30;
$min_player_layer = 29;
$playerposx = 0;
$playerposy = 0;
$playerSprite = 0;
function add_sprite(%imagemap, %layer,%w, %h, %x, %y,%tag){
%sprite = new t2dStaticSprite() { scenegraph = $minmapscene; };
%sprite.setImageMap(%imagemap);
echo("in add sprite");
%sprite.setSize(%w, %h);
%sprite.setPosition(%x, %y);
%sprite.setLayer(%layer);
%sprite.setGraphGroup(0);
%sprite.tag = %tag;
%sprite.remove = 0; // this is setup so that if you do add enemies on their death set to one
$minmapobj.add( %sprite );
return %sprite;
}
function start_minimap(){
echo("in onlevelloaded");
%s= add_sprite("entireMapImageMap",30,106.340,96.150,-0.267,-10.580,"bg");
$playerSprite = add_sprite("playerMiniMapImageMap",29,5,5,$playerposx,$playerposy,"player");
schedule(50,0, update_map);
minimap.mount($playerSprite);
}
function update_map(){
%s = player.getPosition();
$playerposx = getWord(%s,0);
$playerposy = getWord(%s,1);
$playerSprite.setPosition($playerposx, $playerposy);//after updating the player objects you
//can add a loop throught the simset removing the sprites that have .remove=1 and updating the others.
// you need to add the enemies to another simset in your main window with a tag (id) that is how
//you can match and update positions
schedule(50, 0,update_map);
}as you see the player name for scripting is very important you could apply this to a global to make it more reusable.entireMapImageMap is what we created in the first part of this tutorial.
.mount mounts the camera to the object
i feel things are well commented if you have questions ask me
the beauty of doing it this way is you are rendering 1 obj i made a 1x1 green block for the player in the minimap playerminimapimagemap
you could also set this to a global in a global.cs file.
run my project inspect the scripting properties for the objects in test.t2d u see i used the downloadable shootercontrols just to make this something everyone has access to. inspect mainscreengui look at all the names that i put it it is very important.
this is a very versitile system setup to add as much as you can imagine.
so lets keep this going adding enemies explaing simsets etc! making up dates this is what everyoen is crying for for tgb dont just dl and not share if you base totally off my code.
imagination 7
michael bailey
About the author
#2
04/07/2010 (8:51 am)
Well done Michael! You should post this as resource.
#3
04/11/2011 (8:50 am)
This is a very great resource Mike, I can't remember the countless times people asked for this functionality. You code is quite simple and clean as well so newer users should be able to grasp what's going on. A while ago the guy from I think "rake in the grass games, posted a link to there source code for a game called larva mortis and if people had put in the work there was mini map code in there as well. Any contribution is a great contribution but if you can get this in the TDN with some pictures it would be much better received. I look forward to more of your posts. BTW I know this isn't the post but there is a post that walks you through implementing Box 2d into TGB pro and that process should help you figure out how to add other functionality to the engine.
#4
I'll have to give it a go even though I'm not working on something that would need a minimap.
Just a quick though about your 1x1 green player. If you make that image white, you could then use color blending to make multiple different points on the minimap, like a yellow block for gold or something.
I'll see if I can whip something up!
04/11/2011 (2:52 pm)
Hey Mike this looks awesome.I'll have to give it a go even though I'm not working on something that would need a minimap.
Just a quick though about your 1x1 green player. If you make that image white, you could then use color blending to make multiple different points on the minimap, like a yellow block for gold or something.
I'll see if I can whip something up!
Torque Owner michael bailey