替换玩家模型素材,添加 ChildrenMap 配置挂点

This commit is contained in:
2026-10-04 17:57:16 +08:00
parent 75ec52f3d4
commit aaa8cf3ee2
98 changed files with 6483 additions and 274 deletions
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using SepCore.Battle;
using SepCore.CustomUtility;
using SepCore.Definition;
using SepCore.Entity;
using UnityEngine;
@@ -49,7 +50,7 @@ namespace SepCore.Components
_weapons = new List<WeaponBase>(WeaponCapacity);
_props = new List<PropItem>();
_statComponent = GetComponent<StatComponent>();
_statComponent = player.StatComponent;
_synergyManager.Initialize();
}
@@ -76,12 +77,64 @@ namespace SepCore.Components
if (result)
{
GameEntry.Entity.DetachEntity(weapon.Id, _player.Id);
RealignWeaponSlots();
_synergyManager.RefreshSynergies(_weapons, _statComponent.Pipeline);
}
return result;
}
public void RealignWeaponSlots()
{
if (_weaponSlots == null || _player == null) return;
for (int i = 0; i < _weapons.Count; i++)
{
if (i < _weaponSlots.Length && _weapons[i] != null)
{
GameEntry.Entity.AttachEntity(_weapons[i].Id, _player.Id, _weaponSlots[i]);
}
}
}
public bool CanCombineWeapon(int index, out int matchIndex, out WeaponConfig nextTierConfig)
{
matchIndex = -1;
nextTierConfig = null;
if (_weapons == null || index < 0 || index >= _weapons.Count)
{
return false;
}
WeaponBase target = _weapons[index];
if (target?.WeaponData == null)
{
return false;
}
if (!WeaponUtility.TryGetNextTierConfig(target.WeaponData.Config, out nextTierConfig))
{
return false;
}
for (int i = 0; i < _weapons.Count; i++)
{
if (i == index) continue;
WeaponBase other = _weapons[i];
if (other?.WeaponData == null) continue;
if (other.WeaponData.WeaponType == target.WeaponData.WeaponType &&
other.WeaponData.Rarity == target.WeaponData.Rarity)
{
matchIndex = i;
return true;
}
}
return false;
}
public bool AttachProp(PropItem prop)
{
_props.Add(prop);
@@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace SepCore.Components
{
[DisallowMultipleComponent]
public class ChildrenMap : MonoBehaviour
{
[Serializable]
public class Entry
{
[SerializeField] private string _key = string.Empty;
[SerializeField] private GameObject _target = null;
public string Key => _key;
public GameObject Target => _target;
public Entry()
{
}
public Entry(string key, GameObject target)
{
_key = key;
_target = target;
}
}
[SerializeField] private List<Entry> _entries = new List<Entry>();
private Dictionary<string, GameObject> _map;
private void Awake()
{
InitMap();
}
public GameObject Get(string key)
{
if (_map == null)
{
InitMap();
}
if (string.IsNullOrEmpty(key))
{
Log.Error("[ChildrenMap] Key is null or empty when getting child on '{0}'.", gameObject.name);
return null;
}
if (!_map.TryGetValue(key, out GameObject target))
{
Log.Error("[ChildrenMap] Key '{0}' not found on '{1}'.", key, gameObject.name);
return null;
}
if (target == null)
{
Log.Error("[ChildrenMap] Target GameObject for key '{0}' is null on '{1}'.", key, gameObject.name);
return null;
}
return target;
}
private void InitMap()
{
if (_map != null)
{
return;
}
_map = new Dictionary<string, GameObject>(_entries.Count);
for (int i = 0; i < _entries.Count; i++)
{
Entry entry = _entries[i];
if (entry == null)
{
continue;
}
if (string.IsNullOrEmpty(entry.Key))
{
Log.Error("[ChildrenMap] Key is null or empty on '{0}'.", gameObject.name);
continue;
}
if (_map.ContainsKey(entry.Key))
{
Log.Error("[ChildrenMap] Duplicate key '{0}' on '{1}'.", entry.Key, gameObject.name);
continue;
}
_map.Add(entry.Key, entry.Target);
}
}
#if UNITY_EDITOR
private void OnValidate()
{
if (_entries == null)
{
return;
}
var seenKeys = new HashSet<string>();
for (int i = 0; i < _entries.Count; i++)
{
Entry entry = _entries[i];
if (entry == null || string.IsNullOrEmpty(entry.Key))
{
continue;
}
if (!seenKeys.Add(entry.Key))
{
Debug.LogWarning($"[ChildrenMap] Duplicate key '{entry.Key}' detected on '{gameObject.name}'.", this);
}
}
}
#endif
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fb474f3f35451124b830a5d5cf7b367c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -37,16 +37,9 @@ namespace SepCore.Components
_cachedTransform = target;
_direction = Vector3.forward;
_avoidEnemyOverlap = avoidEnemyOverlap;
float defaultRadius = GameEntry.Luban != null && GameEntry.Luban.IsReady
? GameEntry.Luban.Global.EnemyBodyRadius / 1000f
: 0.5f;
int defaultIterations = GameEntry.Luban != null && GameEntry.Luban.IsReady
? GameEntry.Luban.Global.EnemySeparationIterations
: 4;
_enemyBodyRadius = Mathf.Max(0.01f, enemyBodyRadius ?? defaultRadius);
_separationIterations = Mathf.Max(1, separationIterations ?? defaultIterations);
_enemyBodyRadius = GameEntry.Luban.Global.EnemyBodyRadius;
_separationIterations = GameEntry.Luban.Global.EnemySeparationIterations;
TryGetComponent(out _player);
TryGetComponent(out _enemy);
@@ -0,0 +1,111 @@
using SepCore.Definition;
using UnityEngine;
namespace SepCore.Components
{
public class PlayerMovementComponent : MonoBehaviour
{
private static readonly int WalkStateHash = Animator.StringToHash("Base Layer.Walk");
[SerializeField] private Transform _model = null;
[SerializeField] private Animator _animator = null;
[SerializeField] private float _turnSpeed = 720f;
private Transform _target;
private StatComponent _statComponent;
private Vector3 _direction;
private Quaternion _initialModelRotation = Quaternion.identity;
private bool _canMove;
private bool _isWalking;
public float Speed => _statComponent.GetStatValue(StatType.MoveSpeed);
public Vector3 Direction => _direction;
public bool IsMoving => _isWalking;
public void OnInit(float speed, Transform target, StatComponent statComponent)
{
_target = target;
_statComponent = statComponent;
_statComponent.Pipeline.SetBaseValue(StatType.MoveSpeed, speed);
_initialModelRotation = _model.localRotation;
_direction = Vector3.zero;
_canMove = false;
_isWalking = false;
_animator.applyRootMotion = false;
_animator.Rebind();
ResetWalkPose();
}
public void SetMove(bool canMove)
{
_canMove = canMove;
if (!canMove)
{
_direction = Vector3.zero;
SetWalking(false);
}
}
public void SetDirection(Vector3 direction)
{
direction.y = 0f;
_direction = Vector3.ClampMagnitude(direction, 1f);
}
public void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
if (!_canMove || elapseSeconds <= 0f)
{
return;
}
float speed = Speed;
bool walking = speed > 0f && _direction.sqrMagnitude > Mathf.Epsilon;
SetWalking(walking);
if (!walking)
{
return;
}
_target.position += _direction * (speed * elapseSeconds);
Quaternion rotation = Quaternion.LookRotation(_direction, Vector3.up);
_model.rotation = Quaternion.RotateTowards(_model.rotation, rotation, _turnSpeed * elapseSeconds);
}
public void OnReset()
{
_canMove = false;
_isWalking = false;
_direction = Vector3.zero;
_target = null;
_statComponent = null;
_model.localRotation = _initialModelRotation;
ResetWalkPose();
}
private void SetWalking(bool walking)
{
if (_isWalking == walking)
{
return;
}
_isWalking = walking;
if (walking)
{
_animator.speed = 1f;
}
else
{
ResetWalkPose();
}
}
private void ResetWalkPose()
{
_animator.speed = 0f;
_animator.Play(WalkStateHash, 0, 0f);
_animator.Update(0f);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a9366dc6d1e945058db4c74cb557ae41
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,128 @@
using System;
using System.Threading;
using Cysharp.Threading.Tasks;
using SepCore.AsyncTask;
using SepCore.CustomUtility;
using SepCore.Definition;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace SepCore.Components
{
public class SkinComponent : MonoBehaviour
{
private static readonly int BaseMapPropertyId = Shader.PropertyToID("_BaseMap");
private MeshRenderer[] _renderers;
private Material _instanceMaterial;
private Texture2D _loadedTexture;
private int _generation;
public async UniTask OnInitAsync(RoleConfig role, CancellationToken cancellationToken = default)
{
int generation = ++_generation;
EnsureRenderersAndMaterial();
SetRenderersVisible(false);
_instanceMaterial.SetTexture(BaseMapPropertyId, null);
ReleaseLoadedTexture();
cancellationToken.ThrowIfCancellationRequested();
string textureAssetName = AssetUtility.GetSpriteAsset(role.Texture_Ref.AtlasPath);
Texture2D texture;
try
{
texture = await GameEntry.Resource.LoadAssetAsync<Texture2D>(textureAssetName);
}
catch (Exception exception) when (!(exception is OperationCanceledException))
{
Log.Error("SkinComponent.OnInitAsync: Failed to load skin texture '{0}'. {1}", textureAssetName,
exception.Message);
throw;
}
if (generation != _generation || cancellationToken.IsCancellationRequested)
{
GameEntry.Resource.UnloadAsset(texture);
throw new OperationCanceledException(cancellationToken);
}
_loadedTexture = texture;
_instanceMaterial.SetTexture(BaseMapPropertyId, _loadedTexture);
SetRenderersVisible(true);
}
public void OnReset()
{
_generation++;
if (_renderers != null)
{
SetRenderersVisible(false);
}
if (_instanceMaterial != null)
{
_instanceMaterial.SetTexture(BaseMapPropertyId, null);
}
ReleaseLoadedTexture();
}
private void OnDestroy()
{
OnReset();
if (_instanceMaterial == null)
{
return;
}
if (Application.isPlaying)
{
Destroy(_instanceMaterial);
}
else
{
DestroyImmediate(_instanceMaterial);
}
_instanceMaterial = null;
}
private void EnsureRenderersAndMaterial()
{
if (_renderers != null)
{
return;
}
_renderers = GetComponentsInChildren<MeshRenderer>(true);
_instanceMaterial = new Material(_renderers[0].sharedMaterial);
for (int i = 0; i < _renderers.Length; i++)
{
_renderers[i].sharedMaterial = _instanceMaterial;
}
}
private void SetRenderersVisible(bool visible)
{
for (int i = 0; i < _renderers.Length; i++)
{
_renderers[i].enabled = visible;
}
}
private void ReleaseLoadedTexture()
{
if (_loadedTexture == null)
{
return;
}
GameEntry.Resource.UnloadAsset(_loadedTexture);
_loadedTexture = null;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6f3d7be5910a4a0499f0a5cf0f870e96
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: