Newer
Older
using System;
using System.Collections;
using TMPro;
using UnityEngine;
Marco Zimmer
committed
using static GlobalBehaviour;
public class FactSpawner : MonoBehaviour
{
Marco Zimmer
committed
public GameObject
Sphere,
Line,
Ray,
Angle,
Marco Zimmer
committed
AddFactEvent.AddListener(SpawnFactRepresentation_Wrapped);
RemoveFactEvent.AddListener(DeleteObject);
AnimateNonExistingFactEvent.AddListener(animateNonExistingFactTrigger);
private void SpawnFactRepresentation_Wrapped(Fact fact) => SpawnFactRepresentation(fact);
public Fact SpawnFactRepresentation(Fact fact)
Func<Fact, Fact> func = fact switch
PointFact => SpawnPoint,
LineFact => SpawnLine,
RightAngleFact => SpawnAngle, //needed for Hint System
AngleFact => SpawnAngle,
RayFact => SpawnRay,
CircleFact => SpawnRingAndCircle,
return func?.Invoke(fact);
public Fact SpawnPoint(Fact pointFact)
PointFact fact = ((PointFact)pointFact);
GameObject point = GameObject.Instantiate(Sphere);
point.transform.position = fact.Point;
point.transform.up = fact.Normal;
point.GetComponentInChildren<TextMeshPro>().text = fact.Label;
Marco Zimmer
committed
point.GetComponent<FactObject>().URI = fact.Id;
LineFact lineFact = ((LineFact)fact);
John Schihada
committed
//Change FactRepresentation to Line
GameObject line = GameObject.Instantiate(Line);
John Schihada
committed
//Place the Line in the centre of the two points
John Schihada
committed
//Change scale and rotation, so that the two points are connected by the line
//Get the Line-GameObject as the first Child of the Line-Prefab -> That's the Collider
var v3T = line.transform.GetChild(0).localScale;
John Schihada
committed
//For every Coordinate x,y,z we have to devide it by the LocalScale of the Child,
//because actually the Child should be of this length and not the parent, which is only the Collider
v3T.x = lineFact.LocalScale.x / line.transform.GetChild(0).GetChild(0).localScale.x;
John Schihada
committed
//Change Scale/Rotation of the Line-GameObject without affecting Scale of the Text
line.transform.GetChild(0).localScale = v3T;
line.transform.GetChild(0).rotation = lineFact.Rotation;
John Schihada
committed
line.GetComponentInChildren<TextMeshPro>().text =
lineFact.Label + " = " + Math.Round(lineFact.Distance, 2) + " m";
Marco Zimmer
committed
line.GetComponentInChildren<FactObject>().URI = lineFact.Id;
lineFact.Representation = line;
John Schihada
committed
}
RayFact rayFact = ((RayFact)fact);
Marco Zimmer
committed
//Change FactRepresentation to Line
GameObject line = GameObject.Instantiate(Ray);
//Place the Line in the centre of the two points
//Change scale and rotation, so that the two points are connected by the line
//Get the Line-GameObject as the first Child of the Line-Prefab -> That's the Collider
var v3T = line.transform.GetChild(0).localScale;
Marco Zimmer
committed
//For every Coordinate x,y,z we have to devide it by the LocalScale of the Child,
//because actually the Child should be of this length and not the parent, which is only the Collider
v3T.x = rayFact.LocalScale.x / line.transform.GetChild(0).GetChild(0).localScale.x;
//Change Scale/Rotation of the Line-GameObject without affecting Scale of the Text
line.transform.GetChild(0).localScale = v3T;
line.transform.GetChild(0).rotation = rayFact.Rotation;
line.GetComponentInChildren<TextMeshPro>().text = rayFact.Label;
Marco Zimmer
committed
line.GetComponentInChildren<FactObject>().URI = rayFact.Id;
rayFact.Representation = line;
return rayFact;
John Schihada
committed
//Spawn an angle: point with id = angleFact.Pid2 is the point where the angle gets applied
public Fact SpawnAngle(Fact fact)
Vector3 Psotion;
Quaternion Rotation;
float angleValue;
// TODO: just use simple inheritence or similar!!
if (fact is RightAngleFact rightangleFact)
{
Psotion = rightangleFact.Position;
Rotation = rightangleFact.Rotation;
angleValue = 90f;
Psotion = angleFact.Position;
Rotation = angleFact.Rotation;
angleValue = angleFact.angle;
throw new Exception("Invalid Type:" + fact.GetType().ToString());
//Change FactRepresentation to Angle
GameObject angle = GameObject.Instantiate(Angle);
//Place the Angle at position of point2
angle.transform.SetPositionAndRotation(Psotion, 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 = Math.Round(angleValue, 2) + "°";
}
//Generate angle mesh
CircleSegmentGenerator[] segments = angle.GetComponentsInChildren<CircleSegmentGenerator>();
foreach (CircleSegmentGenerator c in segments)
c.setAngle(angleValue);
angle.GetComponentInChildren<FactObject>().URI = fact.Id;
fact.Representation = angle;
return fact;
public Fact SpawnRingAndCircle(Fact fact)
{
var ringAndCircleGO = new GameObject("RingAndCircle");
SpawnRing(circleFact, ringAndCircleGO.transform);
SpawnCircle(circleFact, ringAndCircleGO.transform);
//Move Ring to middlePoint
ringAndCircleGO.transform.position = circleFact.Position;
fact.Representation = ringAndCircleGO;
return fact;
public void SpawnRing(CircleFact circleFact, Transform parent = null)
{
GameObject ring = GameObject.Instantiate(Ring, parent);
var tori = ring.GetComponentsInChildren<TorusGenerator>();
var tmpText = ring.GetComponentInChildren<TextMeshPro>();
var FactObj = ring.GetComponentInChildren<FactObject>();
//Set radii
FactObj.URI = circleFact.Id;
}
public void SpawnCircle(CircleFact circleFact, Transform parent = null)
var FactObj = circle.GetComponentInChildren<FactObject>();
circle.transform.localScale = Vector3.Scale(circle.transform.localScale, circleFact.LocalScale);
FactObj.URI = circleFact.Id;
}
John Schihada
committed
public void DeleteObject(Fact fact)
public void animateNonExistingFactTrigger(Fact fact)
{
StartCoroutine(animateNonExistingFact(fact));
MaZiFAU
committed
IEnumerator animateNonExistingFact(Fact fact)
{
Fact returnedFact = SpawnFactRepresentation(fact);
MaZiFAU
committed
ShinyThings.HighlightFact(returnedFact, FactObject.FactMaterials.Hint);
MaZiFAU
committed
yield return new WaitForSeconds(GlobalBehaviour.hintAnimationDuration);
MaZiFAU
committed
GameObject.Destroy(returnedFact.Representation);
}
John Schihada
committed
}
MaZiFAU
committed