-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionDetectSystem.cs
More file actions
101 lines (86 loc) · 3.54 KB
/
SelectionDetectSystem.cs
File metadata and controls
101 lines (86 loc) · 3.54 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
using System;
using Leopotam.EcsLite;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
public class SelectionDetectSystem : IEcsRunSystem, IEcsInitSystem
{
private EcsWorld _world;
private EcsFilter _gameObjectFilter;
private EcsPool<SelectionBox> _selectionPool;
private EcsPool<GameObject> _gameObjectPool;
private EcsPool<Selected> _selectedPool;
private EcsPool<Bounds> _boundsPool;
private EcsPool<ColorComponent> _colorPool;
private EcsFilter _selectionFilter;
private SelectionInputSystem _selectionInputSystem;
private bool _previousIsActive;
// Сохраняем конструктор, как просили
public SelectionDetectSystem(EcsWorld world, SelectionInputSystem selectionInputSystem)
{
_world = world;
_selectionInputSystem = selectionInputSystem;
}
public void Init(IEcsSystems systems)
{
// Инициализируем пулы и фильтры
_gameObjectFilter = _world.Filter<GameObject>().End();
_selectionPool = _world.GetPool<SelectionBox>();
_gameObjectPool = _world.GetPool<GameObject>();
_selectedPool = _world.GetPool<Selected>();
_boundsPool = _world.GetPool<Bounds>();
_colorPool = _world.GetPool<ColorComponent>();
_selectionFilter = _world.Filter<SelectionBox>().End();
_selectionInputSystem.startSelect += ClearStartSelect;
}
public void Run(IEcsSystems systems)
{
MouseState currentMouseState = Mouse.GetState();
Vector2 mousePosition = new Vector2(currentMouseState.X, currentMouseState.Y);
foreach (int selectionEntity in _selectionFilter)
{
if (!_selectionPool.Has(selectionEntity)) continue;
ref SelectionBox selection = ref _selectionPool.Get(selectionEntity);
if(!selection.IsActive) continue;
ClearStartSelect();
// Проверяем пересечения
foreach (int entity in _gameObjectFilter)
{
if (!_gameObjectPool.Has(entity) || !_boundsPool.Has(entity) || !_colorPool.Has(entity)) continue;
ref GameObject gameObject = ref _gameObjectPool.Get(entity);
ref Bounds bounds = ref _boundsPool.Get(entity);
ref ColorComponent color = ref _colorPool.Get(entity);
if (selection.Bounds.Intersects(bounds.Rectangle))
{
if (!_selectedPool.Has(entity))
{
ref Selected selected = ref _selectedPool.Add(entity);
color.Value = Color.Blue;
}
}
}
_previousIsActive = selection.IsActive;
}
}
public void ClearStartSelect()
{
_selectedPool = _world.GetPool<Selected>();
foreach (int selectionEntity in _selectionFilter)
{
if (!_selectionPool.Has(selectionEntity)) continue;
// Снимаем выделение со всех
foreach (int entity in _gameObjectFilter)
{
if (_selectedPool.Has(entity))
{
_selectedPool.Del(entity);
if (_colorPool.Has(entity))
{
ref ColorComponent color = ref _colorPool.Get(entity);
color.Value = Color.White;
}
}
}
// _world.DelEntity(selectionEntity);
}
}
}