private List <int> getIngredientsOnPizza(int pizzaIndex) { List <int> currentIngredients = new List <int>(); IngredientData.SetFilter(pizzaData.PizzaGroup[pizzaIndex]); var length = IngredientData.CalculateLength(); for (int i = 0; i < length; i++) { currentIngredients.Add(IngredientData.GetComponentDataArray <Ingredient>()[i].IngredientType); } return(currentIngredients); }
/// <summary> /// Entities (game objects) are all ready there /// we need to just make them visible on the screen /// Here we retrieve an entity from the pool /// </summary> /// <param name="x">transform.position.x</param> /// <param name="y">transform.position.y</param> /// <param name="z">transform.position.z</param> /// <returns>Entity entity (operate with an entity manager later)</returns> private Entity Show(float x, float y, float z) { // filtering out entities for faster selection m_Group.SetFilter(new Indexer { Value = groupIndex }, new Visible { Value = 0 }); // put filtered entities in a EntityArray TODO find a better way EntityArray entityArray = m_Group.GetEntityArray(); // the entity we are going to return will be the first in the list // no need to iterate all the array Entity entity = entityArray[0]; // mark as visible to filter it out from the next call entityManager.SetSharedComponentData(entity, new Visible { Value = 1 }); //entityManager.AddComponentData(entity, new Static { });//TODO add static object option //entityManager.RemoveComponent(entity, typeof(Frozen)); //TODO add static object option // set the position of the entity entityManager.AddComponentData(entity, new Position { Value = new float3(x, y, z) }); // TODO implement this later // normally we should calculate AABB for the object and adjust values before the call // at the moment we should assign the culling sphere center at where the object is // improves performance entityManager.SetComponentData(entity, new MeshRenderBounds { Center = new float3(x, y, z), Radius = 1.0f }); // magic return(entity); }
public void UnloadSceneImmediate(Entity scene) { if (EntityManager.HasComponent <StreamingState>(scene)) { m_SceneFilter.SetFilter(new SceneTag { SceneEntity = scene }); EntityManager.DestroyEntity(m_SceneFilter); m_SceneFilter.ResetFilter(); EntityManager.RemoveComponent <StreamingState>(scene); } }
protected override JobHandle OnUpdate(JobHandle deps) { m_Group = GetComponentGroup( typeof(EcsTestData), typeof(EcsTestData2), ComponentType.ReadOnly(typeof(EcsTestSharedComp))); m_Group.SetFilter(m_sharedComp); DeltaJob job = new DeltaJob(); job.data = m_Group.GetComponentDataArray <EcsTestData>(); job.data2 = m_Group.GetComponentDataArray <EcsTestData2>(); return(job.Schedule(job.data.Length, 1, deps)); }
protected override void OnCreateManager() { base.OnCreateManager(); updateSystem = World.GetExistingManager <ComponentUpdateSystem>(); interpolationGroup = GetComponentGroup( ComponentType.Create <BufferedTransform>(), ComponentType.Create <DeferredUpdateTransform>(), ComponentType.ReadOnly <TransformInternal.Component>(), ComponentType.ReadOnly <SpatialEntityId>(), ComponentType.ReadOnly <InterpolationConfig>(), ComponentType.ReadOnly <TransformInternal.ComponentAuthority>() ); interpolationGroup.SetFilter(TransformInternal.ComponentAuthority.NotAuthoritative); }
protected override void OnCreateManager() { base.OnCreateManager(); worker = World.GetExistingManager <WorkerSystem>(); updateSystem = World.GetExistingManager <ComponentUpdateSystem>(); transformGroup = GetComponentGroup( ComponentType.Create <TransformToSet>(), ComponentType.ReadOnly <TransformInternal.Component>(), ComponentType.ReadOnly <SpatialEntityId>(), ComponentType.ReadOnly <DirectReceiveTag>(), ComponentType.ReadOnly <TransformInternal.ComponentAuthority>() ); transformGroup.SetFilter(TransformInternal.ComponentAuthority.NotAuthoritative); }
protected override void OnUpdate() { EntityManager.GetAllUniqueSharedComponentData(m_UniqueTypes); for (int sharedIndex = 0, numShared = m_UniqueTypes.Count; sharedIndex < numShared; ++sharedIndex) { ActivatableObject activatableObject = m_UniqueTypes[sharedIndex]; m_MainGroup.SetFilter(activatableObject); if (m_MainGroup.CalculateLength() == 0) { continue; } UpdateGameObjectActive(ref activatableObject); } }
private void UpdateTransformData() { transformGroup.SetFilter(TransformInternal.ComponentAuthority.NotAuthoritative); var transformArray = transformGroup.GetComponentArray <UnityEngine.Transform>(); var transformToSetArray = transformGroup.GetComponentDataArray <TransformToSet>(); for (int i = 0; i < transformArray.Length; ++i) { var transformToSet = transformToSetArray[i]; var transform = transformArray[i]; transform.localPosition = transformToSet.Position; transform.localRotation = transformToSet.Orientation; } }
private void UpdateRigidbodyData() { rigidbodyGroup.SetFilter(TransformInternal.ComponentAuthority.NotAuthoritative); var rigidbodyArray = rigidbodyGroup.GetComponentArray <Rigidbody>(); var transformToSetArray = rigidbodyGroup.GetComponentDataArray <TransformToSet>(); for (int i = 0; i < rigidbodyArray.Length; ++i) { var transform = transformToSetArray[i]; var rigidbody = rigidbodyArray[i]; rigidbody.MovePosition(transform.Position); rigidbody.MoveRotation(transform.Orientation); rigidbody.AddForce(transform.Velocity - rigidbody.velocity, ForceMode.VelocityChange); } }
void InstantiateOuterWalls(Board board, Entity boardEntity) { // The outer walls are one unit left, right, up and down from the board. float halfWidth = (board.GridStep.x * board.GridCount.x) * 0.5f; float halfHeight = (board.GridStep.y * board.GridCount.y) * 0.5f; float halfStepX = board.GridStep.x * 0.5f; float halfStepY = board.GridStep.y * 0.5f; float leftEdgeX = (-halfWidth) + halfStepX; float rightEdgeX = halfWidth - halfStepX; float bottomEdgeY = (-halfHeight) + halfStepY; float topEdgeY = halfHeight - halfStepY; // Shift outer wall outward one step leftEdgeX -= board.GridStep.x; rightEdgeX += board.GridStep.x; topEdgeY += board.GridStep.y; bottomEdgeY -= board.GridStep.y; var boardReference = new BoardReference { TileSetId = board.TileSetId }; OuterWallTileGroup.SetFilter(boardReference); var outerWallTileGroupEntities = OuterWallTileGroup.GetEntityArray(); if (outerWallTileGroupEntities.Length > 0) { // copy data from ComponentGroup var tileEntities = new NativeArray <Entity>(outerWallTileGroupEntities.Length, Allocator.Temp, NativeArrayOptions.UninitializedMemory); for (int i = 0; i < outerWallTileGroupEntities.Length; i++) { tileEntities[i] = outerWallTileGroupEntities[i]; } // Instantiate both vertical walls (one on each side). InstantiateVerticalOuterWall(leftEdgeX, bottomEdgeY, topEdgeY, board.GridStep.y, tileEntities, boardEntity); InstantiateVerticalOuterWall(rightEdgeX, bottomEdgeY, topEdgeY, board.GridStep.y, tileEntities, boardEntity); // Instantiate both horizontal walls, these are one in left and right from the outer walls. InstantiateHorizontalOuterWall(leftEdgeX + board.GridStep.x, rightEdgeX - board.GridStep.x, bottomEdgeY, board.GridStep.x, tileEntities, boardEntity); InstantiateHorizontalOuterWall(leftEdgeX + board.GridStep.x, rightEdgeX - board.GridStep.x, topEdgeY, board.GridStep.x, tileEntities, boardEntity); tileEntities.Dispose(); } }
protected override void OnUpdate() { EntityManager.GetAllUniqueSharedComponentData(cachedUniqueSharedSprites); for (int i = 0; i != cachedUniqueSharedSprites.Count; i++) { var sharedSprite = cachedUniqueSharedSprites[i]; componentGroup.SetFilter(sharedSprite); var positions = componentGroup.GetComponentDataArray <Position>(); for (int j = 0; j < positions.Length; j++) { Graphics.DrawMesh(sharedSprite.mesh, positions[j].Value, Quaternion.identity, sharedSprite.mat, 0); } } cachedUniqueSharedSprites.Clear(); }
private void UpdateNewEntityGroup() { newEntityGroup.SetFilter(TransformInternal.ComponentAuthority.NotAuthoritative); var rigidbodyArray = newEntityGroup.GetComponentArray <Rigidbody>(); var kinematicStateWhenAuthArray = newEntityGroup.GetComponentDataArray <KinematicStateWhenAuth>(); for (int i = 0; i < rigidbodyArray.Length; ++i) { var rigidbody = rigidbodyArray[i]; kinematicStateWhenAuthArray[i] = new KinematicStateWhenAuth { KinematicWhenAuthoritative = rigidbody.isKinematic }; rigidbody.isKinematic = true; } }
private void UpdateTransformData() { transformGroup.SetFilter(TransformInternal.ComponentAuthority.Authoritative); var transformArray = transformGroup.GetComponentArray <UnityEngine.Transform>(); var transformToSendArray = transformGroup.GetComponentDataArray <TransformToSend>(); for (int i = 0; i < transformArray.Length; ++i) { var transform = transformArray[i]; var transformToSend = new TransformToSend { Position = transform.position, Velocity = Vector3.zero, Orientation = transform.rotation }; transformToSendArray[i] = transformToSend; } }
private void UpdateRigidbodyData() { rigidbodyGroup.SetFilter(TransformInternal.ComponentAuthority.Authoritative); var rigidbodyArray = rigidbodyGroup.GetComponentArray <Rigidbody>(); var transformToSendArray = rigidbodyGroup.GetComponentDataArray <TransformToSend>(); for (int i = 0; i < rigidbodyArray.Length; ++i) { var rigidbody = rigidbodyArray[i]; var transformToSend = new TransformToSend { Position = rigidbody.position, Velocity = rigidbody.velocity, Orientation = rigidbody.rotation }; transformToSendArray[i] = transformToSend; } }
protected override void OnCreateManager() { base.OnCreateManager(); newPlayerGroup = GetComponentGroup( ComponentType.ReadOnly <PlayerInput.Component>(), ComponentType.ReadOnly <PlayerInput.ComponentAuthority>(), ComponentType.Subtractive <Speed>() ); newPlayerGroup.SetFilter(PlayerInput.ComponentAuthority.Authoritative); playerInputGroup = GetComponentGroup( ComponentType.Create <Rigidbody>(), ComponentType.Create <Speed>(), ComponentType.ReadOnly <PlayerInput.Component>(), ComponentType.ReadOnly <TransformInternal.ComponentAuthority>() ); playerInputGroup.SetFilter(TransformInternal.ComponentAuthority.Authoritative); }
protected override void OnCreateManager() { base.OnCreateManager(); componentUpdateSystem = World.GetExistingManager <ComponentUpdateSystem>(); launcherGroup = GetComponentGroup( ComponentType.ReadOnly <Launcher.Component>(), ComponentType.ReadOnly <SpatialEntityId>(), ComponentType.ReadOnly <Launcher.ComponentAuthority>() ); launcherGroup.SetFilter(Launcher.ComponentAuthority.Authoritative); scoreGroup = GetComponentGroup( ComponentType.ReadOnly <Score.Component>(), ComponentType.ReadOnly <SpatialEntityId>(), ComponentType.ReadOnly <Score.ComponentAuthority>() ); scoreGroup.SetFilter(Score.ComponentAuthority.Authoritative); }
protected override void OnUpdate() { m_Group.SetFilter(new NetworkEntity() { IsLocal = true }); var networkComponents = m_Group.GetSharedComponentDataArray <NetworkEntity>(); var inputComponents = m_Group.GetComponentDataArray <FreeMovementInputReceiver>(); for (int i = 0; i != networkComponents.Length; i++) { var netComponent = networkComponents[i]; var inputComponent = inputComponents[i]; if (netComponent.LocalControlId == 0) //< We only work with the first player for now { } } }
protected override void OnCreateManager() { base.OnCreateManager(); updateSystem = World.GetExistingManager <ComponentUpdateSystem>(); newEntityGroup = GetComponentGroup( ComponentType.Create <KinematicStateWhenAuth>(), ComponentType.ReadOnly <Rigidbody>(), ComponentType.ReadOnly <NewlyAddedSpatialOSEntity>(), ComponentType.ReadOnly <TransformInternal.ComponentAuthority>() ); newEntityGroup.SetFilter(TransformInternal.ComponentAuthority.NotAuthoritative); authChangeGroup = GetComponentGroup( ComponentType.Create <KinematicStateWhenAuth>(), ComponentType.ReadOnly <Rigidbody>(), ComponentType.ReadOnly <SpatialEntityId>(), ComponentType.Subtractive <NewlyAddedSpatialOSEntity>() ); }
protected override void OnCreateManager() { base.OnCreateManager(); rigidbodyGroup = GetComponentGroup( ComponentType.ReadOnly <Rigidbody>(), ComponentType.ReadOnly <TransformToSet>(), ComponentType.ReadOnly <SetTransformToGameObjectTag>(), ComponentType.ReadOnly <TransformInternal.ComponentAuthority>() ); rigidbodyGroup.SetFilter(TransformInternal.ComponentAuthority.NotAuthoritative); transformGroup = GetComponentGroup( ComponentType.Subtractive <Rigidbody>(), ComponentType.ReadOnly <UnityEngine.Transform>(), ComponentType.ReadOnly <TransformToSet>(), ComponentType.ReadOnly <SetTransformToGameObjectTag>(), ComponentType.ReadOnly <TransformInternal.ComponentAuthority>() ); transformGroup.SetFilter(TransformInternal.ComponentAuthority.NotAuthoritative); }
protected override void OnUpdate() { positionGroup.SetFilter(Position.ComponentAuthority.Authoritative); var rateLimitedConfigArray = positionGroup.GetSharedComponentDataArray <RateLimitedSendConfig>(); var positionArray = positionGroup.GetComponentDataArray <Position.Component>(); var transformArray = positionGroup.GetComponentDataArray <TransformInternal.Component>(); var lastSentPositionArray = positionGroup.GetComponentDataArray <LastPositionSentData>(); for (int i = 0; i < positionArray.Length; ++i) { var position = positionArray[i]; var lastPositionSent = lastSentPositionArray[i]; lastPositionSent.TimeSinceLastUpdate += Time.deltaTime; lastSentPositionArray[i] = lastPositionSent; if (lastPositionSent.TimeSinceLastUpdate < 1.0f / rateLimitedConfigArray[i].MaxPositionUpdateRateHz) { continue; } var transform = transformArray[i]; var coords = transform.Location.ToCoordinates(); if (!TransformUtils.HasChanged(coords, position.Coords)) { continue; } position.Coords = coords; positionArray[i] = position; lastPositionSent.TimeSinceLastUpdate = 0.0f; lastPositionSent.Position = position; lastSentPositionArray[i] = lastPositionSent; } }
protected override JobHandle OnUpdate(JobHandle inputDeps) { _trees.Clear(); EntityManager.GetAllUniqueSharedComponentDatas <TreeRuntime>(_trees); //Debug.Log($"Num trees {_trees.Count}"); for (int treeIdx = 1; treeIdx < _trees.Count; ++treeIdx) { var tree = _trees[treeIdx]; _group.SetFilter(tree); for (int i = tree.Def.Nodes.Length - 1; i >= 0; --i) { inputDeps = new Job(tree.Def.Nodes[i], new Job.ExecutionParams { NodeData = tree.GetData(i), }).Schedule(inputDeps); } } return(inputDeps); }
private float DoAddition(int index) { if (additiveGroup.CalculateLength() == 0) { return(0); } additiveGroup.SetFilter(new ScoringGroup() { GroupId = index }); var addScores = additiveGroup.GetComponentDataArray <AddScore>(); int valueToAdd = 0; for (int i = 0; i < addScores.Length; i++) { valueToAdd += addScores[i].Value; } EntityManager.DestroyEntity(additiveGroup.GetEntityArray().GetChunkArray(0, additiveGroup.CalculateLength())); return(valueToAdd); }
protected override JobHandle OnUpdate(JobHandle deps) { EntityManager.GetAllUniqueSharedComponentDatas(_renderers); EntityManager.GetAllUniqueSharedComponentDatas(_variants); var job = new TJob(); for (var i1 = 0; i1 < _renderers.Count; i1++) { var renderer = _renderers[i1]; for (var i2 = 0; i2 < _variants.Count; i2++) { var variant = _variants[i2]; _group.SetFilter(renderer, variant); var groupCount = _group.CalculateLength(); if (groupCount == 0) { continue; } job.Initialize( variant, _group, renderer.Vertices, renderer.Normals, renderer.ConcurrentCounter ); deps = job.Schedule(groupCount, 8, deps); } } _renderers.Clear(); _variants.Clear(); return(deps); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { var neuralNetworks = new List <NeuralNetworkComponentData>(20); EntityManager.GetAllUniqueSharedComponentData(neuralNetworks); for (int i = 0, length = neuralNetworks.Count; i < length; ++i) { var neuralNetwork = neuralNetworks[i]; neuralNetworkGroup.SetFilter(neuralNetwork); var movements = neuralNetworkGroup.GetComponentDataArray <MovementData>(); if (movements.Length != 0) { var job = new UpdateMovementTargetPos { Movements = movements, Positions = neuralNetworkGroup.GetComponentDataArray <Position>(), NeuralNetwork = neuralNetwork, IDs = neuralNetworkGroup.GetComponentDataArray <NeuralNetworkIDComponentData>() }; Utils.Utils.ScheduleBatchedJobAndComplete(job.Schedule(movements.Length, 32, inputDeps)); } } return(inputDeps); }
protected override void OnUpdate() { var uniqueSpawners = new List <WaveGenerator>(2); EntityManager.GetAllUniqueSharedComponentData(uniqueSpawners); var spawner = uniqueSpawners[1]; m_Spawners.SetFilter(spawner); var spawnedCubeEntity = EntityManager.Instantiate(spawner.Prefab[Random.Range(0, spawner.Prefab.Length)]); var randomPosition = spawner.Positions[Random.Range(0, spawner.Positions.Length)]; time = Time.time; // Set the position of the newly spawned cube to the origin. var position = new Position { Value = randomPosition.position }; EntityManager.SetComponentData(spawnedCubeEntity, position); }
private float DoSubtraction(int index) { if (subtractiveGroup.CalculateLength() == 0) { return(0); } subtractiveGroup.SetFilter(new ScoringGroup() { GroupId = index }); var subtractScores = subtractiveGroup.GetComponentDataArray <DeductScore>(); int valueToSubtract = 0; for (int i = 0; i < subtractScores.Length; i++) { valueToSubtract += subtractScores[i].Value; } EntityManager.DestroyEntity(subtractiveGroup.GetEntityArray().GetChunkArray(0, subtractiveGroup.CalculateLength())); return(valueToSubtract); }
protected override void OnUpdate() { // We want to iterate over all unique MeshInstanceRenderer shared component data, // that are attached to any entities in the world EntityManager.GetAllUniqueSharedComponentDatas(m_CacheduniqueRendererTypes); for (int i = 0; i != m_CacheduniqueRendererTypes.Count; i++) { // For each unique MeshInstanceRenderer data, we want to get all entities with a TransformMatrix // SharedComponentData gurantees that all those entities are packed togehter in a chunk with linear memory layout. // As a result the copy of the matrices out is internally done via memcpy. var renderer = m_CacheduniqueRendererTypes[i]; m_InstanceRendererGroup.SetFilter(renderer); var transforms = m_InstanceRendererGroup.GetComponentDataArray <TransformMatrix>(); // Graphics.DrawMeshInstanced has a set of limitations that are not optimal for working with ECS. // Specifically: // * No way to push the matrices from a job // * no NativeArray API, currently uses Matrix4x4[] // As a result this code is not yet jobified. // We are planning to adjust this API to make it more efficient for this use case. // For now, we have to copy our data into Matrix4x4[] with a specific upper limit of how many instances we can render in one batch. // So we just have a for loop here, representing each Graphics.DrawMeshInstanced batch int beginIndex = 0; while (beginIndex < transforms.Length) { int length = math.min(m_MatricesArray.Length, transforms.Length - beginIndex); CopyMatrices(transforms, beginIndex, length, m_MatricesArray); Graphics.DrawMeshInstanced(renderer.mesh, 0, renderer.material, m_MatricesArray, length, null, renderer.castShadows, renderer.receiveShadows); beginIndex += length; } } m_CacheduniqueRendererTypes.Clear(); }
protected override void OnCreateManager() { base.OnCreateManager(); workerSystem = World.GetExistingManager <WorkerSystem>(); componentUpdateSystem = World.GetExistingManager <ComponentUpdateSystem>(); commandSystem = World.GetExistingManager <CommandSystem>(); initGroup = GetComponentGroup( ComponentType.ReadOnly <HealthRegenComponent.Component>(), ComponentType.Subtractive <HealthRegenData>(), ComponentType.ReadOnly <HealthComponent.ComponentAuthority>() ); initGroup.SetFilter(HealthComponent.ComponentAuthority.Authoritative); regenGroup = GetComponentGroup( ComponentType.Create <HealthRegenComponent.Component>(), ComponentType.Create <HealthRegenData>(), ComponentType.ReadOnly <HealthComponent.Component>(), ComponentType.ReadOnly <SpatialEntityId>(), ComponentType.ReadOnly <HealthComponent.ComponentAuthority>() ); regenGroup.SetFilter(HealthComponent.ComponentAuthority.Authoritative); }
protected override JobHandle OnUpdate(JobHandle deps) { var commandBuffer = _barrier.CreateCommandBuffer(); EntityManager.GetAllUniqueSharedComponentDatas(_variants); foreach (var variant in _variants) { _group.SetFilter(variant); var job = new ParticleExpirationJob() { Entities = _group.GetEntityArray(), Particles = _group.GetComponentDataArray <Particle>(), Life = variant.GetLife(), CommandBuffer = commandBuffer }; deps = job.Schedule(deps); } _variants.Clear(); return(deps); }
protected override void OnUpdate() { // Enumerate all the buffers. EntityManager.GetAllUniqueSharedComponentData <SpawnInfo>(_uniques); int tempValue = -_maxValue; int heightCount = 0; float randomSpeed = 0f; Random random = new Random((uint)System.DateTime.Now.Ticks); for (var i = 0; i < _uniques.Count; i++) { _group.SetFilter(_uniques[i]); // Get a copy of the entity array. // Don't directly use the iterator -- we're going to remove // the buffer components, and it will invalidate the iterator. var iterator = _group.GetEntityArray(); var entities = new NativeArray <Entity>(iterator.Length, Allocator.Temp); iterator.CopyTo(entities); // Instantiate actors along with the buffer entities. for (var j = 0; j < entities.Length; j++) { // Create the first voxel. var voxel = EntityManager.CreateEntity(_actorArchetype); EntityManager.SetComponentData(voxel, new Brick { ID = _counter++ }); EntityManager.SetSharedComponentData(voxel, _uniques[i].RendererSettings); //EntityManager.SetSharedComponentData(voxel, rm); // Make clones from the first voxel. var cloneCount = _uniques[i].MaxCount - 1; if (cloneCount > 0) { var clones = new NativeArray <Entity>(cloneCount, Allocator.Temp); EntityManager.Instantiate(voxel, clones); randomSpeed = random.NextFloat(0f, 2f); //RenderMesh rm = GetUniqueMaterial(heightCount, _uniques[i].RendererSettings); for (var k = 0; k < cloneCount; k++) { EntityManager.SetComponentData(clones[k], new Position { Value = new float3(tempValue, heightCount, heightCount) }); EntityManager.SetComponentData(clones[k], new Scale { Value = new float3(1, 1, 1) }); EntityManager.SetComponentData(clones[k], new Brick { ID = _counter++, offset = 0, bIncrease = 1, moveSpeed = randomSpeed, oriPosition = new float3(tempValue, heightCount, heightCount) }); //, //TODO:give each brick a different Material value seems to be impossible,i will do it in the Shader //EntityManager.SetSharedComponentData(clones[k], rm); tempValue = tempValue + 1; if (tempValue > _maxValue) { heightCount++; tempValue = -_maxValue; randomSpeed = random.NextFloat(0f, 0.2f); //rm = GetUniqueMaterial(heightCount, _uniques[i].RendererSettings); } } clones.Dispose(); } // Remove the buffer component from the entity. EntityManager.RemoveComponent(entities[j], typeof(SpawnInfo)); } entities.Dispose(); } _uniques.Clear(); }