public void TestEntitiesCreation_Pass() { // new entities that are created in continuous manner // should have increasing identifiers with a delta equals to 1 for (uint i = 0; i < 10; ++i) { Assert.AreEqual(i, (uint)mWorldContext.CreateEntity()); Assert.AreEqual(i, (uint)mWorldContext.GetEntityById((EntityId)i).Id); } }
/// <summary> /// The method creates a new entity with the only component that's already attached to it. /// The component is TDisposableComponent. All entities that have that component are destroyed /// at an end of a frame with a special system. /// </summary> /// <param name="worldContext">A reference to IWorldContext implementation</param> /// <param name="name">A name of an entity (optional)</param> /// <returns>The method returns an identifier of created entity</returns> public static EntityId CreateDisposableEntity(this IWorldContext worldContext, string name = null) { IEntity entity = worldContext.GetEntityById(worldContext.CreateEntity(name)); entity.AddComponent <TDisposableComponent>(); return(entity.Id); }
/// <summary> /// The method prepares the view for initialization step /// </summary> /// <param name="worldContext">A reference to IWorldContext implementation</param> public void PreInit(IWorldContext worldContext) { WorldContext = worldContext; // create a new event which is an entity with attached component to inform system to register this view in the world's context IEntity registerViewRequestEntity = worldContext.GetEntityById(worldContext.CreateEntity()); registerViewRequestEntity.AddComponent(new TOnViewWaitForInitEventComponent { mView = this }); }
protected void _addEntityToReactiveSystemsBuffer(EntityId entityId) { IEntity entity = mWorldContext.GetEntityById(entityId); //NOTE: Maybe we should check up for duplicates of the entity later if (entity == null || mReactiveSystemsBuffer.Contains(entity)) { return; } mReactiveSystemsBuffer.Add(entity); }
public InputSystem(IWorldContext worldContext, Camera mainCamera) { mWorldContext = worldContext; // We create a new entity which will be unique and store information about user's clicks // // For the sake of safety methods of the framework were designed in way when they return handles not references. // So when you need to get some it's better to seek for the instance via its handle mClickInfoEntity = mWorldContext.GetEntityById(mWorldContext.CreateEntity("ClickInfoEntity")); mClickInfoEntity.AddComponent <TClickComponent>(/* You can also set some initial values for the component here*/); mMainCamera = mainCamera; }
public void OnEvent(TNewEntityCreatedEvent eventData) { IEntity entity = mWorldContext.GetEntityById(eventData.mEntityId); #if false//DEBUG Debug.Log($"[WorldContextsManager] A new entity ({entity.Name}) was created"); #endif GameObject entityGO = _createEntityGOView(entity, _cachedTransform); // initializes the observer of the entity EntityObserver entityObserver = entityGO.AddComponent <EntityObserver>(); entityObserver.Init(mWorldContext, entity.Id); }
/// <summary> /// The method instantiates a new instance of a given prefab /// </summary> /// <param name="prefab">A reference to a prefab that should be created dynamically</param> /// <param name="position">A position of a new object</param> /// <param name="rotation">An orientation of an object in a space</param> /// <param name="parent">A parent of a new created object</param> /// <returns>A reference to a new created entity which is attached to a new created GameObject</returns> public IEntity Spawn(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent) { GameObject gameObjectInstance = GameObject.Instantiate(prefab, position, rotation, parent); IView viewComponent = gameObjectInstance.GetComponent <IView>() ?? throw new NullReferenceException(); EntityId linkedEntityId = mWorldContext.CreateEntity(/*gameObjectInstance.name*/); IEntity linkedEntity = mWorldContext.GetEntityById(linkedEntityId); viewComponent.WorldContext = mWorldContext; linkedEntity.AddComponent(new TViewComponent { mView = viewComponent as BaseView }); viewComponent.Link(linkedEntityId); return(linkedEntity); }
public void TestUpdateMethodOfSystem_PassCorrectData_SuccessfullyProcessesData() { string entityName = "entity01"; // prepare data var e = mWorldContext.GetEntityById(mWorldContext.CreateEntity(entityName)); e.AddComponent <TOnViewWaitForInitEventComponent>(); Assert.IsNotNull(e); Assert.AreEqual(e.Name, entityName); // execute system mSystemManager.Update(0.0f); // check results Assert.DoesNotThrow(() => { Assert.IsFalse(e.HasComponent <TOnViewWaitForInitEventComponent>()); Assert.IsTrue(e.HasComponent <TViewComponent>()); }); }
/// <summary> /// The system is an update one that destroys all disposable entities at an end of a frame /// </summary> /// <param name="worldContext"></param> /// <param name="dt"></param> public static void DisposableEntitiesCollectorSystem(IWorldContext worldContext, float dt) { var disposableEntities = worldContext.GetEntitiesWithAll(typeof(TDisposableComponent)); for (int i = 0; i < disposableEntities.Count; ++i) { IEntity currEntity = worldContext.GetEntityById(disposableEntities[i]); if (currEntity.HasComponent <TEntityLifetimeComponent>()) { TEntityLifetimeComponent lifetimeData = currEntity.GetComponent <TEntityLifetimeComponent>(); currEntity.AddComponent(new TEntityLifetimeComponent { mCounter = lifetimeData.mCounter - 1 }); if (lifetimeData.mCounter > 0) { continue; } } worldContext.DestroyEntity(disposableEntities[i]); } }
public void Update(float deltaTime) { // get all entities with TRotatingCubeComponent component List <uint> entities = mWorldContext.GetEntitiesWithAll(typeof(TRotatingCubeComponent)); IEntity currEntity = null; for (int i = 0; i < entities.Count; ++i) { currEntity = mWorldContext.GetEntityById(entities[i]); // now we have two ways: strongly coupled code or use TinyECS approach // first version: /* * uncomment to test this version * * TViewComponent viewComponent = currEntity.GetComponent<TViewComponent>(); * * TRotatingCubeComponent rotatingCubeComponent = currEntity.GetComponent<TRotatingCubeComponent>(); * * viewComponent.mView.transform.Rotate(Vector3.up, rotatingCubeComponent.mSpeed); */ // second version (TinyECS approach) /* */ TRotatingCubeComponent rotatingCubeComponent = currEntity.GetComponent <TRotatingCubeComponent>(); TRotationComponent rotationComponent = currEntity.GetComponent <TRotationComponent>(); currEntity.AddComponent(new TRotationComponent { mRotation = rotationComponent.mRotation * Quaternion.Euler(0.0f, rotatingCubeComponent.mSpeed, 0.0f) }); } }
public static IEntity CreateAndGetEntity(this IWorldContext worldContext, string name = null) { return(worldContext.GetEntityById(worldContext.CreateEntity(name))); }