Loading tilemaps made with the editor?
by C. N. · in Torque Game Builder · 09/01/2005 (5:02 pm) · 11 replies
Hi,
What is the most basic way to load a tilemap? I'm still learning T2D, so I'm just trying to load a single map into the game. I'm not aiming for scrolling, yet. Just want a single screen full of tiles to load.
I tried doing it the way from the BasicTutorial.pdf like for the spacescroller demo, but it doesn't seem to like the map I made with the tileeditor.
Any suggestions?
Thanks.
What is the most basic way to load a tilemap? I'm still learning T2D, so I'm just trying to load a single map into the game. I'm not aiming for scrolling, yet. Just want a single screen full of tiles to load.
I tried doing it the way from the BasicTutorial.pdf like for the spacescroller demo, but it doesn't seem to like the map I made with the tileeditor.
Any suggestions?
Thanks.
#2
09/01/2005 (5:28 pm)
Hmm, that code above is exactly what I'm doing. I'm not sure why it's not working. I'm just seeing the default Torque2D background when I run the game. I haven't even touched any camera stuff, but shouldn't it load the map at least improperly if my camera isn't set up? So I could at least see a part of it somewhere on the screen?
#3
Um, where do you begin setting up cameras? It seems that the units between actual bitmap sizes and what T2D uses as units are different. I think that was why I wasn't seeing the map at first (just the default T2D backdrop) because my tilemap was outside of my camera view.
Any advice?
09/01/2005 (5:34 pm)
Ah, now I can see the map... Something I'm not sure about: the size of the tiles I'm using are 64x64 pixels, but that doesn't seem to be what the units are in %tileMap.setTileSize() eh?Um, where do you begin setting up cameras? It seems that the units between actual bitmap sizes and what T2D uses as units are different. I think that was why I wasn't seeing the map at first (just the default T2D backdrop) because my tilemap was outside of my camera view.
Any advice?
#4
When I get home, I'll post the code that I'm using to setup my camera and display my tilemap.
09/01/2005 (5:39 pm)
The difference between pixels and world units is a hard thing for me to wrap my mind around. When you are setting the size of the tiles in script, you're doing so in World units. It took me a bit of playing to get mine to look right. It just depends on how your camera is setup. When I get home, I'll post the code that I'm using to setup my camera and display my tilemap.
#5
Yeah, I'm having to play around with the different units, too. I seem to have gotten the single-screen tilemap from the editor to load and fill up one screen in the game. Odd how the units work, but with some tweaking it seems ok.
Safe journey home. :)
09/01/2005 (5:42 pm)
Nice! Thanks for the response.Yeah, I'm having to play around with the different units, too. I seem to have gotten the single-screen tilemap from the editor to load and fill up one screen in the game. Odd how the units work, but with some tweaking it seems ok.
Safe journey home. :)
#6
I have an hour drive home (at over $3.00 a gallon for gas! ouch!), so I should post my code in about 2 hours.
09/01/2005 (5:47 pm)
Thanks, I'm off in 15 minutes, and I can't wait!!! Starting tonight I'm on a 6day vacation. Off until NEXT thursday. I'm hoping to get some work going on my project again.I have an hour drive home (at over $3.00 a gallon for gas! ouch!), so I should post my code in about 2 hours.
#7
09/01/2005 (6:30 pm)
Dang, that's a far commute...
#8
anyways, here is how I did it.
First, here is how I setup my camera
Next, here is the code I used to setup my Tilemap
This is kinda unrelated, but here is the script I used for a scrolling parallax cloud layer
Here's the Datablock I used for the Tilemap
I didn't go through and comment everything... Let me know if I left anything out or if you have any questions.
09/01/2005 (9:00 pm)
Ok, so it was 3 hours... I had supper and watched some TV =)anyways, here is how I did it.
First, here is how I setup my camera
function Setup_Camera()
{
//Set the Camera's Position to the Party's Position
sceneWindow2D.setCurrentCameraPosition($Party.Sprite.GetPosition() SPC "640 480" );
//Get the Bottom Layer of the Current Tilemap
%curlayer = $CurMap.GetTileLayer(0);
//Find Maps Top-Left Position
%mX1 = getWord(%curlayer.getposition(), 0) - (getWord(%curlayer.getsize(), 0)/2);
%mY1 = getWord(%curlayer.getposition(), 1) - (getWord(%curlayer.getsize(), 1)/2);
//Find Maps Bottom-Right Position
%mX2 = getWord(%curlayer.getposition(), 0) + (getWord(%curlayer.getsize(), 0) / 2);
%mY2 = getWord(%curlayer.getposition(), 1) + (getWord(%curlayer.getsize(), 1) / 2);
//Set the View Limit to the map's dimensions
echo("View Limit set to " SPC %mX1 SPC %mY1 SPC %mX2 SPC %mY2);
SceneWindow2D.SetViewLimitOn(%mX1 SPC %mY1 SPC %mX2 SPC %mY2);
//Mount the Camera to follow the player
SceneWindow2D.mount($Party.Sprite, "0 0", 2);
//Set the World limit to the limits of the map. Clamp keeps the camera from scrolling past that point.
$Party.Sprite.SetWorldLimit(clamp, %mX1 SPC %mY1 SPC %mX2 SPC %mY2);
}Next, here is the code I used to setup my Tilemap
function SetupTileMap()
{
// Nama_World_Setup
$Nama_World_Map = new fxTileMap2D() { scenegraph = t2dSceneGraph; };
$Nama_World_Map.LoadTileMap("~/client/maps/NamaWorld.map");
$Nama_World_L0 = $Nama_World_Map.getTileLayer( 0 );
$Nama_World_L1 = $Nama_World_Map.getTileLayer( 1 );
$Nama_World_L0.SetPosition("0 0");
$Nama_World_L1.SetPosition("0 0");
$Nama_World_L1.SetLayer("4");
$Nama_World_L0.SetCollisionMaterial( immovableMaterial );
$Nama_World_L0.SetCollisionActive(false, true);
$Nama_World_L0.SetCollisionCallback(true);
$Nama_World_L0.SetGroup($Collision::MapGroup);
$Nama_World_L0.SetLayer($Collision::MapLayer);
$Nama_World_L0.SetCollisionMasks($Collision::PLayerGroupMask, $Collision::PlayerLayerMask);
$CurMap = $Nama_World_Map;
//(Here is what the $Collision namespace variables are set to)
$collision::MapGroupMask = BIT(10);
$collision::MapLayerMask = BIT(10);
$collision::MapGroup = 10;
$collision::MapLayer = 10;
$collision::PlayerGroupMask = BIT(5);
$collision::PlayerLayerMask = BIT(5);
$collision::PlayerGroup = 5;
$collision::PlayerLayer = 5;This is kinda unrelated, but here is the script I used for a scrolling parallax cloud layer
function SetupScrollLayers()
{
//Parralax scrolling layers are setup here
//Test cloud layer
$cloudlayer = new fxScroller2D() {scenegraph = t2dSceneGraph; };
$cloudlayer.setImageMap(WS_Cloud1);
$cloudlayer.setScroll("7 10");
$cloudlayer.setRepeat (getWord($Nama_World_L0.GetSize(),0)/1500 SPC getWord($Nama_World_L0.GetSize(),1)/1500);
$cloudlayer.setBlending(true);
$cloudlayer.setBlendcolour("255 255 255 40");
$cloudlayer.setPosition($Nama_World_L0.GetPosition());
$cloudlayer.setSize($Nama_World_L0.GetSize());
}Here's the Datablock I used for the Tilemap
datablock fxImageMapDatablock2D(WS_Tile1)
{
mode = cell;
cellWidth = 48;
cellHeight = 48;
textureName = "~/client/images/outdoortiles";
};I didn't go through and comment everything... Let me know if I left anything out or if you have any questions.
#9
09/01/2005 (9:03 pm)
Also, with the Camera Mounted as above, I actually gave the camera a small force and it moves like it's being pulled by the player. When the player stops, it takes a few moments to catch up. At least that's how it works in my top-down RPG. It depends completely on what you are doing =)
#10
Thanks. I'll have some questions for ya soon, hehe. Long day at work tomorrow...
09/01/2005 (11:03 pm)
Nice, that's a bit to take in. Should give me some ideas, indeed...Thanks. I'll have some questions for ya soon, hehe. Long day at work tomorrow...
#11
The reason I do it that way, is that when you call getPosition() on a tilemap, it returns the maps CENTER position. It gives you the X and Y coords of the very CENTER of the map. Not the top-left corner.
So, to find the extent of the map so I can set limits up, I take the center position and subtract 1/2 the maps width and 1/2 the maps height to get the x/y coord of the top-left. I then do the opposite to get the bottom-right coord.
I can then use those coords to set the World Limit for my game, so the map never scrolls out of the window.
the SceneWindow2D.SetViewLimit() call tells the program the limits to set on the camera.
Party.Sprite.SetWorldLimit() call tells the program the limits to set on the main character.
If I just used SetViewLimit then the camera would stop scrolling at the edge of the map, but the player could keep walking right off the edge.
If I just used the last one, then the camera would move to keep the player at the center, and would reveal a black void at the edges of the screen when I get near the edge or a corner of the map.
I may just be confusing everyone more, but hopefully that helps. I wrote this script 3 months ago, so I'm writing this to refresh myself as much as show you how I did it =)
09/01/2005 (11:20 pm)
About those locations where I get the map's top-left and bottom-right coords... Those are probably the most confusing part, at least I would think so.The reason I do it that way, is that when you call getPosition() on a tilemap, it returns the maps CENTER position. It gives you the X and Y coords of the very CENTER of the map. Not the top-left corner.
So, to find the extent of the map so I can set limits up, I take the center position and subtract 1/2 the maps width and 1/2 the maps height to get the x/y coord of the top-left. I then do the opposite to get the bottom-right coord.
I can then use those coords to set the World Limit for my game, so the map never scrolls out of the window.
the SceneWindow2D.SetViewLimit() call tells the program the limits to set on the camera.
Party.Sprite.SetWorldLimit() call tells the program the limits to set on the main character.
If I just used SetViewLimit then the camera would stop scrolling at the edge of the map, but the player could keep walking right off the edge.
If I just used the last one, then the camera would move to keep the player at the center, and would reveal a black void at the edges of the screen when I get near the edge or a corner of the map.
I may just be confusing everyone more, but hopefully that helps. I wrote this script 3 months ago, so I'm writing this to refresh myself as much as show you how I did it =)
Torque Owner Chris "Hyena" Vogel
your loading code would look something like this...
%tilemap = new fxTileMap2D() { scenegraph = t2dSceneGraph; }; %tilemap.loadTileMap("~/client/maps/tilemap.map");After the tilemap is loaded, you have to reference and use the LAYERS of that tilemap.
You can then use the %GroundLayer variable and set it's position, etc. It should show in your game when you run it, if you set it's position right and set your camera up right....
Post here if you're still having problems. I don't have my code with me (I'm at work) but I can look how I did mine and post it later.