From c3bc5d1aaf8c9dae0004579891c2af21103385d2 Mon Sep 17 00:00:00 2001 From: Saibotk Date: Tue, 24 Apr 2018 03:52:38 +0200 Subject: [PATCH] Doors now should open correctly --- Assets/Scenes/CodeDEV.unity | 2 +- Assets/Scripts/Door.cs | 11 +++++++++-- Assets/Scripts/GameController.cs | 4 +++- Assets/Scripts/Room.cs | 9 +++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Assets/Scenes/CodeDEV.unity b/Assets/Scenes/CodeDEV.unity index 11d5854..6530665 100644 --- a/Assets/Scenes/CodeDEV.unity +++ b/Assets/Scenes/CodeDEV.unity @@ -1543,7 +1543,7 @@ Prefab: - target: {fileID: 224150154901314796, guid: 460d856ea4eb14cedb5fecde1fe6d743, type: 2} propertyPath: m_AnchoredPosition.x - value: 0 + value: 38.545288 objectReference: {fileID: 0} - target: {fileID: 224150154901314796, guid: 460d856ea4eb14cedb5fecde1fe6d743, type: 2} diff --git a/Assets/Scripts/Door.cs b/Assets/Scripts/Door.cs index ec61c79..b1a2dd9 100644 --- a/Assets/Scripts/Door.cs +++ b/Assets/Scripts/Door.cs @@ -68,10 +68,17 @@ public class Door : MonoBehaviour { /// /// private void OnTriggerExit2D(Collider2D collision) { + // TODO only works correct for entering a room! if ( collision.tag == "Player") { Player player = collision.gameObject.GetComponent(); - if ((boundingBox.offset - parent.GetCenter()).sqrMagnitude < ((Vector2) player.transform.position - parent.GetCenter()).sqrMagnitude) - return; + Vector2 centerToCollider = (Vector2) gameObject.transform.position - parent.GetPosition() + parent.GetCenter(); + Vector2 centerToPlayer = (Vector2) player.gameObject.transform.position - parent.GetPosition() + parent.GetCenter(); + if (centerToCollider.magnitude - 1 < centerToPlayer.magnitude) { + Debug.Log(centerToCollider.magnitude - 1); + Debug.Log(centerToPlayer.magnitude); + return; + } + Debug.Log("Leaving Trigger"); if(parent == null) { Debug.Log("This door has no parent Room!"); diff --git a/Assets/Scripts/GameController.cs b/Assets/Scripts/GameController.cs index f1cf9a3..6c27ab3 100644 --- a/Assets/Scripts/GameController.cs +++ b/Assets/Scripts/GameController.cs @@ -217,6 +217,7 @@ public class GameController : MonoBehaviour { doorRoot.transform.SetParent(goStart.transform); start = goStart.AddComponent(); start.SetCenter(dg.start.GetCenter()); + start.SetPosition(dg.start.roomPosition); lt = lt.FindAll(x => x.tag == "door"); lt.ForEach(x => { x.SetParent(doorRoot.transform); @@ -247,7 +248,7 @@ public class GameController : MonoBehaviour { doorRootf.transform.SetParent(goFinish.transform); ltf = ltf.FindAll(x => x.tag == "door"); finish = goFinish.AddComponent(); - + finish.SetPosition(dg.end.roomPosition); finish.SetCenter(dg.end.GetCenter()); ltf.ForEach(x => { x.SetParent(doorRootf.transform); @@ -279,6 +280,7 @@ public class GameController : MonoBehaviour { doorRootg.transform.SetParent(groom.transform); Room grom = groom.AddComponent(); grom.SetCenter(gr.GetCenter()); + grom.SetPosition(gr.roomPosition); ltg = ltg.FindAll(x => x.tag == "door"); ltg.ForEach(x => { x.SetParent(doorRootg.transform); diff --git a/Assets/Scripts/Room.cs b/Assets/Scripts/Room.cs index d3382ac..8afe8af 100644 --- a/Assets/Scripts/Room.cs +++ b/Assets/Scripts/Room.cs @@ -9,6 +9,7 @@ public class Room : MonoBehaviour { } Vector2Int center; + Vector2Int position; List doors; List spawnpoints; @@ -51,6 +52,14 @@ public class Room : MonoBehaviour { center = v; } + public void SetPosition(Vector2Int v) { + position = v; + } + + public Vector2Int GetPosition() { + return position; + } + /// /// Returns the Center as global from the generation. TODO change this /// Is available after Generation and after Start Phase.