formatting

This commit is contained in:
klemek
2020-12-19 17:21:06 +01:00
parent 63e8f2c5f6
commit 205a663e07
56 changed files with 1690 additions and 1297 deletions
+91 -91
View File
@@ -5,116 +5,116 @@ using UnityEngine;
namespace UntitledLogicGame.Workspace
{
public class Anchor : MonoBehaviour
{
#region Unity Properties
public class Anchor : MonoBehaviour
{
#region Unity Properties
public string Name;
public bool IsInput;
public float ScaleIncrease;
public Vector2 Orientation;
public string Name;
public bool IsInput;
public float ScaleIncrease;
public Vector2 Orientation;
#endregion
#endregion
#region Public Properties
#region Public Properties
public List<Cable> Cables { get; set; }
public Gate Gate { get; set; }
public bool Activated
{
get
{
if (IsInput)
return Cables.Count > 0 && Cables.First().Activated;
else
return _activated;
}
set
{
if (!IsInput)
_activated = value;
}
}
public bool Hovering { get; internal set; }
public List<Cable> Cables { get; set; }
public Gate Gate { get; set; }
public bool Activated
{
get
{
if (IsInput)
return Cables.Count > 0 && Cables.First().Activated;
else
return _activated;
}
set
{
if (!IsInput)
_activated = value;
}
}
public bool Hovering { get; internal set; }
#endregion
#endregion
#region Private Properties
#region Private Properties
private Vector3 _scale;
private SpriteRenderer _sprite;
private bool _activated;
private bool? _lastActivated;
private Vector3 _scale;
private SpriteRenderer _sprite;
private bool _activated;
private bool? _lastActivated;
#endregion
#endregion
#region Unity Methods
#region Unity Methods
// Start is called before the first frame update
private void Start()
{
Gate = GetComponentInParent<Gate>();
Utils.RandomName($"{Gate.GateType}_{Name}", gameObject);
_scale = transform.localScale;
_sprite = GetComponent<SpriteRenderer>();
Cables = new List<Cable>();
Orientation = Orientation.normalized;
}
// Start is called before the first frame update
private void Start()
{
Gate = GetComponentInParent<Gate>();
Utils.RandomName($"{Gate.GateType}_{Name}", gameObject);
_scale = transform.localScale;
_sprite = GetComponent<SpriteRenderer>();
Cables = new List<Cable>();
Orientation = Orientation.normalized;
}
// Update is called once per frame
private void Update()
{
UpdateState();
}
// Update is called once per frame
private void Update()
{
UpdateState();
}
private void OnMouseEnter()
{
transform.localScale = _scale * ScaleIncrease;
GameManager.Instance.CurrentAnchor = this;
Hovering = true;
}
private void OnMouseEnter()
{
transform.localScale = _scale * ScaleIncrease;
GameManager.Instance.CurrentAnchor = this;
Hovering = true;
}
private void OnMouseExit()
{
transform.localScale = _scale;
if (Equals(GameManager.Instance.CurrentAnchor))
GameManager.Instance.CurrentAnchor = null;
Hovering = false;
}
private void OnMouseExit()
{
transform.localScale = _scale;
if (Equals(GameManager.Instance.CurrentAnchor))
GameManager.Instance.CurrentAnchor = null;
Hovering = false;
}
private void OnDestroy()
{
foreach(var cable in Cables)
{
Destroy(cable.gameObject);
}
}
private void OnDestroy()
{
foreach(var cable in Cables)
{
Destroy(cable.gameObject);
}
}
#endregion
#endregion
#region Public Methods
#region Public Methods
public bool HasInputAnchor(Anchor target)
{
if (IsInput)
return Cables.Any(c => c.HasInputAnchor(target));
else
return Gate.HasInputAnchor(target);
}
public bool HasInputAnchor(Anchor target)
{
if (IsInput)
return Cables.Any(c => c.HasInputAnchor(target));
else
return Gate.HasInputAnchor(target);
}
#endregion
#endregion
#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;
}
}
private void UpdateState()
{
if (_lastActivated == null || _lastActivated != Activated)
{
_sprite.color = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor;
_lastActivated = Activated;
}
}
#endregion
}
#endregion
}
}
+153 -153
View File
@@ -5,183 +5,183 @@ using UnityEngine;
namespace UntitledLogicGame.Workspace
{
public class Cable : MonoBehaviour
{
#region Unity Properties
public class Cable : MonoBehaviour
{
#region Unity Properties
#endregion
#endregion
#region Public Properties
#region Public Properties
public Anchor StartAnchor
{
get => _startAnchor;
set
{
_startAnchor = value;
if (value.IsInput)
{
if (value.Cables.Count > 0)
Destroy(value.Cables.First().gameObject);
}
}
}
public Anchor EndAnchor
{
get => _endAnchor;
set
{
if (!value.IsInput)
{
_endAnchor = StartAnchor;
StartAnchor = value;
}
else
{
_endAnchor = value;
}
public Anchor StartAnchor
{
get => _startAnchor;
set
{
_startAnchor = value;
if (value.IsInput)
{
if (value.Cables.Count > 0)
Destroy(value.Cables.First().gameObject);
}
}
}
public Anchor EndAnchor
{
get => _endAnchor;
set
{
if (!value.IsInput)
{
_endAnchor = StartAnchor;
StartAnchor = value;
}
else
{
_endAnchor = value;
}
if (StartAnchor.HasInputAnchor(EndAnchor))
{
// Loop detected
Destroy(gameObject);
}
else
{
StartAnchor.Cables.Add(this);
if (EndAnchor.Cables.Count > 0)
Destroy(EndAnchor.Cables.First().gameObject);
EndAnchor.Cables = new List<Cable> { this };
}
}
}
public bool Activated => StartAnchor != null && !StartAnchor.IsInput && StartAnchor.Activated;
public Vector3 FallbackEndPos { get; set; }
if (StartAnchor.HasInputAnchor(EndAnchor))
{
// Loop detected
Destroy(gameObject);
}
else
{
StartAnchor.Cables.Add(this);
if (EndAnchor.Cables.Count > 0)
Destroy(EndAnchor.Cables.First().gameObject);
EndAnchor.Cables = new List<Cable> { this };
}
}
}
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 _endAnchor;
private LineRenderer _line;
private bool? _lastActivated;
private Vector3 _lastStartPos;
private Vector3 _lastEndPos;
private Anchor _startAnchor;
private Anchor _endAnchor;
private LineRenderer _line;
private bool? _lastActivated;
#endregion
#endregion
#region Unity Methods
#region Unity Methods
// Start is called before the first frame update
private void Start()
{
_line = GetComponent<LineRenderer>();
Utils.RandomName("Cable", gameObject);
// Start is called before the first frame update
private void Start()
{
_line = GetComponent<LineRenderer>();
Utils.RandomName("Cable", gameObject);
}
}
// Update is called once per frame
private void Update()
{
UpdateColor();
UpdateLine();
}
// 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);
}
private void OnDestroy()
{
if(StartAnchor != null)
StartAnchor.Cables.Remove(this);
if (EndAnchor != null)
EndAnchor.Cables.Remove(this);
}
#endregion
#endregion
#region Public Methods
#region Public Methods
public bool HasInputAnchor(Anchor target)
{
return StartAnchor.HasInputAnchor(target);
}
public bool HasInputAnchor(Anchor target)
{
return StartAnchor.HasInputAnchor(target);
}
#endregion
#endregion
#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 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;
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;
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);
_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));
}
}
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;
}
}
_line.SetPosition(3, endPos);
}
}
}
else
{
_line.positionCount = 0;
}
}
#endregion
#endregion
}
}
}
+83 -83
View File
@@ -6,106 +6,106 @@ using UntitledLogicGame.Workspace.Gates;
namespace UntitledLogicGame.Workspace
{
public class Gate : MonoBehaviour
{
#region Unity Properties
public class Gate : MonoBehaviour
{
#region Unity Properties
public GateType GateType;
public GateType GateType;
#endregion
#endregion
#region Public Properties
#region Public Properties
public IEnumerable<Anchor> Anchors
{
get
{
if(_anchors == null)
_anchors = GetComponentsInChildren<Anchor>().ToList();
return _anchors;
}
}
public IEnumerable<Anchor> InputAnchors => Anchors.Where(a => a.IsInput);
public IEnumerable<Anchor> OutputAnchors => Anchors.Where(a => !a.IsInput);
public BoxCollider2D Box {
get
{
if (_box == null)
_box = GetComponentInChildren<BoxCollider2D>();
return _box;
}
}
public GateSprite Sprite
{
get
{
if(_sprite == null)
_sprite = GetComponentInChildren<GateSprite>();
return _sprite;
}
}
public GateDefinition Definition
{
get
{
if(_definition == null)
_definition = GateDefinition.Get(GateType, this);
return _definition;
}
}
public IEnumerable<Anchor> Anchors
{
get
{
if(_anchors == null)
_anchors = GetComponentsInChildren<Anchor>().ToList();
return _anchors;
}
}
public IEnumerable<Anchor> InputAnchors => Anchors.Where(a => a.IsInput);
public IEnumerable<Anchor> OutputAnchors => Anchors.Where(a => !a.IsInput);
public BoxCollider2D Box {
get
{
if (_box == null)
_box = GetComponentInChildren<BoxCollider2D>();
return _box;
}
}
public GateSprite Sprite
{
get
{
if(_sprite == null)
_sprite = GetComponentInChildren<GateSprite>();
return _sprite;
}
}
public GateDefinition Definition
{
get
{
if(_definition == null)
_definition = GateDefinition.Get(GateType, this);
return _definition;
}
}
#endregion
#endregion
#region Private Properties
#region Private Properties
private IEnumerable<Anchor> _anchors;
private GateDefinition _definition;
private int _lastState = -1;
private BoxCollider2D _box;
private GateSprite _sprite;
private IEnumerable<Anchor> _anchors;
private GateDefinition _definition;
private int _lastState = -1;
private BoxCollider2D _box;
private GateSprite _sprite;
#endregion
#endregion
#region Unity Methods
#region Unity Methods
private void Start()
{
Utils.RandomName(Definition.Name, gameObject);
}
private void Start()
{
Utils.RandomName(Definition.Name, gameObject);
}
// Update is called once per frame
private void Update()
{
UpdateState();
}
// Update is called once per frame
private void Update()
{
UpdateState();
}
#endregion
#endregion
#region Public Methods
#region Public Methods
public bool HasInputAnchor(Anchor target)
{
return !Definition.HasState && (
InputAnchors.Contains(target) ||
InputAnchors.Any(a => a.HasInputAnchor(target))
);
}
public bool HasInputAnchor(Anchor target)
{
return !Definition.HasState && (
InputAnchors.Contains(target) ||
InputAnchors.Any(a => a.HasInputAnchor(target))
);
}
#endregion
#endregion
#region Private Methods
#region Private Methods
private void UpdateState()
{
var state = Definition.GetState(this).ToInt();
if (state != _lastState)
{
Definition.Compute(this);
_lastState = state;
}
}
private void UpdateState()
{
var state = Definition.GetState(this).ToInt();
if (state != _lastState)
{
Definition.Compute(this);
_lastState = state;
}
}
#endregion
#endregion
}
}
}
+32 -32
View File
@@ -6,53 +6,53 @@ using UntitledLogicGame.Workspace.Gates;
namespace UntitledLogicGame.Workspace
{
public class GateSprite : MonoBehaviour
{
#region Unity Properties
public class GateSprite : MonoBehaviour
{
#region Unity Properties
#endregion
#endregion
#region Public Properties
#region Public Properties
public bool Hovering { get; internal set; }
public bool Hovering { get; internal set; }
#endregion
#endregion
#region Private Properties
#region Private Properties
private Gate _gate;
private Gate _gate;
#endregion
#endregion
#region Unity Methods
#region Unity Methods
private void Start()
{
_gate = GetComponentInParent<Gate>();
}
private void Start()
{
_gate = GetComponentInParent<Gate>();
}
private void OnMouseEnter()
{
GameManager.Instance.CurrentGate = _gate;
Hovering = true;
}
private void OnMouseEnter()
{
GameManager.Instance.CurrentGate = _gate;
Hovering = true;
}
private void OnMouseExit()
{
if (_gate.Equals(GameManager.Instance.CurrentGate))
GameManager.Instance.CurrentGate = null;
Hovering = false;
}
private void OnMouseExit()
{
if (_gate.Equals(GameManager.Instance.CurrentGate))
GameManager.Instance.CurrentGate = null;
Hovering = false;
}
#endregion
#endregion
#region Public Methods
#region Public Methods
#endregion
#endregion
#region Private Methods
#region Private Methods
#endregion
#endregion
}
}
}
+24 -24
View File
@@ -1,35 +1,35 @@
namespace UntitledLogicGame.Workspace.Gates
{
public enum GateType
{
// 000 - Technical
None = 000,
// 100 - I/O
IN = 100,
OUT = 110,
// 200 - Basic
BUF = 200,
AND = 210,
OR = 220,
XOR = 230,
public enum GateType
{
// 000 - Technical
None = 000,
// 100 - I/O
IN = 100,
OUT = 110,
// 200 - Basic
BUF = 200,
AND = 210,
OR = 220,
XOR = 230,
NOT = 240,
NAND = 250,
NOR = 260,
XNOR = 270,
// 300 - Latches
SRLatch = 300,
JKLatch = 310,
DLatch = 320,
// 500 - Flip-Flops
SRFlipFlop = 400,
JKFlipFlop = 410,
DFlipFlop = 420,
TFlipFlop = 430,
// 500 - Arithmetic
HalfAdd = 500,
FullAdd = 510,
HalfSub = 520,
FullSub = 530,
JKLatch = 310,
DLatch = 320,
// 500 - Flip-Flops
SRFlipFlop = 400,
JKFlipFlop = 410,
DFlipFlop = 420,
TFlipFlop = 430,
// 500 - Arithmetic
HalfAdd = 500,
FullAdd = 510,
HalfSub = 520,
FullSub = 530,
// 600 - Data
Mux = 610,
Demux = 620,
+57 -57
View File
@@ -3,77 +3,77 @@ using System.Linq;
namespace UntitledLogicGame.Workspace.Gates
{
public class InputState : State
{
public InputState(IEnumerable<bool> args) : base(args) { }
public class InputState : State
{
public InputState(IEnumerable<bool> args) : base(args) { }
public InputState(params bool[] args) : base(args) { }
public InputState(params bool[] args) : base(args) { }
public InputState(int len) : base(len) { }
}
public InputState(int len) : base(len) { }
}
public class OutputState : State
{
public OutputState(IEnumerable<bool> args) : base(args) { }
{
public OutputState(IEnumerable<bool> args) : base(args) { }
public OutputState(params bool[] args) : base(args) { }
public OutputState(params bool[] args) : base(args) { }
public OutputState(int len) : base(len) { }
}
public OutputState(int len) : base(len) { }
}
public abstract class State
{
internal int Length => values.Length;
{
internal int Length => values.Length;
internal bool[] values;
internal bool[] values;
public State(IEnumerable<bool> args)
{
values = args.ToArray();
}
public State(IEnumerable<bool> args)
{
values = args.ToArray();
}
public State(params bool[] args)
{
values = args;
}
public State(params bool[] args)
{
values = args;
}
public State(int len)
{
values = new bool[len];
}
public State(int len)
{
values = new bool[len];
}
public bool this[int index]
{
get
{
if (index < 0 || index >= values.Length)
return false;
return values[index];
}
set
{
if (index >= 0 && index < values.Length)
values[index] = value;
}
}
public bool this[int index]
{
get
{
if (index < 0 || index >= values.Length)
return false;
return values[index];
}
set
{
if (index >= 0 && index < values.Length)
values[index] = value;
}
}
public override bool Equals(object obj)
{
return obj is State state && Enumerable.SequenceEqual(values, state.values);
}
public override bool Equals(object obj)
{
return obj is State state && Enumerable.SequenceEqual(values, state.values);
}
public override int GetHashCode()
{
//https://stackoverflow.com/questions/6832139/gethashcode-from-booleans-only
int hash = 17;
for (int index = 0; index < values.Length; index++)
hash = hash * 23 + values[index].GetHashCode();
return hash;
}
public override int GetHashCode()
{
//https://stackoverflow.com/questions/6832139/gethashcode-from-booleans-only
int hash = 17;
for (int index = 0; index < values.Length; index++)
hash = hash * 23 + values[index].GetHashCode();
return hash;
}
public override string ToString()
{
return string.Join(",", values);
}
}
public override string ToString()
{
return string.Join(",", values);
}
}
}
+41 -41
View File
@@ -5,62 +5,62 @@ using UnityEngine;
namespace UntitledLogicGame.Workspace
{
public class InputGate : Gate
{
#region Unity Properties
public class InputGate : Gate
{
#region Unity Properties
#endregion
#endregion
#region Public Properties
#region Public Properties
public bool State { get; set; }
public bool State { get; set; }
#endregion
#endregion
#region Private Properties
#region Private Properties
private Anchor OutputAnchor {
get
{
if (_outputAnchor == null)
_outputAnchor = Anchors.First(g => g.Name == "Q");
return _outputAnchor;
}
}
private Anchor _outputAnchor;
private Anchor OutputAnchor {
get
{
if (_outputAnchor == null)
_outputAnchor = Anchors.First(g => g.Name == "Q");
return _outputAnchor;
}
}
private Anchor _outputAnchor;
#endregion
#endregion
#region Unity Methods
#region Unity Methods
private void Start()
{
Utils.RandomName("Input", gameObject);
}
private void Start()
{
Utils.RandomName("Input", gameObject);
}
private void Update()
{
UpdateState();
}
private void Update()
{
UpdateState();
}
#endregion
#endregion
#region Public Methods
#region Public Methods
#endregion
#endregion
#region Private Methods
#region Private Methods
private void UpdateState()
{
if ((Sprite.Hovering || OutputAnchor.Hovering) && PointerManager.Instance.DoubleClick())
{
State = !State;
OutputAnchor.Activated = State;
}
}
private void UpdateState()
{
if ((Sprite.Hovering || OutputAnchor.Hovering) && PointerManager.Instance.DoubleClick())
{
State = !State;
OutputAnchor.Activated = State;
}
}
#endregion
}
#endregion
}
}
+34 -34
View File
@@ -5,53 +5,53 @@ using UnityEngine;
namespace UntitledLogicGame.Workspace
{
public class OutputGate : Gate
{
#region Unity Properties
public class OutputGate : Gate
{
#region Unity Properties
#endregion
#endregion
#region Public Properties
#region Public Properties
public bool State {
get
{
return InputAnchor.Activated;
}
}
public bool State {
get
{
return InputAnchor.Activated;
}
}
#endregion
#endregion
#region Private Properties
#region Private Properties
private Anchor InputAnchor {
get
{
if (_inputAnchor == null)
_inputAnchor = Anchors.First(g => g.Name == "A");
return _inputAnchor;
}
}
private Anchor _inputAnchor;
private Anchor InputAnchor {
get
{
if (_inputAnchor == null)
_inputAnchor = Anchors.First(g => g.Name == "A");
return _inputAnchor;
}
}
private Anchor _inputAnchor;
#endregion
#endregion
#region Unity Methods
#region Unity Methods
private void Start()
{
Utils.RandomName("Output", gameObject);
}
private void Start()
{
Utils.RandomName("Output", gameObject);
}
#endregion
#endregion
#region Public Methods
#region Public Methods
#endregion
#endregion
#region Private Methods
#region Private Methods
#endregion
}
#endregion
}
}