refactoring again
This commit is contained in:
@@ -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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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)
|
|
||||||
{
|
|
||||||
if (GameManager.Instance.CurrentAnchor == null || _currentCable.StartAnchor.IsInput == GameManager.Instance.CurrentAnchor.IsInput)
|
|
||||||
{
|
|
||||||
Destroy(_currentCable.gameObject);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_currentCable.EndAnchor = GameManager.Instance.CurrentAnchor;
|
UpdateDrop();
|
||||||
}
|
|
||||||
_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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
@@ -80,6 +83,33 @@ namespace UntitledLogicGame.Workspace
|
|||||||
|
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
private void Update()
|
private void Update()
|
||||||
|
{
|
||||||
|
UpdateColor();
|
||||||
|
UpdateLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDestroy()
|
||||||
|
{
|
||||||
|
if(StartAnchor != null)
|
||||||
|
StartAnchor.Cables.Remove(this);
|
||||||
|
if (EndAnchor != null)
|
||||||
|
EndAnchor.Cables.Remove(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Public Methods
|
||||||
|
|
||||||
|
public bool HasInputAnchor(Anchor target)
|
||||||
|
{
|
||||||
|
return StartAnchor.HasInputAnchor(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Private Methods
|
||||||
|
|
||||||
|
private void UpdateColor()
|
||||||
{
|
{
|
||||||
if (_lastActivated == null || _lastActivated != Activated)
|
if (_lastActivated == null || _lastActivated != Activated)
|
||||||
{
|
{
|
||||||
@@ -87,20 +117,26 @@ namespace UntitledLogicGame.Workspace
|
|||||||
_line.endColor = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor;
|
_line.endColor = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor;
|
||||||
_lastActivated = Activated;
|
_lastActivated = Activated;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(StartAnchor != null)
|
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)
|
if (EndAnchor == null)
|
||||||
{
|
{
|
||||||
_line.positionCount = 2;
|
_line.positionCount = 2;
|
||||||
_line.SetPosition(0, StartAnchor.transform.position);
|
_line.SetPosition(0, startPos);
|
||||||
_line.SetPosition(1, PointerManager.MousePos);
|
_line.SetPosition(1, endPos);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var startPos = StartAnchor.transform.position;
|
|
||||||
var startOr = StartAnchor.Orientation;
|
var startOr = StartAnchor.Orientation;
|
||||||
var endPos = EndAnchor.transform.position;
|
|
||||||
var endOr = StartAnchor.Orientation;
|
var endOr = StartAnchor.Orientation;
|
||||||
|
|
||||||
_line.positionCount = 4;
|
_line.positionCount = 4;
|
||||||
@@ -138,33 +174,13 @@ namespace UntitledLogicGame.Workspace
|
|||||||
_line.SetPosition(3, endPos);
|
_line.SetPosition(3, endPos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_line.positionCount = 0;
|
_line.positionCount = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDestroy()
|
|
||||||
{
|
|
||||||
if(StartAnchor != null)
|
|
||||||
StartAnchor.Cables.Remove(this);
|
|
||||||
if (EndAnchor != null)
|
|
||||||
EndAnchor.Cables.Remove(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Public Methods
|
|
||||||
|
|
||||||
public bool HasInputAnchor(Anchor target)
|
|
||||||
{
|
|
||||||
return StartAnchor.HasInputAnchor(target);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Private Methods
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user