private void OnDestroy() { Log($"---------- {MethodBase.GetCurrentMethod().Name} ----------"); UnitManager.RemoveUnit(this); DeleteSavedPath(); CurrentGridBlock?.ResetCurrentUnit(this); Destroy(_minimapIcon); if (_cC != null) { _cC.OnCursorMoveEvent -= OnCursorMove; } IsDestroyed = true; Log("----------------------------------------"); }
private void SetCursorState() { if (CurrentGridBlock.IsCurrentUnitEnemy(Player)) { CursorState = Enums.CursorState.Attack; } else if (CurrentGridBlock.Unpassable) { CursorState = Enums.CursorState.Null; } else { CursorState = Enums.CursorState.Selected; if (CurrentGridBlock.ActiveSpace == Enums.ActiveSpace.Move) { _moves = _pM.CreatePath(_orgGridBlock, CurrentGridBlock).ToList(); } } }
public void MoveTo(List <GridBlock> movePoints, bool attackWhenInRange = false) { Log($"---------- {MethodBase.GetCurrentMethod().Name} ----------"); Log($"Number of moves: {movePoints.Count}"); if (CooldownTimer <= 0 && !Moved && movePoints.Count > 0) { _attackWhenInRange = attackWhenInRange; _tasked = true; // Task has been given MeleeAttackedCount = 0; for (int x = 0; x < movePoints.Count; x++) { var point = movePoints[x]; _movePositions.Enqueue(point); } Log("Moving"); CurrentGridBlock.ResetCurrentUnit(this); GetNextPoint(); } Log("----------------------------------------"); }
void Update() { if (CooldownTimer <= 0) { if (PlusAction > 0) { PlusAction -= Time.deltaTime; PlusActionText.alpha = PlusAction / PLUSACTIONTIME; } else if (PlusAction <= 0 && _unitState == Enums.UnitState.PlusAction && !_selected) { _unitState = Enums.UnitState.Idle; } //if (_unitState == Enums.UnitState.Idle && CurrentGridBlock != null && CurrentGridBlock.CurrentUnit != this) // This is a redundency in case the gridblock is out of sync with the current unit // CurrentGridBlock.CurrentUnit = this; if ((_nextPoint != null || _movePositions.Count > 0) && _unitState != Enums.UnitState.Attacking && _unitState != Enums.UnitState.Hurt) { if (_nextPoint == null) { GetNextPoint(); } Vector2 moveVector = Vector2.MoveTowards(transform.position, _nextPoint.Position, Speed * Time.deltaTime); _minimapIcon.rectTransform.anchoredPosition = Utility.UITilePosition(_minimapIcon.rectTransform, transform); transform.position = moveVector; if (transform.position.V2() == _nextPoint.Position) { Log("Arrived at point"); _prevPositions.FirstOrDefault()?.Path_Delete(this); _prevPositions.Push(_nextPoint); if (CurrentGridBlock.CurrentUnit == null && _attackWhenInRange && _attackTarget != null && Position.GridDistance(_attackTarget.Position) <= MaxAttackDistance && CurrentGridBlock?.CurrentUnit == this) { DeleteSavedPath(); _nextPoint = null; } else if (!_movePositions.IsEmpty()) { Log("Get next point"); GetNextPoint(); } else { if (CurrentGridBlock != _nextPoint) // Arrived at the last point, make sure it's the currentgridblock { CurrentGridBlock = _nextPoint; } Log("Last move"); _nextPoint = null; } if (_movePositions.IsEmpty() && _nextPoint == null) { if (CurrentGridBlock.CurrentUnit == null) { Log($"Ends move"); Moving = false; Moved = true; CurrentGridBlock.SetCurrentUnit(this); _unitState = Enums.UnitState.Idle; } else { FindGoodPreviousSpot(); } } } } if (!_attack && Target != null && _movePositions.IsEmpty() && _nextPoint == null) { Log($"checks for attack"); CheckAttack(); } if (!_selected && !Moving && _animator.GetBool("Moving")) { _animator.SetBool("Moving", false); } if (_attack && !_animator.GetCurrentAnimatorStateInfo(0).IsName("Launch") && !Attacked) { Log($"attacks"); Attack(); } if (_nextPoint == null && _unitState == Enums.UnitState.Idle && _tasked && !Moving && !_attack && (Moved || Attacked)) { Log($"goes on cooldown"); if (!Utility.TrueNull(CurrentGridBlock.CurrentUnit) && CurrentGridBlock.CurrentUnit != this) { FindGoodPreviousSpot(); } else { GoOnCooldown(); } } } if (CooldownTimer > 0) { CooldownTimer -= Time.deltaTime; if (CooldownTimer <= 0) { Log($"Come of cooldown"); if (Type == Enums.UnitType.Melee) { CooldownReduction?.gameObject.SetActive(false); } if (UnitManager?.AvailableUnits <= 0) { _cC?.SetPosition(transform.position); } _animator.SetBool("Cooldown", OnCooldown = false); _minimapIcon.color = Player == Enums.Player.Player1 ? Colors.Player_Idle : Colors.Enemy_Idle; if (OffCooldownObject != null) { Log("Create cooldown object"); Instantiate(OffCooldownObject, transform.position, Quaternion.identity); } if (Player != Enums.Player.Player1) // This is for non player units to make sure all units are looped through { UnitManager?.AddUnit(this); } _unitState = Enums.UnitState.Idle; } } if (Player != Enums.Player.Player1 && _nextPoint == null && _unitState == Enums.UnitState.Idle && !OnCooldown && !Moving && !Moved && !Attacked) { if (!UnitManager.PlayerInfo.Units.Contains(this)) { Log($"added to unit list"); UnitManager.AddUnit(this); } } }