forked from kibble-cabal/ability-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbilitySystem.cs
175 lines (163 loc) · 6.98 KB
/
AbilitySystem.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
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
using Godot;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace AS
{
public interface IInstanceWrapper<T> where T : GodotObject
{
T Instance { get; set; }
void SetInstance(T instance)
{
ArgumentNullException.ThrowIfNull(instance);
if (!ClassDB.IsParentClass(instance.GetClass(), GetType().Name)) throw new ArgumentException("\"instance\" has the wrong type.");
Instance = instance;
}
}
internal static class Utils
{
static readonly Dictionary<ulong, dynamic> CachedInstances = [];
static DateTime PreviousClearTime = DateTime.Now;
public static T? CreateWrapperFromObject<T>(GodotObject instance) where T : class
{
if (instance == null)
return null;
ulong id = instance.GetInstanceId();
if (CachedInstances.TryGetValue(id, out dynamic? value))
return (T)value;
if ((DateTime.Now - PreviousClearTime).TotalSeconds > 1)
{
var query = CachedInstances.Where((i) => GodotObject.IsInstanceIdValid(i.Key)).ToArray();
foreach (var i in query)
CachedInstances.Remove(i.Key);
PreviousClearTime = DateTime.Now;
}
switch (instance.GetClass())
{
case "Ability":
{
var newInstance = new Ability((Resource)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "AbilityEvent":
{
var newInstance = new AbilityEvent((Resource)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "AbilitySystem":
{
var newInstance = new AbilitySystem((Node)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "Attribute":
{
var newInstance = new Attribute((Resource)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "Tag":
{
var newInstance = new Tag((Resource)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "Effect":
{
var newInstance = new Effect((Resource)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "AttributeEffect":
{
var newInstance = new AttributeEffect((Resource)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "LoopEffect":
{
var newInstance = new LoopEffect((Resource)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "TagEffect":
{
var newInstance = new TagEffect((Resource)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "TryActivateAbilityEffect":
{
var newInstance = new TryActivateAbilityEffect((Resource)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "WaitEffect":
{
var newInstance = new WaitEffect((Resource)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "AbilitySystemViewer":
{
var newInstance = new AbilitySystemViewer((VBoxContainer)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "AttributeViewer":
{
var newInstance = new AttributeViewer((Control)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "AbilityViewer":
{
var newInstance = new AbilityViewer((Control)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "EventViewer":
{
var newInstance = new EventViewer((Control)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
case "TagViewer":
{
var newInstance = new TagViewer((Control)instance);
CachedInstances[id] = newInstance;
return newInstance as T;
}
}
throw new NotImplementedException();
}
private static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> value) => value.Where(value => value != null).Select<T?, T>(value => value!);
public static T? TryAs<[MustBeVariant] T>(this Variant variant) where T : class
{
try
{
return variant.As<T>();
}
catch
{
return null;
}
}
public static IEnumerable<OutType> Convert<[MustBeVariant] InType, OutType>(this IEnumerable values)
where InType : GodotObject
where OutType : class, IInstanceWrapper<InType> => values.Cast<Variant>()
.Select(TryAs<InType>)
.WhereNotNull()
.Select(CreateWrapperFromObject<OutType>)
.WhereNotNull();
public static IEnumerable<OutType> Convert<[MustBeVariant] InType, OutType>(this Variant variant)
where InType : GodotObject
where OutType : class, IInstanceWrapper<InType> => variant.As<Godot.Collections.Array<InType>>().Convert<InType, OutType>();
public static IEnumerable<OutType> Convert<InType, [MustBeVariant] OutType>(this IEnumerable<InType> values)
where InType : class, IInstanceWrapper<OutType>
where OutType : GodotObject => values.Select(val => val.Instance).WhereNotNull();
}
}