moved scripts

This commit is contained in:
klemek
2020-12-16 17:35:07 +01:00
parent ac021a68ea
commit eeaf611158
14 changed files with 11 additions and 9 deletions
+112
View File
@@ -0,0 +1,112 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UntitledLogicGame.Workspace
{
public class Anchor : MonoBehaviour
{
#region Unity Properties
public string Name;
public bool IsInput;
public float ScaleIncrease;
public Vector2 Orientation;
#endregion
#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;
}
}
#endregion
#region Private Properties
private Vector3 _scale;
private SpriteRenderer _sprite;
private bool _activated;
private bool? _lastActivated;
#endregion
#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;
}
// 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;
}
}
private void OnMouseEnter()
{
transform.localScale = _scale * ScaleIncrease;
GameManager.Instance.CurrentAnchor = this;
}
private void OnMouseExit()
{
transform.localScale = _scale;
if (Equals(GameManager.Instance.CurrentAnchor))
GameManager.Instance.CurrentAnchor = null;
}
private void OnDestroy()
{
foreach(var cable in Cables)
{
Destroy(cable.gameObject);
}
}
#endregion
#region Public Methods
public bool HasInputAnchor(Anchor target)
{
if (IsInput)
return Cables.Any(c => c.HasInputAnchor(target));
else
return Gate.HasInputAnchor(target);
}
#endregion
#region Private Methods
#endregion
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d38ff3984e75ad44398ee5927f8fbcf8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+171
View File
@@ -0,0 +1,171 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UntitledLogicGame.Workspace
{
public class Cable : MonoBehaviour
{
#region Unity Properties
#endregion
#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;
}
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;
#endregion
#region Private Properties
private Anchor _startAnchor;
private Anchor _endAnchor;
private LineRenderer _line;
private bool? _lastActivated;
#endregion
#region Unity Methods
// 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()
{
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;
}
if(StartAnchor != null)
{
if (EndAnchor == null)
{
_line.positionCount = 2;
_line.SetPosition(0, StartAnchor.transform.position);
_line.SetPosition(1, PointerManager.MousePos);
}
else
{
var startPos = StartAnchor.transform.position;
var startOr = StartAnchor.Orientation;
var endPos = EndAnchor.transform.position;
var endOr = StartAnchor.Orientation;
_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));
}
}
_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
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3412f6b754d14c749830830aa2faa62d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+81
View File
@@ -0,0 +1,81 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UntitledLogicGame.Workspace.Gates;
namespace UntitledLogicGame.Workspace
{
public class Gate : MonoBehaviour
{
#region Unity Properties
public bool HasState;
public GateType GateType;
#endregion
#region Public Properties
public List<Anchor> Anchors { get; private set; }
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;
}
}
#endregion
#region Private Properties
private GateDefinition _definition;
private int _lastState = -1;
private BoxCollider2D _box;
#endregion
#region Unity Methods
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();
if(state != _lastState)
{
_definition.Compute(this);
_lastState = state;
}
}
#endregion
#region Public Methods
public bool HasInputAnchor(Anchor target)
{
return !HasState && (
InputAnchors.Contains(target) ||
InputAnchors.Any(a => a.HasInputAnchor(target))
);
}
#endregion
#region Private Methods
#endregion
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4b336fb04307cae45953d977c01c393d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+252
View File
@@ -0,0 +1,252 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace UntitledLogicGame.Workspace.Gates
{
public enum GateType
{
None,
Buffer,
NOTGate,
ANDGate,
ORGate,
XORGate,
NANDGate,
NORGate,
XNORGate
}
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;
private static void LoadAll()
{
Definitions = new Dictionary<GateType, GateDefinition>();
foreach (var gateType in Enum.GetValues(typeof(GateType)).Cast<GateType>())
{
Type t = Type.GetType($"{typeof(GateDefinition).Namespace}.{gateType}", true);
Definitions[gateType] = (GateDefinition)t.GetConstructor(new Type[0]).Invoke(new object[0]);
}
}
public static GateDefinition Get(GateType gateType, Gate gate)
{
if (gateType == GateType.None)
throw new InvalidOperationException("GateType is set to None");
if (Definitions == null)
LoadAll();
GateDefinition definition = Definitions[gateType];
foreach (var inputName in definition.Inputs)
{
if(!gate.InputAnchors.Any(a => a.Name.Equals(inputName)))
throw new InvalidOperationException($"Gate has no {inputName} input anchor");
}
foreach (var outputName in definition.Outputs)
{
if (!gate.OutputAnchors.Any(a => a.Name.Equals(outputName)))
throw new InvalidOperationException($"Gate has no {outputName} output anchor");
}
return definition;
}
internal GateDefinition()
{
foreach(var key in TruthTable.Keys)
{
if (key.Length != Inputs.Length)
throw new InvalidOperationException($"{GetType()} invalid inputs ({key})");
}
if(Inputs.Length != 0)
{
foreach (var key in Utils.AllBoolArrayValues(Inputs.Length).Select(b => new State(b)))
{
if (!TruthTable.Keys.Contains(key))
throw new InvalidOperationException($"{GetType()} no outputs for ({key})");
var values = TruthTable[key];
if (values.Length != Outputs.Length)
throw new InvalidOperationException($"{GetType()} invalid outputs for ({key})");
}
}
}
public bool[] GetState(Gate gate)
{
return Inputs.Select(i => gate.InputAnchors.First(a => a.Name.Equals(i)).Activated).ToArray();
}
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 }))
{
gate.OutputAnchors.First(a => a.Name.Equals(output.value)).Activated = values[output.i];
}
}
}
internal class State
{
internal int Length => values.Length;
internal bool[] values;
public State(IEnumerable<bool> args)
{
values = args.ToArray();
}
public State(params bool[] args)
{
values = args;
}
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 string ToString()
{
return string.Join(",", values);
}
}
#region Gates
internal class None : GateDefinition
{
public override string[] Inputs { get; } = new string[] { };
public override string[] Outputs { get; } = new string[] { };
internal override Dictionary<State, State> TruthTable { get; } = new Dictionary<State, State>
{
};
}
internal class Buffer : GateDefinition
{
public override string[] Inputs { get; } = new string[] { "A" };
public override string[] Outputs { get; } = new string[] { "Q" };
internal override Dictionary<State, State> TruthTable { get; } = new Dictionary<State, State>
{
{ new State( false ), new State( false ) },
{ new State( true ), new State( true ) },
};
}
internal class NOTGate : GateDefinition
{
public override string[] Inputs { get; } = new string[] { "A" };
public override string[] Outputs { get; } = new string[] { "Q" };
internal override Dictionary<State, State> TruthTable { get; } = new Dictionary<State, State>
{
{ new State( false ), new State( true ) },
{ new State( true ), new State( false ) },
};
}
internal class ANDGate : GateDefinition
{
public override string[] Inputs { get; } = new string[] { "A", "B" };
public override string[] Outputs { get; } = new string[] { "Q" };
internal override Dictionary<State, State> TruthTable { get; } = new Dictionary<State, State>
{
{ new State( false, false ), new State( false ) },
{ new State( false, true ), new State( false ) },
{ new State( true, false ), new State( false ) },
{ new State( true, true ), new State( true ) },
};
}
internal class ORGate : GateDefinition
{
public override string[] Inputs { get; } = new string[] { "A", "B" };
public override string[] Outputs { get; } = new string[] { "Q" };
internal override Dictionary<State, State> TruthTable { get; } = new Dictionary<State, State>
{
{ new State( false, false ), new State( false ) },
{ new State( false, true ), new State( true ) },
{ new State( true, false ), new State( true ) },
{ new State( true, true ), new State( true ) },
};
}
internal class XORGate : GateDefinition
{
public override string[] Inputs { get; } = new string[] { "A", "B" };
public override string[] Outputs { get; } = new string[] { "Q" };
internal override Dictionary<State, State> TruthTable { get; } = new Dictionary<State, State>
{
{ new State( false, false ), new State( false ) },
{ new State( false, true ), new State( true ) },
{ new State( true, false ), new State( true ) },
{ new State( true, true ), new State( false ) },
};
}
internal class NANDGate : GateDefinition
{
public override string[] Inputs { get; } = new string[]{ "A", "B" };
public override string[] Outputs { get; } = new string[] { "Q" };
internal override Dictionary<State, State> TruthTable { get; } = new Dictionary<State, State>
{
{ new State( false, false ), new State( true ) },
{ new State( false, true ), new State( true ) },
{ new State( true, false ), new State( true ) },
{ new State( true, true ), new State( false ) },
};
}
internal class NORGate : GateDefinition
{
public override string[] Inputs { get; } = new string[] { "A", "B" };
public override string[] Outputs { get; } = new string[] { "Q" };
internal override Dictionary<State, State> TruthTable { get; } = new Dictionary<State, State>
{
{ new State( false, false ), new State( true ) },
{ new State( false, true ), new State( false ) },
{ new State( true, false ), new State( false ) },
{ new State( true, true ), new State( false ) },
};
}
internal class XNORGate : GateDefinition
{
public override string[] Inputs { get; } = new string[] { "A", "B" };
public override string[] Outputs { get; } = new string[] { "Q" };
internal override Dictionary<State, State> TruthTable { get; } = new Dictionary<State, State>
{
{ new State( false, false ), new State( true ) },
{ new State( false, true ), new State( false ) },
{ new State( true, false ), new State( false ) },
{ new State( true, true ), new State( true ) },
};
}
#endregion
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d351d9e762a4bc64c8491ae8fabcdede
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+60
View File
@@ -0,0 +1,60 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UntitledLogicGame.Workspace.Gates;
namespace UntitledLogicGame.Workspace
{
public class GateSprite : MonoBehaviour
{
#region Unity Properties
#endregion
#region Public Properties
#endregion
#region Private Properties
private Gate _gate;
#endregion
#region Unity Methods
private void Start()
{
_gate = GetComponentInParent<Gate>();
}
// Update is called once per frame
private void Update()
{
}
private void OnMouseEnter()
{
GameManager.Instance.CurrentGate = _gate;
}
private void OnMouseExit()
{
if (_gate.Equals(GameManager.Instance.CurrentGate))
GameManager.Instance.CurrentGate = null;
}
#endregion
#region Public Methods
#endregion
#region Private Methods
#endregion
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 419a02bb4cfe59848a1f7677a1018aa3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: