public void Execute() { while (entityQueue.Count > 0) { Entity entityToDestroy = entityQueue.Dequeue(); if (entityTransaction.Exists(entityToDestroy)) { //Get the EntityTypeData component to figure out what type of entity we are deleting EntityTypeData entityToDestroyTypeData = entityTransaction.GetSharedComponentData <EntityTypeData>(entityToDestroy); switch (entityToDestroyTypeData.entityType) { case EntityTypeData.EntityType.Asteroid: case EntityTypeData.EntityType.EnemyShip: case EntityTypeData.EntityType.AllyShip: case EntityTypeData.EntityType.PlayerShip: { //Those type of entity will require some additional logic after destruction, //create the info needed and add it to the list EntityInstanceRenderData entityToDestroyRenderData = entityTransaction.GetComponentData <EntityInstanceRenderData>(entityToDestroy); InfoForLogicAfterDestroy newInfo = new InfoForLogicAfterDestroy { entityTypeData = entityToDestroyTypeData, renderData = entityToDestroyRenderData, }; infoForLogic.Add(newInfo); } break; case EntityTypeData.EntityType.Bolt: { //Player bolts are only destroyed when they collided with enemies or obstacle, // add to the score in that case BoltTypeData boltTypeData = entityTransaction.GetSharedComponentData <BoltTypeData>(entityToDestroy); if (boltTypeData.boltType == BoltTypeData.BoltType.PlayerBolt) { UIData uiData = uiDataArray[0]; uiData.score += scoreValue; uiDataArray[0] = uiData; } } break; } //This will remove the entity from the entity manager entityTransaction.DestroyEntity(entityToDestroy); } } }
//Function do some logic after entities have been destroyed (in this case spawn particles) void DestroyLogic(NativeList <InfoForLogicAfterDestroy> infoLogicTmpDataArray) { const float nonPriorityVFXMaxDistance = -50.0f; const float priorityVFXMaxDistance = -23.0f; for (int i = 0; i < infoLogicTmpDataArray.Length; i++) { InfoForLogicAfterDestroy infoLogic = infoLogicTmpDataArray[i]; float3 infoLogicPosition = infoLogic.entityPosition.Value; switch (infoLogic.entityTypeData.entityType) { case EntityTypeData.EntityType.Asteroid: { if (infoLogicPosition.y > nonPriorityVFXMaxDistance && MonoBehaviourECSBridge.Instance.asteroidExplosion != null) { bool priorityParticle = infoLogicPosition.y > priorityVFXMaxDistance ? true : false; MonoBehaviourECSBridge.Instance.asteroidExplosion.SpawnParticle(priorityParticle, infoLogicPosition, Quaternion.identity); } } break; case EntityTypeData.EntityType.EnemyShip: { if (infoLogicPosition.y > nonPriorityVFXMaxDistance && MonoBehaviourECSBridge.Instance.enemyExplosion != null) { bool priorityParticle = infoLogicPosition.y > priorityVFXMaxDistance ? true : false; MonoBehaviourECSBridge.Instance.enemyExplosion.SpawnParticle(priorityParticle, infoLogicPosition, Quaternion.identity); } } break; case EntityTypeData.EntityType.AllyShip: { if (infoLogicPosition.y > nonPriorityVFXMaxDistance && MonoBehaviourECSBridge.Instance.allyExplosion != null) { bool priorityParticle = infoLogicPosition.y > priorityVFXMaxDistance ? true : false; MonoBehaviourECSBridge.Instance.allyExplosion.SpawnParticle(priorityParticle, infoLogicPosition, Quaternion.identity); } } break; case EntityTypeData.EntityType.PlayerShip: { if (MonoBehaviourECSBridge.Instance.playerExplosion != null) { MonoBehaviourECSBridge.Instance.playerExplosion.SpawnParticle(true, infoLogicPosition, Quaternion.identity); } } break; } } }
//Function do some logic after entities have been destroyed (in this case spawn particles) void DestroyLogic(NativeList <InfoForLogicAfterDestroy> infoLogicTmpDataArray) { for (int i = 0; i < infoLogicTmpDataArray.Length; i++) { InfoForLogicAfterDestroy infoLogic = infoLogicTmpDataArray[i]; switch (infoLogic.entityTypeData.entityType) { case EntityTypeData.EntityType.Asteroid: { if (MonoBehaviourECSBridge.Instance.asteroidExplosion != null) { //Fow now only spawn particles close to the player position, this is a normal game object spawn and is slow if (infoLogic.renderData.position.y > -23) { GameObject.Instantiate(MonoBehaviourECSBridge.Instance.asteroidExplosion, infoLogic.renderData.position, Quaternion.LookRotation(infoLogic.renderData.forward, infoLogic.renderData.up)); } } } break; case EntityTypeData.EntityType.Bolt: { } break; case EntityTypeData.EntityType.EnemyShip: { if (MonoBehaviourECSBridge.Instance.enemyExplosion != null) { //Fow now only spawn particles close to the player position, this is a normal game object spawn and is slow if (infoLogic.renderData.position.y > -23) { GameObject.Instantiate(MonoBehaviourECSBridge.Instance.enemyExplosion, infoLogic.renderData.position, Quaternion.LookRotation(infoLogic.renderData.forward, infoLogic.renderData.up)); } } } break; case EntityTypeData.EntityType.AllyShip: { if (MonoBehaviourECSBridge.Instance.allyExplosion != null) { //Fow now only spawn particles close to the player position, this is a normal game object spawn and is slow if (infoLogic.renderData.position.y > -23) { GameObject.Instantiate(MonoBehaviourECSBridge.Instance.allyExplosion, infoLogic.renderData.position, Quaternion.LookRotation(infoLogic.renderData.forward, infoLogic.renderData.up)); } } } break; case EntityTypeData.EntityType.PlayerShip: { if (MonoBehaviourECSBridge.Instance.playerExplosion != null) { GameObject.Instantiate(MonoBehaviourECSBridge.Instance.playerExplosion, infoLogic.renderData.position, Quaternion.LookRotation(infoLogic.renderData.forward, infoLogic.renderData.up)); // Large shake to indicate player has died CameraController.Instance.OverrideWithShake(CameraController.SHAKE_SIZE.Large); } } break; } } }