Newer
Older
using System;
using System.Collections;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UIElements;
public class FactSpawner : MonoBehaviour
{
Marco Zimmer
committed
public GameObject
Sphere,
Line,
Ray,
Angle,
Prism,
Cone,
Marco Zimmer
committed
AddFactEvent.AddListener(SpawnFactRepresentation);
AnimateNonExistingFactEvent.AddListener(AnimateNonExistingFactTrigger);
public void SpawnFactRepresentation(Fact fact)
switch (fact)
case PointFact PointFact:
SpawnPoint(PointFact); break;
case LineFact LineFact:
SpawnLine(LineFact); break;
//case RightAngleFact AngleFact: //needed for Hint System
case AbstractAngleFact AngleFact:
SpawnAngle(AngleFact); break;
case RayFact RayFact:
SpawnRay(RayFact); break;
case CircleFact CircleFact:
SpawnRingAndCircle(CircleFact); break;
case AttachedPositionFunction AttachedPositionFunction:
SpawnAttachedPositionFunction(AttachedPositionFunction); break;
case QuadFact QuadFact:
SpawnQuad(QuadFact); break;
case ConeVolumeFact ConeVolumeFact:
SpawnCone(ConeVolumeFact); break;
case TruncatedConeVolumeFact TruncatedConeVolumeFact:
SpawnTruncatedCone(TruncatedConeVolumeFact); break;
default: break;
public void SpawnAttachedPositionFunction(AttachedPositionFunction fact)
var attachedBehaviour = fact.Fact.WorldRepresentation.gameObject
.AddComponent<AttachedPositionFunctionBehaviour>();
attachedBehaviour.Fact = fact;
fact.WorldRepresentation = attachedBehaviour;
}
public void SpawnPoint(PointFact fact)
{
GameObject point = GameObject.Instantiate(Sphere);
fact.WorldRepresentation = point.GetComponent<FactObject3D>();
point.transform.SetPositionAndRotation(fact.Position, fact.Rotation);
public void SpawnLine(LineFact fact)
John Schihada
committed
//Change FactRepresentation to Line
GameObject line = GameObject.Instantiate(Line);
fact.WorldRepresentation = line.GetComponentInChildren<FactObject3D>();
John Schihada
committed
//Place the Line in the centre of the two points
line.transform.position = fact.Position;
//Change scale and rotation, so that the two points are connected by the line
//and without affecting Scale of the Text
line.transform.GetChild(0).localScale = fact.LocalScale;
line.transform.GetChild(0).rotation = fact.Rotation;
John Schihada
committed
fact.Label + " = " + Math.Round(fact.Distance, 2) + " m";
John Schihada
committed
}
public void SpawnRay(RayFact fact)
Marco Zimmer
committed
//Change FactRepresentation to Line
GameObject line = GameObject.Instantiate(Ray);
fact.WorldRepresentation = line.GetComponentInChildren<FactObject3D>();
//Place the Line in the centre of the two points
line.transform.position = fact.Position;
//Change scale and rotation, so that the two points are connected by the line
//and without affecting Scale of the Text
line.transform.GetChild(0).localScale = fact.LocalScale;
line.transform.GetChild(0).rotation = fact.Rotation;
John Schihada
committed
//Spawn an angle: point with id = angleFact.Pid2 is the point where the angle gets applied
public void SpawnAngle(AbstractAngleFact fact)
{
//Change FactRepresentation to Angle
GameObject angle = GameObject.Instantiate(Angle);
fact.WorldRepresentation = angle.GetComponentInChildren<FactObject3D>();
fact.WorldRepresentation.Fact = fact;
angle.transform.SetPositionAndRotation(fact.Position, fact.Rotation);
//Set text of angle
TextMeshPro[] texts = angle.GetComponentsInChildren<TextMeshPro>();
foreach (TextMeshPro t in texts)
{
//Change Text not to the id, but to the angle-value (from both sides)
}
//Generate angle mesh
CircleSegmentGenerator[] segments = angle.GetComponentsInChildren<CircleSegmentGenerator>();
foreach (CircleSegmentGenerator c in segments)
public void SpawnRingAndCircle(CircleFact fact)
var ringAndCircleGO = new GameObject("RingAndCircle");
SpawnRing(fact, ringAndCircleGO.transform);
SpawnCircle(fact, ringAndCircleGO.transform);
ringAndCircleGO.transform.SetPositionAndRotation(fact.Position, fact.Rotation);
fact.WorldRepresentation = ringAndCircleGO.AddComponent<FactObject3D>();
public void SpawnRing(CircleFact circleFact, Transform parent = null)
{
GameObject ring = GameObject.Instantiate(Ring, parent);
TorusGenerator[] tori = ring.GetComponentsInChildren<TorusGenerator>();
torus.torusRadius = circleFact.radius + (float)Math3d.vectorPrecission;
}
public void SpawnCircle(CircleFact circleFact, Transform parent = null)
circle.transform.localScale = Vector3.Scale(circle.transform.localScale, circleFact.LocalScale);
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
public void SpawnQuad(QuadFact fact)
{
GameObject prism = GameObject.Instantiate(Prism);
fact.WorldRepresentation = prism.GetComponentInChildren<FactObject3D>();
fact.WorldRepresentation.Fact = fact;
prism.transform.SetPositionAndRotation(fact.Position, fact.Rotation);
PrismGenerator[] prisms = prism.GetComponentsInChildren<PrismGenerator>();
foreach (var gen in prisms)
gen.Vertices = fact.Points.Select(p => p.Position).ToArray();
}
public void SpawnCone(ConeVolumeFact fact)
{
GameObject prism = GameObject.Instantiate(Cone);
fact.WorldRepresentation = prism.GetComponentInChildren<FactObject3D>();
fact.WorldRepresentation.Fact = fact;
prism.transform.SetPositionAndRotation(fact.Position, fact.Rotation);
ConeGenerator[] prisms = prism.GetComponentsInChildren<ConeGenerator>();
foreach (var gen in prisms)
{
gen.topPosition = fact.Point.Position - fact.Circle.Position;
gen.topRadius = 0f;
gen.bottomRadius = fact.Circle.radius;
}
}
public void SpawnTruncatedCone(TruncatedConeVolumeFact fact)
{
GameObject prism = GameObject.Instantiate(Cone);
fact.WorldRepresentation = prism.GetComponentInChildren<FactObject3D>();
fact.WorldRepresentation.Fact = fact;
prism.transform.SetPositionAndRotation(fact.Position, fact.Rotation);
ConeGenerator[] prisms = prism.GetComponentsInChildren<ConeGenerator>();
foreach (var gen in prisms)
{
gen.topPosition = fact.CircleTop.Position - fact.CircleBase.Position;
gen.topRadius = fact.CircleTop.radius;
gen.bottomRadius = fact.CircleBase.radius;
}
}
public void AnimateNonExistingFactTrigger(Fact fact)
MaZiFAU
committed
{
SpawnFactRepresentation(fact);
yield return new WaitForSeconds(GlobalBehaviour.HintAnimationDuration);
MaZiFAU
committed
}
John Schihada
committed
}
MaZiFAU
committed