From 285477b33310fe14c3dfde68bbdecb57eb4d5475 Mon Sep 17 00:00:00 2001
From: Richard Marcus <richard.marcus@fau.de>
Date: Wed, 19 Feb 2020 23:16:34 +0100
Subject: [PATCH] finalized online prototype

---
 Assets/FactManager.cs                   | 68 +++++++++++++++++++++++--
 Assets/InteractionEngine/FactSpawner.cs |  2 +-
 Assets/InteractionEngine/WorldCursor.cs |  4 +-
 Assets/InventoryStuff/DisplayFacts.cs   | 15 +++++-
 Assets/TreeWorld_02.unity               |  4 ++
 ProjectSettings/TagManager.asset        |  2 +-
 6 files changed, 86 insertions(+), 9 deletions(-)

diff --git a/Assets/FactManager.cs b/Assets/FactManager.cs
index 9d5d6007..8451cc07 100644
--- a/Assets/FactManager.cs
+++ b/Assets/FactManager.cs
@@ -61,8 +61,52 @@ RayFact AddRayFact(int pid1, int pid2, int id)
         oLid = GetFirstEmptyID();
         Facts.Insert(oLid, new OnLineFact(oLid, pid2, id));
 
-        //TODO: check for more points, question: should MMT do this instead?
+        var p1 = Facts.Find(x => x.Id == pid1);
+        var p2 = Facts.Find(x => x.Id == pid2);
 
+        Vector3 dir = p2.Representation.transform.position - p1.Representation.transform.position;
+
+    
+
+        // Bit shift the index of the layer Point to get a bit mask
+        int layerMask = 1 << LayerMask.NameToLayer("Point");
+        // This casts rays only against colliders in layer 8
+
+
+        RaycastHit[] hits;
+        hits = Physics.RaycastAll(p1.Representation.transform.position - dir * 1000, dir, Mathf.Infinity, layerMask);
+
+        Debug.Log(hits.Length + " hits");
+        for (int i = 0; i < hits.Length; i++)
+        {
+            RaycastHit hit = hits[i];
+
+            bool exists = false;
+
+            foreach (Fact fact in Facts)
+            {
+                if (typeof(OnLineFact).IsInstanceOfType(fact))
+                {
+                    OnLineFact oLFact = (OnLineFact)fact;
+                    if ((oLFact.Lid == id && oLFact.Pid == hit.transform.gameObject.GetComponent<FactObject>().Id))
+                    {
+                        exists = true;
+                        break;
+                    }
+                }
+            }
+
+
+            if (!exists)
+            {
+                oLid = GetFirstEmptyID();
+                var olF = new OnLineFact(oLid, hit.transform.gameObject.GetComponent<FactObject>().Id, id);
+                Facts.Insert(oLid, olF);
+            }
+
+
+        }
+        
         return Facts.Find(x => x.Id == id) as RayFact;
     }
 
@@ -122,7 +166,8 @@ public void OnToolModeChanged(ToolMode ActiveToolMode)
                 {
                 
                     GameObject gO = fact.Representation;
-                    if ((gO.layer == LayerMask.NameToLayer("Line")))
+                    if (gO == null) continue;
+                    if ((gO.layer == LayerMask.NameToLayer("Ray")))
                             gO.GetComponentInChildren<Collider>().enabled = true;
                 }
                 break;
@@ -136,7 +181,8 @@ public void OnToolModeChanged(ToolMode ActiveToolMode)
                 foreach (Fact fact in Facts)
                 {
                     GameObject gO = fact.Representation;
-                    if (gO.layer == LayerMask.NameToLayer("Line") || gO.layer == LayerMask.NameToLayer("Angle"))
+                    if (gO == null) continue;
+                    if (gO.layer == LayerMask.NameToLayer("Line") || gO.layer == LayerMask.NameToLayer("Angle")|| gO.layer == LayerMask.NameToLayer("Ray"))
                     {
                         gO.GetComponentInChildren<Collider>().enabled = false;
                     }
@@ -155,6 +201,7 @@ public void OnToolModeChanged(ToolMode ActiveToolMode)
                 foreach (Fact fact in Facts)
                 {
                     GameObject gO = fact.Representation;
+                    if (gO == null) continue;
                     if (gO.layer == LayerMask.NameToLayer("Line") || gO.layer == LayerMask.NameToLayer("Angle"))
                     {
                         gO.GetComponentInChildren<Collider>().enabled = false;
@@ -291,7 +338,16 @@ public void OnHit(RaycastHit hit)
         {
             //If Left-Mouse-Button was pressed in MarkPointMode
             case ToolMode.MarkPointMode:
-                CommunicationEvents.AddFactEvent.Invoke(this.AddPointFact(hit, this.GetFirstEmptyID()));
+
+                var pid = this.GetFirstEmptyID();
+                CommunicationEvents.AddFactEvent.Invoke(this.AddPointFact(hit,pid ));
+                if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Ray")){
+
+                    var oLid = GetFirstEmptyID();
+                    Facts.Insert(oLid, new OnLineFact(oLid, pid, hit.transform.GetComponent<FactObject>().Id));
+                }
+
+
                 break;
 
             //same as for linemode atm
@@ -313,7 +369,11 @@ public void OnHit(RaycastHit hit)
                             if(ActiveToolMode==ToolMode.CreateLineMode)
                                 CommunicationEvents.AddFactEvent.Invoke(this.AddLineFact(this.lineModeFirstPointSelected.Id, tempFact.Id, this.GetFirstEmptyID()));
                             else
+                            {
                                 CommunicationEvents.AddFactEvent.Invoke(this.AddRayFact(this.lineModeFirstPointSelected.Id, tempFact.Id, this.GetFirstEmptyID()));
+                               
+                            }
+                               
 
                         this.lineModeIsFirstPointSelected = false;
                         this.lineModeFirstPointSelected = null;
diff --git a/Assets/InteractionEngine/FactSpawner.cs b/Assets/InteractionEngine/FactSpawner.cs
index 5797a466..c501bdfb 100644
--- a/Assets/InteractionEngine/FactSpawner.cs
+++ b/Assets/InteractionEngine/FactSpawner.cs
@@ -116,7 +116,7 @@ public void SpawnRay(RayFact lineFact)
         point2 += dir * 100;
 
          //Change FactRepresentation to Line
-         this.FactRepresentation = (GameObject)Resources.Load("Prefabs/Line", typeof(GameObject));
+         this.FactRepresentation = (GameObject)Resources.Load("Prefabs/Ray", typeof(GameObject));
         GameObject line = GameObject.Instantiate(FactRepresentation);
         //Place the Line in the centre of the two points
         line.transform.position = Vector3.Lerp(point1, point2, 0.5f);
diff --git a/Assets/InteractionEngine/WorldCursor.cs b/Assets/InteractionEngine/WorldCursor.cs
index 79388502..09a8e8f4 100644
--- a/Assets/InteractionEngine/WorldCursor.cs
+++ b/Assets/InteractionEngine/WorldCursor.cs
@@ -68,10 +68,10 @@ void Update()
             // Debug.Log(Hit.transform.tag);
             if (Hit.collider.transform.CompareTag("SnapZone"))
             {
-                if(Hit.collider.gameObject.layer == LayerMask.NameToLayer("Line")){
+                if(Hit.collider.gameObject.layer == LayerMask.NameToLayer("Ray")){
 
                     int id = Hit.collider.gameObject.GetComponent<FactObject>().Id;
-                    LineFact lineFact = CommunicationEvents.Facts.Find((x => x.Id == id)) as LineFact;
+                    RayFact lineFact = CommunicationEvents.Facts.Find((x => x.Id == id)) as RayFact;
                     PointFact p1 =  CommunicationEvents.Facts.Find((x => x.Id == lineFact.Pid1)) as PointFact;
                     PointFact p2 = CommunicationEvents.Facts.Find((x => x.Id == lineFact.Pid2)) as PointFact;
 
diff --git a/Assets/InventoryStuff/DisplayFacts.cs b/Assets/InventoryStuff/DisplayFacts.cs
index c683ecd8..6462343f 100644
--- a/Assets/InventoryStuff/DisplayFacts.cs
+++ b/Assets/InventoryStuff/DisplayFacts.cs
@@ -13,6 +13,8 @@ public class DisplayFacts : MonoBehaviour
     public GameObject prefab_Distance;
     public GameObject prefab_Angle;
     public GameObject prefab_Default;
+    public GameObject prefab_OnLine;
+    public GameObject prefab_Line;
 
     public int x_Start;
     public int y_Start;
@@ -65,7 +67,7 @@ private GameObject CreateDisplay(Transform transform, Fact fact)
                 }
             case RayFact f:
                 {
-                    var obj = Instantiate(prefab_Distance, Vector3.zero, Quaternion.identity, transform);
+                    var obj = Instantiate(prefab_Line, Vector3.zero, Quaternion.identity, transform);
                     obj.transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>().text = "" + getLetter(CommunicationEvents.Facts[f.Pid1].Id);
                     obj.transform.GetChild(1).gameObject.GetComponent<TextMeshProUGUI>().text = "" + getLetter(CommunicationEvents.Facts[f.Pid2].Id);
                     obj.GetComponent<FactWrapper>().fact = f;
@@ -89,6 +91,17 @@ private GameObject CreateDisplay(Transform transform, Fact fact)
                     obj.GetComponent<FactWrapper>().fact = f;
                     return obj;
                 }
+            case OnLineFact f:
+                {
+                    var obj = Instantiate(prefab_OnLine, Vector3.zero, Quaternion.identity, transform);
+                    obj.transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>().text = "" + getLetter(CommunicationEvents.Facts[f.Lid].Id);
+                    obj.transform.GetChild(1).gameObject.GetComponent<TextMeshProUGUI>().text = "" + getLetter(CommunicationEvents.Facts[f.Pid].Id);
+                    obj.GetComponent<FactWrapper>().fact = f;
+                    return obj;
+                }
+
+
+
             default:
                 {
                     var obj = Instantiate(prefab_Default, Vector3.zero, Quaternion.identity, transform);
diff --git a/Assets/TreeWorld_02.unity b/Assets/TreeWorld_02.unity
index 62f195a7..b90350e6 100644
--- a/Assets/TreeWorld_02.unity
+++ b/Assets/TreeWorld_02.unity
@@ -41807,6 +41807,10 @@ MonoBehaviour:
     type: 3}
   prefab_Default: {fileID: 858001163752551619, guid: f019e9f67e8dab947bc60028223b6cec,
     type: 3}
+  prefab_OnLine: {fileID: 6050914136827205310, guid: 496d04605465c7f4dba7638cc6e2dc6e,
+    type: 3}
+  prefab_Line: {fileID: 7510387096843212865, guid: d6ee990520a44954fb494468665d19e9,
+    type: 3}
   x_Start: -125
   y_Start: 475
   X_Pacece_Between_Items: 105
diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset
index e1e6d97f..76c9a3ba 100644
--- a/ProjectSettings/TagManager.asset
+++ b/ProjectSettings/TagManager.asset
@@ -21,7 +21,7 @@ TagManager:
   - Point
   - Line
   - Angle
-  - 
+  - Ray
   - 
   - 
   - 
-- 
GitLab