initial commit
This commit is contained in:
Executable
+98
@@ -0,0 +1,98 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
public class Anchor : MonoBehaviour
|
||||
{
|
||||
#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
|
||||
{
|
||||
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()
|
||||
{
|
||||
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
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d38ff3984e75ad44398ee5927f8fbcf8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Executable
+104
@@ -0,0 +1,104 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Cable : MonoBehaviour
|
||||
{
|
||||
#region Unity Properties
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public Anchor StartAnchor
|
||||
{
|
||||
get => _startAnchor;
|
||||
set
|
||||
{
|
||||
_startAnchor = value;
|
||||
}
|
||||
}
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public bool HasInputAnchor(Anchor target)
|
||||
{
|
||||
return StartAnchor.HasInputAnchor(target);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3412f6b754d14c749830830aa2faa62d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Executable
+85
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
#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()
|
||||
{
|
||||
if (Instance != null)
|
||||
throw new InvalidOperationException("More than one GameManager in scene");
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
fileFormatVersion: 2
|
||||
guid: de472061361ce144cbda576b1723ecf4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- AnchorPrefab: {fileID: -7307756874029176930, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3}
|
||||
- CablePrefab: {fileID: -8717160041275297970, guid: c5518c2f8bda4d746a830a3f3e01330c, type: 3}
|
||||
- GatesGroup: {instanceID: 0}
|
||||
- CablesGroup: {instanceID: 0}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Executable
+58
@@ -0,0 +1,58 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
public class Gate : MonoBehaviour
|
||||
{
|
||||
#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()
|
||||
{
|
||||
Utils.RandomName("Gate", gameObject);
|
||||
Anchors = GetComponentsInChildren<Anchor>().ToList();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#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
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4b336fb04307cae45953d977c01c393d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
public static class Utils
|
||||
{
|
||||
#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 Unity Utils
|
||||
|
||||
public static void RandomName(string prefix, GameObject obj)
|
||||
{
|
||||
obj.name = $"{prefix}_{RandomString(5)}";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fc2965269c4d5ae44bbbce1d7a5178f3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user