From e43a0c6de09785c525cb9113a9cf557ca6cd4935 Mon Sep 17 00:00:00 2001 From: klemek Date: Fri, 11 Dec 2020 17:15:37 +0100 Subject: [PATCH] snap to grid --- Assets/Prefabs/Anchor.prefab | 2 +- Assets/Prefabs/Cable.prefab | 2 +- Assets/Prefabs/Gates/NAND gate.prefab | 86 ++++++++++++++++++++++++++- Assets/Scripts/MouseManager.cs | 14 ++++- Assets/Scripts/Utils.cs | 5 ++ ProjectSettings/TagManager.asset | 2 +- 6 files changed, 103 insertions(+), 8 deletions(-) diff --git a/Assets/Prefabs/Anchor.prefab b/Assets/Prefabs/Anchor.prefab index 8db5f78..373e954 100755 --- a/Assets/Prefabs/Anchor.prefab +++ b/Assets/Prefabs/Anchor.prefab @@ -27,7 +27,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1824742176375353076} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0.5, y: 0.5, z: 0} + m_LocalPosition: {x: 0.5, y: 0.5, z: -1} m_LocalScale: {x: 0.5, y: 0.5, z: 1} m_Children: [] m_Father: {fileID: 0} diff --git a/Assets/Prefabs/Cable.prefab b/Assets/Prefabs/Cable.prefab index a3d08b1..8b17fb4 100755 --- a/Assets/Prefabs/Cable.prefab +++ b/Assets/Prefabs/Cable.prefab @@ -26,7 +26,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 2180376356050932351} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalPosition: {x: 0, y: 0, z: -0.5} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} diff --git a/Assets/Prefabs/Gates/NAND gate.prefab b/Assets/Prefabs/Gates/NAND gate.prefab index 3036c53..2372ae2 100755 --- a/Assets/Prefabs/Gates/NAND gate.prefab +++ b/Assets/Prefabs/Gates/NAND gate.prefab @@ -32,6 +32,7 @@ Transform: - {fileID: 118974315500811181} - {fileID: 555726995092159938} - {fileID: 120012632069647147} + - {fileID: 1658337153186624383} m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -139,6 +140,85 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 419a02bb4cfe59848a1f7677a1018aa3, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!1 &5358625439717083291 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1658337153186624383} + - component: {fileID: 5623837103916873807} + - component: {fileID: 2169103976044701791} + m_Layer: 0 + m_Name: box + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1658337153186624383 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5358625439717083291} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 3, y: -2.5, z: 1} + m_LocalScale: {x: 5.5, y: 4.5, z: 1} + m_Children: [] + m_Father: {fileID: 2070848596161447923} + m_RootOrder: 4 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!61 &5623837103916873807 +BoxCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5358625439717083291} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IsTrigger: 1 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + m_SpriteTilingProperty: + border: {x: 0, y: 0, z: 0, w: 0} + pivot: {x: 0, y: 0} + oldSize: {x: 0, y: 0} + newSize: {x: 0, y: 0} + adaptiveTilingThreshold: 0 + drawMode: 0 + adaptiveTiling: 0 + m_AutoTiling: 0 + serializedVersion: 2 + m_Size: {x: 1, y: 1} + m_EdgeRadius: 0 +--- !u!50 &2169103976044701791 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5358625439717083291} + m_BodyType: 1 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 0 + m_Mass: 1 + m_LinearDrag: 0 + m_AngularDrag: 0.05 + m_GravityScale: 0 + m_Material: {fileID: 0} + m_Interpolate: 0 + m_SleepingMode: 1 + m_CollisionDetection: 0 + m_Constraints: 0 --- !u!1001 &14861468116553427 PrefabInstance: m_ObjectHideFlags: 0 @@ -251,7 +331,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 8599692958760628507, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3} propertyPath: m_LocalPosition.z - value: 0 + value: -1 objectReference: {fileID: 0} - target: {fileID: 8599692958760628507, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3} propertyPath: m_LocalRotation.w @@ -321,7 +401,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 8599692958760628507, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3} propertyPath: m_LocalPosition.z - value: 0 + value: -1 objectReference: {fileID: 0} - target: {fileID: 8599692958760628507, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3} propertyPath: m_LocalRotation.w @@ -395,7 +475,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 8599692958760628507, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3} propertyPath: m_LocalPosition.z - value: 0 + value: -1 objectReference: {fileID: 0} - target: {fileID: 8599692958760628507, guid: d2ad1e1266d8af64ca87541a40be7e9f, type: 3} propertyPath: m_LocalRotation.w diff --git a/Assets/Scripts/MouseManager.cs b/Assets/Scripts/MouseManager.cs index b12d78c..167f072 100755 --- a/Assets/Scripts/MouseManager.cs +++ b/Assets/Scripts/MouseManager.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace UntitledLogicGame @@ -20,6 +21,7 @@ namespace UntitledLogicGame private Cable _currentCable; private Gate _currentGate; + private Vector3 _currentGateInitialPos; private Vector3 _currentGateDelta; #endregion @@ -31,7 +33,7 @@ namespace UntitledLogicGame } - private void Update() + private void FixedUpdate() { var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePos.z = 0f; @@ -54,7 +56,8 @@ namespace UntitledLogicGame { _currentGate = GameManager.Instance.CurrentGate; _currentGateDelta = MousePos - _currentGate.transform.position; - foreach(var renderer in _currentGate.GetComponentsInChildren()) + _currentGateInitialPos = _currentGate.transform.position; + foreach (var renderer in _currentGate.GetComponentsInChildren()) { renderer.sortingLayerName = "moving"; } @@ -79,6 +82,13 @@ namespace UntitledLogicGame { renderer.sortingLayerName = "default"; } + _currentGate.transform.position = _currentGate.transform.position.Round(); + var currentBox = _currentGate.GetComponentInChildren(); + if (FindObjectsOfType() + .Where(g => !g.Equals(_currentGate)) + .Select(g => g.GetComponentInChildren()) + .Any(b => currentBox.IsTouching(b))) // Collision with another gate + _currentGate.transform.position = _currentGateInitialPos; // Reset pos _currentGate = null; } } diff --git a/Assets/Scripts/Utils.cs b/Assets/Scripts/Utils.cs index f209722..2a44ba1 100755 --- a/Assets/Scripts/Utils.cs +++ b/Assets/Scripts/Utils.cs @@ -51,5 +51,10 @@ namespace UntitledLogicGame { return array.Select((v, i) => (v ? 1 : 0) << (array.Length - i - 1)).Sum(); } + + public static Vector3 Round(this Vector3 v) + { + return new Vector3(Mathf.Round(v.x), Mathf.Round(v.y), Mathf.Round(v.z)); + } } } \ No newline at end of file diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index fdce32b..c6cf0f9 100755 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -14,7 +14,7 @@ TagManager: - - - Background Image - - + - test - - -