You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.7 KiB
68 lines
2.7 KiB
1 month ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using Entitas;
|
||
|
|
||
|
namespace CoreGame.Render
|
||
|
{
|
||
|
public class SuppressorSys : IExecuteSystem
|
||
|
{
|
||
|
private readonly IGroup<CombatEntity> m_SuppressorGroup;
|
||
|
private readonly IGroup<CombatEntity> m_BulletGroup;
|
||
|
|
||
|
private readonly List<CombatEntity> m_CacheSuppressorEntities = new List<CombatEntity>();
|
||
|
private readonly List<CombatEntity> m_CacheBulletEntities = new List<CombatEntity>();
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|