using Movement; using WeaponWork; using System; using UnityEngine; namespace Core { [RequireComponent(typeof(Rigidbody2D)), DisallowMultipleComponent()] public sealed class Unit : MonoBehaviour, IActor, IProvider, IProvider, IProvider, ITeamProvider { [SerializeField] private ControllerType _controllerType; [SerializeField] private int _maxHealth; [SerializeField] private string _name; [SerializeField] private float _speed; [SerializeField] private float _rotationSpeed; [SerializeField] private int _teamNumber; [SerializeField] private bool _canChangeTeam; private Transform _cached; private Rigidbody2D _cachedRigidbody; private IHealth _health; private IWeapon _weapon; private Motor _motor; private IController _controller; private void Awake() { _cached = transform; _cachedRigidbody = GetComponent(); _motor = new Motor(_speed, _rotationSpeed, this, transform, _cachedRigidbody); _health = new Health(_maxHealth, _maxHealth); if (!transform.TryGetComponentInChildren(out _weapon)) { Debug.LogWarning($"Unit: {_name} does not have weapon"); } var comps = transform.GetComponentsInChildren(true); for (int i = 0, length = comps.Length; i < length; i++) { comps[i].Actor = this; } } public Vector2 Position => _cached.position; public float Rotation => _cached.eulerAngles.z; public IController Controller => _controller; public string Name => _name; public ref IHealth Value => ref _health; ref IWeapon IProvider.Value => ref _weapon; ref Motor IProvider.Value => ref _motor; public float Scale => throw new NotImplementedException(); public int TeamNumber => _teamNumber; public event Action OnControllerChange; public event Action OnTeamNumberChange; private enum ControllerType { PlayerOnly, PlayerAndAI, AIOnly } } }