-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObject3D.cs
More file actions
88 lines (75 loc) · 2.82 KB
/
Copy pathObject3D.cs
File metadata and controls
88 lines (75 loc) · 2.82 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Text;
namespace EnesShahn
{
namespace RayTracingEngine
{
class Object3D
{
public int id;
public Vector3 position = new Vector3(0, 0, 0);
public Vector3 rotation = new Vector3(0, 0, 0); // TODO: Currently Using Euler Rotation, Convert to Quaternion
public Vector3 scale = new Vector3(1, 1, 1);
public Matrix4x4 modelMatrix = Matrix4x4.Identity;
private Object3D parent;
private readonly List<Component> components = new List<Component>();
private readonly List<Object3D> children = new List<Object3D>();
public Object3D()
{
}
public Object3D(Vector3 position, Vector3 rotation, Vector3 scale)
{
this.position = position;
this.rotation = rotation;
this.scale = scale;
}
public T AddComponent<T>() where T : Component, new()
{
T newComp = new T();
newComp.SetOwner(this);
components.Add(newComp);
return newComp;
}
public void RemoveComponent(Component component) => components.Remove(component);
public bool HasComponent<T>() where T : Component
{
Type tType = typeof(T);
foreach (Component component in components)
{
if (component.GetType().IsSubclassOf(tType) || component.GetType() == typeof(T))
return true;
}
return false;
}
public T GetComponent<T>() where T : Component
{
Type tType = typeof(T);
foreach (Component component in components)
{
if (component.GetType().IsSubclassOf(tType) || component.GetType() == typeof(T))
return (T)component;
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error: No {tType} Component Attached The Object3D");
Console.ForegroundColor = ConsoleColor.White;
return null;
}
public int ChildCount() => children.Count;
public void RemoveChild(Object3D child)
{
child.parent = null;
children.Remove(child);
}
public Object3D GetChild(int index) => children[index];
public void AddChild(Object3D child)
{
if (parent != null)
{
parent.RemoveChild(this);
}
children.Add(child);
child.parent = this;
}
}
}