using System.Collections.Generic; using Entitas; using UnityEngine; namespace CoreGame.Render { // 只对LocalPlayer起效 public class RandomMoveSys : IExecuteSystem { private readonly IGroup m_ConfuseMoveGroup; private readonly IGroup m_ScareMoveGroup; private readonly List m_CacheEntities = new(); public RandomMoveSys(CombatContext contexts) { var confuseMoveMatcher = (Matcher)Matcher .AllOf(CombatComponentsLookup.ConfuseMove) .NoneOf(CombatComponentsLookup.Dead); m_ConfuseMoveGroup = contexts.GetGroup(confuseMoveMatcher); var scareMoveMatcher = (Matcher)Matcher.AllOf(CombatComponentsLookup.ScareMove) .NoneOf(CombatComponentsLookup.Dead, CombatComponentsLookup.ForceNav); m_ScareMoveGroup = contexts.GetGroup(scareMoveMatcher); } public void Execute(float deltaTime) { m_ConfuseMoveGroup.GetEntities(m_CacheEntities); for (int i = 0; i < m_CacheEntities.Count; i++) { var entity = m_CacheEntities[i]; var confuseMove = entity.confuseMove; confuseMove.cdAcc += deltaTime; if (confuseMove.cdAcc >= confuseMove.cd) { confuseMove.cdAcc = 0f; confuseMove.randomAngle = RandomSrv.Range(-0.75f * 180f, 0.75f * 180f); } if (entity.hasMovement) { entity.movement.direction += confuseMove.randomAngle; } } m_ScareMoveGroup.GetEntities(m_CacheEntities); for (int i = 0; i < m_CacheEntities.Count; i++) { var entity = m_CacheEntities[i]; var scareMove = entity.scareMove; scareMove.cdAcc += deltaTime; if (scareMove.cdAcc >= scareMove.cd) { scareMove.cdAcc = 0f; scareMove.randomAngle = RandomSrv.Range(0f, 360f); } entity.ReplaceMovement(BattleConst.JoySpeed, scareMove.randomAngle); } } } }