diff --git a/Assets/AngleTool.cs b/Assets/AngleTool.cs index f1f4eb440cbf28b40d4fdb1088ed3a75dec9c153..d6db88f1ada18c6ad761024b521f2319f156b25a 100644 --- a/Assets/AngleTool.cs +++ b/Assets/AngleTool.cs @@ -6,80 +6,186 @@ public class AngleTool : Gadget { + //Variables for AngleMode distinction + private bool angleModeIsFirstPointSelected = false; + private PointFact angleModeFirstPointSelected = null; + private bool angleModeIsSecondPointSelected = false; + private PointFact angleModeSecondPointSelected = null; + + //Attributes for simulating the drawing of a curve + private bool curveDrawingActivated; + public WorldCursor Cursor; + public LineRenderer lineRenderer; + private List<Vector3> linePositions = new List<Vector3>(); + public Material anglePreviewMaterial; + + //Vertices for the Curve + private int curveDrawingVertexCount = 36; + private Vector3 curveEndPoint; + private Vector3 angleMiddlePoint; + private float curveRadius; + + //Initialize Gadget when enabled AND activated + void OnEnable() + { + this.ResetGadget(); + } + + void Start() + { + if (FactManager == null) FactManager = GameObject.FindObjectOfType<FactManager>(); + CommunicationEvents.TriggerEvent.AddListener(OnHit); + if (this.Cursor == null) this.Cursor = GameObject.FindObjectOfType<WorldCursor>(); + } + public override void OnHit(RaycastHit hit) { if (!this.isActiveAndEnabled) return; if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Point")) { - Fact tempFact = Facts[hit.transform.GetComponent<FactObject>().Id]; + PointFact tempFact = (PointFact)Facts[hit.transform.GetComponent<FactObject>().Id]; //If two points were already selected and now the third point got selected - if (FactManager.angleModeIsFirstPointSelected && FactManager.angleModeIsSecondPointSelected) + if (this.angleModeIsFirstPointSelected && this.angleModeIsSecondPointSelected) { - //Event for end of curve-drawing in "ShinyThings" - CommunicationEvents.StopCurveDrawingEvent.Invoke(null); //Create AngleFact //Check if new Point is equal to one of the previous points -> if true -> cancel - if (!(FactManager.angleModeFirstPointSelected.Id == tempFact.Id || FactManager.angleModeSecondPointSelected.Id == tempFact.Id)) + if (!(this.angleModeFirstPointSelected.Id == tempFact.Id || this.angleModeSecondPointSelected.Id == tempFact.Id)) { //Check if exactly the same angle already exists - if (!FactManager.factAlreadyExists(new int[] { ((PointFact)FactManager.angleModeFirstPointSelected).Id, ((PointFact)FactManager.angleModeSecondPointSelected).Id, ((PointFact)tempFact).Id })) - CommunicationEvents.AddFactEvent.Invoke(FactManager.AddAngleFact(((PointFact)FactManager.angleModeFirstPointSelected).Id, ((PointFact)FactManager.angleModeSecondPointSelected).Id, ((PointFact)tempFact).Id, FactManager.GetFirstEmptyID())); + if (!FactManager.factAlreadyExists(new int[] { ((PointFact)this.angleModeFirstPointSelected).Id, ((PointFact)this.angleModeSecondPointSelected).Id, ((PointFact)tempFact).Id })) + CommunicationEvents.AddFactEvent.Invoke(FactManager.AddAngleFact(((PointFact)this.angleModeFirstPointSelected).Id, ((PointFact)this.angleModeSecondPointSelected).Id, ((PointFact)tempFact).Id, FactManager.GetFirstEmptyID())); } - FactManager.angleModeIsFirstPointSelected = false; - FactManager.angleModeFirstPointSelected = null; - FactManager.angleModeIsSecondPointSelected = false; - FactManager.angleModeSecondPointSelected = null; + ResetGadget(); } //If only one point was already selected - else if (FactManager.angleModeIsFirstPointSelected && !FactManager.angleModeIsSecondPointSelected) + else if (this.angleModeIsFirstPointSelected && !this.angleModeIsSecondPointSelected) { //Check if the 2 selected points are the same: If not - if (FactManager.angleModeFirstPointSelected.Id != tempFact.Id) + if (this.angleModeFirstPointSelected.Id != tempFact.Id) { - FactManager.angleModeIsSecondPointSelected = true; - FactManager.angleModeSecondPointSelected = tempFact; - - //Event for start of curve-drawing in "ShinyThings" - //Create new LineFact with the 2 points - LineFact tempLineFact = new LineFact(); - tempLineFact.Pid1 = FactManager.angleModeFirstPointSelected.Id; - tempLineFact.Pid2 = FactManager.angleModeSecondPointSelected.Id; - CommunicationEvents.StartCurveDrawingEvent.Invoke(tempLineFact); + this.angleModeIsSecondPointSelected = true; + this.angleModeSecondPointSelected = tempFact; + + ActivateCurveDrawing(); } else { - FactManager.angleModeFirstPointSelected = null; - FactManager.angleModeIsFirstPointSelected = false; + this.angleModeFirstPointSelected = null; + this.angleModeIsFirstPointSelected = false; } } //If no point was selected before else { //Save the first point selected - FactManager.angleModeIsFirstPointSelected = true; - FactManager.angleModeFirstPointSelected = tempFact; + this.angleModeIsFirstPointSelected = true; + this.angleModeFirstPointSelected = tempFact; } } //No point was hit else { - if (FactManager.angleModeIsFirstPointSelected && FactManager.angleModeIsSecondPointSelected) - { - //Event for end of curve-drawing in "ShinyThings" - CommunicationEvents.StopCurveDrawingEvent.Invoke(null); - } - - //Reset Angle-Preview-Attributes - FactManager.angleModeIsFirstPointSelected = false; - FactManager.angleModeFirstPointSelected = null; - FactManager.angleModeIsSecondPointSelected = false; - FactManager.angleModeSecondPointSelected = null; + ResetGadget(); //TODO: Hint that only an angle can be created between 3 already existing points } } - + void Update() + { + if (!this.isActiveAndEnabled) return; + if (this.curveDrawingActivated) + UpdateCurveDrawing(this.Cursor.transform.position); + } + + private void ResetGadget() + { + this.angleModeIsFirstPointSelected = false; + this.angleModeFirstPointSelected = null; + this.angleModeIsSecondPointSelected = false; + this.angleModeSecondPointSelected = null; + DeactivateCurveDrawing(); + } + + //Expect a LineFact here, where Line.Pid2 will be the Basis-Point of the angle + public void ActivateCurveDrawing() + { + //In AngleMode with 3 Points we want to draw nearly a rectangle so we add a startPoint and an Endpoint to this preview + this.lineRenderer.positionCount = curveDrawingVertexCount + 2; + this.lineRenderer.material = this.anglePreviewMaterial; + + lineRenderer.startWidth = 0.05f; + lineRenderer.endWidth = 0.05f; + + //Set CurveDrawing activated + this.curveDrawingActivated = true; + + //curveEndPoint is a point on the Line selected, with some distance from point2 + curveEndPoint = angleModeSecondPointSelected.Point + 0.3f * (angleModeFirstPointSelected.Point - angleModeSecondPointSelected.Point).magnitude * (angleModeFirstPointSelected.Point - angleModeSecondPointSelected.Point).normalized; + + angleMiddlePoint = angleModeSecondPointSelected.Point; + curveRadius = (curveEndPoint - angleModeSecondPointSelected.Point).magnitude; + } + + public void UpdateCurveDrawing(Vector3 currentPosition) + { + + //Find the nearest of all potential third points + PointFact nearestPoint = null; + foreach (Fact fact in Facts) + { + if (fact is PointFact && fact.Id != angleModeFirstPointSelected.Id && fact.Id != angleModeSecondPointSelected.Id && nearestPoint == null) + nearestPoint = (PointFact)fact; + else if (fact is PointFact && fact.Id != angleModeFirstPointSelected.Id && fact.Id != angleModeSecondPointSelected.Id && (nearestPoint.Point - currentPosition).magnitude > (((PointFact)fact).Point - currentPosition).magnitude) + nearestPoint = (PointFact)fact; + } + + Vector3 startPoint = new Vector3(0, 0, 0); + + if (nearestPoint != null) + { + Vector3 planePoint = Vector3.ProjectOnPlane(currentPosition, Vector3.Cross((nearestPoint.Point - angleMiddlePoint), (curveEndPoint - angleMiddlePoint))); + + //Determine the Start-Point for the nearest third-point + startPoint = angleMiddlePoint + curveRadius * (planePoint - angleMiddlePoint).normalized; + } + else + { + //Determine the Start-Point + startPoint = angleMiddlePoint + curveRadius * (currentPosition - angleMiddlePoint).normalized; + } + + //Determine the Center of Start-Point and End-Point + Vector3 tempCenterPoint = Vector3.Lerp(startPoint, curveEndPoint, 0.5f); + Vector3 curveMiddlePoint = angleMiddlePoint + curveRadius * (tempCenterPoint - angleMiddlePoint).normalized; + + linePositions = new List<Vector3>(); + //Start: AngleMiddlePoint -> FirstPoint of Curve + linePositions.Add(angleModeSecondPointSelected.Point); + + for (float ratio = 0; ratio <= 1; ratio += 1.0f / this.curveDrawingVertexCount) + { + var tangentLineVertex1 = Vector3.Lerp(startPoint, curveMiddlePoint, ratio); + var tangentLineVertex2 = Vector3.Lerp(curveMiddlePoint, curveEndPoint, ratio); + var bezierPoint = Vector3.Lerp(tangentLineVertex1, tangentLineVertex2, ratio); + linePositions.Add(bezierPoint); + } + + //End: LastPoint of Curve -> AngleMiddlePoint + linePositions.Add(angleModeSecondPointSelected.Point); + + lineRenderer.positionCount = linePositions.Count; + lineRenderer.SetPositions(linePositions.ToArray()); + + } + + public void DeactivateCurveDrawing() + { + this.lineRenderer.positionCount = 0; + this.linePositions = new List<Vector3>(); + this.curveDrawingActivated = false; + } + } diff --git a/Assets/InteractionEngine/FactManager.cs b/Assets/InteractionEngine/FactManager.cs index d734f61456c236ac339f070942a226c67e4c2a5d..3f2fa82082663763e797d7c0aceac469726e43e0 100644 --- a/Assets/InteractionEngine/FactManager.cs +++ b/Assets/InteractionEngine/FactManager.cs @@ -9,12 +9,6 @@ public class FactManager : MonoBehaviour public GameObject SmartMenu; private List<int> NextEmpties = new List<int>(); - //Variables for AngleMode distinction - public bool angleModeIsFirstPointSelected = false; - public Fact angleModeFirstPointSelected = null; - public bool angleModeIsSecondPointSelected = false; - public Fact angleModeSecondPointSelected = null; - //Solving game parameters public GameObject snapZoneTop; public GameObject snapZoneBottom; diff --git a/Assets/InteractionEngine/ShinyThings.cs b/Assets/InteractionEngine/ShinyThings.cs index 98401fd7e4f3d54fcfcf337edadd32926564e558..3b4d5fc4feba76669df3dc99027f4500bf70ac0b 100644 --- a/Assets/InteractionEngine/ShinyThings.cs +++ b/Assets/InteractionEngine/ShinyThings.cs @@ -14,20 +14,6 @@ public class ShinyThings : MonoBehaviour public Material defaultMaterial; public Material highlightMaterial; - - //Attributes for simulating the drawing of a line/curve - public LineRenderer lineRenderer; - private List<Vector3> linePositions = new List<Vector3>(); - public Material anglePreviewMaterial; - - private bool curveDrawingActivated; - //These are only the vertices for the Curve - private int curveDrawingVertexCount = 36; - private LineFact curveDrawingStartLine; - private Vector3 curveEndPoint; - private Vector3 angleMiddlePoint; - private float curveRadius; - //Variables for Pushout-Highlighting private Fact highlightedPushoutFact; private GameObject extraHighlight; @@ -53,9 +39,6 @@ public class ShinyThings : MonoBehaviour public void Start() { if (Cursor == null) Cursor = GetComponent<WorldCursor>(); - CommunicationEvents.StartCurveDrawingEvent.AddListener(ActivateCurveDrawing); - CommunicationEvents.StopCurveDrawingEvent.AddListener(DeactivateCurveDrawing); - CommunicationEvents.StopPreviewsEvent.AddListener(StopPreviews); CommunicationEvents.PushoutFactEvent.AddListener(StartPushoutFactHighlighting); CommunicationEvents.PushoutFactFailEvent.AddListener(StartPushoutFactFailHighlighting); speedSlowDown = timerDurationEnd * 10; @@ -84,9 +67,6 @@ public void Update() //@John before: hit.point //Debug.Log(this.transform.position); - - if (this.curveDrawingActivated) - UpdateCurveDrawing(this.transform.position); //If the Timer is Active, check Pushout-Highlighting if (this.timerActive) @@ -365,92 +345,4 @@ public void slowDownAnimation(ParticleSystem main1, ParticleSystem main2) { } } } - - //Expect a LineFact here, where Line.Pid2 will be the Basis-Point of the angle - public void ActivateCurveDrawing(Fact startFact) - { - //In AngleMode with 3 Points we want to draw nearly a rectangle so we add a startPoint and an Endpoint to this preview - this.lineRenderer.positionCount = curveDrawingVertexCount + 2; - this.lineRenderer.material = this.anglePreviewMaterial; - - lineRenderer.startWidth = 0.05f; - lineRenderer.endWidth = 0.05f; - - //Set CurveDrawing activated - this.curveDrawingActivated = true; - - curveDrawingStartLine = (LineFact)startFact; - PointFact curveDrawingPoint1 = (PointFact)Facts.Find(x => x.Id == curveDrawingStartLine.Pid1); - PointFact curveDrawingPoint2 = (PointFact)Facts.Find(x => x.Id == curveDrawingStartLine.Pid2); - - //curveEndPoint is a point on the Line selected, with some distance from point2 - curveEndPoint = curveDrawingPoint2.Point + 0.3f * (curveDrawingPoint1.Point - curveDrawingPoint2.Point).magnitude * (curveDrawingPoint1.Point - curveDrawingPoint2.Point).normalized; - - angleMiddlePoint = curveDrawingPoint2.Point; - curveRadius = (curveEndPoint - curveDrawingPoint2.Point).magnitude; - } - - public void UpdateCurveDrawing(Vector3 currentPosition) - { - - //Find the nearest of all potential third points - PointFact nearestPoint = null; - foreach (Fact fact in Facts) { - if (fact is PointFact && fact.Id != curveDrawingStartLine.Pid1 && fact.Id != curveDrawingStartLine.Pid2 && nearestPoint == null) - nearestPoint = (PointFact)fact; - else if (fact is PointFact && fact.Id != curveDrawingStartLine.Pid1 && fact.Id != curveDrawingStartLine.Pid2 && (nearestPoint.Point - currentPosition).magnitude > (((PointFact)fact).Point - currentPosition).magnitude) - nearestPoint = (PointFact)fact; - } - - Vector3 startPoint = new Vector3(0,0,0); - - if (nearestPoint != null) - { - Vector3 planePoint = Vector3.ProjectOnPlane(currentPosition, Vector3.Cross((nearestPoint.Point-angleMiddlePoint), (curveEndPoint-angleMiddlePoint))); - - //Determine the Start-Point for the nearest third-point - startPoint = angleMiddlePoint + curveRadius * (planePoint - angleMiddlePoint).normalized; - } - else - { - //Determine the Start-Point - startPoint = angleMiddlePoint + curveRadius * (currentPosition - angleMiddlePoint).normalized; - } - - //Determine the Center of Start-Point and End-Point - Vector3 tempCenterPoint = Vector3.Lerp(startPoint, curveEndPoint, 0.5f); - Vector3 curveMiddlePoint = angleMiddlePoint + curveRadius * (tempCenterPoint - angleMiddlePoint).normalized; - - linePositions = new List<Vector3>(); - //Start: AngleMiddlePoint -> FirstPoint of Curve - linePositions.Add(((PointFact)Facts.Find(x => x.Id == curveDrawingStartLine.Pid2)).Point); - - for (float ratio = 0; ratio <= 1; ratio += 1.0f / this.curveDrawingVertexCount) - { - var tangentLineVertex1 = Vector3.Lerp(startPoint, curveMiddlePoint, ratio); - var tangentLineVertex2 = Vector3.Lerp(curveMiddlePoint, curveEndPoint, ratio); - var bezierPoint = Vector3.Lerp(tangentLineVertex1, tangentLineVertex2, ratio); - linePositions.Add(bezierPoint); - } - - //End: LastPoint of Curve -> AngleMiddlePoint - linePositions.Add(((PointFact)Facts.Find(x => x.Id == curveDrawingStartLine.Pid2)).Point); - - lineRenderer.positionCount = linePositions.Count; - lineRenderer.SetPositions(linePositions.ToArray()); - - } - - public void DeactivateCurveDrawing(Fact startFact) - { - - this.lineRenderer.positionCount = 0; - this.linePositions = new List<Vector3>(); - this.curveDrawingActivated = false; - } - - public void StopPreviews(Fact startFact) { - if (curveDrawingActivated) - DeactivateCurveDrawing(null); - } } diff --git a/Assets/LineTool.cs b/Assets/LineTool.cs index 09a1e61eb5f61b232f9d9912a410ecd25e59feb3..ea72df96169808468b6c3d1e3c3a45cfef70bc51 100644 --- a/Assets/LineTool.cs +++ b/Assets/LineTool.cs @@ -43,7 +43,6 @@ public override void OnHit(RaycastHit hit) //If first point was already selected AND second point != first point if (this.LineModeIsFirstPointSelected && this.LineModeFirstPointSelected.Id != tempFact.Id) { - this.DeactivateLineDrawing(); //Create LineFact //Check if exactly the same line/distance already exists if (!FactManager.factAlreadyExists(new int[] { this.LineModeFirstPointSelected.Id, tempFact.Id })) @@ -110,7 +109,6 @@ public override void OnHit(RaycastHit hit) { //Deactivate LineDrawing and first point selection this.ResetGadget(); - this.DeactivateLineDrawing(); } //TODO: Hint that only a line can be drawn between already existing points diff --git a/Assets/Tape.cs b/Assets/Tape.cs index f9e254782204e1ae620c9f1b4d3fc2ef0abd1cd1..e29372f2e049a63d4ce75c820215923ad3080b82 100644 --- a/Assets/Tape.cs +++ b/Assets/Tape.cs @@ -5,7 +5,7 @@ public class Tape : Gadget { - //Variables for LineMode distinction + //Variables for TapeMode distinction private bool TapeModeIsFirstPointSelected = false; private Fact TapeModeFirstPointSelected = null; @@ -43,7 +43,6 @@ public override void OnHit(RaycastHit hit) //If first point was already selected AND second point != first point if (this.TapeModeIsFirstPointSelected && this.TapeModeFirstPointSelected.Id != tempFact.Id) { - this.DeactivateLineDrawing(); //Create LineFact //Check if exactly the same line/distance already exists if (!FactManager.factAlreadyExists(new int[] { this.TapeModeFirstPointSelected.Id, tempFact.Id })) @@ -110,7 +109,6 @@ public override void OnHit(RaycastHit hit) { //Deactivate LineDrawing and first point selection this.ResetGadget(); - this.DeactivateLineDrawing(); } //TODO: Hint that only a line can be drawn between already existing points diff --git a/Assets/TreeWorld_02.unity b/Assets/TreeWorld_02.unity index 9b5a621a670856681f9fa3cac2dbe0c3b5dbbddf..f4e50110578ab97b7019e12836787f46b63651eb 100644 --- a/Assets/TreeWorld_02.unity +++ b/Assets/TreeWorld_02.unity @@ -31463,9 +31463,6 @@ MonoBehaviour: Cursor: {fileID: 1661088666} defaultMaterial: {fileID: 2100000, guid: 8ae9adf4dc782964387385c1e8c0eb72, type: 2} highlightMaterial: {fileID: 2100000, guid: c7daa82e15f0cf04d92d0f41ce84f9df, type: 2} - lineRenderer: {fileID: 1661088668} - anglePreviewMaterial: {fileID: 2100000, guid: 8a28cccde2536794c97ec91954e34e90, - type: 2} directionalLight: {fileID: 138245305} pushoutMaterial: {fileID: 2100000, guid: d9c43ce51f1a01d41a18fae03c0d406c, type: 2} --- !u!114 &1661088670 @@ -31482,8 +31479,6 @@ MonoBehaviour: m_EditorClassIdentifier: SmartMenu: {fileID: 5601740127768851631, guid: e693bf633c633d243b0254d117ec3893, type: 3} - angleModeIsFirstPointSelected: 0 - angleModeIsSecondPointSelected: 0 snapZoneTop: {fileID: 1563243733} snapZoneBottom: {fileID: 1009368148} --- !u!23 &1661088671 stripped @@ -40476,6 +40471,18 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 0} m_Modifications: + - target: {fileID: 729203331, guid: 2ba8d552442ba664e8e567adee683a11, type: 3} + propertyPath: Cursor + value: + objectReference: {fileID: 1661088666} + - target: {fileID: 729203331, guid: 2ba8d552442ba664e8e567adee683a11, type: 3} + propertyPath: lineRenderer + value: + objectReference: {fileID: 1661088668} + - target: {fileID: 729203331, guid: 2ba8d552442ba664e8e567adee683a11, type: 3} + propertyPath: anglePreviewMaterial + value: + objectReference: {fileID: 2100000, guid: 8a28cccde2536794c97ec91954e34e90, type: 2} - target: {fileID: 1085307962, guid: 2ba8d552442ba664e8e567adee683a11, type: 3} propertyPath: GadgetUI value: