input/output + refactoring
This commit is contained in:
@@ -15,6 +15,10 @@ namespace UntitledLogicGame
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region Private Properties
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region Unity Methods
|
||||
|
||||
// #endregion
|
||||
|
||||
@@ -10,7 +10,15 @@ namespace UntitledLogicGame
|
||||
{
|
||||
#region Static Properties
|
||||
|
||||
public static GameManager Instance { get; set; }
|
||||
public static GameManager Instance {
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
_instance = FindObjectOfType<GameManager>();
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
private static GameManager _instance;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -35,43 +43,36 @@ namespace UntitledLogicGame
|
||||
#region Public Properties
|
||||
|
||||
public Anchor CurrentAnchor { get; set; }
|
||||
|
||||
public Gate CurrentGate { get; set; }
|
||||
|
||||
public PointerManager MouseManager { get; private set; }
|
||||
public PointerManager PointerManager
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_pointerManager == null)
|
||||
_pointerManager = GetComponent<PointerManager>();
|
||||
return _pointerManager;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Properties
|
||||
|
||||
private PointerManager _pointerManager;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Methods
|
||||
|
||||
// Start is called before the first frame update
|
||||
private void Start()
|
||||
{
|
||||
if (Instance != null)
|
||||
throw new InvalidOperationException("More than one GameManager in scene");
|
||||
Instance = this;
|
||||
MouseManager = GetComponent<PointerManager>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void CreateGate(Gate gatePrefab, Vector3 position)
|
||||
public void CreateGate(Gate gatePrefab)
|
||||
{
|
||||
var gate = Instantiate(gatePrefab, GatesGroup);
|
||||
gate.transform.position = PointerManager.MousePos - gate.Box.transform.position;
|
||||
MouseManager.DragGate(gate, true);
|
||||
PointerManager.DragGate(gate, true);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -11,12 +11,17 @@ namespace UntitledLogicGame
|
||||
{
|
||||
#region Static Properties
|
||||
|
||||
public static PointerManager Instance => GameManager.Instance.MouseManager;
|
||||
public static PointerManager Instance => GameManager.Instance.PointerManager;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Properties
|
||||
|
||||
[Header("Click")]
|
||||
public float DoubleClickThreshold;
|
||||
public float DoubleClickDelay;
|
||||
|
||||
[Header("Cursor")]
|
||||
public Texture2D DefaultCursor;
|
||||
public Texture2D PointerCursor;
|
||||
public Texture2D MoveCursor;
|
||||
@@ -26,13 +31,9 @@ namespace UntitledLogicGame
|
||||
#region Public Properties
|
||||
|
||||
public static Vector3 MousePos { get; set; }
|
||||
|
||||
public bool Interacting => _currentCable != null || _currentGate != null;
|
||||
|
||||
public bool MovingObject => _currentGate != null;
|
||||
|
||||
public static bool Clicking => Input.GetButton("Fire1");
|
||||
|
||||
public bool Clicking => Input.GetButton("Fire1");
|
||||
public bool DeleteOnRelease { get; set; }
|
||||
|
||||
#endregion
|
||||
@@ -44,6 +45,8 @@ namespace UntitledLogicGame
|
||||
private Vector3? _currentGateInitialPos;
|
||||
private Vector3 _currentGateDelta;
|
||||
private Texture2D _currentCursor;
|
||||
private float _clicked = 0f;
|
||||
private float _clicktime = 0f;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -147,6 +150,33 @@ namespace UntitledLogicGame
|
||||
}
|
||||
}
|
||||
|
||||
public bool DoubleClick()
|
||||
{
|
||||
|
||||
if (Clicking)
|
||||
{
|
||||
_clicked += Time.deltaTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(_clicked >= DoubleClickThreshold)
|
||||
{
|
||||
if(Time.time - _clicktime < DoubleClickDelay)
|
||||
{
|
||||
_clicked = 0f;
|
||||
_clicktime = 0f;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_clicktime = Time.time;
|
||||
}
|
||||
}
|
||||
_clicked = 0f;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
@@ -26,7 +26,6 @@ namespace UntitledLogicGame.UI
|
||||
gameObject.name = "UI_" + _gatePrefab.GateType.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public RectTransform RectTransform
|
||||
{
|
||||
get
|
||||
@@ -50,9 +49,7 @@ namespace UntitledLogicGame.UI
|
||||
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
var position = Camera.main.ScreenToWorldPoint(transform.position);
|
||||
position.z = 0f;
|
||||
GameManager.Instance.CreateGate(_gatePrefab, position);
|
||||
GameManager.Instance.CreateGate(_gatePrefab);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Executable
+61
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8aba9e174e9d3b34685f9f11106848d8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Executable
+57
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e1d1d57c543bd5842917ec7313c7fbe0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user