初始化仓库

This commit is contained in:
SepComet
2026-02-07 19:38:49 +08:00
commit 88641dae11
1540 changed files with 371772 additions and 0 deletions
@@ -0,0 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using Entity;
using UnityEngine;
namespace Components
{
public class AttackComponent : MonoBehaviour
{
private bool _isAttacking = false;
private List<WeaponBase> _weapons;
public void SetIsAttacking(bool isAttacking) => _isAttacking = isAttacking;
public void OnInit()
{
}
public void OnUpdate(float elapsedTime, float realElapseSeconds)
{
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a2b35b5ae4185cb4cab2d84b0569db3d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,100 @@
using System.Collections.Generic;
using CustomEvent;
using Definition.DataStruct;
using Entity;
using UnityEngine;
namespace Components
{
public class BackpackComponent : MonoBehaviour
{
/// <summary>
/// 武器槽容量,由DRRole决定。
/// </summary>
private int _weaponCapacity;
/// <summary>
/// 玩家类引用。
/// </summary>
private Player _player;
/// <summary>
/// 武器列表。
/// </summary>
private List<WeaponBase> _weapons;
/// <summary>
/// 道具列表。
/// </summary>
private List<PropItem> _props;
/// <summary>
/// 玩家状态组件引用,用于道具效果的应用。
/// </summary>
private StatComponent _statComponent;
/// <summary>
/// 武器装备槽,在 Editor 中进行配置,表示武器在世界中的显示位置。
/// </summary>
[SerializeField] private Transform[] _weaponSlots;
public void OnInit(Player player, int weaponCapacity)
{
_weaponCapacity = weaponCapacity;
_player = player;
_weapons = new List<WeaponBase>(_weaponCapacity);
_props = new List<PropItem>();
_statComponent = GetComponent<StatComponent>();
}
public void OnReset()
{
_weapons.Clear();
_props.Clear();
}
public bool AttachWeapon(WeaponBase weapon)
{
if (_weapons.Count == _weaponCapacity) return false;
GameEntry.Entity.AttachEntity(weapon.Id, _player.Id, _weaponSlots[_weapons.Count]);
_weapons.Add(weapon);
weapon.SetEnabled(true);
return true;
}
public bool DetachWeapon(WeaponBase weapon)
{
bool result = _weapons.Remove(weapon);
if (result)
{
GameEntry.Entity.DetachEntity(weapon.Id, _player.Id);
}
return result;
}
public bool AttachProp(PropItem prop)
{
_props.Add(prop);
prop.OnAttach(_statComponent);
return true;
}
public bool DetachProp(PropItem prop)
{
_props.Remove(prop);
prop.OnDetach(_statComponent);
return true;
}
public void SetWeaponState(bool state)
{
if (_weapons == null) return;
foreach (var weapon in _weapons)
{
weapon.SetEnabled(state);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2980af28a40c84247a688f6b684dd4c3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,122 @@
using System;
using CustomEvent;
using Definition.DataStruct;
using Definition.Enum;
using UnityEngine;
namespace Components
{
public class HealthComponent : MonoBehaviour
{
private int _maxHealthBase;
private int _currentHealth;
private bool _isDead => _currentHealth <= 0;
private StatComponent _statComponent;
private StatProperty _maxHealthStat;
private Action<StatModifier, bool> _maxHealthStatCallback;
public StatProperty MaxHealthStat => _maxHealthStat;
private StatProperty _defenseStat;
private Action<StatModifier, bool> _defenseStatCallback;
public StatProperty DefenseStat => _defenseStat;
private StatProperty _dodgeStat;
private Action<StatModifier, bool> _dodgeStatCallback;
public StatProperty DodgeStat => _dodgeStat;
private bool _isPlayer => _statComponent != null;
public float HealthRatio => MaxHealth > 0 ? (float)CurrentHealth / MaxHealth : 0f;
public int MaxHealth => Mathf.CeilToInt((_maxHealthBase + _maxHealthStat.Value) * _maxHealthStat.Percent);
public int CurrentHealth
{
get => _currentHealth;
set => _currentHealth = value;
}
#region FSM
public void OnInit(int maxHealth, StatComponent statComponent = null)
{
_maxHealthBase = maxHealth;
_currentHealth = maxHealth;
_statComponent = statComponent;
_statComponent = statComponent;
if (_statComponent != null)
{
_maxHealthStat = _statComponent.GetStat(StatType.MaxHealth);
_maxHealthStatCallback = (modifier, isApply) =>
_statComponent.UpdateStat(_maxHealthStat, modifier, isApply);
_statComponent.Subscribe(StatType.MaxHealth, _maxHealthStatCallback);
_defenseStat = _statComponent.GetStat(StatType.Defense);
_defenseStatCallback = (modifier, isApply) =>
_statComponent.UpdateStat(_defenseStat, modifier, isApply);
_statComponent.Subscribe(StatType.Defense, _defenseStatCallback);
_dodgeStat = _statComponent.GetStat(StatType.Dodge);
_dodgeStatCallback = (modifier, isApply) =>
_statComponent.UpdateStat(_dodgeStat, modifier, isApply);
_statComponent.Subscribe(StatType.Dodge, _dodgeStatCallback);
}
else
{
_maxHealthStat = new StatProperty();
_defenseStat = new StatProperty();
_dodgeStat = new StatProperty();
}
}
public void OnReset()
{
if (_statComponent != null)
{
_statComponent.Unsubscribe(StatType.MaxHealth, _maxHealthStatCallback);
_maxHealthStatCallback = null;
_statComponent.Unsubscribe(StatType.Defense, _defenseStatCallback);
_defenseStatCallback = null;
_statComponent.Unsubscribe(StatType.Dodge, _dodgeStatCallback);
_dodgeStatCallback = null;
}
_statComponent = null;
}
#endregion
public void TakeDamage(int damage)
{
if (_isDead) return;
if (_dodgeStat.Value > UnityEngine.Random.value)
{
damage = 0;
}
else
{
int defenseValue = (int)_defenseStat.Value;
float defensePercent = _defenseStat.Percent;
damage -= defenseValue;
damage = Mathf.CeilToInt(damage / defensePercent);
}
_currentHealth -= damage;
CauseDamage(damage);
if (_isPlayer)
GameEntry.Event.Fire(this, PlayerHealthChangeEventArgs.Create(damage, CurrentHealth, MaxHealth));
}
private void CauseDamage(int damage)
{
//TODO:受击效果:跳字、粒子
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ce4964a40efccaa4fb22df63713b567b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,51 @@
using UnityEngine;
namespace Components
{
public class InputComponent : MonoBehaviour
{
private PlayerInputActions _inputActions;
[SerializeField] private bool _isListening = false;
[SerializeField] private Vector3 _direction = Vector3.zero;
public Vector3 Direction => _direction;
public void OnInit()
{
_inputActions = new();
}
public void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
if (_isListening && _inputActions != null)
{
ReadInput();
}
}
public void OnReset()
{
_inputActions = null;
_isListening = false;
}
private void ReadInput()
{
Vector2 rawDir = _inputActions.Game.Move.ReadValue<Vector2>();
_direction = new Vector3(rawDir.x, 0, rawDir.y);
}
public void SetListening(bool isListening)
{
_isListening = isListening;
if (_isListening)
{
_inputActions.Enable();
}
else
{
_inputActions.Disable();
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f754cc5f339dcfa4aaec3aa32260c4b8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,75 @@
using System;
using Definition.DataStruct;
using Definition.Enum;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace Components
{
public class MovementComponent : MonoBehaviour
{
[SerializeField] private bool _isMoving;
[SerializeField] private Vector3 _direction;
[SerializeField] private Transform _cachedTransform;
public float Speed => (_speedBase + _movementStat.Value) * _movementStat.Percent;
[SerializeField] private float _speedBase;
private StatComponent _statComponent;
private StatProperty _movementStat;
private Action<StatModifier, bool> _movementStatCallback;
public void OnInit(float speed, Transform target, StatComponent statComponent = null)
{
_speedBase = speed;
_cachedTransform = target;
_direction = Vector3.forward;
_statComponent = statComponent;
if (_statComponent != null)
{
_movementStat = _statComponent.GetStat(StatType.MovementSpeed);
_movementStatCallback = (modifier, isApply) =>
_statComponent.UpdateStat(_movementStat, modifier, isApply);
_statComponent.Subscribe(StatType.MovementSpeed, _movementStatCallback);
}
else
{
_movementStat = new StatProperty();
}
}
public void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
if (_isMoving && _cachedTransform != null)
{
Move(elapseSeconds);
}
}
public void OnReset()
{
_speedBase = 0;
_cachedTransform = null;
_direction = Vector3.zero;
_isMoving = false;
if (_statComponent != null)
{
_statComponent.Unsubscribe(StatType.MovementSpeed, _movementStatCallback);
_movementStatCallback = null;
}
_statComponent = null;
}
private void Move(float deltaTime = 0)
{
this.transform.Translate(Speed * deltaTime * _direction);
}
public void SetMove(bool isMoving) => _isMoving = isMoving;
public void SetDirection(Vector3 direction) => _direction = direction;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9baf591df06b9bb44ba5313457ba84b3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using Definition.DataStruct;
using Definition.Enum;
using UnityEngine;
namespace Components
{
public class StatComponent : MonoBehaviour
{
private Dictionary<StatType, List<StatModifier>> _statModifiers;
private Dictionary<StatType, Action<StatModifier, bool>> _statModifierCallbacks;
public void OnInit()
{
_statModifiers = new Dictionary<StatType, List<StatModifier>>();
_statModifierCallbacks = new Dictionary<StatType, Action<StatModifier, bool>>();
}
public void OnReset()
{
_statModifiers.Clear();
_statModifierCallbacks.Clear();
}
public void AddModifier(StatModifier modifier)
{
if (!_statModifiers.TryGetValue(modifier.StatType, out var modifiers))
{
_statModifiers.Add(modifier.StatType, modifiers = new List<StatModifier>());
modifiers.Add(modifier);
}
else
{
modifiers.Add(modifier);
}
if (_statModifierCallbacks.TryGetValue(modifier.StatType, out var callbacks))
callbacks?.Invoke(modifier, true);
}
public void RemoveModifier(StatModifier modifier)
{
if (_statModifiers.TryGetValue(modifier.StatType, out var modifiers))
{
_statModifiers[modifier.StatType].Remove(modifier);
}
if (_statModifierCallbacks.TryGetValue(modifier.StatType, out var callbacks))
callbacks?.Invoke(modifier, false);
}
public void Subscribe(StatType statType, Action<StatModifier, bool> callback)
{
if (!_statModifierCallbacks.TryAdd(statType, callback))
{
_statModifierCallbacks[statType] += callback;
}
}
public void Unsubscribe(StatType statType, Action<StatModifier, bool> callback)
{
if (!_statModifierCallbacks.TryGetValue(statType, out var callbacks))
{
return;
}
callbacks -= callback;
if (callbacks == null)
{
_statModifierCallbacks.Remove(statType);
}
}
public StatProperty GetStat(StatType statType)
{
var result = new StatProperty();
if (!_statModifiers.TryGetValue(statType, out var modifiers)) return result;
foreach (var modifier in modifiers)
{
if (modifier.IsPercent) result.Percent += modifier.Value;
else result.Value += modifier.Value;
}
return result;
}
public void UpdateStat(StatProperty stat, StatModifier modifier, bool isApply)
{
float coef = isApply ? 1f : -1f;
if (modifier.IsPercent)
{
stat.Percent += modifier.Value * coef;
}
else
{
stat.Value += modifier.Value * coef;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5befb68803e86e248873416ce662184b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: