public static void RunEntityTest() { var entityA = new Entity(); entityA.ID = "EntityA"; entityA.Fields.SetValue(StringField.Country, "Russia"); entityA.Fields.SetValue(StringField.City, "Kaliningrad"); entityA.Fields.SetValue(IntField.AppleCount, 4); entityA.Fields.SetValue(IntField.OrangeCount, 3); var entityB = new Entity(); entityB.Fields.SetValue(StringField.Country, "China"); entityB.Fields.SetValue(IntField.BananaCount, 77); entityB.Fields.SetValue(StringField.Notes, "this is notes"); entityA.Fields.SetValue(EntityField.Link, entityB); entityB.Fields.SetValue(EntityField.Link, entityA); var settings = new SerializationSettings(SerializationFlags.Binary); var bytes = Serializer.Instance.Serialize(entityA, settings); var json = Serializer.Instance.ToJson(bytes); Logger.Instance.Print(json); entityA.Release(); entityB.Release(); var deserializedEntity = Serializer.Instance.Deserialize <Entity>(bytes, settings); Logger.Instance.Print("Deserialized"); }
void removeEntitySilently(Entity entity) { var removed = _entities.Remove(entity); if (removed) { _entitiesCache = null; _singleEntityCache = null; entity.Release(); } }
public void Unlink() { if (_entity == null) { throw new Exception("EntityLink is already unlinked!"); } _entity.Release(this); _entity = null; _pool = null; }
public static void DrawEntity(Pool pool, Entity entity) { var bgColor = GUI.backgroundColor; GUI.backgroundColor = Color.red; if (GUILayout.Button("Destroy Entity")) { pool.DestroyEntity(entity); } GUI.backgroundColor = bgColor; DrawComponents(pool, entity); EditorGUILayout.Space(); EditorGUILayout.LabelField("Retained by (" + entity.retainCount + ")", EditorStyles.boldLabel); #if !ENTITAS_FAST_AND_UNSAFE EntitasEditorLayout.BeginVerticalBox(); { foreach (var owner in entity.owners.ToArray()) { EntitasEditorLayout.BeginHorizontal(); { EditorGUILayout.LabelField(owner.ToString()); if (GUILayout.Button("Release", GUILayout.Width(88), GUILayout.Height(14))) { entity.Release(owner); } EntitasEditorLayout.EndHorizontal(); } } } EntitasEditorLayout.EndVertical(); #endif }
public virtual void DestroyEntity(Entity entity) { var removed = _entities.Remove(entity); if (!removed) { throw new PoolDoesNotContainEntityException(entity, "Could not destroy entity!"); } _entitiesCache = null; if (OnEntityWillBeDestroyed != null) { OnEntityWillBeDestroyed(this, entity); } entity.destroy(); if (OnEntityDestroyed != null) { OnEntityDestroyed(this, entity); } if (entity._refCount == 1) { entity.OnEntityReleased -= _cachedOnEntityReleased; _reusableEntities.Push(entity); } else { _retainedEntities.Add(entity); } entity.Release(); }
void removeEntity(Entity entity, int index, IComponent component) { var removed = _entities.Remove(entity); if (removed) { _entitiesCache = null; _singleEntityCache = null; if (OnEntityRemoved != null) { OnEntityRemoved(this, entity, index, component); } entity.Release(this); } }
bool removeEntitySilently(Entity entity) { var removed = _entities.Remove(entity); if (removed) { _entitiesCache = null; _singleEntityCache = null; entity.Release(this); } return removed; }
void when_created() { Entity e = null; before = () => { e = this.CreateEntity(); }; context["initial state"] = () => { it["throws when attempting to get component of type which hasn't been added"] = expect <EntityDoesNotHaveComponentException>(() => { e.GetComponentA(); }); it["gets empty array of components when no components were added"] = () => { e.GetComponents().should_be_empty(); }; it["gets empty array of component indices when no components were added"] = () => { e.GetComponentIndices().should_be_empty(); }; it["doesn't have component of type when no component of that type was added"] = () => { e.HasComponentA().should_be_false(); }; it["doesn't have components of types when no components of these types were added"] = () => { e.HasComponents(_indicesA).should_be_false(); }; it["doesn't have any components of types when no components of these types were added"] = () => { e.HasAnyComponent(_indicesA).should_be_false(); }; it["returns entity when adding a component"] = () => { e.AddComponent(0, null).should_be_same(e); }; it["adds a component"] = () => { e.AddComponentA(); assertHasComponentA(e); }; it["throws when attempting to remove a component of type which hasn't been added"] = expect <EntityDoesNotHaveComponentException>(() => { e.RemoveComponentA(); }); it["replacing a non existing component adds component"] = () => { e.ReplaceComponentA(Component.A); assertHasComponentA(e); }; }; context["when component added"] = () => { before = () => { e.AddComponentA(); }; it["throws when adding a component of the same type twice"] = expect <EntityAlreadyHasComponentException>(() => { e.AddComponentA(); e.AddComponentA(); }); it["returns entity when removing a component"] = () => { e.RemoveComponent(CID.ComponentA).should_be_same(e); }; it["removes a component of type"] = () => { e.RemoveComponentA(); assertHasNotComponentA(e); }; it["returns entity when replacing a component"] = () => { e.ReplaceComponent(CID.ComponentA, null).should_be_same(e); }; it["replaces existing component"] = () => { var newComponentA = new ComponentA(); e.ReplaceComponentA(newComponentA); assertHasComponentA(e, newComponentA); }; it["doesn't have components of types when not all components of these types were added"] = () => { e.HasComponents(_indicesAB).should_be_false(); }; it["has any components of types when any component of these types was added"] = () => { e.HasAnyComponent(_indicesAB).should_be_true(); }; context["when adding another component"] = () => { before = () => { e.AddComponentB(); }; it["gets all components"] = () => { var components = e.GetComponents(); components.Length.should_be(2); components.should_contain(Component.A); components.should_contain(Component.B); }; it["gets all component indices"] = () => { var componentIndices = e.GetComponentIndices(); componentIndices.Length.should_be(2); componentIndices.should_contain(CID.ComponentA); componentIndices.should_contain(CID.ComponentB); }; it["has other component"] = () => { e.HasComponentB().should_be_true(); }; it["has components of types when all components of these types were added"] = () => { e.HasComponents(_indicesAB).should_be_true(); }; it["removes all components"] = () => { e.RemoveAllComponents(); e.HasComponentA().should_be_false(); e.HasComponentB().should_be_false(); e.GetComponents().should_be_empty(); e.GetComponentIndices().should_be_empty(); }; it["can ToString"] = () => { e.AddComponent(0, new SomeComponent()); e.ToString().should_be("Entity_0(Some, ComponentA, ComponentB)"); }; }; }; context["events"] = () => { int didDispatch = 0; before = () => { didDispatch = 0; }; it["dispatches OnComponentAdded when adding a component"] = () => { e.OnComponentAdded += (entity, index, component) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); component.should_be_same(Component.A); }; e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail(); e.AddComponentA(); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved when removing a component"] = () => { e.AddComponentA(); e.OnComponentRemoved += (entity, index, component) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); component.should_be_same(Component.A); }; e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail(); e.RemoveComponentA(); didDispatch.should_be(1); }; it["dispatches OnComponentReplaced when replacing a component"] = () => { e.AddComponentA(); var newComponentA = new ComponentA(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); previousComponent.should_be_same(Component.A); newComponent.should_be_same(newComponentA); }; e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.ReplaceComponentA(newComponentA); didDispatch.should_be(1); }; it["provides previous and new component OnComponentReplaced when replacing with different component"] = () => { var prevComp = new ComponentA(); var newComp = new ComponentA(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => { didDispatch += 1; entity.should_be_same(e); previousComponent.should_be_same(prevComp); newComponent.should_be_same(newComp); }; e.AddComponent(CID.ComponentA, prevComp); e.ReplaceComponent(CID.ComponentA, newComp); didDispatch.should_be(1); }; it["provides previous and new component OnComponentReplaced when replacing with same component"] = () => { e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => { didDispatch += 1; entity.should_be_same(e); previousComponent.should_be_same(Component.A); newComponent.should_be_same(Component.A); }; e.AddComponentA(); e.ReplaceComponentA(Component.A); didDispatch.should_be(1); }; it["doesn't dispatch anything when replacing a non existing component with null"] = () => { e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.ReplaceComponentA(null); }; it["dispatches OnComponentAdded when attempting to replace a component which hasn't been added"] = () => { var newComponentA = new ComponentA(); e.OnComponentAdded += (entity, index, component) => { didDispatch += 1; entity.should_be_same(e); index.should_be(CID.ComponentA); component.should_be_same(newComponentA); }; e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail(); e.OnComponentRemoved += (entity, index, component) => this.Fail(); e.ReplaceComponentA(newComponentA); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved when replacing a component with null"] = () => { e.AddComponentA(); e.OnComponentRemoved += (entity, index, component) => { didDispatch += 1; }; e.OnComponentAdded += (entity, index, component) => this.Fail(); e.OnComponentReplaced += (entity, index, previousComponent, newComponent) => this.Fail(); e.ReplaceComponentA(null); didDispatch.should_be(1); }; it["dispatches OnComponentRemoved when removing all components"] = () => { e.AddComponentA(); e.AddComponentB(); e.OnComponentRemoved += (entity, index, component) => didDispatch += 1; e.RemoveAllComponents(); didDispatch.should_be(2); }; }; context["reference counting"] = () => { it["retains entity"] = () => { e.RefCount().should_be(0); e.Retain(); e.RefCount().should_be(1); }; it["releases entity"] = () => { e.Retain(); e.Release(); e.RefCount().should_be(0); }; it["throws when releasing more than it has been retained"] = expect <EntityIsAlreadyReleasedException>(() => { e.Retain(); e.Release(); e.Release(); }); context["events"] = () => { it["doesn't dispatch OnEntityReleased when retaining"] = () => { e.OnEntityReleased += entity => this.Fail(); e.Retain(); }; it["dispatches OnEntityReleased when retain and release"] = () => { var didDispatch = 0; e.OnEntityReleased += entity => { didDispatch += 1; entity.should_be_same(e); }; e.Retain(); e.Release(); }; }; }; context["internal caching"] = () => { context["components"] = () => { IComponent[] cache = null; before = () => { e.AddComponentA(); cache = e.GetComponents(); }; it["caches components"] = () => { e.GetComponents().should_be_same(cache); }; it["updates cache when a new component was added"] = () => { e.AddComponentB(); e.GetComponents().should_not_be_same(cache); }; it["updates cache when a component was removed"] = () => { e.RemoveComponentA(); e.GetComponents().should_not_be_same(cache); }; it["updates cache when a component was replaced"] = () => { e.ReplaceComponentA(new ComponentA()); e.GetComponents().should_not_be_same(cache); }; it["doesn't update cache when a component was replaced with same component"] = () => { e.ReplaceComponentA(Component.A); e.GetComponents().should_be_same(cache); }; it["updates cache when all components were removed"] = () => { e.RemoveAllComponents(); e.GetComponents().should_not_be_same(cache); }; }; context["component indices"] = () => { int[] cache = null; before = () => { e.AddComponentA(); cache = e.GetComponentIndices(); }; it["caches component indices"] = () => { e.GetComponentIndices().should_be_same(cache); }; it["updates cache when a new component was added"] = () => { e.AddComponentB(); e.GetComponentIndices().should_not_be_same(cache); }; it["updates cache when a component was removed"] = () => { e.RemoveComponentA(); e.GetComponentIndices().should_not_be_same(cache); }; it["doesn't update cache when a component was replaced"] = () => { e.ReplaceComponentA(new ComponentA()); e.GetComponentIndices().should_be_same(cache); }; it["updates cache when adding a new component with ReplaceComponent"] = () => { e.ReplaceComponentC(Component.C); e.GetComponentIndices().should_not_be_same(cache); }; it["updates cache when all components were removed"] = () => { e.RemoveAllComponents(); e.GetComponentIndices().should_not_be_same(cache); }; }; context["ToString"] = () => { context["when component was added"] = () => { string cache = null; before = () => { e.AddComponentA(); cache = e.ToString(); }; it["caches entity description"] = () => { e.ToString().should_be_same(cache); }; it["updates cache when a new component was added"] = () => { e.AddComponentB(); e.ToString().should_not_be_same(cache); }; it["updates cache when a component was removed"] = () => { e.RemoveComponentA(); e.ToString().should_not_be_same(cache); }; it["doesn't update cache when a component was replaced"] = () => { e.ReplaceComponentA(new ComponentA()); e.ToString().should_be_same(cache); }; it["updates cache when all components were removed"] = () => { e.RemoveAllComponents(); e.ToString().should_not_be_same(cache); }; }; it["updates cache when RemoveAllComponents is called, even if entity has no components"] = () => { var str = e.ToString(); e.RemoveAllComponents(); e.ToString().should_not_be_same(str); }; }; }; }
public void ReleaseEntity() { _entity.Release(this); }
public virtual void DestroyEntity(Entity entity) { var removed = _entities.Remove(entity); if (!removed) { throw new PoolDoesNotContainEntityException("'" + this + "' cannot destroy " + entity + "!", "Did you call pool.DestroyEntity() on a wrong pool?"); } _entitiesCache = null; if (OnEntityWillBeDestroyed != null) { OnEntityWillBeDestroyed(this, entity); } entity.destroy(); if (OnEntityDestroyed != null) { OnEntityDestroyed(this, entity); } if (entity.retainCount == 1) { entity.OnEntityReleased -= _cachedOnEntityReleased; _reusableEntities.Push(entity); } else { _retainedEntities.Add(entity); } entity.Release(this); }
public static void DrawEntity(Pool pool, Entity entity) { var bgColor = GUI.backgroundColor; GUI.backgroundColor = Color.red; if (GUILayout.Button("Destroy Entity")) { pool.DestroyEntity(entity); } GUI.backgroundColor = bgColor; bool[] unfoldedComponents; if (!_poolToUnfoldedComponents.TryGetValue(pool, out unfoldedComponents)) { unfoldedComponents = new bool[pool.totalComponents]; for (int i = 0; i < unfoldedComponents.Length; i++) { unfoldedComponents[i] = true; } _poolToUnfoldedComponents.Add(pool, unfoldedComponents); } EntitasEditorLayout.BeginVerticalBox(); { EntitasEditorLayout.BeginHorizontal(); { EditorGUILayout.LabelField("Components (" + entity.GetComponents().Length + ")", EditorStyles.boldLabel); if (GUILayout.Button("▸", GUILayout.Width(21), GUILayout.Height(14))) { for (int i = 0; i < unfoldedComponents.Length; i++) { unfoldedComponents[i] = false; } } if (GUILayout.Button("▾", GUILayout.Width(21), GUILayout.Height(14))) { for (int i = 0; i < unfoldedComponents.Length; i++) { unfoldedComponents[i] = true; } } } EntitasEditorLayout.EndHorizontal(); EditorGUILayout.Space(); var componentNames = entity.poolMetaData.componentNames; var index = EditorGUILayout.Popup("Add Component", -1, componentNames); if (index >= 0) { var componentType = entity.poolMetaData.componentTypes[index]; var component = (IComponent)Activator.CreateInstance(componentType); entity.AddComponent(index, component); } EditorGUILayout.Space(); _componentNameSearchTerm = EditorGUILayout.TextField("Search", _componentNameSearchTerm); EditorGUILayout.Space(); var indices = entity.GetComponentIndices(); var components = entity.GetComponents(); for (int i = 0; i < components.Length; i++) { DrawComponent(unfoldedComponents, entity, indices[i], components[i]); } EditorGUILayout.Space(); EditorGUILayout.LabelField("Retained by (" + entity.retainCount + ")", EditorStyles.boldLabel); #if !ENTITAS_FAST_AND_UNSAFE EntitasEditorLayout.BeginVerticalBox(); { foreach (var owner in entity.owners.ToArray()) { EntitasEditorLayout.BeginHorizontal(); { EditorGUILayout.LabelField(owner.ToString()); if (GUILayout.Button("Release", GUILayout.Width(88), GUILayout.Height(14))) { entity.Release(owner); } EntitasEditorLayout.EndHorizontal(); } } } EntitasEditorLayout.EndVertical(); #endif } EntitasEditorLayout.EndVertical(); }
void when_throwing() { Pool pool = null; Entity entity = null; before = () => { var componentNames = new [] { "Health", "Position", "View" }; var metaData = new PoolMetaData("My Pool", componentNames, null); pool = new Pool(componentNames.Length, 42, metaData); entity = pool.CreateEntity(); }; it["creates exception with hint separated by newLine"] = () => { var msg = "Message"; var hint = "Hint"; var ex = new EntitasException(msg, hint); ex.Message.should_be(msg + "\n" + hint); }; it["ignores hint when null"] = () => { var msg = "Message"; string hint = null; var ex = new EntitasException(msg, hint); ex.Message.should_be(msg); }; context["Entity"] = () => { context["when not enabled"] = () => { before = () => { pool.DestroyEntity(entity); }; it["add a component"] = () => printErrorMessage(() => entity.AddComponentA()); it["remove a component"] = () => printErrorMessage(() => entity.RemoveComponentA()); it["replace a component"] = () => printErrorMessage(() => entity.ReplaceComponentA(Component.A)); }; context["when enabled"] = () => { it["add a component twice"] = () => printErrorMessage(() => { entity.AddComponentA(); entity.AddComponentA(); }); it["remove a component that doesn't exist"] = () => printErrorMessage(() => { entity.RemoveComponentA(); }); it["get a component that doesn't exist"] = () => printErrorMessage(() => { entity.GetComponentA(); }); it["retain an entity twice"] = () => printErrorMessage(() => { var owner = new object(); entity.Retain(owner); entity.Retain(owner); }); it["release an entity with wrong owner"] = () => printErrorMessage(() => { var owner = new object(); entity.Release(owner); }); }; }; context["Group"] = () => { it["get single entity when multiple exist"] = () => printErrorMessage(() => { pool.CreateEntity().AddComponentA(); pool.CreateEntity().AddComponentA(); var matcher = (Matcher)Matcher.AllOf(CID.ComponentA); matcher.componentNames = new [] { "Health", "Position", "View" }; var group = pool.GetGroup(matcher); group.GetSingleEntity(); }); }; context["GroupObserver"] = () => { it["unbalanced goups"] = () => printErrorMessage(() => { var g1 = new Group(Matcher.AllOf(CID.ComponentA)); var g2 = new Group(Matcher.AllOf(CID.ComponentB)); var e1 = GroupEventType.OnEntityAdded; new GroupObserver(new [] { g1, g2 }, new [] { e1 }); }); }; context["Pool"] = () => { it["wrong PoolMetaData componentNames count"] = () => printErrorMessage(() => { var componentNames = new [] { "Health", "Position", "View" }; var metaData = new PoolMetaData("My Pool", componentNames, null); new Pool(1, 0, metaData); }); it["destroy entity which is not in pool"] = () => printErrorMessage(() => { pool.DestroyEntity(new Entity(0, null)); }); it["destroy retained entities"] = () => printErrorMessage(() => { pool.CreateEntity().Retain(this); pool.DestroyAllEntities(); }); it["releases entity before destroy"] = () => printErrorMessage(() => { entity.Release(pool); }); }; context["CollectionExtension"] = () => { it["get single entity when more than one exist"] = () => printErrorMessage(() => { new Entity[2].SingleEntity(); }); }; // TODO // context["ComponentBlueprint"] = () => { // // it["type doesn't implement IComponent"] = () => printErrorMessage(() => { // new ComponentBlueprint(42, typeof(FakeComponent).FullName, null); // }); // // it["type doesn't exist"] = () => printErrorMessage(() => { // new ComponentBlueprint(42, "UnknownType", null); // }); // // it["invalid field name"] = () => printErrorMessage(() => { // new ComponentBlueprint(42, typeof(ComponentA).FullName, new SerializableField { // fieldName = "unknownField", // value = 42 // }); // }); // // it["mssing [SerializableAttribute]"] = () => printErrorMessage(() => { // new ComponentBlueprint(1, new ComponentA()); // }); // }; }
public virtual void DestroyEntity(Entity entity) { var removed = _entities.Remove(entity); if(!removed) { throw new ContextDoesNotContainEntityException( "'" + this + "' cannot destroy " + entity + "!", "Did you call context.DestroyEntity() on a wrong context?" ); } _entitiesCache = null; if(OnEntityWillBeDestroyed != null) { OnEntityWillBeDestroyed(this, entity); } entity.destroy(); if(OnEntityDestroyed != null) { OnEntityDestroyed(this, entity); } if(entity.retainCount == 1) { // Can be released immediately without // adding to _retainedEntities entity.OnEntityReleased -= _cachedEntityReleased; _reusableEntities.Push(entity); entity.Release(this); entity.removeAllOnEntityReleasedHandlers(); } else { _retainedEntities.Add(entity); entity.Release(this); } }