-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentSystem.cs
More file actions
181 lines (139 loc) · 4.86 KB
/
ComponentSystem.cs
File metadata and controls
181 lines (139 loc) · 4.86 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
180
181
using System;
using System.Collections.Generic;
namespace MoD
{
public abstract class ComponentSystem
{
private Dictionary<ComponentType,Dictionary<string,IComponent>> _components = new Dictionary<ComponentType, Dictionary<string, IComponent>>();
public event Action? OnRun;
public void Event_OnRun(){
OnRun?.Invoke();
}
public enum ComponentType
{
Property,
Behavior
}
public interface IComponent {
public ComponentType Type {get; }
public void Register(ComponentSystem owner);
public void Deregister(ComponentSystem owner);
}
public class PropertyComponent : IComponent
{
public ComponentType Type => ComponentType.Property;
public string Name = "";
public string Description = "";
private object? _value;
public object? Value
{
get { return _value; }
set { _value = value; }
}
private ComponentSystem? _owner = null;
public void Register(ComponentSystem owner)
{
_owner = owner;
}
public void Deregister(ComponentSystem owner)
{
_owner = null;
}
public PropertyComponent(ComponentSystem owner, string name, object? value)
{
Register(owner);
Name = name;
Value = value;
}
}
public abstract class BehaviorComponent : IComponent
{
public ComponentType Type => ComponentType.Behavior;
public virtual void OnRun()
{
}
private ComponentSystem? _owner = null;
public void Register(ComponentSystem owner)
{
_owner = owner;
owner.OnRun += OnRun;
}
public void Deregister(ComponentSystem owner)
{
_owner = null;
owner.OnRun -= OnRun;
}
}
public string AddComponent(ComponentType type, IComponent comp, string name="")
{
if(name == "")
name = Guid.NewGuid().ToString();
if(!_components.ContainsKey(type))
_components.Add(type, new Dictionary<string, IComponent>());
if(_components[type].ContainsKey(name)){
_components[type][name].Deregister(this);
_components[type].Remove(name);
}
_components[type].Add(name,comp);
_components[type][name].Register(this);
return name;
}
public IComponent? FindComponent(string name)
{
foreach(var type in _components.Keys){
foreach(var icname in _components[type].Keys){
if(icname == name)
return _components[type][icname];
}
}
return null;
}
public void RemoveComponent(ComponentType type, string name="")
{
if(_components.ContainsKey(type) && _components[type].ContainsKey(name)){
_components[type][name].Deregister(this);
_components[type].Remove(name);
}
}
public string AddProperty(string name, object value)
{
return AddComponent(ComponentType.Property, new PropertyComponent(this, name, value), name);
}
public string SetPropertyValue(string name, object value)
{
var type = ComponentType.Property;
if(!_components.ContainsKey(type))
_components.Add(type, new Dictionary<string, IComponent>());
if(_components[type].ContainsKey(name)){
((PropertyComponent)_components[type][name]).Value = value;
} else {
AddProperty(name,value);
}
return name;
}
public object? GetPropertyValue(string name)
{
if(_components[ComponentType.Property].ContainsKey(name))
return _components[ComponentType.Property][name];
return null;
}
public T GetPropertyValue<T>(string name)
{
object tmp = GetPropertyValue(name);
//Console.WriteLine(name);
//Console.WriteLine(tmp.ToString());
//Console.WriteLine(typeof(tmp));
return (T)Convert.ChangeType(tmp, typeof(T));
}
}
public class TestPlayer : ComponentSystem
{
}
public class HeartBeat : ComponentSystem.BehaviorComponent
{
public override void OnRun()
{
Console.WriteLine("thump, thump");
}
}
}