diff --git a/Assets/Scripts/Units/Unit.cs b/Assets/Scripts/Units/Unit.cs index cf4a7320..04e65afe 100644 --- a/Assets/Scripts/Units/Unit.cs +++ b/Assets/Scripts/Units/Unit.cs @@ -28,6 +28,8 @@ namespace Units private Weapon _weapon; private Vector2 _direction; private BarCanvas _barCanvas; + private bool _isHardToCapture; + public bool IsBusy => _isBusy; @@ -42,17 +44,27 @@ namespace Units _isAlive = false; _hexGrid = hexGrid; _isBusy = false; + _isHardToCapture = false; } public void Move(HexDirection direction) { if (!_cell.GetNeighbor(direction) || _isBusy) return; + _unitView.StopHardCature(); if (_cell.GetNeighbor(direction).Color == _data.color) { DoTransit(direction); } + else if (_cell.GetNeighbor(direction).Color != UnitColor.GREY) + { + _isHardToCapture = true; + _unitView.RegenMana(_mana); + DoTransit(direction); + } + else if (_mana - _hexGrid.HexCaptureCost >= 0) { + _mana -= _hexGrid.HexCaptureCost; _unitView.RegenMana(_mana); UpdateBarCanvas(); @@ -64,13 +76,17 @@ namespace Units { _isBusy = true; _cell = _cell.GetNeighbor(direction); - RotateUnit(new Vector2((_cell.transform.position - _instance.transform.position).normalized.x, (_cell.transform.position - _instance.transform.position).normalized.z)); _animator.SetTrigger("Move"); _animator.SetBool("isMoving", _isBusy); _instance.transform.DOMove(_cell.transform.position, _animLength.Move); } + private void CaptureHex() + { + _cell.PaintHex(_data.color); + } + private void SetAnimLength() { AnimationClip[] clips = _animator.runtimeAnimatorController.animationClips; @@ -108,7 +124,7 @@ namespace Units _animator = _instance.GetComponent(); _unitView = _instance.GetComponent(); _barCanvas = _unitView.BarCanvas.GetComponent(); - _unitView.SetUp(_barCanvas.SpawnShotUI(_weapon.shots), _weapon, RegenMana, _data.manaRegen); + _unitView.SetUp(_barCanvas.SpawnShotUI(_weapon.shots), _weapon, RegenMana, _data.manaRegen, CaptureHex); SetAnimLength(); _mana = _data.maxMana; _hp = _data.maxHP; @@ -125,8 +141,16 @@ namespace Units private void MoveEnd() { _isBusy = false; - _cell.PaintHex(_data.color); _animator.SetBool("isMoving", _isBusy); + if (_isHardToCapture) + { + _unitView.HardCaptureHex(); + } + else + { + CaptureHex(); + } + _isHardToCapture = false; } private void AttackEnd() diff --git a/Assets/Scripts/Units/Views/UnitView.cs b/Assets/Scripts/Units/Views/UnitView.cs index cd0b018d..5eeeece4 100644 --- a/Assets/Scripts/Units/Views/UnitView.cs +++ b/Assets/Scripts/Units/Views/UnitView.cs @@ -15,7 +15,7 @@ public class UnitView : MonoBehaviour public Action OnHit; [SerializeField] private GameObject barCanvas; [SerializeField] private GameObject aimCanvas; - + private Stack _shootUIStack; private Stack _toReloadStack; @@ -25,17 +25,31 @@ public class UnitView : MonoBehaviour private Coroutine _previosRegen; private Coroutine _previosReload; private int _mana; + private Action _capureHex; + private Coroutine _captureHexCoroutine; public GameObject BarCanvas => barCanvas; public GameObject AimCanvas => aimCanvas; - public void SetUp(Stack shots, Weapon weapon, Action RegenMana, int manaRegen) + public void SetUp(Stack shots, Weapon weapon, Action regenMana, int manaRegen, Action captureHex) { _shootUIStack = shots; _weapon = weapon; _toReloadStack = new Stack(); - _startRegen = RegenMana; + _startRegen = regenMana; _manaRegen = manaRegen; + _capureHex = captureHex; + } + + public void HardCaptureHex() + { + _captureHexCoroutine = StartCoroutine(HardCapture()); + } + + public void StopHardCature() + { + if (_captureHexCoroutine != null) + StopCoroutine(_captureHexCoroutine); } public bool Shoot() @@ -48,6 +62,7 @@ public class UnitView : MonoBehaviour { StopCoroutine(_previosReload); } + _previosReload = StartCoroutine(Reload()); return true; } @@ -85,9 +100,9 @@ public class UnitView : MonoBehaviour { OnHit?.Invoke(weaponView.Weapon.damage); other.transform.DOComplete(); - + other.transform.position = transform.position; - + Destroy(other.gameObject); } } @@ -109,9 +124,16 @@ public class UnitView : MonoBehaviour { yield break; } + yield return new WaitForSeconds(1f); _mana += _manaRegen; _startRegen.Invoke(); StartCoroutine(Regen()); } + + private IEnumerator HardCapture() + { + yield return new WaitForSeconds(3f); + _capureHex.Invoke(); + } } \ No newline at end of file