-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoMapHoverService.cs
More file actions
152 lines (132 loc) · 4.71 KB
/
AutoMapHoverService.cs
File metadata and controls
152 lines (132 loc) · 4.71 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
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
144
145
146
147
148
149
150
151
152
using Bindito.Core;
using System;
using Timberborn.Automation;
using Timberborn.AutomationUI; // Requerido para el acceso directo
using Timberborn.SelectionSystem;
using Timberborn.SingletonSystem;
using UnityEngine;
namespace Calloatti.AutoTools
{
public class AutoMapHoverService : ILoadableSingleton, IDisposable
{
private readonly EntitySelectionService _selectionService;
private readonly AutoMapService _mapService;
private readonly TransmitterPickerTool _pickerTool;
private GameObject _hoverContainer;
private LineRenderer _dynamicLine;
[Inject]
public AutoMapHoverService(
EntitySelectionService selectionService,
AutoMapService mapService,
TransmitterPickerTool pickerTool)
{
_selectionService = selectionService;
_mapService = mapService;
_pickerTool = pickerTool;
}
public void Load()
{
// Setup del contenedor y la línea
_hoverContainer = new GameObject("AutoMap_HoverLineContainer");
_dynamicLine = _hoverContainer.AddComponent<LineRenderer>();
Material lineMat = new Material(Shader.Find("Sprites/Default"));
lineMat.SetInt("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);
lineMat.renderQueue = 4000;
_dynamicLine.material = lineMat;
_dynamicLine.startWidth = 0.05f;
_dynamicLine.endWidth = 0.05f;
_dynamicLine.useWorldSpace = true;
_dynamicLine.sortingOrder = 32767;
_dynamicLine.enabled = false;
var updater = _hoverContainer.AddComponent<HoverLineUpdater>();
updater.Setup(this, _selectionService, _mapService, _dynamicLine);
}
public void Dispose()
{
if (_dynamicLine != null && _dynamicLine.material != null)
{
UnityEngine.Object.Destroy(_dynamicLine.material);
}
if (_hoverContainer != null)
{
UnityEngine.Object.Destroy(_hoverContainer);
}
}
public Automator GetHoveredAutomator()
{
// Acceso directo a los campos anteriormente privados gracias al publicizer
if (_pickerTool != null && _pickerTool._transmitterPickerToolHighlighter != null)
{
return _pickerTool._transmitterPickerToolHighlighter._hoveredTransmitter;
}
return null;
}
}
public class HoverLineUpdater : MonoBehaviour
{
private AutoMapHoverService _hoverService;
private EntitySelectionService _selectionService;
private AutoMapService _mapService;
private LineRenderer _dynamicLine;
private Automator _lastHoveredAutomator;
public void Setup(
AutoMapHoverService hoverService,
EntitySelectionService selectionService,
AutoMapService mapService,
LineRenderer dynamicLine)
{
_hoverService = hoverService;
_selectionService = selectionService;
_mapService = mapService;
_dynamicLine = dynamicLine;
}
void Update()
{
if (!_selectionService.IsAnythingSelected)
{
_dynamicLine.enabled = false;
return;
}
Automator selectedAutomator = _selectionService.SelectedObject.GetComponent<Automator>();
if (selectedAutomator == null)
{
_dynamicLine.enabled = false;
return;
}
Automator hoveredAutomator = _hoverService.GetHoveredAutomator();
if (hoveredAutomator != null && hoveredAutomator != selectedAutomator)
{
_dynamicLine.enabled = true;
if (hoveredAutomator != _lastHoveredAutomator)
{
_lastHoveredAutomator = hoveredAutomator;
Color previewColor = _mapService.GetPartitionColor(hoveredAutomator.Partition);
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] { new GradientColorKey(previewColor, 0.0f), new GradientColorKey(previewColor, 1.0f) },
new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f), new GradientAlphaKey(1.0f, 1.0f) }
);
_dynamicLine.colorGradient = gradient;
// Matemáticas movidas aquí adentro para calcular la curva solo cuando cambia el target
Vector3 start = _mapService.GetCenterPosition(selectedAutomator);
Vector3 end = _mapService.GetCenterPosition(hoveredAutomator);
float distance = Vector3.Distance(start, end);
float arcHeight = Mathf.Max(1.2f, distance * 0.2f);
_dynamicLine.positionCount = 21;
for (int i = 0; i <= 20; i++)
{
float t = i / 20f;
Vector3 pos = Vector3.Lerp(start, end, t);
pos.y += Mathf.Sin(t * Mathf.PI) * arcHeight;
_dynamicLine.SetPosition(i, pos);
}
}
}
else
{
_dynamicLine.enabled = false;
_lastHoveredAutomator = null;
}
}
}
}