input/output + refactoring

This commit is contained in:
klemek
2020-12-16 19:00:40 +01:00
parent eeaf611158
commit 1a70942e8e
23 changed files with 1272 additions and 52 deletions
+3
View File
@@ -35,6 +35,7 @@ namespace UntitledLogicGame.Workspace
_activated = value;
}
}
public bool Hovering { get; internal set; }
#endregion
@@ -74,6 +75,7 @@ namespace UntitledLogicGame.Workspace
{
transform.localScale = _scale * ScaleIncrease;
GameManager.Instance.CurrentAnchor = this;
Hovering = true;
}
private void OnMouseExit()
@@ -81,6 +83,7 @@ namespace UntitledLogicGame.Workspace
transform.localScale = _scale;
if (Equals(GameManager.Instance.CurrentAnchor))
GameManager.Instance.CurrentAnchor = null;
Hovering = false;
}
private void OnDestroy()
+31 -5
View File
@@ -17,7 +17,15 @@ namespace UntitledLogicGame.Workspace
#region Public Properties
public List<Anchor> Anchors { get; private set; }
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 {
@@ -28,14 +36,34 @@ namespace UntitledLogicGame.Workspace
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
#region Private Properties
private IEnumerable<Anchor> _anchors;
private GateDefinition _definition;
private int _lastState = -1;
private BoxCollider2D _box;
private GateSprite _sprite;
#endregion
@@ -44,17 +72,15 @@ namespace UntitledLogicGame.Workspace
private void Start()
{
Utils.RandomName(GateType.ToString(), gameObject);
Anchors = GetComponentsInChildren<Anchor>().ToList();
_definition = GateDefinition.Get(GateType, this);
}
// Update is called once per frame
private void Update()
{
var state = _definition.GetState(this).ToInt();
var state = Definition.GetState(this).ToInt();
if(state != _lastState)
{
_definition.Compute(this);
Definition.Compute(this);
_lastState = state;
}
}
+10 -13
View File
@@ -22,13 +22,10 @@ namespace UntitledLogicGame.Workspace.Gates
public abstract class GateDefinition
{
public abstract string[] Inputs { get; }
public abstract string[] Outputs { get; }
internal abstract Dictionary<State, State> TruthTable { get; }
private static Dictionary<GateType, GateDefinition> Definitions;
public abstract string[] Inputs { get; }
public abstract string[] Outputs { get; }
internal abstract Dictionary<State, State> TruthTable { get; }
private static void LoadAll()
{
@@ -42,9 +39,6 @@ namespace UntitledLogicGame.Workspace.Gates
public static GateDefinition Get(GateType gateType, Gate gate)
{
if (gateType == GateType.None)
throw new InvalidOperationException("GateType is set to None");
if (Definitions == null)
LoadAll();
@@ -92,11 +86,14 @@ namespace UntitledLogicGame.Workspace.Gates
public void Compute(Gate gate)
{
State key = new State(GetState(gate));
bool[] values = TruthTable[key].values;
foreach (var output in Outputs.Select((value, i) => new { i, value }))
if(TruthTable.Count > 0)
{
gate.OutputAnchors.First(a => a.Name.Equals(output.value)).Activated = values[output.i];
State key = new State(GetState(gate));
bool[] values = TruthTable[key].values;
foreach (var output in Outputs.Select((value, i) => new { i, value }))
{
gate.OutputAnchors.First(a => a.Name.Equals(output.value)).Activated = values[output.i];
}
}
}
}
+4
View File
@@ -14,6 +14,8 @@ namespace UntitledLogicGame.Workspace
#region Public Properties
public bool Hovering { get; internal set; }
#endregion
#region Private Properties
@@ -38,12 +40,14 @@ namespace UntitledLogicGame.Workspace
private void OnMouseEnter()
{
GameManager.Instance.CurrentGate = _gate;
Hovering = true;
}
private void OnMouseExit()
{
if (_gate.Equals(GameManager.Instance.CurrentGate))
GameManager.Instance.CurrentGate = null;
Hovering = false;
}
#endregion
+61
View File
@@ -0,0 +1,61 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UntitledLogicGame.Workspace
{
public class InputGate : Gate
{
#region Unity Properties
#endregion
#region Public Properties
public bool State { get; set; }
#endregion
#region Private Properties
private Anchor OutputAnchor {
get
{
if (_outputAnchor == null)
_outputAnchor = Anchors.First(g => g.Name == "Q");
return _outputAnchor;
}
}
private Anchor _outputAnchor;
#endregion
#region Unity Methods
private void Start()
{
Utils.RandomName("Input", gameObject);
}
private void FixedUpdate()
{
if ((Sprite.Hovering || OutputAnchor.Hovering) && PointerManager.Instance.DoubleClick())
{
State = !State;
OutputAnchor.Activated = State;
}
}
#endregion
#region Public Methods
#endregion
#region Private Methods
#endregion
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8aba9e174e9d3b34685f9f11106848d8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+57
View File
@@ -0,0 +1,57 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UntitledLogicGame.Workspace
{
public class OutputGate : Gate
{
#region Unity Properties
#endregion
#region Public Properties
public bool State {
get
{
return InputAnchor.Activated;
}
}
#endregion
#region Private Properties
private Anchor InputAnchor {
get
{
if (_inputAnchor == null)
_inputAnchor = Anchors.First(g => g.Name == "A");
return _inputAnchor;
}
}
private Anchor _inputAnchor;
#endregion
#region Unity Methods
private void Start()
{
Utils.RandomName("Output", gameObject);
}
#endregion
#region Public Methods
#endregion
#region Private Methods
#endregion
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e1d1d57c543bd5842917ec7313c7fbe0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: