Jump to content
  • Advertisement
Sign in to follow this  

Tile-based game help

This topic is 2886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone. I've been learning XNA GS for awhile. I know C# pretty well and some C/C++. (and I used to use Blitz3D). I am totally familiar with 2D stuff in XNA, and have created a few classes for 2D sprites, animated 2D sprites, and for a few other things.
I want to eventually create a small 2D RTS.
For starters, I just want to get be able to scroll around a tile map and get a few sprites moving around.
Now, I know how to load a tile map, but don't really know how to handle scrolling. I don't think I will be doing any zooming right now.
I assume you will have world and camera/view coordinates, and that stuff outside of the viewport shouldn't be drawn.
So, basically I need help figuring out how to move around the map.
Also, what would be the best way to store the location of all of the tiles? Probably in classes?, because I need to at least store if the tile is walkable or not (since I plan to use A* pathfinding later on) and the coordinates.

Also, after I get the map working, I'd need to know how to translate screen/viewport-coordinates into world coordinates (for when something is clicked on)

Thanks for the help in advance!

Share this post

Link to post
Share on other sites
What I would do is add a "Camera" object and use it to determine what tiles / sprites are within the Viewable region. I have actually just done something like this for a project I am working on right now. Simply created a Camera object that held properties for Focus, Top, Left, Right and bottom. The Focus is a Vector2 for the X / Y "Center" of the screen. Top, Left, Right and Bottom simply calculate and return the borders of the screen based on the Focus point.

In my project I have everything converted to world units, but here's an example. My game shows 100X100 "world units" on screen at any time. I have a level that is 2000x2000 world units. The 0,0 position is the top left of my screen right now, poor design but simple to understand. So if you set the camera's focus point to say 100 x 100, it would calculate and return 50 for top and left and 150 for right and bottom. Now that I have those borders, I simply iterate my tile / sprite lists. I will draw only the ones that exist within those bounds. Each of the tiles / sprites have their own "World Unit" positions (can be pixel position too). So I just subtract the Camera.Top from Sprite.Y (On screen position). Subtract Camera.Left from Sprite.X (On screen position).

So long story shot, use a "Camera" object to store the screens borders. Use the screens borders to determine what to draw. If your tiles / sprites positions use the same numbering system as the camera measurements simply subtract the Camera's Top and Left to give you on screen positions. If you have any more questions or would like me to whip you up a quick example just shoot me an email.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!