-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridRenderSystem.cs
More file actions
69 lines (55 loc) · 2.76 KB
/
GridRenderSystem.cs
File metadata and controls
69 lines (55 loc) · 2.76 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
using Leopotam.EcsLite;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RTS;
public class GridRenderSystem : IEcsRunSystem
{
private readonly SpriteBatch _spriteBatch;
private readonly Texture2D _pixelTexture; // Автоматически генерируемая белая текстура 1x1
private readonly Camera _camera;
public GridRenderSystem(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Camera camera)
{
_spriteBatch = spriteBatch;
_camera = camera;
// Автоматическая генерация _pixelTexture
_pixelTexture = new Texture2D(graphicsDevice, 1, 1);
_pixelTexture.SetData(new Color[] { Color.White });
}
public void Run(IEcsSystems systems)
{
DrawGrid();
}
private void DrawGrid()
{
if (Game1.WorldGrid == null) return;
Color gridColor = Color.Gray * 0.5f;
int lineThickness = MathHelper.Max(1, (int)(1 / _camera.Zoom+1));
Rectangle visibleArea = GetVisibleWorldBounds();
// Вертикальные линии сетки
for (int x = 0; x <= Game1.WorldGrid.Width; x++)
{
float worldX = Game1.XLLimit + x * Game1.WorldGrid.CellSize;
if (worldX < visibleArea.Left - lineThickness || worldX > visibleArea.Right + lineThickness) continue;
Vector2 start = new Vector2(worldX, MathHelper.Max(Game1.YLLimit, visibleArea.Top));
Vector2 end = new Vector2(worldX, MathHelper.Min(Game1.YRLimit, visibleArea.Bottom));
float length = end.Y - start.Y;
_spriteBatch.Draw(_pixelTexture, new Rectangle((int)worldX, (int)start.Y, lineThickness, (int)length), gridColor);
}
// Горизонтальные линии сетки
for (int y = 0; y <= Game1.WorldGrid.Height; y++)
{
float worldY = Game1.YLLimit + y * Game1.WorldGrid.CellSize;
if (worldY < visibleArea.Top - lineThickness || worldY > visibleArea.Bottom + lineThickness) continue;
Vector2 start = new Vector2(MathHelper.Max(Game1.XLLimit, visibleArea.Left), worldY);
Vector2 end = new Vector2(MathHelper.Min(Game1.XRLimit, visibleArea.Right), worldY);
float length = end.X - start.X;
_spriteBatch.Draw(_pixelTexture, new Rectangle((int)start.X, (int)worldY, (int)length, lineThickness), gridColor);
}
}
private Rectangle GetVisibleWorldBounds()
{
Vector2 topLeft = _camera.ScreenToWorld(new Vector2(0, 0));
Vector2 bottomRight = _camera.ScreenToWorld(new Vector2(Game1.Xmap, Game1.Ymap));
return new Rectangle((int)topLeft.X, (int)topLeft.Y, (int)(bottomRight.X - topLeft.X), (int)(bottomRight.Y - topLeft.Y));
}
}