stuff less important than the shader
This commit is contained in:
+98
-86
@@ -3,96 +3,108 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
public class Anchor : MonoBehaviour
|
||||
namespace UntitledLogicGame
|
||||
{
|
||||
#region Unity Properties
|
||||
|
||||
public bool IsInput;
|
||||
public string Name;
|
||||
public float ScaleIncrease;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public List<Cable> Cables { get; set; }
|
||||
public Gate Gate { get; set; }
|
||||
public bool Activated
|
||||
public class Anchor : MonoBehaviour
|
||||
{
|
||||
get
|
||||
#region Unity Properties
|
||||
|
||||
public string Name;
|
||||
public bool IsInput;
|
||||
public float ScaleIncrease;
|
||||
|
||||
#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>();
|
||||
}
|
||||
|
||||
// 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.Count > 0 && Cables.First().Activated;
|
||||
return Cables.Any(c => c.HasInputAnchor(target));
|
||||
else
|
||||
return _activated;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (!IsInput)
|
||||
_activated = value;
|
||||
return Gate.HasInputAnchor(target);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#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()
|
||||
{
|
||||
Utils.RandomName("Anchor", gameObject);
|
||||
_scale = transform.localScale;
|
||||
_sprite = GetComponent<SpriteRenderer>();
|
||||
Cables = new List<Cable>();
|
||||
Gate = GetComponentInParent<Gate>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
if(_lastActivated == null || _lastActivated != Activated)
|
||||
{
|
||||
_sprite.color = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#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
|
||||
}
|
||||
}
|
||||
+108
-90
@@ -1,104 +1,122 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
public class Cable : MonoBehaviour
|
||||
namespace UntitledLogicGame
|
||||
{
|
||||
#region Unity Properties
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public Anchor StartAnchor
|
||||
public class Cable : MonoBehaviour
|
||||
{
|
||||
get => _startAnchor;
|
||||
set
|
||||
{
|
||||
_startAnchor = value;
|
||||
}
|
||||
}
|
||||
public Anchor EndAnchor
|
||||
{
|
||||
get => _endAnchor;
|
||||
set
|
||||
{
|
||||
if (!value.IsInput)
|
||||
{
|
||||
_endAnchor = StartAnchor;
|
||||
StartAnchor = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
_endAnchor = value;
|
||||
}
|
||||
#region Unity Properties
|
||||
|
||||
if (StartAnchor.HasInputAnchor(EndAnchor))
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public Anchor StartAnchor
|
||||
{
|
||||
get => _startAnchor;
|
||||
set
|
||||
{
|
||||
// Loop detected
|
||||
Destroy(gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
StartAnchor.Cables.Add(this);
|
||||
EndAnchor.Cables = new List<Cable> { this };
|
||||
_startAnchor = value;
|
||||
if (value.IsInput)
|
||||
{
|
||||
if (value.Cables.Count > 0)
|
||||
Destroy(value.Cables.First().gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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)
|
||||
public Anchor EndAnchor
|
||||
{
|
||||
_line.startColor = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor;
|
||||
_line.endColor = Activated ? GameManager.Instance.ActivatedColor : GameManager.Instance.DeadColor;
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
mousePos.z = 0f;
|
||||
var startPos = StartAnchor == null ? mousePos : StartAnchor.transform.position;
|
||||
var endPos = EndAnchor == null ? mousePos : EndAnchor.transform.position;
|
||||
_line.positionCount = 2;
|
||||
_line.SetPosition(0, startPos);
|
||||
_line.SetPosition(1, endPos);
|
||||
// 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;
|
||||
}
|
||||
|
||||
var startPos = StartAnchor == null ? MouseManager.MousePos : StartAnchor.transform.position;
|
||||
var endPos = EndAnchor == null ? MouseManager.MousePos : EndAnchor.transform.position;
|
||||
_line.positionCount = 2;
|
||||
_line.SetPosition(0, startPos);
|
||||
_line.SetPosition(1, endPos);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public bool HasInputAnchor(Anchor target)
|
||||
{
|
||||
return StartAnchor.HasInputAnchor(target);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
Executable
+71
@@ -0,0 +1,71 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UntitledLogicGame
|
||||
{
|
||||
public class CameraManager : MonoBehaviour
|
||||
{
|
||||
#region Unity Properties
|
||||
|
||||
[Header("Zooming")]
|
||||
public int MinSize;
|
||||
public int MaxSize;
|
||||
public float ScrollSensitivity;
|
||||
|
||||
[Header("Moving")]
|
||||
public Camera Camera101;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Properties
|
||||
|
||||
private Vector3 _startDragMousePos;
|
||||
private Vector3? _startDragPos;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Methods
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
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);
|
||||
|
||||
if (Input.GetMouseButton(2))
|
||||
{
|
||||
var mousePos = Camera101.ScreenToWorldPoint(Input.mousePosition);
|
||||
mousePos.z = 0f;
|
||||
|
||||
if (_startDragPos == null)
|
||||
{
|
||||
_startDragMousePos = mousePos;
|
||||
_startDragPos = Camera.main.transform.position;
|
||||
}
|
||||
|
||||
Camera.main.transform.position = _startDragPos.Value - (mousePos - _startDragMousePos) * Camera.main.orthographicSize;
|
||||
}
|
||||
else if(_startDragPos != null)
|
||||
{
|
||||
_startDragPos = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ad29b44c506923441adfb71b13f0f9dd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -2,84 +2,70 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UntitledLogicGame.Gates;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
namespace UntitledLogicGame
|
||||
{
|
||||
#region Static Properties
|
||||
|
||||
public static GameManager Instance { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Properties
|
||||
|
||||
[Header("Prefabs")]
|
||||
public Cable CablePrefab;
|
||||
|
||||
[Header("Groups")]
|
||||
public Transform GatesGroup;
|
||||
public Transform CablesGroup;
|
||||
|
||||
[Header("Colors")]
|
||||
public Color DeadColor;
|
||||
public Color ActivatedColor;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public Anchor CurrentAnchor { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Properties
|
||||
|
||||
private Cable _currentCable;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Methods
|
||||
|
||||
// Start is called before the first frame update
|
||||
private void Start()
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
if (Instance != null)
|
||||
throw new InvalidOperationException("More than one GameManager in scene");
|
||||
Instance = this;
|
||||
#region Static Properties
|
||||
|
||||
public static GameManager Instance { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Properties
|
||||
|
||||
[Header("Prefabs")]
|
||||
public Cable CablePrefab;
|
||||
|
||||
[Header("Groups")]
|
||||
public Transform GatesGroup;
|
||||
public Transform CablesGroup;
|
||||
|
||||
[Header("Colors")]
|
||||
public Color DeadColor;
|
||||
public Color ActivatedColor;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public Anchor CurrentAnchor { get; set; }
|
||||
|
||||
public Gate CurrentGate { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Properties
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
if (CurrentAnchor != null && _currentCable == null)
|
||||
{
|
||||
_currentCable = Instantiate(CablePrefab, CablesGroup, true);
|
||||
_currentCable.StartAnchor = CurrentAnchor;
|
||||
}
|
||||
}
|
||||
else if (_currentCable != null)
|
||||
{
|
||||
if (CurrentAnchor == null || _currentCable.StartAnchor.IsInput == CurrentAnchor.IsInput)
|
||||
{
|
||||
Destroy(_currentCable.gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentCable.EndAnchor = CurrentAnchor;
|
||||
}
|
||||
_currentCable = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+63
-48
@@ -2,57 +2,72 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UntitledLogicGame.Gates;
|
||||
|
||||
public class Gate : MonoBehaviour
|
||||
namespace UntitledLogicGame
|
||||
{
|
||||
#region Unity Properties
|
||||
|
||||
private bool HasState;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public List<Anchor> Anchors { get; set; }
|
||||
public IEnumerable<Anchor> InputAnchors => Anchors.Where(a => a.IsInput);
|
||||
public IEnumerable<Anchor> OutputAnchors => Anchors.Where(a => !a.IsInput);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Properties
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Methods
|
||||
|
||||
private void Start()
|
||||
public class Gate : MonoBehaviour
|
||||
{
|
||||
Utils.RandomName("Gate", gameObject);
|
||||
Anchors = GetComponentsInChildren<Anchor>().ToList();
|
||||
}
|
||||
#region Unity Properties
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
public bool HasState;
|
||||
public GateType GateType;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public List<Anchor> Anchors { get; set; }
|
||||
public IEnumerable<Anchor> InputAnchors => Anchors.Where(a => a.IsInput);
|
||||
public IEnumerable<Anchor> OutputAnchors => Anchors.Where(a => !a.IsInput);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Properties
|
||||
|
||||
private GateDefinition _definition;
|
||||
private int _lastState = -1;
|
||||
|
||||
#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
|
||||
|
||||
}
|
||||
|
||||
#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
|
||||
|
||||
}
|
||||
}
|
||||
Executable
+60
@@ -0,0 +1,60 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UntitledLogicGame.Gates;
|
||||
|
||||
namespace UntitledLogicGame
|
||||
{
|
||||
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
|
||||
|
||||
}
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 419a02bb4cfe59848a1f7677a1018aa3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 55a2af5530764a041ab519b9d3fa15a8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Executable
+252
@@ -0,0 +1,252 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UntitledLogicGame.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
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d351d9e762a4bc64c8491ae8fabcdede
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Executable
+96
@@ -0,0 +1,96 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UntitledLogicGame
|
||||
{
|
||||
public class MouseManager : MonoBehaviour
|
||||
{
|
||||
#region Unity Properties
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public static Vector3 MousePos { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Properties
|
||||
|
||||
private Cable _currentCable;
|
||||
private Gate _currentGate;
|
||||
private Vector3 _currentGateDelta;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Methods
|
||||
|
||||
private void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
mousePos.z = 0f;
|
||||
MousePos = mousePos;
|
||||
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
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)
|
||||
{
|
||||
_currentGate = GameManager.Instance.CurrentGate;
|
||||
_currentGateDelta = MousePos - _currentGate.transform.position;
|
||||
foreach(var renderer in _currentGate.GetComponentsInChildren<SpriteRenderer>())
|
||||
{
|
||||
renderer.sortingLayerName = "moving";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (_currentCable != null)
|
||||
{
|
||||
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)
|
||||
{
|
||||
foreach (var renderer in _currentGate.GetComponentsInChildren<SpriteRenderer>())
|
||||
{
|
||||
renderer.sortingLayerName = "default";
|
||||
}
|
||||
_currentGate = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 11e573998e532eb42bdb43b80953d23f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+47
-17
@@ -1,25 +1,55 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
public static class Utils
|
||||
namespace UntitledLogicGame
|
||||
{
|
||||
#region String Utils
|
||||
|
||||
public static string RandomString(int length)
|
||||
public static class Utils
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
return new string(Enumerable.Repeat(chars, length)
|
||||
.Select(s => s[Random.Range(0, s.Length)]).ToArray());
|
||||
#region String Utils
|
||||
|
||||
public static string RandomString(int length)
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
return new string(Enumerable.Repeat(chars, length)
|
||||
.Select(s => s[Random.Range(0, s.Length)]).ToArray());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Bool Utils
|
||||
|
||||
public static bool[][] AllBoolArrayValues(int length)
|
||||
{
|
||||
int count = (int)Math.Pow(2, length);
|
||||
return new ArrayList[count].Select((v, i) => i.ToBoolArray(length)).ToArray();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Utils
|
||||
|
||||
public static void RandomName(string prefix, GameObject obj)
|
||||
{
|
||||
obj.name = $"{prefix}_{RandomString(5)}";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Utils
|
||||
|
||||
public static void RandomName(string prefix, GameObject obj)
|
||||
public static class Extensions
|
||||
{
|
||||
obj.name = $"{prefix}_{RandomString(5)}";
|
||||
}
|
||||
public static bool[] ToBoolArray(this int value, int length)
|
||||
{
|
||||
string str = Convert.ToString(value, 2).PadLeft(length, '0');
|
||||
return str.Select((x) => x == '1').ToArray();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
public static int ToInt(this bool[] array)
|
||||
{
|
||||
return array.Select((v, i) => (v ? 1 : 0) << (array.Length - i - 1)).Sum();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user