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,
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 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 ConeVolumeFact ConeVolumeFact:
SpawnCone(ConeVolumeFact); break;
case TruncatedConeVolumeFact TruncatedConeVolumeFact:
SpawnTruncatedCone(TruncatedConeVolumeFact); break;
default: break;
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 + " = " + 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>();
//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);
155
156
157
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
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);
fact.freeAutoLabel();
}
}
public void AnimateFunctionCalls()
{
220
221
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
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, StageStatic.stage.factState, true);
foreach (Fact fact in factlist)
SpawnFactRepresentation(fact);
yield return null;
foreach (Fact fact in factlist)
{
RemoveFactEvent.Invoke(fact);
fact.freeAutoLabel();
}
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, StageStatic.stage.factState, 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, StageStatic.stage.factState, 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();
}
yield return null;
current_time = Time.time - trigger_time;
}
foreach (Fact fact in factlist)
{
RemoveFactEvent.Invoke(fact);
fact.freeAutoLabel();
}
}
MaZiFAU
committed
}
John Schihada
committed
}
MaZiFAU
committed