Loading the tile map
Tile Maps:
What is it?
"Tilemaps are a very popular technique in 2D game development, consisting of building the game world or level map out of small, regular-shaped images called tiles. This results in performance and memory usage gains — big image files containing entire level maps are not needed, as they are constructed by small images or image fragments multiple times."
-From developer.mozilla.org
In This tutorial we will be using the Tiled Map Editor
I will not be showing how to use it or anything there are lots of resources for that.
You can use your own tilemap but just in case you want to use the one I will be using
Here it is
Here it is
Want a video version?
The Text version:
The Code and Explanation:
The Tilemap manager class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Graphics; | |
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using TiledSharp; | |
namespace RPG_1 | |
{ | |
public class TileMapManager | |
{ | |
//All the variables which we will need | |
private SpriteBatch spriteBatch; | |
TmxMap map; | |
Texture2D tileset; | |
int tilesetTilesWide; | |
int tileWidth; | |
int tileHeight; | |
public TileMapManager(SpriteBatch _spriteBatch,TmxMap _map, Texture2D _tileset,int _tilesetTilesWide, int _tileWidth,int _tileHeight) | |
//Initializing our vairiables | |
{ | |
spriteBatch = _spriteBatch; | |
map = _map; | |
tileset = _tileset; | |
tilesetTilesWide = _tilesetTilesWide; | |
tileWidth = _tileWidth; | |
tileHeight = _tileHeight; | |
} | |
public void Draw()//This is where the magic happens :D | |
{ | |
spriteBatch.Begin();//Strating the drawing to the screen | |
for (var i=0;i<map.TileLayers.Count;i++)//This loops through all the tile map layers present on our tile map | |
{ | |
for(var j=0;j<map.TileLayers[i].Tiles.Count;j++)//this loops through the tiles in each tile layer | |
{ | |
int gid = map.TileLayers[i].Tiles[j].Gid;//Getting the GID | |
if(gid==0) | |
{ | |
//If empty then do nothing | |
} | |
else//If not empty | |
{//Some complex math to check for the tile position :( | |
int tileFrame = gid - 1; | |
int column = tileFrame % tilesetTilesWide; | |
int row = (int)Math.Floor((double)tileFrame / (double)tilesetTilesWide); | |
float x = (j % map.Width) * map.TileWidth; | |
float y = (float)Math.Floor(j / (double)map.Width) * map.TileHeight; | |
Rectangle tilesetRec = new Rectangle((tileWidth) * column, (tileHeight) * row, tileWidth, tileHeight);//The origin rectangle | |
spriteBatch.Draw(tileset,new Rectangle((int)x,(int)y,tileWidth,tileHeight),tilesetRec,Color.White);//Drawing the tile | |
} | |
} | |
} | |
spriteBatch.End();//End of the Draw to screen | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Graphics; | |
using Microsoft.Xna.Framework.Input; | |
using MonoGame.Extended.Serialization; | |
using MonoGame.Extended.Sprites; | |
using MonoGame.Extended.Content; | |
using TiledSharp; | |
using System.Collections.Generic; | |
namespace RPG_1 | |
{ | |
public class Game1 : Game | |
{ | |
private GraphicsDeviceManager _graphics; | |
private SpriteBatch _spriteBatch; | |
private Player player; | |
private TmxMap map; | |
private TileMapManager mapManager; | |
public Game1() | |
{ | |
_graphics = new GraphicsDeviceManager(this); | |
Content.RootDirectory = "Content"; | |
IsMouseVisible = true; | |
} | |
protected override void Initialize() | |
{ | |
// TODO: Add your initialization logic here | |
player = new Player(); | |
base.Initialize(); | |
} | |
protected override void LoadContent() | |
{ | |
_spriteBatch = new SpriteBatch(GraphicsDevice); | |
map = new TmxMap("Content/map.tmx"); | |
var tileset = Content.Load<Texture2D>("Tiny Adventure Pack/"+map.Tilesets[0].Name.ToString()); | |
var tileWidth = map.Tilesets[0].TileWidth; | |
var tileHeight = map.Tilesets[0].TileHeight; | |
var TileSetTilesWide = tileset.Width / tileWidth; | |
mapManager = new TileMapManager(_spriteBatch,map,tileset,TileSetTilesWide,tileWidth,tileHeight); | |
SpriteSheet[] sheets = { Content.Load<SpriteSheet>("Tiny Adventure Pack/Character/char_two/Idle/playerSheetIdle.sf",new JsonContentLoader()), | |
Content.Load<SpriteSheet>("Tiny Adventure Pack/Character/char_two/Walk/playerSheetWalk.sf",new JsonContentLoader())}; | |
player.Load(sheets); | |
// TODO: use this.Content to load your game content here | |
} | |
protected override void Update(GameTime gameTime) | |
{ | |
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) | |
Exit(); | |
var initpos = player.pos; | |
player.Update(gameTime); | |
// TODO: Add your update logic here | |
base.Update(gameTime); | |
} | |
protected override void Draw(GameTime gameTime) | |
{ | |
GraphicsDevice.Clear(Color.CornflowerBlue); | |
mapManager.Draw(); | |
player.Draw(_spriteBatch); | |
// TODO: Add your drawing code here | |
base.Draw(gameTime); | |
} | |
} | |
} |
hi, thanks for the content, do you have any idea how to handle the multiple screen resolutions and the size of the sprites according to this, thanks
ReplyDeleteHeyy thanks for replying. :)
DeleteI am actually pretty new to this so no I don't have any idea currently
You should probably ask in the monogame discord. They are a helpful bunch