-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionInputSystem.cs
More file actions
68 lines (61 loc) · 3.21 KB
/
SelectionInputSystem.cs
File metadata and controls
68 lines (61 loc) · 3.21 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
using System;
using Leopotam.EcsLite;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using RTS;
public class SelectionInputSystem : IEcsRunSystem
{
private EcsWorld _world;
private EcsPool<SelectionBox> _selectionPool;
private int _selectionEntity = -1; // ID сущности с selection box (если активна)
public Action startSelect;
public Action endSelect;
private Camera _camera;
public SelectionInputSystem(EcsWorld world, Camera camera)
{
_world = world;
_camera = camera;
_selectionPool = _world.GetPool<SelectionBox>();
}
public void Run(IEcsSystems systems)
{
Vector2 mousePosition = Game1.MouseGlobalPosition;
// Если левая кнопка только что нажата
if (Game1.CurrentMouseState.LeftButton == ButtonState.Pressed && Game1.PreviousMouseState.LeftButton == ButtonState.Released)
{
// Создаём новую сущность для selection box
_selectionEntity = _world.NewEntity();
ref SelectionBox selection = ref _selectionPool.Add(_selectionEntity);
selection.StartPosition = mousePosition;
selection.CurrentPosition = mousePosition;
selection.IsActive = true;
selection.Bounds = new Rectangle((int)mousePosition.X, (int)mousePosition.Y, 0, 0);
startSelect?.Invoke();
}
// Если левая кнопка зажата и selection активна
else if (Game1.CurrentMouseState.LeftButton == ButtonState.Pressed && _selectionEntity != -1 && _selectionPool.Has(_selectionEntity))
{
ref SelectionBox selection = ref _selectionPool.Get(_selectionEntity);
selection.CurrentPosition = mousePosition;
// Вычисляем границы: от min(start, current) до max(start, current)
int x = (int)MathHelper.Min(selection.StartPosition.X, mousePosition.X);
int y = (int)MathHelper.Min(selection.StartPosition.Y, mousePosition.Y);
int width = (int)MathHelper.Max(selection.StartPosition.X, mousePosition.X) - x;
int height = (int)MathHelper.Max(selection.StartPosition.Y, mousePosition.Y) - y;
selection.Bounds = new Rectangle(x, y, width, height);
}
// Если левая кнопка отпущена и selection активна
else if (Game1.CurrentMouseState.LeftButton == ButtonState.Released && Game1.PreviousMouseState.LeftButton == ButtonState.Pressed && _selectionEntity != -1)
{
// Деактивируем selection box (здесь можно добавить логику выделения юнитов внутри Bounds)
if (_selectionPool.Has(_selectionEntity))
{
ref SelectionBox selection = ref _selectionPool.Get(_selectionEntity);
selection.IsActive = false;
// Опционально: удалить сущность или оставить для рендеринга последнего состояния
// _world.DelEntity(_selectionEntity);
// _selectionEntity = -1;
}
}
}
}