refactoring again
This commit is contained in:
@@ -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);
|
||||
@@ -59,13 +76,5 @@ namespace UntitledLogicGame
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (_currentCable != null)
|
||||
{
|
||||
if (GameManager.Instance.CurrentAnchor == null || _currentCable.StartAnchor.IsInput == GameManager.Instance.CurrentAnchor.IsInput)
|
||||
{
|
||||
Destroy(_currentCable.gameObject);
|
||||
UpdateDrag();
|
||||
}
|
||||
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;
|
||||
UpdateDrop();
|
||||
}
|
||||
|
||||
SetCursor();
|
||||
UpdateCursor();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -181,13 +123,18 @@ 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)
|
||||
{
|
||||
cursor = PointerCursor;
|
||||
@@ -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<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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
@@ -80,6 +83,33 @@ namespace UntitledLogicGame.Workspace
|
||||
|
||||
// Update is called once per frame
|
||||
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)
|
||||
{
|
||||
@@ -87,20 +117,26 @@ namespace UntitledLogicGame.Workspace
|
||||
_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, StartAnchor.transform.position);
|
||||
_line.SetPosition(1, PointerManager.MousePos);
|
||||
_line.SetPosition(0, startPos);
|
||||
_line.SetPosition(1, endPos);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startPos = StartAnchor.transform.position;
|
||||
var startOr = StartAnchor.Orientation;
|
||||
var endPos = EndAnchor.transform.position;
|
||||
var endOr = StartAnchor.Orientation;
|
||||
|
||||
_line.positionCount = 4;
|
||||
@@ -138,33 +174,13 @@ namespace UntitledLogicGame.Workspace
|
||||
_line.SetPosition(3, endPos);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_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
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
}
|
||||
|
||||
@@ -31,12 +31,6 @@ namespace UntitledLogicGame.Workspace
|
||||
_gate = GetComponentInParent<Gate>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void OnMouseEnter()
|
||||
{
|
||||
GameManager.Instance.CurrentGate = _gate;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user