using Unity.Entities; using Unity.Mathematics; using Unity.Tiny.Input; using Unity.Transforms; using Unity.Tiny; namespace Unity.Spaceship { public class RestartSystem : SystemBase { private EntityQuery m_AsteroidsQuery; private EntityQuery m_MissileQuery; protected override void OnCreate() { base.OnCreate(); RequireSingletonForUpdate(); RequireSingletonForUpdate(); RequireSingletonForUpdate(); m_AsteroidsQuery = GetEntityQuery(new ComponentType[] { ComponentType.ReadOnly(), ComponentType.ReadOnly() }); m_MissileQuery = GetEntityQuery(new ComponentType[] { ComponentType.ReadOnly(), ComponentType.ReadOnly() }); } protected override void OnUpdate() { var gameState = GetSingleton(); var input = World.GetExistingSystem(); var playerEntity = GetSingletonEntity(); if (gameState.Value == GameStates.Start && IsPressingToStart(input)) { // remove all stuff EntityManager.DestroyEntity(m_AsteroidsQuery); EntityManager.DestroyEntity(m_MissileQuery); // put the ship back in the center EntityManager.SetComponentData(playerEntity, new Translation { Value = float3.zero }); EntityManager.SetComponentData(playerEntity, new Rotation { Value = quaternion.identity }); AudioUtils.PlaySound(EntityManager, AudioTypes.ExtraShip); gameState.Value = GameStates.InGame; SetSingleton(gameState); } } private static bool IsPressingToStart(InputSystem input) { var isTouchSupported = input.IsTouchSupported(); if (isTouchSupported && input.TouchCount() > 0 && input.GetTouch(0).phase == TouchState.Began) return true; else if (!isTouchSupported && input.GetKeyDown(KeyCode.Space)) return true; return false; } } }