Exemple #1
0
 public void SetSpellType(Form form)
 {
     if (form.type == Form.Type.Projectile && form.aim != Form.Aim.Point)
     {
         // Add Projectile Scripts and Objects to
         // the spell gameobject
         this.gameObject.AddComponent <ProjectileController>();
         StartCoroutine(CreateCollider(0.1f));
         AdjustProjectile(_cast.GetProjectileRange(), _cast.GetProjectileSpeed());
     }
     if (form.type == Form.Type.Area)
     {
         // Add Area Scripts and Objects to
         // the spell gameobject
         this.gameObject.AddComponent <AreaController>();
     }
     else if (form.aim == Form.Aim.Point)
     {
         // Add Projectile Scripts and Objects to
         // the spell gameobject
         this.gameObject.AddComponent <ProjectileController>();
         StartCoroutine(CreateCollider(0.001f));
         AdjustProjectile(_cast.GetProjectileRange(), _cast.GetProjectileSpeed());
     }
 }
    void Update()
    {
        // Update Debug Log
        _unit.DrawJumpRay();


        #region Input handler
        // First check if this unit is selected
        if (_selectable.isSelected)
        {
            HeroInputs();
        }
        #endregion

        // State Machine
        switch (state)
        {
            #region State Idling
        case State.Idling:
            // If unit has a move target
            if (_unit.CheckHasMoveTarget())
            {
                _unit.MoveToTarget();
                state = State.Moving;
            }
            // If unit has an cast target
            if (_cast.CheckHasTarget())
            {
                // and it is in casting range
                if (_cast.CheckTargetInRange(_spell.GetSelectedSpell(), 0))
                {
                    _cast.StartCast(CastUtil.Type.AutoSpell, _spell.GetSelectedSpell());
                    state = State.AutoCasting;
                }
                // If it is not in range, move towards it
                else
                {
                    state = State.Moving;
                }
            }
            break;
            #endregion


            #region State Moving
        case State.Moving:
            // Update move target
            _unit.MoveToTarget();
            // If move target reached, stop moving
            if (_unit.CheckTargetReached())
            {
                _unit.StopMoving();
                state = State.Idling;
            }
            // If unit has an cast target
            if (_cast.CheckHasTarget())
            {
                // and it is in casting range
                if (_cast.CheckTargetInRange(_spell.GetSelectedSpell(), 0))
                {
                    _unit.StopMoving();
                    _cast.StartCast(CastUtil.Type.AutoSpell, _spell.GetSelectedSpell());
                    state = State.AutoCasting;
                }
                // If it not in range, keep moving towards it
                else
                {
                    _unit.SetMoveTarget(_cast.castTarget.transform.position);
                }
            }
            break;
            #endregion


            #region State AutoCasting
        case State.AutoCasting:
            // If unit has an cast target
            if (_cast.CheckHasTarget())
            {
                // and it is in casting range
                if (_cast.CheckTargetInRange(_spell.GetSelectedSpell(), 0))
                {
                    // Start casting
                    this.transform.LookAt(_cast.castTarget.transform);
                    StartCoroutine(_cast.StartCast(CastUtil.Type.AutoSpell, _spell.GetSelectedSpell()));
                    if (!_select.isActive)
                    {
                        _select.SetSelectManagerActive(true);
                    }
                }
                // If it is not in range, keep moving towards it
                else
                {
                    state = State.Moving;
                }
            }
            // If unit has move target
            else if (_unit.CheckHasMoveTarget())
            {
                _cast.StopCast();
                state = State.Moving;
            }
            // If unit has no cast and move target
            else
            {
                _cast.StopCast();
                state = State.Idling;
            }
            break;
            #endregion


            #region State Aiming
        case State.Aiming:
            // Right click movement while aiming is still possible
            if (_unit.CheckHasMoveTarget())
            {
                _unit.MoveToTarget();
            }
            // If move target reached, stop moving
            if (_unit.CheckTargetReached())
            {
                _unit.StopMoving();
            }

            Spell spell = _spell.GetSelectedSpell();

            // UI Draw / Cast range, aim indicators
            if (spell.form.type == Form.Type.Projectile)
            {
                _ui.DrawUI(spell, _cast.GetProjectileRange());
            }
            else
            {
                _ui.DrawUI(spell, _cast.GetAreaRange());
            }

            var distanceMouse = Vector3.Distance(_unit.GetCorrectPosition(), _select.GetMousePos());
            // On Left click action
            if (Input.GetMouseButtonDown(0))
            {
                // Detect left click target here
                Debug.Log("Aim Try Trigger");
                if (!_cast.CheckTargetInRange(spell, distanceMouse))
                {
                    Debug.Log("Out of cast range");
                }
                // If the left click target is in range
                else
                {
                    if (!_cast.isWaitingForCoolDown)
                    {
                        GameObject clickedObj = null;
                        if (_select.GetClickedObject() != null)
                        {
                            clickedObj = _select.GetClickedObject();
                        }
                        //Debug.Log("Hit: " + clickedObj.transform.parent.gameObject.name);

                        Vector3 clickedPos = _select.GetMousePos();
                        Debug.Log("Hit Vector: " + clickedPos);

                        // Check if spell needs a target object
                        if (spell.form.aim == Form.Aim.Auto)
                        {
                            // If it is targetable, the check sets it as target object
                            if (_cast.CheckTargetTargetable(clickedObj, spell))
                            {
                                Debug.Log("Cast Auto Target Spell");
                                this.transform.LookAt(_cast.castTarget.transform);
                                _unit.StopMoving();
                                // Start casting
                                StartCoroutine(_cast.StartCast(CastUtil.Type.SingleSpell, spell));
                                state = State.Casting;
                                _ui.RemoveUnitDraws();
                                _select.SetSelectManagerActive(true);
                            }
                            else
                            {
                                Debug.Log("Not Targetable");
                            }
                        }
                        else if (spell.form.aim == Form.Aim.Directional || spell.form.aim == Form.Aim.Point)
                        {
                            Debug.Log("Cast Directional Spell");
                            var castDir = clickedPos;
                            _cast.castDir = castDir;
                            this.transform.LookAt(clickedPos);
                            _unit.StopMoving();
                            // Start casting
                            StartCoroutine(_cast.StartCast(CastUtil.Type.SingleSpell, spell));
                            state = State.Casting;
                            _ui.RemoveUnitDraws();
                            _select.SetSelectManagerActive(true);
                        }
                    }
                }
            }
            break;
            #endregion


            #region State Casting
        case State.Casting:
            // If unit has move target

            if (_unit.CheckHasMoveTarget() && !_cast.IsCastLocked)
            {
                _cast.StopCast();
                state = State.Moving;
            }

            break;
            #endregion

            #region State Jumping
        case State.Jumping:
            _unit.JumpToTarget();
            // If jump target reached stop the jump
            if (_unit.CheckJumpTargetReached())
            {
                _unit.StopMoving();
                state = State.Idling;
            }
            // or if unit hits obstacle during jump
            if (_unit.CheckJumpCollision())
            {
                _unit.StopMoving();
                _unit.SetJumpCollision(false);
                state = State.Idling;
            }
            // *** Detection Hotfix if unit got stuck on an obstacle ***
            if (_unit.CheckIfStoppedMoving())
            {
                _unit.StopMoving();
                state = State.Idling;
            }
            break;
            #endregion
        }
    }