Example #1
0
    void Awake()
    {
        BoxCollider = GetComponent <BoxCollider2D>();
        _animator   = GetComponent <Animator>();
        _sR         = GetComponent <SpriteRenderer>();
        _damagable  = GetComponent <Damageable>();

        _unitState     = Enums.UnitState.Idle;
        _movePositions = new Queue <GridBlock>();
        _prevPositions = new Stack <GridBlock>();
        _nextPoint     = null;
    }
Example #2
0
    public void EnterHurtState()
    {
        Log($"---------- {MethodBase.GetCurrentMethod().Name} ----------");
        if (_selected)
        {
            OnUnitInterupt?.Invoke();
        }

        _prevState = _unitState;
        _unitState = Enums.UnitState.Hurt;
        Log("----------------------------------------");
    }
Example #3
0
    private void GoOnCooldown()
    {
        Log($"---------- {MethodBase.GetCurrentMethod().Name} ----------");
        Reset();
        _unitState         = Enums.UnitState.Cooldown;
        _minimapIcon.color = Player == Enums.Player.Player1 ? Colors.Player_Cooldown : Colors.Enemy_Cooldown;
        //_pM?.PlayerUnitMoveDown(this);
        var crc = CheckReducedCooldown();

        CooldownTimer = Cooldown * (!Attacked ? 1 : 1.4f) * crc;
        _animator.SetBool("Cooldown", OnCooldown = true);
        Log("----------------------------------------");
    }
Example #4
0
    public void EnterAttackState()
    {
        Log($"---------- {MethodBase.GetCurrentMethod().Name} ----------");
        if (_selected)
        {
            OnUnitInterupt?.Invoke();
        }

        LookAt(_attackPos);
        _prevState = _unitState;
        _unitState = Enums.UnitState.Attacking;
        Log("----------------------------------------");
    }
Example #5
0
 public void Select(bool select)
 {
     Log($"---------- {MethodBase.GetCurrentMethod().Name} ----------");
     if (CooldownTimer <= 0)
     {
         if (_unitState != Enums.UnitState.PlusAction)
         {
             _unitState = select ? Enums.UnitState.Selected : Enums.UnitState.Idle;
         }
         _selected = select;
     }
     Log("----------------------------------------");
 }
Example #6
0
    public void ExitHurtState()
    {
        Log($"---------- {MethodBase.GetCurrentMethod().Name} ----------");

        if ((Blocked || Moving) && !Attacked)
        {
            CollisionClear();
        }

        if (!TookAction && (!_tasked || _unitState == Enums.UnitState.Attacking) && _collisionTarget != null)
        {
            CheckAttack(_collisionTarget.CurrentGridBlock, true);
        }

        _unitState = _prevState == Enums.UnitState.Hurt ? Enums.UnitState.Idle : _prevState; // DO NOT reassign Attacking as a state;
        Log("----------------------------------------");
    }
Example #7
0
 private void CollisionClear()
 {
     Log($"---------- {MethodBase.GetCurrentMethod().Name} ----------");
     if (!_movingBack)
     {
         _unitState = Enums.UnitState.Idle;
         Target     = null;
         DeleteSavedPath();
         _attackWhenInRange = false;
         if (!_prevPositions.IsEmpty())
         {
             FindGoodPreviousSpot();
             _movingBack = true;
         }
         Blocked = false;
     }
     Log("----------------------------------------");
 }
Example #8
0
    private void GetNextPoint()
    {
        var currentPoint  = _nextPoint;
        var possiblePoint = _movePositions.Peek();

        if (possiblePoint == null || possiblePoint.Position.GridDistance(Position) > 1) // Make sure the next point is one point away, this should trigger VERY rarely
        {
            var orderedNeighbors = CurrentGridBlock.Neighbors.OrderByDistance(possiblePoint);
            foreach (GridBlock gB in orderedNeighbors)
            {
                if (gB.Position.GridDistance(possiblePoint.Position) <= 1)
                {
                    possiblePoint = gB;
                    break;
                }
            }
        }
        else
        {
            _movePositions.Dequeue();                                                                                                            // point is fine go ahead and remove it from queue
        }
        if (currentPoint != null && _movePositions.IsEmpty() && possiblePoint.CurrentUnit != null && !possiblePoint.CurrentUnit.IsEnemy(Player)) // if this is the last point in the queue make sure it's not empty
        {
            if (currentPoint.CurrentUnit == this)                                                                                                // Last spot is taken and current spot is safe to stay at
            {
                _nextPoint = null;
                return;
            }
            else // current spot isn't safe so find a new spot
            {
                FindGoodPreviousSpot();
                GetNextPoint();
                return;
            }
        }

        _nextPoint         = possiblePoint;
        Moving             = true;
        _minimapIcon.color = Player == Enums.Player.Player1 ? Colors.Player_Moving : Colors.Enemy_Moving;
        _unitState         = Enums.UnitState.Moving;
        BoxCollider.size   = ColliderSizeMoving;
        _animator.SetBool("Moving", true);
        LookAt(possiblePoint.Position);
    }
Example #9
0
    public void ExitAttackState()
    {
        Log($"---------- {MethodBase.GetCurrentMethod().Name} ----------");
        if (Type == Enums.UnitType.Horse && _prevPositions.Count < MoveDistance && !Blocked)
        {
            Moved                = false;
            _unitState           = Enums.UnitState.PlusAction;
            PlusAction           = PLUSACTIONTIME;
            PlusActionText.alpha = 1;
            PlusActionText.gameObject.SetActive(true);
            //_originalPoint = CurrentGridBlock;
            ResetLook();

            _attackWhenInRange = false;
            _attack            = false;
            Target             = null;
        }
        else if (Attacked)
        {
            if (Blocked && (Target?.CurrentUnit != null || _collisionTarget != null))
            {
                CollisionClear();
            }

            _unitState         = _prevState == Enums.UnitState.Attacking ? Enums.UnitState.Idle : _prevState; // DO NOT reassign Attacking as a state
            _attackWhenInRange = false;
            _attack            = false;
            Target             = null;
        }

        if (!_tasked)
        {
            Attacked = false; // This means the unit attacked without being tasked to which shouldn't hinder it from attacking again.
        }
        Log("----------------------------------------");
    }
Example #10
0
    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);
            }
        }
    }