From 947e847d3b3af88c95bec62cf983eb0f0238474c Mon Sep 17 00:00:00 2001 From: klemek Date: Thu, 17 Dec 2020 13:47:47 +0100 Subject: [PATCH] refactoring again --- Assets/Scripts/CameraManager.cs | 27 +++-- Assets/Scripts/PointerManager.cs | 155 ++++++++++++++----------- Assets/Scripts/UI/UIManager.cs | 19 +-- Assets/Scripts/Workspace/Anchor.cs | 15 ++- Assets/Scripts/Workspace/Cable.cs | 138 ++++++++++++---------- Assets/Scripts/Workspace/Gate.cs | 17 ++- Assets/Scripts/Workspace/GateSprite.cs | 6 - Assets/Scripts/Workspace/InputGate.cs | 17 ++- 8 files changed, 227 insertions(+), 167 deletions(-) diff --git a/Assets/Scripts/CameraManager.cs b/Assets/Scripts/CameraManager.cs index f43d9e8..bf7a40b 100755 --- a/Assets/Scripts/CameraManager.cs +++ b/Assets/Scripts/CameraManager.cs @@ -32,13 +32,30 @@ namespace UntitledLogicGame #region Unity Methods private void FixedUpdate() + { + UpdateZoom(); + UpdateDrag(); + } + + #endregion + + #region Public Methods + + #endregion + + #region Private Methods + + private void UpdateZoom() { var size = Camera.main.orthographicSize; size -= Input.GetAxis("Mouse ScrollWheel") * ScrollSensitivity; size = Mathf.Clamp(size, MinSize, MaxSize); Camera.main.orthographicSize = 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)) { var mousePos = Camera101.ScreenToWorldPoint(Input.mousePosition); @@ -52,20 +69,12 @@ namespace UntitledLogicGame Camera.main.transform.position = _startDragPos.Value - (mousePos - _startDragMousePos) * Camera.main.orthographicSize; } - else if(_startDragPos != null) + else if (_startDragPos != null) { _startDragPos = null; } } #endregion - - #region Public Methods - - #endregion - - #region Private Methods - - #endregion } } \ No newline at end of file diff --git a/Assets/Scripts/PointerManager.cs b/Assets/Scripts/PointerManager.cs index ffc49de..b02f016 100755 --- a/Assets/Scripts/PointerManager.cs +++ b/Assets/Scripts/PointerManager.cs @@ -54,76 +54,18 @@ namespace UntitledLogicGame private void FixedUpdate() { - var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); - mousePos.z = 0f; - MousePos = mousePos; + UpdateMousePos(); if (Clicking) { - if(_currentCable == null) - { - 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); - } - } + UpdateDrag(); } - else if (_currentCable != null) + else { - 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) - { - if (DeleteOnRelease) - { - Destroy(_currentGate.gameObject); - } - else - { - foreach (var renderer in _currentGate.GetComponentsInChildren()) - { - renderer.sortingLayerName = "default"; - } - _currentGate.transform.position = _currentGate.transform.position.Round(); - var currentBox = _currentGate.Box; - if (FindObjectsOfType() - .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; + UpdateDrop(); } - SetCursor(); + UpdateCursor(); } #endregion @@ -181,14 +123,19 @@ namespace UntitledLogicGame #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 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; position = new Vector2(cursor.width / 2f, 0f); @@ -201,11 +148,85 @@ namespace UntitledLogicGame 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); _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()) + { + renderer.sortingLayerName = "default"; + } + _currentGate.transform.position = _currentGate.transform.position.Round(); + var currentBox = _currentGate.Box; + if (FindObjectsOfType() + .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 } } \ No newline at end of file diff --git a/Assets/Scripts/UI/UIManager.cs b/Assets/Scripts/UI/UIManager.cs index 292030e..c1b2bcf 100755 --- a/Assets/Scripts/UI/UIManager.cs +++ b/Assets/Scripts/UI/UIManager.cs @@ -48,13 +48,7 @@ namespace UntitledLogicGame.UI private void FixedUpdate() { - if(PointerManager.Instance.Interacting != _lastMouseInteracting) - { - //TODO animate go down - GateBar.SetActive(!PointerManager.Instance.Interacting); - MovingBar.SetActive(PointerManager.Instance.MovingObject); - _lastMouseInteracting = PointerManager.Instance.Interacting; - } + UpdateUI(); } #endregion @@ -65,6 +59,17 @@ namespace UntitledLogicGame.UI #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 } } \ No newline at end of file diff --git a/Assets/Scripts/Workspace/Anchor.cs b/Assets/Scripts/Workspace/Anchor.cs index 758f28c..ebedbf4 100755 --- a/Assets/Scripts/Workspace/Anchor.cs +++ b/Assets/Scripts/Workspace/Anchor.cs @@ -64,11 +64,7 @@ namespace UntitledLogicGame.Workspace // Update is called once per frame private void Update() { - if (_lastActivated == null || _lastActivated != Activated) - { - _sprite.color = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor; - _lastActivated = Activated; - } + UpdateState(); } private void OnMouseEnter() @@ -110,6 +106,15 @@ namespace UntitledLogicGame.Workspace #region Private Methods + private void UpdateState() + { + if (_lastActivated == null || _lastActivated != Activated) + { + _sprite.color = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor; + _lastActivated = Activated; + } + } + #endregion } } \ No newline at end of file diff --git a/Assets/Scripts/Workspace/Cable.cs b/Assets/Scripts/Workspace/Cable.cs index 7ba2a9d..c98fb48 100755 --- a/Assets/Scripts/Workspace/Cable.cs +++ b/Assets/Scripts/Workspace/Cable.cs @@ -56,11 +56,14 @@ namespace UntitledLogicGame.Workspace } } public bool Activated => StartAnchor != null && !StartAnchor.IsInput && StartAnchor.Activated; + public Vector3 FallbackEndPos { get; set; } #endregion #region Private Properties + private Vector3 _lastStartPos; + private Vector3 _lastEndPos; private Anchor _startAnchor; private Anchor _endAnchor; private LineRenderer _line; @@ -81,67 +84,8 @@ namespace UntitledLogicGame.Workspace // Update is called once per frame private void Update() { - 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; - } - - 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; - } + UpdateColor(); + UpdateLine(); } private void OnDestroy() @@ -165,6 +109,78 @@ namespace UntitledLogicGame.Workspace #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 } diff --git a/Assets/Scripts/Workspace/Gate.cs b/Assets/Scripts/Workspace/Gate.cs index 5f6207c..88e5f8f 100755 --- a/Assets/Scripts/Workspace/Gate.cs +++ b/Assets/Scripts/Workspace/Gate.cs @@ -76,12 +76,7 @@ namespace UntitledLogicGame.Workspace // Update is called once per frame private void Update() { - var state = Definition.GetState(this).ToInt(); - if(state != _lastState) - { - Definition.Compute(this); - _lastState = state; - } + UpdateState(); } #endregion @@ -100,6 +95,16 @@ namespace UntitledLogicGame.Workspace #region Private Methods + private void UpdateState() + { + var state = Definition.GetState(this).ToInt(); + if (state != _lastState) + { + Definition.Compute(this); + _lastState = state; + } + } + #endregion } diff --git a/Assets/Scripts/Workspace/GateSprite.cs b/Assets/Scripts/Workspace/GateSprite.cs index 28462ba..2132ebe 100755 --- a/Assets/Scripts/Workspace/GateSprite.cs +++ b/Assets/Scripts/Workspace/GateSprite.cs @@ -31,12 +31,6 @@ namespace UntitledLogicGame.Workspace _gate = GetComponentInParent(); } - // Update is called once per frame - private void Update() - { - - } - private void OnMouseEnter() { GameManager.Instance.CurrentGate = _gate; diff --git a/Assets/Scripts/Workspace/InputGate.cs b/Assets/Scripts/Workspace/InputGate.cs index f2b9df2..456ea06 100755 --- a/Assets/Scripts/Workspace/InputGate.cs +++ b/Assets/Scripts/Workspace/InputGate.cs @@ -38,13 +38,9 @@ namespace UntitledLogicGame.Workspace Utils.RandomName("Input", gameObject); } - private void FixedUpdate() + private void Update() { - if ((Sprite.Hovering || OutputAnchor.Hovering) && PointerManager.Instance.DoubleClick()) - { - State = !State; - OutputAnchor.Activated = State; - } + UpdateState(); } #endregion @@ -55,6 +51,15 @@ namespace UntitledLogicGame.Workspace #region Private Methods + private void UpdateState() + { + if ((Sprite.Hovering || OutputAnchor.Hovering) && PointerManager.Instance.DoubleClick()) + { + State = !State; + OutputAnchor.Activated = State; + } + } + #endregion }