-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame1.cs
More file actions
114 lines (88 loc) · 3.37 KB
/
Copy pathGame1.cs
File metadata and controls
114 lines (88 loc) · 3.37 KB
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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended.Tiled;
using MonoGame.Extended.Tiled.Renderers;
namespace TiledExample
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private TiledMap _tiledMap;
private TiledMapRenderer _tiledMapRenderer;
private SpriteFont _tileTextFont;
private string _tileText;
private Vector2 _tileTextPosition;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
_tileTextFont = Content.Load<SpriteFont>("Fonts/Bold18Font");
_tiledMap = Content.Load<TiledMap>("Map/samplemap");
_tiledMapRenderer = new TiledMapRenderer(GraphicsDevice, _tiledMap);
_tileText = "";
_tileTextPosition = new Vector2(10, 10);
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
_tiledMapRenderer.Update(gameTime);
// show tile at the mouse position
MouseState mouse = Mouse.GetState();
UpdateTileText(mouse.X, mouse.Y);
base.Update(gameTime);
}
private void UpdateTileText(int x, int y)
{
if (ContainsXY(x, y))
{
int tileX, tileY;
GetTileXYAtPoint(x, y, out tileX, out tileY);
var tile = _tiledMap.TileLayers[0].GetTile((ushort)tileX, (ushort)tileY);
_tileText = $"{tileX}, {tileY} TileType: [{GetTileText(_tiledMap, tile.GlobalIdentifier)}]";
}
}
private string GetTileText(TiledMap map, int id)
{
foreach (var tileSet in map.Tilesets)
{
int firstGid = map.GetTilesetFirstGlobalIdentifier(tileSet);
if ((id >= firstGid) && (id < firstGid + tileSet.TileCount))
return $"{tileSet.Name}: {id - firstGid}";
}
return "Unknown";
}
public bool ContainsXY(int x, int y)
{
int tileX = x / 32;
int tileY = y / 32;
return (tileX >= 0) && (tileX < _tiledMap.Width) && (tileY >= 0) && (tileY < _tiledMap.Height);
}
public void GetTileXYAtPoint(int x, int y, out int tileX, out int tileY)
{
tileX = x / 32;
tileY = y / 32;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_tiledMapRenderer.Draw();
_spriteBatch.Begin();
_spriteBatch.DrawString(_tileTextFont, _tileText, _tileTextPosition, Color.Yellow, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}