Created
October 18, 2016 19:50
-
-
Save Langerz82/337a96d26deddba30bfcc7efe0c9b901 to your computer and use it in GitHub Desktop.
Monogame - TiledSharp - Parsing TMX file with several layers, Scrolling Enabled. - Joshua Langley
This file contains hidden or 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 System; | |
using System.Diagnostics; | |
using System.Xml.Linq; | |
using TiledSharp; | |
namespace GameExample | |
{ | |
/// <summary> | |
/// Copyright Joshua Langley 2016 | |
/// email: [email protected] | |
/// </summary> | |
public class Game2 : Game | |
{ | |
GraphicsDeviceManager graphics; | |
SpriteBatch spriteBatch; | |
TmxMap map; | |
Texture2D tileset; | |
int tileWidth; | |
int tileHeight; | |
int tilesetTilesWide; | |
int tilesetTilesHigh; | |
int tilesWidth = 28; | |
int tilesHeight = 18; | |
int tilesOffsetX = 0; | |
int tilesOffsetY = 0; | |
bool movingDown = false; | |
bool movingUp = false; | |
bool movingLeft = false; | |
bool movingRight = false; | |
public Game2() | |
{ | |
graphics = new GraphicsDeviceManager(this); | |
Content.RootDirectory = "Content"; | |
} | |
/// <summary> | |
/// Allows the game to perform any initialization it needs to before starting to run. | |
/// This is where it can query for any required services and load any non-graphic | |
/// related content. Calling base.Initialize will enumerate through any components | |
/// and initialize them as well. | |
/// </summary> | |
protected override void Initialize() | |
{ | |
// TODO: Add your initialization logic here | |
base.Initialize(); | |
} | |
/// <summary> | |
/// LoadContent will be called once per game and is the place to load | |
/// all of your content. | |
/// </summary> | |
protected override void LoadContent() | |
{ | |
// Create a new SpriteBatch, which can be used to draw textures. | |
spriteBatch = new SpriteBatch(GraphicsDevice); | |
map = new TmxMap("Content/gamemap.tmx"); | |
tileset = Content.Load<Texture2D>(map.Tilesets[0].Name.ToString()); | |
tileWidth = map.Tilesets[0].TileWidth; | |
tileHeight = map.Tilesets[0].TileHeight; | |
tilesetTilesWide = tileset.Width / tileWidth; | |
tilesetTilesHigh = tileset.Height / tileHeight; | |
} | |
/// <summary> | |
/// UnloadContent will be called once per game and is the place to unload | |
/// game-specific content. | |
/// </summary> | |
protected override void UnloadContent() | |
{ | |
// TODO: Unload any non ContentManager content here | |
} | |
/// <summary> | |
/// Allows the game to run logic such as updating the world, | |
/// checking for collisions, gathering input, and playing audio. | |
/// </summary> | |
/// <param name="gameTime">Provides a snapshot of timing values.</param> | |
protected override void Update(GameTime gameTime) | |
{ | |
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) | |
Exit(); | |
if (Keyboard.GetState().IsKeyDown(Keys.W)) | |
movingUp = true; | |
if (Keyboard.GetState().IsKeyUp(Keys.W)) | |
movingUp = false; | |
if (Keyboard.GetState().IsKeyDown(Keys.S)) | |
movingDown = true; | |
if (Keyboard.GetState().IsKeyUp(Keys.S)) | |
movingDown = false; | |
if (Keyboard.GetState().IsKeyDown(Keys.A)) | |
movingLeft = true; | |
if (Keyboard.GetState().IsKeyUp(Keys.A)) | |
movingLeft = false; | |
if (Keyboard.GetState().IsKeyDown(Keys.D)) | |
movingRight = true; | |
if (Keyboard.GetState().IsKeyUp(Keys.D)) | |
movingRight = false; | |
if (movingUp) | |
{ | |
tilesOffsetY--; | |
} | |
if (movingDown) | |
{ | |
tilesOffsetY++; | |
} | |
if (movingLeft) | |
{ | |
tilesOffsetX--; | |
} | |
if (movingRight) | |
{ | |
tilesOffsetX++; | |
} | |
tilesOffsetY = Utils.Clamp(tilesOffsetY, 0, map.Height- tilesHeight); | |
tilesOffsetX = Utils.Clamp(tilesOffsetX, 0, map.Width - tilesWidth); | |
base.Update(gameTime); | |
} | |
/// <summary> | |
/// This is called when the game should draw itself. | |
/// </summary> | |
/// <param name="gameTime">Provides a snapshot of timing values.</param> | |
protected override void Draw(GameTime gameTime) | |
{ | |
GraphicsDevice.Clear(Color.CornflowerBlue); | |
spriteBatch.Begin(); | |
for (var j = 0; j < map.Layers.Count; ++j) | |
{ | |
for (var i = 0; i < tilesHeight; ++i) | |
{ | |
for (var k = 0; k < tilesWidth; ++k) | |
{ | |
int gidIndex =(k % tilesWidth)+tilesOffsetX + ((i+tilesOffsetY)*map.Width); | |
int gid = map.Layers[j].Tiles[gidIndex].Gid; | |
// Empty tile, do nothing | |
if (gid == 0) | |
{ | |
} | |
else | |
{ | |
int tileFrame = gid - 1; | |
int column = tileFrame % tilesetTilesWide; | |
int row = (int)Math.Floor((double)tileFrame / (double)tilesetTilesWide); | |
float x = (k) * map.TileWidth; | |
float y = (i) * map.TileHeight; | |
Rectangle tilesetRec = new Rectangle(tileWidth * column, tileHeight * row, tileWidth, tileHeight); | |
spriteBatch.Draw(tileset, new Rectangle((int)x, (int)y, tileWidth, tileHeight), tilesetRec, Color.White); | |
} | |
} | |
} | |
} | |
spriteBatch.End(); | |
base.Draw(gameTime); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment