Newer
Older
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.Mathematics;
public class FactSpawner : MonoBehaviour
{
Marco Zimmer
committed
public GameObject
Sphere,
Marco Zimmer
committed
Line,
Ray,
Angle,
Prism,
Cone,
Marco Zimmer
committed
private void OnEnable()
AddFactEvent.AddListener(SpawnFactRepresentation);
AnimateNonExistingFactEvent.AddListener(AnimateNonExistingFactTrigger);
StartT0Event.AddListener(AnimateFunctionCalls);
}
private void OnDisable()
{
AddFactEvent.RemoveListener(SpawnFactRepresentation);
AnimateNonExistingFactEvent.RemoveListener(AnimateNonExistingFactTrigger);
StartT0Event.RemoveListener(AnimateFunctionCalls);
public void SpawnFactRepresentation(Fact fact)
switch (fact)
case PointFact PointFact:
SpawnPoint(PointFact); break;
case TestPointFact TestPointFact:
SpawnTestPoint(TestPointFact); 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 QuadFact QuadFact:
SpawnQuad(QuadFact); break;
case TriangleFact TrisFact:
SpawnTris(TrisFact); break;
case ConeVolumeFact ConeVolumeFact:
SpawnCone(ConeVolumeFact); break;
case TruncatedConeVolumeFact TruncatedConeVolumeFact:
SpawnTruncatedCone(TruncatedConeVolumeFact); break;
case RectangleFact rectangleFact:
SpawnRectangle(rectangleFact); break;
case TriangleFact2 triangleFact:
SpawnTriangle(triangleFact); break;
default: break;
public void SpawnRectangle(RectangleFact fact){
GameObject rectangle = GameObject.Instantiate(Rectangle);
rectangle.transform.SetPositionAndRotation(fact.Position, fact.Rotation);
rectangle.transform.localScale = Vector3.Scale(rectangle.transform.localScale, fact.LocalScale);
rectangle.GetComponentInChildren<TextMeshPro>().text = fact.GetLabel(StageStatic.stage.factState) + " = " + System.Math.Round(fact.Area, 2) + "m²";
public void SpawnTriangle(TriangleFact2 fact)
{
GameObject triangle = GameObject.Instantiate(Triangle);
triangle.transform.SetPositionAndRotation(fact.Position, fact.Rotation);
triangle.transform.localScale = Vector3.Scale(triangle.transform.localScale, fact.LocalScale);
triangle.GetComponentInChildren<TextMeshPro>().text = fact.GetLabel(StageStatic.stage.factState) + " = " + System.Math.Round(fact.Area, 2) + "m²";
}
public void SpawnPoint(PointFact fact)
{
GameObject point = GameObject.Instantiate(Sphere);
fact.WorldRepresentation = point.GetComponent<FactObject3D>();
point.transform.SetPositionAndRotation(fact.Position, fact.Rotation);
public void SpawnTestPoint(TestPointFact fact)
{
GameObject point = GameObject.Instantiate(TestPoint);
fact.WorldRepresentation = point.GetComponent<FactObject3D>();
fact.WorldRepresentation.Fact = fact;
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>();
fact.WorldRepresentation.Fact = fact;
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.GetLabel(StageStatic.stage.factState) + " = " + System.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>();
fact.WorldRepresentation.Fact = fact;
//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)
t.text = System.Math.Round((float)fact.angle, 2) + "°";
}
//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);
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 SpawnTris(TriangleFact 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.Verticies;
}
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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);
fact.freeAutoLabel(StageStatic.stage.factState);
}
}
public void AnimateFunctionCalls()
{
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
StartCoroutine(_AnimateFunctionCalls());
IEnumerator _AnimateFunctionCalls()
{
float trigger_time = Time.time;
Queue<FunctionCallFact> FCFs = new(
StageStatic.stage.factState.MyFactSpace.Values
.Where(f => f is FunctionCallFact)
.Select(f => f as FunctionCallFact)
.OrderBy(f => f.Domain.t_0)
);
FCFs.TryDequeue(out FunctionCallFact up_next);
while (up_next != null)
{
float curren_time = Time.time - trigger_time;
while (up_next != null && up_next.Domain.t_0 <= curren_time)
{
if (up_next.Domain.t_n <= curren_time)
{
FCFs.TryDequeue(out up_next);
continue;
}
StartCoroutine(__BlossomDragAndDie(up_next));
FCFs.TryDequeue(out up_next);
}
yield return null;
}
#pragma warning disable CS8321 // Die lokale Funktion ist deklariert, wird aber nie verwendet.
IEnumerator __BlossomAndDiePerFrame(FunctionCallFact FCF)
{
float current_time = Time.time - trigger_time;
while (current_time <= FCF.Domain.t_n
&& current_time <= FCF.Domain.t_0 + MaxAnimationDuration)
{
object[] result = FCF.Call(current_time);
List<Fact> factlist = new();
foreach (object i in result)
Fact.MakeFact(factlist, i, null, true);
foreach (Fact fact in factlist)
SpawnFactRepresentation(fact);
yield return null;
foreach (Fact fact in factlist)
{
RemoveFactEvent.Invoke(fact);
fact.freeAutoLabel(StageStatic.stage.factState);
}
current_time = Time.time - trigger_time;
}
}
#pragma warning restore CS8321 // Die lokale Funktion ist deklariert, wird aber nie verwendet.
IEnumerator __BlossomDragAndDie(FunctionCallFact FCF)
{
List<Fact> factlist = new();
object[] result = FCF.Call(FCF.Domain.t_0);
foreach (object i in result)
Fact.MakeFact(factlist, i, null, true);
foreach (Fact fact in factlist)
SpawnFactRepresentation(fact);
float current_time = Time.time - trigger_time;
while (current_time <= FCF.Domain.t_n
&& current_time <= FCF.Domain.t_0 + MaxAnimationDuration)
{
List<Fact> factlist_media = new();
object[] result_media = FCF.Call(current_time);
foreach (object i in result_media)
Fact.MakeFact(factlist_media, i, null, true);
for (int i = 0; i < math.min(factlist.Count, factlist_media.Count); i++)
{
//factlist[i].WorldRepresentation.Fact = factlist_media[i]; //TODO? viable? => FactWrapper.FactUpdated()
factlist[i].WorldRepresentation.transform.SetPositionAndRotation(
factlist_media[i].Position,
factlist_media[i].Rotation
);
factlist_media[i].freeAutoLabel(StageStatic.stage.factState);
}
yield return null;
current_time = Time.time - trigger_time;
}
foreach (Fact fact in factlist)
{
RemoveFactEvent.Invoke(fact);
fact.freeAutoLabel(StageStatic.stage.factState);
}
}
MaZiFAU
committed
}
John Schihada
committed
}
MaZiFAU
committed