-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject.cs
More file actions
179 lines (173 loc) · 5.22 KB
/
Object.cs
File metadata and controls
179 lines (173 loc) · 5.22 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace JLGraphics
{
public interface IComponentEvent : IGlobalScope
{
public bool IsActiveAndEnabled();
}
public interface IUpdate : IComponentEvent
{
public void Update();
}
public interface IFixedUpdate : IComponentEvent
{
public void FixedUpdate();
}
public interface IStart : IComponentEvent
{
public void Start();
}
public interface IOnRender : IComponentEvent
{
public void OnRender(Camera camera);
}
public abstract class Object : IGlobalScope
{
static ulong count = 0;
static Stack<ulong> previousDestroyedObject = new Stack<ulong>();
ulong mId = 0;
public ulong InstanceID => mId;
private bool Null => mId == 0;
protected virtual void OnCreate(params object[] args) { }
internal static void CallCreate(Object @object, params object[] args)
{
if (previousDestroyedObject.Count != 0)
{
@object.mId = previousDestroyedObject.Pop();
}
else
{
count++;
@object.mId = count;
}
if (typeof(IUpdate).IsAssignableFrom(@object.GetType()))
{
//AllUpdates.Add((IUpdate)instance);
InternalGlobalScope<IUpdate>.Values.Add(@object as IUpdate);
}
if (typeof(IFixedUpdate).IsAssignableFrom(@object.GetType()))
{
//AllFixedUpdates.Add((IFixedUpdate)instance);
InternalGlobalScope<IFixedUpdate>.Values.Add(@object as IFixedUpdate);
}
if (typeof(IStart).IsAssignableFrom(@object.GetType()))
{
//StartQueue.Add((IStart)instance);
InternalGlobalScope<IStart>.Values.Add(@object as IStart);
}
if (typeof(IOnRender).IsAssignableFrom(@object.GetType()))
{
//AllOnRenders.Add((IOnRender)instance);
InternalGlobalScope<IOnRender>.Values.Add(@object as IOnRender);
}
@object.OnCreate(args);
}
internal static void CallDestroy(Object @object)
{
if (typeof(IUpdate).IsAssignableFrom(@object.GetType()))
{
InternalGlobalScope<IUpdate>.Values.Remove(@object as IUpdate);
}
if (typeof(IFixedUpdate).IsAssignableFrom(@object.GetType()))
{
InternalGlobalScope<IFixedUpdate>.Values.Remove(@object as IFixedUpdate);
}
if (typeof(IStart).IsAssignableFrom(@object.GetType()))
{
InternalGlobalScope<IStart>.Values.Remove(@object as IStart);
}
if (typeof(IOnRender).IsAssignableFrom(@object.GetType()))
{
InternalGlobalScope<IOnRender>.Values.Remove(@object as IOnRender);
}
@object.InternalOnImmediateDestroy();
previousDestroyedObject.Push(@object.mId);
@object.mId = 0;
}
internal void AssertNull()
{
#if DEBUG
if (Null)
{
throw new NullReferenceException("Entity has been destroyed!");
}
#endif
}
protected virtual void InternalOnImmediateDestroy() { }
public static bool operator ==(Object a, Object b)
{
object ao = a;
object bo = b;
if (bo == null && ao == null)
{
return true;
}
if (ao == null && bo != null)
{
if (b.Null)
{
return true;
}
else
{
return false;
}
}
else if (bo == null && ao != null)
{
if (a.Null)
{
return true;
}
else
{
return false;
}
}
return a.InstanceID == b.InstanceID;
}
public static bool operator !=(Object a, Object b)
{
return !(a == b);
}
public override bool Equals(object? obj)
{
if (obj is null && Null)
{
return true;
}
else if (obj is null)
{
return false;
}
else
{
return InstanceID == ((Object)obj).InstanceID;
}
}
public static implicit operator bool(Object a) => a is null ? false : !a.Null;
public override int GetHashCode()
{
return (int)mId;
}
}
public abstract class NamedObject : Object, IName
{
public NamedObject(string name)
{
Name = name;
}
public string Name { get; set; }
}
public interface IName
{
public string Name { get; set; }
}
}