public void Execute(int index) { AIMoveData aiMoveData = aiMoveDataArray[index]; aiMoveData.position += (aiMoveData.speed * aiMoveData.forwardDirection * deltaTime); aiMoveDataArray[index] = aiMoveData; EntityInstanceRenderData entityInstanceRenderData = entityInstanceRenderDataArray[index]; entityInstanceRenderData.position = aiMoveData.position; entityInstanceRenderData.forward = aiMoveData.forwardDirection; entityInstanceRenderData.up = new float3(0, 1, 0); entityInstanceRenderDataArray[index] = entityInstanceRenderData; EntityBoundCenterData entityBoundCenterData = entityBoundCenterDataArray[index]; EntityBoundMinMaxData entityBoundMinMaxData = entityBoundMinMaxDataArray[index]; entityBoundCenterData.centerPosition = aiMoveData.position + entityBoundOffsetDataArray[index].offset; entityBoundMinMaxData.min = entityBoundCenterData.centerPosition - entityBoundExtendDataArray[index].extend; entityBoundMinMaxData.max = entityBoundCenterData.centerPosition + entityBoundExtendDataArray[index].extend; entityBoundCenterDataArray[index] = entityBoundCenterData; entityBoundMinMaxDataArray[index] = entityBoundMinMaxData; }
void AIMove(NativeArray <AIMoveData> aiMoveDataArray, NativeArray <Position> positionArray, NativeArray <Rotation> rotationArray, NativeArray <EntityBoundCenterData> boundCenterDataArray, NativeArray <EntityBoundMinMaxData> boundMinMaxDataArray, NativeArray <EntityBoundOffsetData> boundOffsetDataArray, NativeArray <EntityBoundExtendData> boundExtendDataArray) { int dataCount = aiMoveDataArray.Length; for (int dataIndex = 0; dataIndex < dataCount; dataIndex++) { Position position = positionArray[dataIndex]; Rotation rotation = rotationArray[dataIndex]; AIMoveData aiMoveData = aiMoveDataArray[dataIndex]; float3 forwardDirection = math.forward(rotation.Value); position.Value += (aiMoveData.speed * forwardDirection * deltaTime); positionArray[dataIndex] = position; EntityBoundCenterData entityBoundCenterData = boundCenterDataArray[dataIndex]; EntityBoundMinMaxData entityBoundMinMaxData = boundMinMaxDataArray[dataIndex]; entityBoundCenterData.centerPosition = position.Value + boundOffsetDataArray[dataIndex].offset; entityBoundMinMaxData.min = entityBoundCenterData.centerPosition - boundExtendDataArray[dataIndex].extend; entityBoundMinMaxData.max = entityBoundCenterData.centerPosition + boundExtendDataArray[dataIndex].extend; boundCenterDataArray[dataIndex] = entityBoundCenterData; boundMinMaxDataArray[dataIndex] = entityBoundMinMaxData; } }
public void Execute(int index) { AIMoveData aiMoveData = aiMoveDataArray[index]; AISpawnBoltData aiSpawnBoltData = aiSpawnBoltDataArray[index]; aiSpawnBoltData.spawnPosition = aiMoveData.position + (aiMoveData.forwardDirection * aiSpawnBoltData.offset); aiSpawnBoltData.spawnDirection = aiMoveData.forwardDirection; aiSpawnBoltData.timeSinceFire += deltaTime; if (aiSpawnBoltData.timeSinceFire > aiSpawnBoltData.fireRate) { spawnBoltEntityQueue.Enqueue(entityArray[index]); aiSpawnBoltData.timeSinceFire = 0.0f; } aiSpawnBoltDataArray[index] = aiSpawnBoltData; }
protected override void OnUpdate() { //Go over all our spawners and figure out if any new entity need to be spawned //Add the spawning info to spawnerSpawnInfoList for (int i = 0; i < spawnerDataGroup.Length; i++) { SpawnerPositionData spawnerPositionData = spawnerDataGroup.spawnerPositionDataArray[i]; SpawnerHazardData spawnerHazardData = spawnerDataGroup.spawnerHazardDataArray[i]; SpawnerSpawnData spawnerSpawnData = spawnerDataGroup.spawnerSpawnDataArray[i]; if (spawnerHazardData.hazardIndexArrayLength == 0) { continue; } spawnerSpawnData.timeSinceLastSpawn += Time.deltaTime; while (spawnerSpawnData.timeSinceLastSpawn >= spawnerSpawnData.spawnDelay) { spawnerSpawnData.timeSinceLastSpawn -= spawnerSpawnData.spawnDelay; float yPositionSpawn = spawnerPositionData.position.y; //Get a random index to spawn, the range depend on the amount of hazards we set in the editor int hazardToSpawnIndex = Random.Range(0, spawnerHazardData.hazardIndexArrayLength); SpawnerSpawnInfo spawnInfo = new SpawnerSpawnInfo { spawnYPosition = yPositionSpawn, hazardIndexToSpawn = hazardToSpawnIndex, isBackgroundSpawn = spawnerHazardData.isBackgroundSpawner, }; spawnerSpawnInfoList.Add(spawnInfo); } spawnerDataGroup.spawnerSpawnDataArray[i] = spawnerSpawnData; } float3 cameraPosition = MonoBehaviourECSBridge.Instance.gameCamera.transform.position; float3 forwardDirection = new float3(0.0f, 0.0f, 1.0f); //We use the view frustum height/witdh at our spawner y position to figure out where to spawn the new entities //The calculation assume a camera pointing down (no angle) for (int i = 0; i < spawnerSpawnInfoList.Length; i++) { SpawnerSpawnInfo spawnInfo = spawnerSpawnInfoList[i]; float yPosition = spawnInfo.spawnYPosition; float ydeltaFromCamera = Mathf.Abs(yPosition - cameraPosition.y); float halfFrustumHeight = ydeltaFromCamera * Mathf.Tan(Camera.main.fieldOfView * 0.5f * Mathf.Deg2Rad); //Spawn outside of the view frustrum to avoid any "popup" halfFrustumHeight *= 1.05f; float halfFrustumWidth = halfFrustumHeight * MonoBehaviourECSBridge.Instance.gameCamera.aspect; //Enemy/Hazard are spawned from the top of the screen, Allies are spawned from the bottom float3 spawnPositionHazard = new Vector3(Random.Range(cameraPosition.x - halfFrustumWidth, cameraPosition.x + halfFrustumWidth), yPosition, cameraPosition.z + halfFrustumHeight); float3 spawnPositionAlly = new Vector3(Random.Range(cameraPosition.x - halfFrustumWidth, cameraPosition.x + halfFrustumWidth), yPosition, cameraPosition.z - halfFrustumHeight); //Spawn the hazard using the index we randomnly generated Entity newHazardEntity = EntityManager.Instantiate(MonoBehaviourECSBridge.Instance.GetPrefabHazardEntity(spawnInfo.hazardIndexToSpawn, spawnInfo.isBackgroundSpawn)); //Make sure to remove the prefab "tag" data component EntityManager.RemoveComponent <EntityPrefabData>(newHazardEntity); //Based on the type of entity spawned, set the data needed for it EntityTypeData entityTypeData = EntityManager.GetSharedComponentData <EntityTypeData>(newHazardEntity); switch (entityTypeData.entityType) { case EntityTypeData.EntityType.Asteroid: { Vector3 spawnRenderForward = new Vector3(Random.value, Random.value, 1.0f); spawnRenderForward.Normalize(); Vector3 spawnRotationAxis = new Vector3(Random.value, 1.0f, Random.value); spawnRotationAxis.Normalize(); AsteroidMoveData moveData = EntityManager.GetComponentData <AsteroidMoveData>(newHazardEntity); moveData.position = spawnPositionHazard; moveData.forwardDirection = -forwardDirection; moveData.renderForward = spawnRenderForward; moveData.rotationAxis = spawnRotationAxis; EntityManager.SetComponentData <AsteroidMoveData>(newHazardEntity, moveData); } break; case EntityTypeData.EntityType.EnemyShip: { AIMoveData moveData = EntityManager.GetComponentData <AIMoveData>(newHazardEntity); moveData.position = spawnPositionHazard; moveData.forwardDirection = -forwardDirection; EntityManager.SetComponentData <AIMoveData>(newHazardEntity, moveData); } break; case EntityTypeData.EntityType.AllyShip: { AIMoveData moveData = EntityManager.GetComponentData <AIMoveData>(newHazardEntity); moveData.position = spawnPositionAlly; moveData.forwardDirection = forwardDirection; EntityManager.SetComponentData <AIMoveData>(newHazardEntity, moveData); } break; } } spawnerSpawnInfoList.Clear(); }