/// <summary> /// Loads the scene at the given index /// </summary> /// <param name="numberInFlow">The index of the scene you want to load</param> /// <param name="entityManager">A reference to the entitymanager instance</param> /// <param name="entities">A reference to the entityQueryBuilder instance</param> /// <returns>A bool to mark the succesfull scene loading</returns> /// <exception cref="TriedToLoadDisabledSceneException">When you try to load a disabled scene</exception> private static bool LoadFlowScene(int numberInFlow, EntityManager entityManager, EntityQueryBuilder entities) { try { bool loadedScene = false; if (_flow[numberInFlow].disabledInFlow) { throw new TriedToLoadDisabledSceneException(); } loadedScene = SceneHandlerSystem.NextScene(_flow[numberInFlow].flowScene, entityManager, entities, false); if (loadedScene) { entities.ForEach((ref FlowStatistics stats) => { stats.currentScene = numberInFlow; }); } return(loadedScene); } catch (Exception e) { Debug.Log(e.ToString()); } return(false); }
/// <summary> /// Checks every spawner /// if on a timer, checks for the timers /// loads scenes if spawner is triggered /// </summary> /// <exception cref="NoTimerFoundException">If no timers are found while spawntype is timed</exception> protected override void OnUpdate() { Entities.ForEach((ref Spawner spawner) => { var currentSpawner = spawner; if (spawner.spawnType == SpawnType.Timed) { var timerArray = GetEntityQuery(typeof(Timer.Timer)).ToEntityArray(Allocator.Temp); if (timerArray.Length == 0) { throw new NoTimerFoundException(currentSpawner.ID); } timerArray.Dispose(); Entities.ForEach((ref Timer.Timer timer) => { if (timer.isMarkedDone && (timer.ID == currentSpawner.timerID)) { currentSpawner.isTriggered = true; TimerSystem.ResetTimer(timer.ID, Entities); } }); } if ((!spawner.isTriggered && !currentSpawner.isTriggered) || spawner.isSpawningPaused) { return; } switch (spawner.translationType) { case (TranslationType.Position): SceneHandlerSystem.LoadSceneAsyncWithPosition(spawner.sceneReferenceToSpawn, false, Entities, spawner.spawnPos); break; case (TranslationType.Offset): SceneHandlerSystem.LoadSceneAsyncWithOffset(spawner.sceneReferenceToSpawn, false, Entities, spawner.spawnOffset); break; case (TranslationType.PositionAndOffset): SceneHandlerSystem.LoadSceneAsyncWithPositionAndOffset(spawner.sceneReferenceToSpawn, false, Entities, spawner.spawnPos, spawner.spawnOffset); break; case TranslationType.None: SceneHandlerSystem.LoadSceneAsync(spawner.sceneReferenceToSpawn, false, Entities); break; default: SceneHandlerSystem.LoadSceneAsync(spawner.sceneReferenceToSpawn, false, Entities); break; } spawner.isTriggered = false; }); }
protected override void OnUpdate() { NativeArray <Entity> maxBoundsArray = GetEntityQuery(typeof(MaxBounds)).ToEntityArray(Allocator.Temp); NativeArray <Entity> aspectArray = GetEntityQuery(typeof(AspectRatio)).ToEntityArray(Allocator.Temp); if (maxBoundsArray.Length > 0) { Entities.ForEach((Entity e, ref MaxBounds maxBounds) => { _maxBoundsPos = maxBounds.maxBoundsCenterPos; _maxBoundsSize = maxBounds.maxBoundsSize; }); } else { if (aspectArray.Length > 0) { var aspectRatio = EntityManager.GetComponentData <AspectRatio>(aspectArray[0]).aspecratio; Entities.ForEach((Entity e, ref Camera2D camera2D, ref Translation translation) => { _maxBoundsPos.x = translation.Value.x; _maxBoundsPos.y = translation.Value.y; _maxBoundsSize.y = 2 * camera2D.halfVerticalSize; _maxBoundsSize.x = (2 * camera2D.halfVerticalSize) * (aspectRatio.x / aspectRatio.y); }); } else { throw new NoAspectRatioSetException(); } } Entities.WithAll(typeof(DestroyWhenOutOfBounds)).ForEach((Entity entity, ref Translation translation) => { if (EntityManager.HasComponent <Sprite2DRendererOptions>(entity)) { var hitbox = EntityManager.GetComponentData <Sprite2DRendererOptions>(entity).size; if (IsOutOfBounds(translation, hitbox)) { PostUpdateCommands.DestroyEntity(entity); } } else if (EntityManager.HasComponent <RectHitBox2D>(entity)) { var hitbox = EntityManager.GetComponentData <RectHitBox2D>(entity).box; if (IsOutOfBounds(translation, hitbox.Size)) { PostUpdateCommands.DestroyEntity(entity); } } else { if (IsOutOfBounds(translation)) { PostUpdateCommands.DestroyEntity(entity); } } }); Entities.WithAll(typeof(DestroyAllWhenOutOfBounds)).ForEach((entity) => { Entities.ForEach((Entity e, ref Translation translation) => { try { if (EntityManager.HasComponent <DoNotDestroyWhenOutOfBounds>(e)) { return; } if (EntityManager.HasComponent <Sprite2DRendererOptions>(e)) { var hitbox = EntityManager.GetComponentData <Sprite2DRendererOptions>(e).size; if (IsOutOfBounds(translation, hitbox)) { PostUpdateCommands.DestroyEntity(e); } } else if (EntityManager.HasComponent <RectHitBox2D>(e)) { var hitbox = EntityManager.GetComponentData <RectHitBox2D>(e).box; if (IsOutOfBounds(translation, hitbox.Size)) { PostUpdateCommands.DestroyEntity(e); } } else { if (IsOutOfBounds(translation)) { PostUpdateCommands.DestroyEntity(e); } } } catch (Exception exception) { Debug.Log(exception.ToString()); } }); }); Entities.WithAll(typeof(UnloadWhenOutOfBounds)).ForEach((Entity entity, ref Translation translation) => { if (EntityManager.HasComponent <Sprite2DRendererOptions>(entity)) { var hitbox = EntityManager.GetComponentData <Sprite2DRendererOptions>(entity).size; if (IsOutOfBounds(translation, hitbox)) { SceneHandlerSystem.MarkSceneForUnload(EntityManager, Entities, SceneHandlerSystem.GetSceneFromEntityHandled(EntityManager, entity)); } } else if (EntityManager.HasComponent <RectHitBox2D>(entity)) { var hitbox = EntityManager.GetComponentData <RectHitBox2D>(entity).box; if (IsOutOfBounds(translation, hitbox.Size)) { SceneHandlerSystem.MarkSceneForUnload(EntityManager, Entities, SceneHandlerSystem.GetSceneFromEntityHandled(EntityManager, entity)); } } else { if (IsOutOfBounds(translation)) { SceneHandlerSystem.MarkSceneForUnload(EntityManager, Entities, SceneHandlerSystem.GetSceneFromEntityHandled(EntityManager, entity)); } } }); Entities.WithAll(typeof(UnloadAllWhenOutOfBounds)).ForEach((entity) => { Entities.ForEach((Entity e, ref Translation translation) => { try { if (EntityManager.HasComponent <DoNotUnloadWhenOutOfBounds>(e)) { return; } if (EntityManager.HasComponent <Sprite2DRendererOptions>(e)) { var hitbox = EntityManager.GetComponentData <Sprite2DRendererOptions>(e).size; if (IsOutOfBounds(translation, hitbox)) { SceneHandlerSystem.MarkSceneForUnload(EntityManager, Entities, SceneHandlerSystem.GetSceneFromEntityHandled(EntityManager, e)); } } else if (EntityManager.HasComponent <RectHitBox2D>(e)) { var hitbox = EntityManager.GetComponentData <RectHitBox2D>(e).box; if (IsOutOfBounds(translation, hitbox.Size)) { SceneHandlerSystem.MarkSceneForUnload(EntityManager, Entities, SceneHandlerSystem.GetSceneFromEntityHandled(EntityManager, e)); } } else { if (IsOutOfBounds(translation)) { SceneHandlerSystem.MarkSceneForUnload(EntityManager, Entities, SceneHandlerSystem.GetSceneFromEntityHandled(EntityManager, e)); } } } catch (Exception exception) { Debug.Log(exception.ToString()); } }); }); }