-
Notifications
You must be signed in to change notification settings - Fork 221
/
ObjectOrientedDesign.cs
65 lines (50 loc) · 2.54 KB
/
ObjectOrientedDesign.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
namespace UnrealEngine.Tests {
public class ObjectOrientedDesign : ISystem {
private Entity[] entities;
private Material material;
private Random random;
private const int maxEntities = 10;
public ObjectOrientedDesign() {
entities = new Entity[maxEntities];
material = Material.Load("/Game/Tests/BasicMaterial");
random = new();
}
public void OnBeginPlay() {
World.GetFirstPlayerController().SetViewTarget(World.GetActor<Camera>("MainCamera"));
for (int i = 0; i < maxEntities; i++) {
string entityName = "Entity" + (i > 0 ? i.ToString() : String.Empty);
entities[i] = new(entityName);
entities[i].CreateMesh(material, 1.0f, "StateComponent", true);
entities[i].StateComponent.SetRelativeRotation(Maths.CreateFromYawPitchRoll(5.0f * i, 0.0f, 0.0f));
entities[i].StateComponent.CreateAndSetMaterialInstanceDynamic(0).SetVectorParameterValue("Color", new((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()));
entities[i].StateComponent.SetRelativeLocation(new(0.0f, 0.0f, 120.0f * i));
entities[i].StateComponent.AddLocalOffset(new(0.0f, 0.0f, -420.0f));
Entity entity = World.GetActor<Entity>(entityName);
StateComponent component = entity.GetComponent<StateComponent>();
Assert.IsTrue(entity.Equals(entities[i]));
Assert.IsTrue(component.Equals(entities[i].StateComponent));
}
Debug.AddOnScreenMessage(-1, 3.0f, Color.LightGreen, "Actors are spawned! Number of actors in the world: " + World.ActorCount);
}
public void OnTick(float deltaTime) {
for (int i = 0; i < maxEntities; i++) {
entities[i].StateComponent.AddLocalRotation(new(Vector3.UnitZ * entities[i].StateComponent.RotationSpeed * deltaTime, -1.0f));
}
}
public void OnEndPlay() => Debug.ClearOnScreenMessages();
private class Entity : Actor {
private StateComponent component;
public StateComponent StateComponent => component;
public Entity(string name = null) : base(name) { }
public void CreateMesh(Material material, float rotationSpeed, string name = null, bool setAsRoot = false) {
component = new(this, rotationSpeed, name, setAsRoot);
component.SetStaticMesh(StaticMesh.Cube);
component.SetMaterial(0, material);
}
}
private class StateComponent : StaticMeshComponent {
public float RotationSpeed { get; set; }
public StateComponent(Entity entity, float rotationSpeed, string name = null, bool setAsRoot = false) : base(entity, name, setAsRoot) => RotationSpeed = rotationSpeed;
}
}
}