working toolbar

This commit is contained in:
klemek
2020-12-15 14:28:00 +01:00
parent e28d91e6bd
commit 78fe021014
11 changed files with 386 additions and 528 deletions
+13
View File
@@ -27,6 +27,9 @@ namespace UntitledLogicGame
public Color DeadColor;
public Color ActivatedColor;
[Header("Gates")]
public List<Gate> GatePrefabs;
#endregion
#region Public Properties
@@ -35,6 +38,8 @@ namespace UntitledLogicGame
public Gate CurrentGate { get; set; }
public MouseManager MouseManager { get; private set; }
#endregion
#region Private Properties
@@ -49,6 +54,7 @@ namespace UntitledLogicGame
if (Instance != null)
throw new InvalidOperationException("More than one GameManager in scene");
Instance = this;
MouseManager = GetComponent<MouseManager>();
}
// Update is called once per frame
@@ -61,6 +67,13 @@ namespace UntitledLogicGame
#region Public Methods
public void CreateGate(Gate gatePrefab, Vector3 position)
{
var gate = Instantiate(gatePrefab, GatesGroup);
gate.transform.position = MouseManager.MousePos - gate.Box.transform.position;
MouseManager.DragGate(gate, true);
}
#endregion
#region Private Methods
+11 -3
View File
@@ -17,9 +17,17 @@ namespace UntitledLogicGame
#region Public Properties
public List<Anchor> Anchors { get; set; }
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
@@ -27,6 +35,7 @@ namespace UntitledLogicGame
private GateDefinition _definition;
private int _lastState = -1;
private BoxCollider2D _box;
#endregion
@@ -36,7 +45,7 @@ namespace UntitledLogicGame
{
Utils.RandomName(GateType.ToString(), gameObject);
Anchors = GetComponentsInChildren<Anchor>().ToList();
_definition = GateDefinition.Get(GateType, this);
_definition = GateDefinition.Get(GateType, this);
}
// Update is called once per frame
@@ -48,7 +57,6 @@ namespace UntitledLogicGame
_definition.Compute(this);
_lastState = state;
}
}
#endregion
+38 -12
View File
@@ -7,6 +7,12 @@ namespace UntitledLogicGame
{
public class MouseManager : MonoBehaviour
{
#region Static Properties
public static MouseManager Instance => GameManager.Instance.MouseManager;
#endregion
#region Unity Properties
#endregion
@@ -15,6 +21,10 @@ namespace UntitledLogicGame
public static Vector3 MousePos { get; set; }
public bool Interacting => _currentCable != null || _currentGate != null;
public static bool Clicking => Input.GetButton("Fire1");
#endregion
#region Private Properties
@@ -39,7 +49,7 @@ namespace UntitledLogicGame
mousePos.z = 0f;
MousePos = mousePos;
if (Input.GetMouseButton(0))
if (Clicking)
{
if(_currentCable == null)
{
@@ -54,13 +64,7 @@ namespace UntitledLogicGame
}
else if (GameManager.Instance.CurrentGate != null)
{
_currentGate = GameManager.Instance.CurrentGate;
_currentGateDelta = MousePos - _currentGate.transform.position;
_currentGateInitialPos = _currentGate.transform.position;
foreach (var renderer in _currentGate.GetComponentsInChildren<SpriteRenderer>())
{
renderer.sortingLayerName = "moving";
}
DragGate(GameManager.Instance.CurrentGate, false);
}
}
}
@@ -83,12 +87,22 @@ namespace UntitledLogicGame
renderer.sortingLayerName = "default";
}
_currentGate.transform.position = _currentGate.transform.position.Round();
var currentBox = _currentGate.GetComponentInChildren<BoxCollider2D>();
var currentBox = _currentGate.Box;
if (FindObjectsOfType<Gate>()
.Where(g => !g.Equals(_currentGate))
.Select(g => g.GetComponentInChildren<BoxCollider2D>())
.Any(b => currentBox.IsTouching(b))) // Collision with another gate
_currentGate.transform.position = _currentGateInitialPos; // Reset pos
.Select(g => g.Box)
.Any(b => currentBox.IsTouching(b)))
{
// Collision with another gate
if(_currentGateInitialPos == null)
{
Destroy(_currentGate.gameObject);
}
else
{
_currentGate.transform.position = _currentGateInitialPos; // Reset pos
}
}
_currentGate = null;
}
}
@@ -97,6 +111,18 @@ namespace UntitledLogicGame
#region Public Methods
public void DragGate(Gate gate, bool created)
{
_currentGate = gate;
_currentGateDelta = MousePos - _currentGate.transform.position;
if(!created)
_currentGateInitialPos = _currentGate.transform.position;
foreach (var renderer in _currentGate.GetComponentsInChildren<SpriteRenderer>())
{
renderer.sortingLayerName = "moving";
}
}
#endregion
#region Private Methods
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ac8985c980f48e84594b68ae9051ce1d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+67
View File
@@ -0,0 +1,67 @@
using System.Collections;
using System.Collections.Generic;
using Unity.VectorGraphics;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace UntitledLogicGame.UI
{
public class UIGate : MonoBehaviour, IPointerDownHandler
{
#region Unity Properties
#endregion
#region Public Properties
public Gate GatePrefab {
set
{
_gatePrefab = value;
var sprite = _gatePrefab.GetComponentInChildren<SpriteRenderer>().sprite;
GetComponent<SVGImage>().sprite = sprite;
RectTransform.sizeDelta = new Vector2(100 * sprite.rect.width / 700f, 100); // TODO get max width from UIManager
gameObject.name = "UI_" + _gatePrefab.GateType.ToString();
}
}
public RectTransform RectTransform
{
get
{
if (_rectTransform == null)
_rectTransform = GetComponent<RectTransform>();
return _rectTransform;
}
}
#endregion
#region Private Properties
private Gate _gatePrefab;
private RectTransform _rectTransform;
#endregion
#region Unity Methods
public void OnPointerDown(PointerEventData eventData)
{
var position = Camera.main.ScreenToWorldPoint(transform.position);
position.z = 0f;
GameManager.Instance.CreateGate(_gatePrefab, position);
}
#endregion
#region Public Methods
#endregion
#region Private Methods
#endregion
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6a451c18bcd06994d8ac3b549cff313d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+67
View File
@@ -0,0 +1,67 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UntitledLogicGame.UI
{
public class UIManager : MonoBehaviour
{
#region Unity Properties
[Header("Components")]
public GameObject GateBar;
[Header("Prefabs")]
public UIGate UIGatePrefab;
#endregion
#region Public Properties
#endregion
#region Private Properties
private bool _lastMouseInteracting;
#endregion
#region Unity Methods
private IEnumerator Start()
{
yield return new WaitUntil(() => GameManager.Instance != null);
// TODO calculate max width
var currentPos = 0f;
foreach(var gatePrefab in GameManager.Instance.GatePrefabs)
{
var uiGate = Instantiate(UIGatePrefab, GateBar.transform);
uiGate.GatePrefab = gatePrefab;
uiGate.RectTransform.anchoredPosition = new Vector2(currentPos, 0);
currentPos += uiGate.RectTransform.sizeDelta.x;
}
}
private void FixedUpdate()
{
if(MouseManager.Instance.Interacting != _lastMouseInteracting)
{
//TODO animate go down
GateBar.SetActive(!MouseManager.Instance.Interacting);
_lastMouseInteracting = MouseManager.Instance.Interacting;
}
}
#endregion
#region Public Methods
#endregion
#region Private Methods
#endregion
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3124eabb2ea5e3547bf503bdcc463cb6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: