Last active
March 9, 2022 16:56
-
-
Save jlreymendez/c2f441aaf6ac7b5f233ecd990314e9cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Svelto.Common; | |
using Svelto.DataStructures; | |
namespace Svelto.ECS | |
{ | |
public static class ExclusiveBuildGroupExtensions | |
{ | |
internal static FasterDictionary<ExclusiveGroupStruct, FasterDictionary<RefWrapper<Type>, ExclusiveBuildGroup>> | |
_removeTransitions = | |
new FasterDictionary<ExclusiveGroupStruct, FasterDictionary<RefWrapper<Type>, ExclusiveBuildGroup>>(); | |
internal static FasterDictionary<ExclusiveGroupStruct, FasterDictionary<RefWrapper<Type>, ExclusiveBuildGroup>> | |
_addTransitions = | |
new FasterDictionary<ExclusiveGroupStruct, FasterDictionary<RefWrapper<Type>, ExclusiveBuildGroup>>(); | |
internal static FasterDictionary<ExclusiveGroupStruct, FasterDictionary<RefWrapper<Type>, ExclusiveBuildGroup>> | |
_swapTransitions = | |
new FasterDictionary<ExclusiveGroupStruct, FasterDictionary<RefWrapper<Type>, ExclusiveBuildGroup>>(); | |
public static void SetTagAddition<T> | |
(this ExclusiveBuildGroup group, ExclusiveBuildGroup target, bool setReverse = true) where T : GroupTag<T> | |
{ | |
if (_addTransitions.TryGetValue(@group, out var transitions) == false) | |
{ | |
transitions = new FasterDictionary<RefWrapper<Type>, ExclusiveBuildGroup>(); | |
_addTransitions[@group] = transitions; | |
} | |
var type = new RefWrapper<Type>(typeof(T)); | |
transitions[type] = target; | |
if (setReverse) | |
{ | |
SetTagRemoval<T>(target, group, false); | |
} | |
} | |
public static void SetTagRemoval<T> | |
(this ExclusiveBuildGroup group, ExclusiveBuildGroup target, bool setReverse = true) where T : GroupTag<T> | |
{ | |
if (_removeTransitions.TryGetValue(@group, out var transitions) == false) | |
{ | |
transitions = new FasterDictionary<RefWrapper<Type>, ExclusiveBuildGroup>(); | |
_removeTransitions[@group] = transitions; | |
} | |
var type = new RefWrapper<Type>(typeof(T)); | |
transitions[type] = target; | |
if (setReverse) | |
{ | |
SetTagAddition<T>(target, group, false); | |
} | |
} | |
public static void SetTagSwap<TRemove, TAdd> | |
(this ExclusiveBuildGroup group, ExclusiveBuildGroup target, bool setReverse = true) | |
where TRemove : GroupTag<TRemove> where TAdd : GroupTag<TAdd> | |
{ | |
if (_swapTransitions.TryGetValue(@group, out var transitions) == false) | |
{ | |
transitions = new FasterDictionary<RefWrapper<Type>, ExclusiveBuildGroup>(); | |
_swapTransitions[group] = transitions; | |
} | |
var type = new RefWrapper<Type>(typeof(TAdd)); | |
transitions[type] = target; | |
// To avoid needing to check if the group already has the tag when swapping (prevent ecs exceptions). | |
// The current groups adds the removed tag pointing to itself. | |
type = new RefWrapper<Type>(typeof(TRemove)); | |
transitions[type] = group; | |
if (setReverse) | |
{ | |
SetTagSwap<TAdd, TRemove>(target, group, false); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Svelto.Common; | |
using Svelto.DataStructures; | |
namespace Svelto.ECS | |
{ | |
public static class ExclusiveGroupStructExtensions | |
{ | |
public static ExclusiveBuildGroup RemoveTag<T>(this ExclusiveGroupStruct group) where T : GroupTag<T> | |
{ | |
if (ExclusiveBuildGroupExtensions._removeTransitions.TryGetValue(@group, out var transitions)) | |
{ | |
var type = new RefWrapper<Type>(typeof(T)); | |
if (transitions.TryGetValue(type, out var result)) | |
{ | |
return result; | |
} | |
} | |
throw new ECSException("No remove transition found for type " | |
.FastConcat(typeof(T).ToString()) | |
.FastConcat(" in group ").FastConcat(@group.ToString()) | |
); | |
} | |
public static ExclusiveBuildGroup AddTag<T>(this ExclusiveGroupStruct group) where T : GroupTag<T> | |
{ | |
if (ExclusiveBuildGroupExtensions._addTransitions.TryGetValue(group, out var transitions)) | |
{ | |
var type = new RefWrapper<Type>(typeof(T)); | |
if (transitions.TryGetValue(type, out var result)) | |
{ | |
return result; | |
} | |
} | |
throw new ECSException("No add transition found for type " | |
.FastConcat(typeof(T).ToString()) | |
.FastConcat(" in group ").FastConcat(@group.ToString()) | |
); | |
} | |
public static ExclusiveBuildGroup SwapTag<TTarget>(this ExclusiveGroupStruct group) | |
where TTarget : GroupTag<TTarget> | |
{ | |
var type = new RefWrapper<Type>(typeof(TTarget)); | |
if (ExclusiveBuildGroupExtensions._swapTransitions.TryGetValue(@group, out var transitions)) | |
{ | |
if (transitions.TryGetValue(type, out var result)) | |
{ | |
return result; | |
} | |
} | |
throw new ECSException("No swap transition found for type " | |
.FastConcat(typeof(TTarget).ToString()) | |
.FastConcat(" in group ").FastConcat(@group.ToString()) | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey!
1- Yeah, it would allow you to swap to targets that are not directly declared in a group class. In reality you are declaring them when you declare the SetTagSwap, SetTagAddition and SetTagRemoval.
2- Yes, correct by specialized I mean something not abstract, an engine that knows all the tags that apply to an entity. Thus it would allow you to create a comprehensive list of the swaps like you see in that code. Remember that when you do
SetTagSwap<SHIP, SUNK_SHIP>
the reverse will also be declared automatically.This is the latest from that code you shared:
3- You would fill them at initialization time, I did it in the Ready() of the engine before entering the game loop. But you could do it in the constructor of the engine. Wherever you find it appropriate before you need to start swapping or changing tags.