-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.cs
200 lines (156 loc) · 7.71 KB
/
Game1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using TiledCS; // dotnet add package tiledcs
namespace TiledCS_example_MonoGame
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private TiledMap map;
private Dictionary<int, TiledTileset> tilesets;
private Texture2D tilesetTexture;
private TiledLayer collisionLayer;
private Rectangle? debugRect;
const int scaleFactor = 3;
private Matrix transformMatrix;
[Flags]
enum Trans
{
None = 0,
Flip_H = 1 << 0,
Flip_V = 1 << 1,
Flip_D = 1 << 2,
Rotate_90 = Flip_D | Flip_H,
Rotate_180 = Flip_H | Flip_V,
Rotate_270 = Flip_V | Flip_D,
Rotate_90AndFlip_H = Flip_H | Flip_V | Flip_D,
}
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
transformMatrix = Matrix.CreateScale(scaleFactor, scaleFactor, 1f);
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
// Set the "Copy to Output Directory" property of these two files to `Copy if newer`
// by clicking them in the solution explorer.
map = new TiledMap(Content.RootDirectory + "\\cavernasMap.tmx");
tilesets = map.GetTiledTilesets(Content.RootDirectory + "/"); // DO NOT forget the / at the end
// Load "exampleTileset.xnb" file, which is the result of building
// the image file with "Content.mgcb"
tilesetTexture = Content.Load<Texture2D>("cavesofgallet_tiles");
// Retrieving objects or layers can be done using Linq or a for loop
collisionLayer = map.Layers.First(l => l.name == "Ground");
// 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();
// Get mouse position on screen
var mousePos = Mouse.GetState().Position.ToVector2();
// Check if mouse is in the bounds of a Tiled object
debugRect = null;
foreach (var obj in collisionLayer.objects)
{
var objRect = new Rectangle((int)obj.x, (int)obj.y, (int)obj.width, (int)obj.height);
if (objRect.Contains(mousePos / scaleFactor))
{
debugRect = objRect;
}
}
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: transformMatrix); // Set samplerState to null to work with high res assets
var tileLayers = map.Layers.Where(x => x.type == TiledLayerType.TileLayer);
foreach (var layer in tileLayers)
{
for (var y = 0; y < layer.height; y++)
{
for (var x = 0; x < layer.width; x++)
{
var index = (y * layer.width) + x; // Assuming the default render order is used which is from right to bottom
var gid = layer.data[index]; // The tileset tile index
var tileX = x * map.TileWidth;
var tileY = y * map.TileHeight;
// Gid 0 is used to tell there is no tile set
if (gid == 0)
{
continue;
}
// Helper method to fetch the right TieldMapTileset instance
// This is a connection object Tiled uses for linking the correct tileset to the gid value using the firstgid property
var mapTileset = map.GetTiledMapTileset(gid);
// Retrieve the actual tileset based on the firstgid property of the connection object we retrieved just now
var tileset = tilesets[mapTileset.firstgid];
// Use the connection object as well as the tileset to figure out the source rectangle
var rect = map.GetSourceRect(mapTileset, tileset, gid);
// Create destination and source rectangles
var source = new Rectangle(rect.x, rect.y, rect.width, rect.height);
var destination = new Rectangle(tileX, tileY, map.TileWidth, map.TileHeight);
// You can use the helper methods to get information to handle flips and rotations
Trans tileTrans = Trans.None;
if (map.IsTileFlippedHorizontal(layer, x, y)) tileTrans |= Trans.Flip_H;
if (map.IsTileFlippedVertical(layer, x, y)) tileTrans |= Trans.Flip_V;
if (map.IsTileFlippedDiagonal(layer, x, y)) tileTrans |= Trans.Flip_D;
SpriteEffects effects = SpriteEffects.None;
double rotation = 0f;
switch (tileTrans)
{
case Trans.Flip_H: effects = SpriteEffects.FlipHorizontally; break;
case Trans.Flip_V: effects = SpriteEffects.FlipVertically; break;
case Trans.Rotate_90:
rotation = Math.PI * .5f;
destination.X += map.TileWidth;
break;
case Trans.Rotate_180:
rotation = Math.PI;
destination.X += map.TileWidth;
destination.Y += map.TileHeight;
break;
case Trans.Rotate_270:
rotation = Math.PI * 3 / 2;
destination.Y += map.TileHeight;
break;
case Trans.Rotate_90AndFlip_H:
effects = SpriteEffects.FlipHorizontally;
rotation = Math.PI * .5f;
destination.X += map.TileWidth;
break;
default:
break;
}
// Render sprite at position tileX, tileY using the rect
_spriteBatch.Draw(tilesetTexture, destination, source, Color.White, (float)rotation, Vector2.Zero, effects, 0);
}
}
}
// If mouse is over a collider, display its bounds
if (debugRect != null)
{
Texture2D _texture = new Texture2D(GraphicsDevice, 1, 1);
_texture.SetData(new Color[] { Color.Green });
_spriteBatch.Draw(_texture, (Rectangle)debugRect, Color.White);
}
_spriteBatch.End();
base.Draw(gameTime);
}
}
}