/
BasicTilemapSystem.cs
143 lines (123 loc) · 3.93 KB
/
BasicTilemapSystem.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
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Monofoxe.Engine.Drawing;
using Monofoxe.Engine.ECS;
namespace Monofoxe.Engine.Utils.Tilemaps
{
/// <summary>
/// System for basic tilemap. Based on Monofoxe.ECS.
/// Draws tilemaps in camera's bounds.
/// </summary>
public class BasicTilemapSystem : BaseSystem
{
public override Type ComponentType => typeof(BasicTilemapComponent);
public override void Draw(Component component)
{
var tilemap = (BasicTilemapComponent)component;
var offsetCameraPos = GraphicsMgr.CurrentCamera.Position
- tilemap.Offset
- GraphicsMgr.CurrentCamera.Offset / GraphicsMgr.CurrentCamera.Zoom;
var scaledCameraSize = GraphicsMgr.CurrentCamera.Size / GraphicsMgr.CurrentCamera.Zoom;
var startX = (int)(offsetCameraPos.X / tilemap.TileWidth) - tilemap.Padding;
var startY = (int)(offsetCameraPos.Y / tilemap.TileHeight) - tilemap.Padding;
var endX = startX + (int)scaledCameraSize.X / tilemap.TileWidth + tilemap.Padding + 2; // One for mama, one for papa.
var endY = startY + (int)scaledCameraSize.Y / tilemap.TileHeight + tilemap.Padding + 2;
// It's faster to determine bounds for whole region.
// Bounding.
if (startX < 0)
{
startX = 0;
}
if (startY < 0)
{
startY = 0;
}
if (endX > tilemap.Width)
{
endX = tilemap.Width;
}
if (endY > tilemap.Height)
{
endY = tilemap.Height;
}
// Bounding.
// Telling whatever is waiting to be drawn to draw itself.
// If graphics mode is not switched, drawing raw sprite batch may interfere with primitives.
GraphicsMgr.SwitchGraphicsMode(GraphicsMode.Sprites);
for(var y = startY; y < endY; y += 1)
{
for(var x = startX; x < endX; x += 1)
{
// It's fine to use unsafe get, since we know for sure, we are in bounds.
var tile = tilemap.GetTileUnsafe(x, y);
if (!tile.IsBlank)
{
var tilesetTile = tile.GetTilesetTile();
if (tilesetTile != null)
{
var flip = SpriteEffects.None;
var offset = Vector2.UnitY * (tilesetTile.Frame.Height - tilemap.TileHeight);
var rotation = 0;
// A bunch of Tiled magic.
/*
* Ok, so here's the deal.
* Monogame, understandibly, has no diagonal flip,
* so it's implemented by offsetting, rotating, and horizontal flipping.
* Also, order actually matters. Diagonal flip should always go first.
*
* Yes, this can be implemented with primitives.
* If you've got nothing better to do -- go bananas.
*
* I'm really sorry, if you'll need to modify this.
*/
if (tile.FlipDiag)
{
rotation = -90;
offset.Y = -tilemap.TileHeight;
offset.X = 0;
flip |= SpriteEffects.FlipHorizontally;
}
if (tile.FlipHor)
{
// Sprite is rotated by 90 degrees, so X axis becomes Y.
if (tile.FlipDiag)
{
flip ^= SpriteEffects.FlipVertically;
}
else
{
flip ^= SpriteEffects.FlipHorizontally;
}
}
if (tile.FlipVer)
{
if (tile.FlipDiag)
{
flip ^= SpriteEffects.FlipHorizontally;
}
else
{
flip ^= SpriteEffects.FlipVertically;
}
}
// A bunch of Tiled magic.
// Mass-drawing srpites with spritebatch is a bit faster.
GraphicsMgr._batch.Draw(
tilesetTile.Frame.Texture,
tilemap.Offset + new Vector2(tilemap.TileWidth * x, tilemap.TileHeight * y) - offset + tile.Tileset.Offset,
tilesetTile.Frame.TexturePosition,
Color.White,
MathHelper.ToRadians(rotation),
Vector2.Zero,
Vector2.One,
flip,
0
);
}
}
}
}
}
}
}