refactoring again

This commit is contained in:
klemek
2020-12-17 13:47:47 +01:00
parent 8a6a1b0465
commit 947e847d3b
8 changed files with 227 additions and 167 deletions
+18 -9
View File
@@ -32,13 +32,30 @@ namespace UntitledLogicGame
#region Unity Methods #region Unity Methods
private void FixedUpdate() private void FixedUpdate()
{
UpdateZoom();
UpdateDrag();
}
#endregion
#region Public Methods
#endregion
#region Private Methods
private void UpdateZoom()
{ {
var size = Camera.main.orthographicSize; var size = Camera.main.orthographicSize;
size -= Input.GetAxis("Mouse ScrollWheel") * ScrollSensitivity; size -= Input.GetAxis("Mouse ScrollWheel") * ScrollSensitivity;
size = Mathf.Clamp(size, MinSize, MaxSize); size = Mathf.Clamp(size, MinSize, MaxSize);
Camera.main.orthographicSize = size; Camera.main.orthographicSize = size;
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, -size); Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, -size);
}
private void UpdateDrag()
{
if (Input.GetMouseButton(2)) if (Input.GetMouseButton(2))
{ {
var mousePos = Camera101.ScreenToWorldPoint(Input.mousePosition); var mousePos = Camera101.ScreenToWorldPoint(Input.mousePosition);
@@ -52,20 +69,12 @@ namespace UntitledLogicGame
Camera.main.transform.position = _startDragPos.Value - (mousePos - _startDragMousePos) * Camera.main.orthographicSize; Camera.main.transform.position = _startDragPos.Value - (mousePos - _startDragMousePos) * Camera.main.orthographicSize;
} }
else if(_startDragPos != null) else if (_startDragPos != null)
{ {
_startDragPos = null; _startDragPos = null;
} }
} }
#endregion #endregion
#region Public Methods
#endregion
#region Private Methods
#endregion
} }
} }
+88 -67
View File
@@ -54,76 +54,18 @@ namespace UntitledLogicGame
private void FixedUpdate() private void FixedUpdate()
{ {
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); UpdateMousePos();
mousePos.z = 0f;
MousePos = mousePos;
if (Clicking) if (Clicking)
{ {
if(_currentCable == null) UpdateDrag();
{
if (_currentGate != null)
{
_currentGate.transform.position = MousePos - _currentGateDelta;
}
else if (GameManager.Instance.CurrentAnchor != null)
{
_currentCable = Instantiate(GameManager.Instance.CablePrefab, GameManager.Instance.CablesGroup, true);
_currentCable.StartAnchor = GameManager.Instance.CurrentAnchor;
}
else if (GameManager.Instance.CurrentGate != null)
{
DragGate(GameManager.Instance.CurrentGate, false);
}
}
} }
else if (_currentCable != null) else
{ {
if (GameManager.Instance.CurrentAnchor == null || _currentCable.StartAnchor.IsInput == GameManager.Instance.CurrentAnchor.IsInput) UpdateDrop();
{
Destroy(_currentCable.gameObject);
}
else
{
_currentCable.EndAnchor = GameManager.Instance.CurrentAnchor;
}
_currentCable = null;
}
else if(_currentGate != null)
{
if (DeleteOnRelease)
{
Destroy(_currentGate.gameObject);
}
else
{
foreach (var renderer in _currentGate.GetComponentsInChildren<SpriteRenderer>())
{
renderer.sortingLayerName = "default";
}
_currentGate.transform.position = _currentGate.transform.position.Round();
var currentBox = _currentGate.Box;
if (FindObjectsOfType<Gate>()
.Where(g => !g.Equals(_currentGate))
.Select(g => g.Box)
.Any(b => currentBox.IsTouching(b)))
{
// Collision with another gate
if (_currentGateInitialPos == null)
{
Destroy(_currentGate.gameObject);
}
else
{
_currentGate.transform.position = _currentGateInitialPos.Value; // Reset pos
}
}
}
_currentGate = null;
DeleteOnRelease = false;
} }
SetCursor(); UpdateCursor();
} }
#endregion #endregion
@@ -181,14 +123,19 @@ namespace UntitledLogicGame
#region Private Methods #region Private Methods
private void SetCursor() private static void UpdateMousePos()
{
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0f;
MousePos = mousePos;
}
private void UpdateCursor()
{ {
var cursor = DefaultCursor; var cursor = DefaultCursor;
var position = Vector2.zero; var position = Vector2.zero;
//TODO fix warning about invalid Texture2D if (!Interacting && GameManager.Instance.CurrentAnchor != null || Interacting && _currentCable != null)
if(!Interacting && GameManager.Instance.CurrentAnchor != null || Interacting && _currentCable != null)
{ {
cursor = PointerCursor; cursor = PointerCursor;
position = new Vector2(cursor.width / 2f, 0f); position = new Vector2(cursor.width / 2f, 0f);
@@ -201,11 +148,85 @@ namespace UntitledLogicGame
if(_currentCursor != cursor) if(_currentCursor != cursor)
{ {
//TODO Invalid texture used for cursor - check importer settings or texture creation. Texture must be RGBA32, readable, have alphaIsTransparency enabled and have no mip chain.
Cursor.SetCursor(cursor, position, CursorMode.Auto); Cursor.SetCursor(cursor, position, CursorMode.Auto);
_currentCursor = cursor; _currentCursor = cursor;
} }
} }
private void UpdateDrag()
{
if (_currentCable != null) // Dragging cable
{
_currentCable.FallbackEndPos = MousePos;
}
else if (_currentGate != null) // Dragging gate
{
_currentGate.transform.position = MousePos - _currentGateDelta;
}
else if (GameManager.Instance.CurrentAnchor != null) // Dragging new cable
{
_currentCable = Instantiate(GameManager.Instance.CablePrefab, GameManager.Instance.CablesGroup, true);
_currentCable.StartAnchor = GameManager.Instance.CurrentAnchor;
_currentCable.FallbackEndPos = MousePos;
}
else if (GameManager.Instance.CurrentGate != null) // Dragging new gate
{
DragGate(GameManager.Instance.CurrentGate, false);
}
}
private void UpdateDrop()
{
if (_currentCable != null) // Dropping cable
{
if (GameManager.Instance.CurrentAnchor == null || _currentCable.StartAnchor.IsInput == GameManager.Instance.CurrentAnchor.IsInput)
{
Destroy(_currentCable.gameObject);
}
else
{
_currentCable.EndAnchor = GameManager.Instance.CurrentAnchor;
}
_currentCable = null;
}
else if (_currentGate != null) // Dropping gate
{
if (DeleteOnRelease)
{
Destroy(_currentGate.gameObject);
}
else
{
foreach (var renderer in _currentGate.GetComponentsInChildren<SpriteRenderer>())
{
renderer.sortingLayerName = "default";
}
_currentGate.transform.position = _currentGate.transform.position.Round();
var currentBox = _currentGate.Box;
if (FindObjectsOfType<Gate>()
.Where(g => !g.Equals(_currentGate))
.Select(g => g.Box)
.Any(b => currentBox.IsTouching(b)))
{
// Collision with another gate
if (_currentGateInitialPos == null)
{
Destroy(_currentGate.gameObject);
}
else
{
_currentGate.transform.position = _currentGateInitialPos.Value; // Reset pos
}
}
}
_currentGate = null;
DeleteOnRelease = false;
}
}
#endregion #endregion
} }
} }
+12 -7
View File
@@ -48,13 +48,7 @@ namespace UntitledLogicGame.UI
private void FixedUpdate() private void FixedUpdate()
{ {
if(PointerManager.Instance.Interacting != _lastMouseInteracting) UpdateUI();
{
//TODO animate go down
GateBar.SetActive(!PointerManager.Instance.Interacting);
MovingBar.SetActive(PointerManager.Instance.MovingObject);
_lastMouseInteracting = PointerManager.Instance.Interacting;
}
} }
#endregion #endregion
@@ -65,6 +59,17 @@ namespace UntitledLogicGame.UI
#region Private Methods #region Private Methods
private void UpdateUI()
{
if (PointerManager.Instance.Interacting != _lastMouseInteracting)
{
//TODO animate go down
GateBar.SetActive(!PointerManager.Instance.Interacting);
MovingBar.SetActive(PointerManager.Instance.MovingObject);
_lastMouseInteracting = PointerManager.Instance.Interacting;
}
}
#endregion #endregion
} }
} }
+10 -5
View File
@@ -64,11 +64,7 @@ namespace UntitledLogicGame.Workspace
// Update is called once per frame // Update is called once per frame
private void Update() private void Update()
{ {
if (_lastActivated == null || _lastActivated != Activated) UpdateState();
{
_sprite.color = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor;
_lastActivated = Activated;
}
} }
private void OnMouseEnter() private void OnMouseEnter()
@@ -110,6 +106,15 @@ namespace UntitledLogicGame.Workspace
#region Private Methods #region Private Methods
private void UpdateState()
{
if (_lastActivated == null || _lastActivated != Activated)
{
_sprite.color = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor;
_lastActivated = Activated;
}
}
#endregion #endregion
} }
} }
+77 -61
View File
@@ -56,11 +56,14 @@ namespace UntitledLogicGame.Workspace
} }
} }
public bool Activated => StartAnchor != null && !StartAnchor.IsInput && StartAnchor.Activated; public bool Activated => StartAnchor != null && !StartAnchor.IsInput && StartAnchor.Activated;
public Vector3 FallbackEndPos { get; set; }
#endregion #endregion
#region Private Properties #region Private Properties
private Vector3 _lastStartPos;
private Vector3 _lastEndPos;
private Anchor _startAnchor; private Anchor _startAnchor;
private Anchor _endAnchor; private Anchor _endAnchor;
private LineRenderer _line; private LineRenderer _line;
@@ -81,67 +84,8 @@ namespace UntitledLogicGame.Workspace
// Update is called once per frame // Update is called once per frame
private void Update() private void Update()
{ {
if (_lastActivated == null || _lastActivated != Activated) UpdateColor();
{ UpdateLine();
_line.startColor = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor;
_line.endColor = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor;
_lastActivated = Activated;
}
if(StartAnchor != null)
{
if (EndAnchor == null)
{
_line.positionCount = 2;
_line.SetPosition(0, StartAnchor.transform.position);
_line.SetPosition(1, PointerManager.MousePos);
}
else
{
var startPos = StartAnchor.transform.position;
var startOr = StartAnchor.Orientation;
var endPos = EndAnchor.transform.position;
var endOr = StartAnchor.Orientation;
_line.positionCount = 4;
_line.SetPosition(0, startPos);
if (Mathf.Abs(startOr.x) > 0)
{
if (Mathf.Abs(endOr.x) > 0)
{
var middle = (startPos.x + endPos.x) / 2;
_line.SetPosition(1, new Vector3(middle, startPos.y, startPos.z));
_line.SetPosition(2, new Vector3(middle, endPos.y, startPos.z));
}
else
{
_line.SetPosition(1, new Vector3(startPos.x, endPos.y, startPos.z));
_line.SetPosition(2, new Vector3(startPos.x, endPos.y, startPos.z));
}
}
else
{
if (Mathf.Abs(endOr.x) > 0)
{
var middle = (startPos.y + endPos.y) / 2;
_line.SetPosition(1, new Vector3(startPos.x, middle, startPos.z));
_line.SetPosition(2, new Vector3(endPos.x, middle, startPos.z));
}
else
{
_line.SetPosition(1, new Vector3(endPos.x, startPos.y, startPos.z));
_line.SetPosition(2, new Vector3(endPos.x, startPos.y, startPos.z));
}
}
_line.SetPosition(3, endPos);
}
}
else
{
_line.positionCount = 0;
}
} }
private void OnDestroy() private void OnDestroy()
@@ -165,6 +109,78 @@ namespace UntitledLogicGame.Workspace
#region Private Methods #region Private Methods
private void UpdateColor()
{
if (_lastActivated == null || _lastActivated != Activated)
{
_line.startColor = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor;
_line.endColor = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor;
_lastActivated = Activated;
}
}
private void UpdateLine()
{
if (StartAnchor != null)
{
var startPos = StartAnchor.transform.position;
var endPos = EndAnchor == null ? FallbackEndPos : EndAnchor.transform.position;
if (startPos != _lastStartPos || endPos != _lastEndPos)
{
if (EndAnchor == null)
{
_line.positionCount = 2;
_line.SetPosition(0, startPos);
_line.SetPosition(1, endPos);
}
else
{
var startOr = StartAnchor.Orientation;
var endOr = StartAnchor.Orientation;
_line.positionCount = 4;
_line.SetPosition(0, startPos);
if (Mathf.Abs(startOr.x) > 0)
{
if (Mathf.Abs(endOr.x) > 0)
{
var middle = (startPos.x + endPos.x) / 2;
_line.SetPosition(1, new Vector3(middle, startPos.y, startPos.z));
_line.SetPosition(2, new Vector3(middle, endPos.y, startPos.z));
}
else
{
_line.SetPosition(1, new Vector3(startPos.x, endPos.y, startPos.z));
_line.SetPosition(2, new Vector3(startPos.x, endPos.y, startPos.z));
}
}
else
{
if (Mathf.Abs(endOr.x) > 0)
{
var middle = (startPos.y + endPos.y) / 2;
_line.SetPosition(1, new Vector3(startPos.x, middle, startPos.z));
_line.SetPosition(2, new Vector3(endPos.x, middle, startPos.z));
}
else
{
_line.SetPosition(1, new Vector3(endPos.x, startPos.y, startPos.z));
_line.SetPosition(2, new Vector3(endPos.x, startPos.y, startPos.z));
}
}
_line.SetPosition(3, endPos);
}
}
}
else
{
_line.positionCount = 0;
}
}
#endregion #endregion
} }
+11 -6
View File
@@ -76,12 +76,7 @@ namespace UntitledLogicGame.Workspace
// Update is called once per frame // Update is called once per frame
private void Update() private void Update()
{ {
var state = Definition.GetState(this).ToInt(); UpdateState();
if(state != _lastState)
{
Definition.Compute(this);
_lastState = state;
}
} }
#endregion #endregion
@@ -100,6 +95,16 @@ namespace UntitledLogicGame.Workspace
#region Private Methods #region Private Methods
private void UpdateState()
{
var state = Definition.GetState(this).ToInt();
if (state != _lastState)
{
Definition.Compute(this);
_lastState = state;
}
}
#endregion #endregion
} }
-6
View File
@@ -31,12 +31,6 @@ namespace UntitledLogicGame.Workspace
_gate = GetComponentInParent<Gate>(); _gate = GetComponentInParent<Gate>();
} }
// Update is called once per frame
private void Update()
{
}
private void OnMouseEnter() private void OnMouseEnter()
{ {
GameManager.Instance.CurrentGate = _gate; GameManager.Instance.CurrentGate = _gate;
+11 -6
View File
@@ -38,13 +38,9 @@ namespace UntitledLogicGame.Workspace
Utils.RandomName("Input", gameObject); Utils.RandomName("Input", gameObject);
} }
private void FixedUpdate() private void Update()
{ {
if ((Sprite.Hovering || OutputAnchor.Hovering) && PointerManager.Instance.DoubleClick()) UpdateState();
{
State = !State;
OutputAnchor.Activated = State;
}
} }
#endregion #endregion
@@ -55,6 +51,15 @@ namespace UntitledLogicGame.Workspace
#region Private Methods #region Private Methods
private void UpdateState()
{
if ((Sprite.Hovering || OutputAnchor.Hovering) && PointerManager.Instance.DoubleClick())
{
State = !State;
OutputAnchor.Activated = State;
}
}
#endregion #endregion
} }