Newer
Older
using System;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityStandardAssets.Vehicles.Car;
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);
RemoveFactEvent.AddListener(DeleteObject);
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;
default: break;
public void SpawnAttachedPositionFunction(AttachedPositionFunction fact)
var attachedBehaviour = fact.Fact.Representation.AddComponent<AttachedPositionFunctionBehaviour>();
attachedBehaviour.URI = fact.Id;
fact.Representation = attachedBehaviour.gameObject;
}
public void SpawnPoint(PointFact fact)
{
GameObject point = GameObject.Instantiate(Sphere);
point.transform.SetPositionAndRotation(fact.Position, fact.Rotation);
point.GetComponentInChildren<TextMeshPro>().text = fact.Label;
Marco Zimmer
committed
point.GetComponent<FactObject>().URI = fact.Id;
public void SpawnLine(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
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";
line.GetComponentInChildren<FactObject>().URI = fact.Id;
fact.Representation = line;
John Schihada
committed
}
public void SpawnRay(RayFact fact)
Marco Zimmer
committed
//Change FactRepresentation to Line
GameObject line = GameObject.Instantiate(Ray);
//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;
line.GetComponentInChildren<TextMeshPro>().text = fact.Label;
line.GetComponentInChildren<FactObject>().URI = fact.Id;
fact.Representation = line;
John Schihada
committed
//Spawn an angle: point with id = angleFact.Pid2 is the point where the angle gets applied
public void SpawnAngle(AbstractAngleFact fact)
Vector3 Psotion = fact.Position;
Quaternion Rotation = fact.Rotation;
float angleValue = fact.angle;
//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;
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);
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)
circle.GetComponent<FactObject>().URI = circleFact.Id;
circle.transform.localScale = Vector3.Scale(circle.transform.localScale, circleFact.LocalScale);
John Schihada
committed
public void DeleteObject(Fact fact)
public void animateNonExistingFactTrigger(Fact fact)
{
StartCoroutine(animateNonExistingFact(fact));
MaZiFAU
committed
IEnumerator animateNonExistingFact(Fact fact)
{
SpawnFactRepresentation(fact);
ShinyThings.HighlightFact(fact, FactObject.FactMaterials.Hint);
MaZiFAU
committed
yield return new WaitForSeconds(GlobalBehaviour.hintAnimationDuration);
GameObject.Destroy(fact.Representation);
MaZiFAU
committed
}
John Schihada
committed
}
MaZiFAU
committed