-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectInitSystem.cs
More file actions
89 lines (72 loc) · 3.46 KB
/
ObjectInitSystem.cs
File metadata and controls
89 lines (72 loc) · 3.46 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
using Leopotam.EcsLite;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
public class ObjectInitSystem : IEcsInitSystem
{
private EcsWorld _world;
private SpriteBatch _spriteBatch;
private GraphicsDevice _graphicsDevice;
private int _width = 50;
private int _height = 50;
public ObjectInitSystem(EcsWorld world, GraphicsDevice graphicsDevice, SpriteBatch spriteBatch)
{
_world = world;
_graphicsDevice = graphicsDevice;
_spriteBatch = spriteBatch;
}
public void Init(IEcsSystems systems)
{
for (int i = 0; i < 550; i++)
{
// Создаём сущность для объекта
int entity = _world.NewEntity();
// Добавляем компоненты
ref GameObject gameObject = ref _world.GetPool<GameObject>().Add(entity);
// Получаем центр экрана
Vector2 screenCenter = new Vector2(
_graphicsDevice.Viewport.Width / 2f, // X-центр
_graphicsDevice.Viewport.Height / 2f // Y-центр
);
// Устанавливаем позицию так, чтобы центр объекта (bounds) был в центре экрана
// Поскольку bounds 50x50, вычитаем половину размера (25x25) от screenCenter
gameObject.Position = new Vector2(-100, 0) - new Vector2(_width/2, _height/2);
gameObject.Texture = GenerateSquareHollowTexture();
gameObject.Scale = new Vector2(1, 1);
// Bounds: верхний левый угол теперь в скорректированной позиции, так что центр bounds совпадает с screenCenter
ref Bounds bounds = ref _world.GetPool<Bounds>().Add(entity);
bounds.Width = _width;
bounds.Height = _height;
bounds.Rectangle = new Rectangle((int)gameObject.Position.X - _width/2, (int)gameObject.Position.Y - _height/2, _width, _height);
ref Movement movement = ref _world.GetPool<Movement>().Add(entity);
movement.Speed = 100;
_world.GetPool<Clickable>().Add(entity); // Объект кликабельный
_world.GetPool<ColorComponent>().Add(entity).Value = Color.White; // Начальный цвет
ref PathComponent path = ref _world.GetPool<PathComponent>().Add(entity);
}
}
private Texture2D GenerateSquareHollowTexture()
{
Texture2D texture = new Texture2D(_graphicsDevice, _width, _height);
Color[] data = new Color[_width * _height];
int borderThickness = 3;
for (int y = 0; y < _height; y++)
{
for (int x = 0; x < _width; x++)
{
int index = y * _width + x;
// Проверяем, находится ли пиксель в пределах границы заданной толщины
if (x < borderThickness || x >= _width - borderThickness ||
y < borderThickness || y >= _height - borderThickness)
{
data[index] = Color.White; // Граница белая
}
else
{
data[index] = Color.Transparent; // Центр прозрачный
}
}
}
texture.SetData(data);
return texture;
}
}