void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities) { for (int i = 0; i < entities.Length; i++) { ECSEntity entity = entities[i]; TimeComponent tc = engine.GetComponent <TimeComponent>(entity); tc.CurrentTime -= Time.DeltaTime; if (tc.CurrentTime <= 0f) { tc.CurrentTime += tc.Time; SpawnPointComponent pc = engine.GetComponent <SpawnPointComponent>(entity); var enemy = engine.CreateEntity(); engine.AddComponent <NameComponent>(enemy, new NameComponent() { Name = "ENEMY" }); engine.AddComponent <PositionComponent>(enemy, new PositionComponent() { Position = pc.SpawnPoint }); engine.AddComponent <VelocityComponent>(enemy, new VelocityComponent() { Velocity = new Vector2(0f, 5f) }); engine.AddComponent(enemy, new BoxColliderComponent() { Size = new Vector2(1f, 1f) }); engine.AddComponent <SpriteRendererComponent>(enemy, new SpriteRendererComponent() { RenderOffset = RenderOffset.Player, Texture = TextureManager.GetTexture("EnemyBlack2"), Sprite = new Sprite(1f, 1f) { pivot = Vector2.One * 0.5f } }); engine.AddComponent <AIComponent>(enemy, new AIComponent()); } engine.SetComponent <TimeComponent>(entity, tc); } }
public void Initialize() { PlayerSpawnPointComponentInstance = Instantiate( playerSpawnPointComponent, new Vector3(-10f, -10f, 0f), Quaternion.identity); EnemySpawnPointComponentInstance = Instantiate( enemySpawnPointComponent, new Vector3(10f, 10f, 0f), Quaternion.identity); }
public Entity Spawn(SpawnPointComponent point) { var settings = SimulationSettings.Instance; int unitCount = SimulationSettings.Instance.UnitsPerFormation; var formationData = new FormationData(unitCount, settings.FormationWidth, point.IsFriendly, point.Type, point.Position); //formationData.EnableMovement = false; var formationEntity = spawnerObject.Spawn(formationData, point.offset, formationWaypoints, true); ++totalRequested; return(formationEntity); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { if (!initialized) { Initialize(); } // Schedule instantiation if (Input.GetKeyDown(KeyCode.S)) { SpawnAdditionalUnits(); } if (!SimulationSettings.Instance.EnableSpawners) { return(inputDeps); } if (SpawnPoints.Length == 0) { return(inputDeps); } inputDeps.Complete(); // Enable spawn point to spawn units in time slots for (int i = 0; i < SpawnPoints.Length; i++) { SpawnPointComponent point = SpawnPoints[i]; float spawnTime = SimulationSettings.Instance.SpawnTimesPerUnit[(int)point.Type]; // spawn point is ready to spawn units if (point.ElapsedTime >= spawnTime) { point.ElapsedTimeSinceLastBatch = point.ElapsedTimeSinceLastBatch + Time.deltaTime; // Ready to spawn. Create new formation data if (point.NumberOfSpawnedUnits == 0) { if (textureAnimatorSystem.initialized && textureAnimatorSystem.perUnitTypeDataHolder[point.Type].Count + SimulationSettings.Instance.UnitsPerFormation >= 34000) { continue; } if (textureAnimatorSystem.initialized) { textureAnimatorSystem.perUnitTypeDataHolder[point.Type].Count = textureAnimatorSystem.perUnitTypeDataHolder[point.Type].Count + SimulationSettings.Instance.UnitsPerFormation; } var entity = Spawn(point); var forData = EntityManager.GetComponentData <FormationData>(entity); forData.FormationState = FormationData.State.Spawning; point.NumberOfSpawnedUnits = point.NumberOfSpawnedUnits + SimulationSettings.countPerSpawner; forData.SpawnedCount = forData.SpawnedCount + SimulationSettings.countPerSpawner; EntityManager.SetComponentData(entity, forData); point.formationEntity = entity; var path = EntityManager.GetComponentData <FormationHighLevelPath>(entity); int pathIndex = point.minPathIndex + (point.SpawnedFormationCount % (point.maxPathIndex + 1 - point.minPathIndex)); List <float3> pathList = newHighLevelRoutes[pathIndex]; path.target0 = point.IsFriendly ? pathList[3] : pathList[0]; path.target1 = point.IsFriendly ? pathList[2] : pathList[1]; path.target2 = point.IsFriendly ? pathList[1] : pathList[2]; path.ultimateDestination = point.IsFriendly ? pathList[0] : pathList[3]; EntityManager.SetComponentData(entity, path); point.SpawnedFormationCount++; } else { var formationsFromEntity = GetComponentDataFromEntity <FormationData>(); var formation = formationsFromEntity[point.formationEntity]; if (point.ElapsedTimeSinceLastBatch >= SimulationSettings.Instance.TimeToSpawnBatch) { point.ElapsedTimeSinceLastBatch = point.ElapsedTimeSinceLastBatch - SimulationSettings.Instance.TimeToSpawnBatch; point.NumberOfSpawnedUnits = point.NumberOfSpawnedUnits + SimulationSettings.countPerSpawner; formation.SpawnedCount = formation.SpawnedCount + SimulationSettings.countPerSpawner; } if (point.NumberOfSpawnedUnits >= SimulationSettings.Instance.UnitsPerFormation) { point.NumberOfSpawnedUnits = 0; point.ElapsedTime = point.ElapsedTime - spawnTime; formation.FormationState = FormationData.State.Walking; } formationsFromEntity[point.formationEntity] = formation; } } else { point.ElapsedTime = point.ElapsedTime + Time.deltaTime; } SpawnPoints[i] = point; } return(new JobHandle()); }