mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-24 03:56:31 +00:00
64 lines
2.4 KiB
Markdown
64 lines
2.4 KiB
Markdown
<a alight="right" href="https://www.mapeditor.org/"><img align="right" src="https://raw.githubusercontent.com/bjorn/tiled/master/src/tiled/images/about-tiled-logo.png" height="200"></a>
|
|
|
|
# Tiled
|
|
The `MonoGame.Extended.Tiled` library loads and renders maps created with the popular [Tiled Map Editor](http://www.mapeditor.org/).
|
|
|
|
Tiled is an [open sourced](https://github.com/bjorn/tiled) free to use "generic tile map editor". Tiled lets you easily design and view tile maps, and through the `Monogame.Extended.Tiled` package, you can load and display a map generated with Tiled in monogame
|
|
|
|
To load a TiledMap into your project, you first need to add it to your `ContentManager`, and in order to compile the tile map for use in `MonoGame` you must add a reference to the pipeline tool.
|
|
|
|
Instructions to do so can be found [here](http://docs.monogameextended.net/Installation/#installation)
|
|
|
|
# Using the map in your game
|
|
|
|
To create and render a map you will need two new properties
|
|
```c#
|
|
// The tile map
|
|
private TiledMap map;
|
|
// The renderer for the map
|
|
private TiledMapRenderer mapRenderer;
|
|
```
|
|
|
|
In your `Initialize()` method, you can initialize the two methods
|
|
```c#
|
|
protected override void Initialize() {
|
|
base.Initialize();
|
|
|
|
// Load the compiled map
|
|
map = Content.Load<TiledMap>("path/to/your/map/file");
|
|
// Create the map renderer
|
|
mapRenderer = new TiledMapRenderer(GraphicsDevice);
|
|
}
|
|
```
|
|
|
|
To finally render and tick the map, call `mapRenderer.Update();` and `mapRenderer.Draw();` in their respective methods
|
|
```c#
|
|
protected override void Update(GameTime gameTime) {
|
|
// Update the map
|
|
// map Should be the `TiledMap`
|
|
mapRenderer.Update(map, gameTime);
|
|
|
|
base.Update(gameTime);
|
|
}
|
|
|
|
protected override void Draw(GameTime gameTime) {
|
|
// Clear the screen
|
|
GraphicsDevice.Clear(Color.Pink);
|
|
|
|
// Transform matrix is only needed if you have a Camera2D
|
|
// Setting the sampler state to `SamplerState.PointClamp` is reccomended to remove gaps between the tiles when rendering
|
|
spriteBatch.Begin(transformMatrix: camera.GetViewMatrix(), samplerState: SamplerState.PointClamp);
|
|
|
|
// map Should be the `TiledMap`
|
|
// Once again, the transform matrix is only needed if you have a Camera2D
|
|
mapRenderer.Draw(map, camera.GetViewMatrix());
|
|
|
|
// End the sprite batch
|
|
spriteBatch.End();
|
|
|
|
base.Draw(gameTime);
|
|
}
|
|
```
|
|
|
|
Good luck and happy coding! :)
|