using System; using System.Collections.Generic; using Entitas; namespace CoreGame.Render { public class SuppressorSys : IExecuteSystem { private readonly IGroup m_SuppressorGroup; private readonly IGroup m_BulletGroup; private readonly List m_CacheSuppressorEntities = new List(); private readonly List m_CacheBulletEntities = new List(); public SuppressorSys(CombatContext contexts) { m_SuppressorGroup = contexts.GetGroup(CombatMatcher.Suppressor); m_BulletGroup = contexts.GetGroup(CombatMatcher.Bullet); } public void Execute(float deltaTime) { m_SuppressorGroup.GetEntities(m_CacheSuppressorEntities); if (m_CacheSuppressorEntities.Count == 0) return; m_BulletGroup.GetEntities(m_CacheBulletEntities); for (int i = 0; i < m_CacheSuppressorEntities.Count; i++) { var suppressorEnt = m_CacheSuppressorEntities[i]; var suppressor = suppressorEnt.suppressor; suppressor.duration -= deltaTime; if (suppressor.duration <= 0) { suppressorEnt.isDestroyEnt = true; continue; } var casterEnt = Contexts.Combat.GetEntity(suppressor.casterEid); if (casterEnt.IsValid() == false || casterEnt.isDead) { suppressorEnt.isDestroyEnt = true; continue; } var suppressorTrans = suppressorEnt.transformProxy; var distanceSqr = suppressor.radius * suppressor.radius; for (int j = 0; j < m_CacheBulletEntities.Count; j++) { var bulletEnt = m_CacheBulletEntities[j]; if (bulletEnt.IsValid() == false || bulletEnt.bullet.hasInvalidated) continue; var bulletTrans = bulletEnt.transformProxy; var curDistanceSqr = (suppressorTrans.position - bulletTrans.position).sqrMagnitude - distanceSqr; var lastDistanceSqr = (suppressorTrans.lastPosition - bulletTrans.lastPosition).sqrMagnitude - distanceSqr; if (Math.Sign(curDistanceSqr) != Math.Sign(lastDistanceSqr)) { bulletEnt.isDestroyEnt = true; } } suppressorTrans.SetPosition(casterEnt.transformProxy.position); suppressorTrans.SetDirection(casterEnt.transformProxy.direction); } } } }