using System; using System.IO; using AOT; using CoreGame; using CoreGame.Render; using Entitas; using UnityEngine; using UnityEngine.Animations; using xFrame; namespace CoreGame.Render { // 资源加载这块很烂,需要整改 public class ReallyAttachAssetSystem : IExecuteSystem { readonly IGroup m_Group; public ReallyAttachAssetSystem(Contexts contexts) { m_Group = contexts.combat.GetGroup(CombatMatcher.AttachAsset); } public void Execute(float deltaTime) { m_Group.GetEntities(Contexts.s_CacheEntities); if (Contexts.s_CacheEntities.Count == 0) return; var entities = Contexts.s_CacheEntities; for (int i = 0; i < entities.Count; i++) { ProcessSingle(entities[i], deltaTime); } } private void AttachHand(BindPointProxy pointProxy, BindPointProxy parentEntityBindPointProxy, int b1, int pb2) { if (pointProxy.GetBindPointMap().TryGetValue(b1, out var bp2) && parentEntityBindPointProxy.GetBindPointMap().TryGetValue(pb2, out var parentBp2)) { if (parentBp2.gameObject.TryGetComponent(out var positionConstraint) == false) { positionConstraint = parentBp2.gameObject.AddComponent(); positionConstraint.AddSource(new ConstraintSource { sourceTransform = bp2, weight = 1 }); } else { positionConstraint.SetSource(0, new ConstraintSource { sourceTransform = bp2, weight = 1 }); } positionConstraint.constraintActive = true; } } private void ProcessSingle(CombatEntity ent, float dt) { var parentEntity = ent.parentRecorder.ParentEntity; var attachAsset = ent.attachAsset; attachAsset.retryTime -= dt; if (attachAsset.retryTime < 0) { XLog.LogWarning($"AttachAssetComponent retryTime < 0 {ent}"); ent.RemoveAttachAsset(); return; } var parentEntityAsset = parentEntity.asset; if (parentEntityAsset == null) return; ref var mainAssetParam = ref parentEntityAsset.mainAssetParam; if (mainAssetParam.reuseGoData.go == false) return; var parentEntityBindPointProxy = parentEntity.bindPointProxy; var pointProxy = ent.bindPointProxy; if (parentEntityBindPointProxy == null) return; parentEntityBindPointProxy.TryGetBindPointOrDefault(attachAsset.bindPointType, out var bindPoint); var asset = ent.asset; if (asset == null) return; var reuseGoData = asset.mainAssetParam.reuseGoData; if (reuseGoData.go == false) return; reuseGoData.go.transform.SetParent(bindPoint, false); reuseGoData.go.SetActiveWrap(attachAsset.isActive); ent.RemoveAttachAsset(); // 非激活状态不挂载 if (attachAsset.isActive == false) return; parentEntityAsset.RecordLinkEnt(ent.creationIndex); if (pointProxy != null) { parentEntityBindPointProxy.AttachFallbackBindPoint(pointProxy.GetBindPointMap()); AttachHand(pointProxy, parentEntityBindPointProxy, (int)BindPointType.LHoldPoint, (int)BindPointType.LHand); AttachHand(pointProxy, parentEntityBindPointProxy, (int)BindPointType.RHoldPoint, (int)BindPointType.RHand); } GunSrv.ShowCircleVfx(parentEntity, ent); // 如果有动画,也要挂载进去 var parentEntityAnimationProxy = parentEntity.animationProxy; if (parentEntityAnimationProxy != null) { var subAssetData = SptPool.Malloc(); subAssetData.reuseGoData = reuseGoData; var path = asset.mainAssetParam.path; var fileName = Path.GetFileName(path); if (path != null) { try { var skinParam = new SkinParam { bpType = attachAsset.bindPointType, path = UnrealNames.GetFName(path.Substring(0, path.Length - fileName.Length - 1)), animPrefix = UnrealNames.GetFName(fileName.Substring(0, fileName.LastIndexOf('.'))) }; parentEntityAnimationProxy.PushPartAnimancer(subAssetData, skinParam); } catch (Exception e) { XLog.LogWarning($"path {path} fileName {fileName} e {e}"); } } subAssetData.reuseGoData = null; SptPool.Free(ref subAssetData); } } } }