Attack() 공개 메소드

public Attack ( GameObject tar ) : int
tar GameObject
리턴 int
    /// <summary>
    /// Check if attacking unit can attack another unit and then attack with attacking unit
    /// </summary>
    /// <param name="attackingUnit">unitcontroller of attacking unit</param>
    /// <param name="defendingUnit">unitcontroller of unit being attacked</param>
    /// <returns>true if attack succeeded, false if defending unit was out of range</returns>
    private bool AttackUnit(UnitController attackingUnit, UnitController defendingUnit)
    {
        Coords enemyUnitCoords = defendingUnit.GetUnitCoords();

        //check if enemy is within range and has path
        if (attackingUnit.CanAttack(enemyUnitCoords))
        {
            attackingUnit.Attack(defendingUnit);

            //check if enemy unit is dead
            if (!defendingUnit.IsAlive())
            {
                //replace this gameobject in grid with null
                grid.SetGridArray(enemyUnitCoords, null);

                //remove from army list - check which army it is in
                if (defendingUnit.CompareTag("Enemy Unit"))
                {
                    enemyArmy.Remove(defendingUnit);
                }
                else
                {
                    playerArmy.Remove(defendingUnit);
                }

                defendingUnit.Die();

                StartCoroutine(CheckForVictor());
            }

            return(true);
        }

        return(false);
    }
예제 #2
0
    IEnumerator Follow()
    {
        int counter = 0;

        if (targetScript.owner != team)
        {
            isAttacking = true;
        }
        else if (targetScript.owner == team)
        {
            isAttacking = false;
        }
        Debug.Log("Follow");

        while (isFollowing)
        {
            if (targetNode == null || targetNode.GetComponent <UnitController>().isDead)
            {
                isMeleeing  = false;
                isAttacking = false;
                isFollowing = false;
                break;
            }
            float dist = Vector3.Distance(targetNode.transform.position, agent.transform.position);
            if (!isMeleeing && isAttacking && dist < UC.attackRange)
            {
                isMeleeing = true;
                StartCoroutine(UC.Attack(targetNode, agent.transform.rotation));
            }
            else
            {
                // isMeleeing = false;
            }

            if (counter < 6)
            {
                bool followed = targetNode.transform.GetChild(2).gameObject.activeInHierarchy;
                targetNode.transform.GetChild(2).gameObject.SetActive(!followed);
            }

            counter          += 1;
            agent.destination = targetNode.transform.position;
            yield return(new WaitForSeconds(0.2f));
        }
    }
예제 #3
0
        void OnTriggerEnter2D(Collider2D other)
        {
            if (_controller == null)
            {
                _controller = transform.parent.GetComponent <UnitController>();
            }

            if (other.tag != "Unit")
            {
                return;
            }

            if (_controller.TargeType != TragetType.None)
            {
                return;
            }

            var unit = other.GetComponent <UnitController>();

            if (unit.PlayerOwner != _controller.PlayerOwner)
            {
                _controller.Attack(unit);
            }
        }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (OnOffMenu)
            {
                _pc.escMenu.gameObject.SetActive(true);
                OnOffMenu = !OnOffMenu;
            }
            else
            {
                _pc.escMenu.gameObject.SetActive(false);
                OnOffMenu = !OnOffMenu;
            }
        }
        if (_pc.staminarecovery)
        {
            _pc.stamina.fillAmount = _pc.stamina.fillAmount + Time.deltaTime;
        }

        if (Input.GetKeyDown(KeyCode.Y))
        {
            Instantiate(_enemy, transform.position, Quaternion.identity);
        }

        var axis = Input.GetAxis("Horizontal");

        if (Mathf.Abs(axis) > 0.01f)
        {
            if (_pc.attacking)
            {
                return;
            }
            _pc.Move(axis);
            _pc.Flip();

            if (Input.GetKeyDown(KeyCode.LeftShift))
            {
                if (_staminabar.fillAmount > _dodgevalue)
                {
                    animationcharacter.SetTrigger("Dodge");
                }
                else
                {
                    animationcharacter.SetTrigger("Idle");
                }
            }
            if (_pc.isGrounded)
            {
                animationcharacter.SetBool("isRunning", true);
            }
        }
        else
        {
            if (Input.GetKeyUp(KeyCode.A) || (Input.GetKeyUp(KeyCode.D)))
            {
                _pc.Move(0);
                _pc.Flip();
                if (!_pc.isGrounded)
                {
                    return;
                }
                if (_pc.isGrounded)
                {
                    animationcharacter.SetBool("isRunning", true);
                }
            }
            else
            {
                animationcharacter.SetBool("isRunning", false);
            }
        }

        if ((Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.Space)))
        {
            if (_pc.attacking)
            {
                return;
            }
            if (_staminabar.fillAmount > _jumpvalue)
            {
                _pc.Jump();
                animationcharacter.SetBool("isFalling", false);
                animationcharacter.SetBool("isJumping", true);
                if (_pc.extraJump > 0)
                {
                    _staminabar.fillAmount -= _jumpvalue;
                    _firstjump              = true;
                    _pc.CooldownStamina();
                }
                else if ((_pc.extraJump == 0) && (_firstjump))
                {
                    _staminabar.fillAmount -= _jumpvalue;
                    _firstjump              = false;
                    _pc.CooldownStamina();
                }
            }
        }
        else
        {
            if (!_pc.isGrounded)
            {
                animationcharacter.SetBool("isFalling", true);
            }
            if (_pc.isGrounded)
            {
                animationcharacter.SetBool("isJumping", false);
                animationcharacter.SetBool("isFalling", false);
            }
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            _pc.Crouch(true);
            animationcharacter.SetBool("isSitDown", true);
        }
        else if (Input.GetKeyUp(KeyCode.S))
        {
            _pc.Crouch(false);
            animationcharacter.SetBool("isSitDown", false);
        }

        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            if (_pc.attacking)
            {
                return;
            }
            if (_pc.isCrouch)
            {
                return;
            }
            if (_staminabar.fillAmount > _dodgevalue)
            {
                _pc.Dodge(axis);
                _staminabar.fillAmount -= _dodgevalue;
                _pc.CooldownStamina();
                animationcharacter.SetTrigger("Dodge");
            }
            else
            {
                animationcharacter.SetTrigger("Idle");
            }
        }
        if (Input.GetKeyDown(KeyCode.Mouse0) && !_pc.attacking)
        {
            if ((_pc.isCrouch) || (_pc.dodgetrigger) || (!_pc.isGrounded))
            {
                return;
            }

            if (_staminabar.fillAmount > _attackvalue)
            {
                _pc.Attack();
                _staminabar.fillAmount -= _attackvalue;
                _pc.CooldownStamina();
                animationcharacter.SetTrigger("AttackComboOne");
            }
            else
            {
                animationcharacter.SetTrigger("Idle");
            }
        }
        //  if (_pc._wallCheck)
        //   {
        //       _pc.WallStay(axis);
        //   }
    }
예제 #5
0
    void Update()
    {
        if (_enemycontroll.heal < 0)
        {
            animatorenemy.SetTrigger("Dead");
            _enemycontroll.Dead();
        }

        if (_enemycontroll.attacking)
        {
            if (_enemycontroll.attackTimer > 0)
            {
                _enemycontroll.attackTimer -= Time.deltaTime;
            }
            else
            {
                _enemycontroll.attacking             = false;
                _enemycontroll.attackTrigger.enabled = false;
            }
        }


        //  _direction = _target.transform.position.x - transform.position.x;
        if (_Triggergate.isTrigger)
        {
            return;
        }
        if (Vector2.Distance(transform.position, _target.position) > _distans)
        {
            if (!_enemycontroll.isGrounded)
            {
                animatorenemy.SetBool("isFalling", true);
            }
            if (_enemycontroll.isGrounded)
            {
                animatorenemy.SetBool("isJumping", false);
                animatorenemy.SetBool("isFalling", false);
            }
            if (_target.transform.position.x - transform.position.x < 0 && _target.position.y < transform.position.y)
            {
                _enemycontroll.Move(-0.7f);
                animatorenemy.SetBool("isRunning", true);
            }
            else if (_target.transform.position.x - transform.position.x > 0 && _target.position.y < transform.position.y)
            {
                _enemycontroll.Move(0.7f);
                animatorenemy.SetBool("isRunning", true);
            }
            else if (_target.transform.position.x - transform.position.x < 0)
            {
                _enemycontroll.Move(-0.7f);
                animatorenemy.SetBool("isRunning", true);
                if (Mathf.Abs(_target.transform.position.y - transform.position.y) < 3)
                {
                    animatorenemy.SetBool("isRunning", false);
                    animatorenemy.SetBool("isJumping", true);
                    _enemycontroll.Jump();
                }
            }
            else if (_target.transform.position.x - transform.position.x > 0)
            {
                _enemycontroll.Move(0.7f);
                animatorenemy.SetBool("isRunning", true);
                if (Mathf.Abs(_target.transform.position.y - transform.position.y) > 3)
                {
                    animatorenemy.SetBool("isRunning", false);
                    animatorenemy.SetBool("isJumping", true);
                    _enemycontroll.Jump();
                }
            }
            //if (!_pc._isGrounded)
            //  {
            //     animationcharacter.SetBool("isFalling", true);
            // }
            // if (_pc._isGrounded)
            // {
            //      animationcharacter.SetBool("isJumping", false);
            //      animationcharacter.SetBool("isFalling", false);
            //  }

            //transform.position = Vector2.MoveTowards(transform.position, _target.position, _enemycontroll._speed * Time.deltaTime);
            _enemycontroll.Flip();
        }
        else if (!_enemycontroll.attacking)
        {
            _enemycontroll.attackCd = 2f;
            _enemycontroll.Attack();
            animatorenemy.SetTrigger("Attack");
        }


        //_enemycontroll.Move(1f);
        // _enemycontroll.Flip();
    }
예제 #6
0
        public PlayerSelectionManager(UnitClickSignal unitClickSignal, BuildingClickSignal buildingClickSignal, TileClickSignal tileClickSignal, PlayerSessionModifiers modifiers, MouseReleaseSignal mouseRelease, PlayerManager playerManager, GameMap map)
        {
            Action clearBuildings = () =>
            {
                if (SelectedBuilding != null)
                {
                    SelectedBuilding.Deselect();
                }

                SelectedBuilding = null;
            };

            Action clearSelection = () =>
            {
                foreach (var s in SelectedUnits)
                {
                    s.Deselect();
                }
                SelectedUnits.Clear();

                clearBuildings();
            };

            mouseRelease.Event += btn =>
            {
                if (btn == 0 && modifiers.Dragging)
                {
                    modifiers.Dragging = false;

                    clearSelection();

                    var startpoint = _camera.ScreenToWorldPoint(modifiers.DragStart);
                    var endpoint   = _camera.ScreenToWorldPoint(modifiers.DragCurrent);

                    var allunits = Physics2D.OverlapAreaAll(startpoint, endpoint);

                    var buildingcandidate = default(BuildingController);
                    var foundunit         = false;

                    foreach (var unit in allunits)
                    {
                        if (unit.tag == "Building")
                        {
                            buildingcandidate = unit.GetComponent <BuildingController>();
                        }

                        if (unit.tag == "Unit")
                        {
                            var ctrl = unit.GetComponent <UnitController>();

                            if (ctrl.PlayerOwner != playerManager.HumanPlayer.ID)
                            {
                                continue;
                            }

                            foundunit = true;
                            ctrl.Select();
                            SelectedUnits.Add(ctrl);
                        }
                    }

                    if (!foundunit && buildingcandidate != null)
                    {
                        SelectedBuilding = buildingcandidate;
                    }
                }
            };


            unitClickSignal.Event += (btn, ctrl) =>
            {
                if (btn == 0)
                {
                    if (ctrl.PlayerOwner != playerManager.HumanPlayer.ID)
                    {
                        return;
                    }

                    if (!modifiers.MultiSelectDown)
                    {
                        clearSelection();
                    }
                    else
                    {
                        if (SelectedUnits.Contains(ctrl))
                        {
                            SelectedUnits.Remove(ctrl);
                            ctrl.Deselect();

                            if (PrimaryUnit == ctrl)
                            {
                                PrimaryUnit = null;
                            }

                            return;
                        }
                    }

                    clearBuildings();
                    SelectedUnits.Add(ctrl);
                    ctrl.Select();
                    PrimaryUnit = ctrl;
                }
                else
                {
                    if (PrimaryUnit != null && ctrl.PlayerOwner != 0)
                    {
                        if (PrimaryUnit.CanAttack)
                        {
                            PrimaryUnit.Attack(ctrl);
                        }
                    }
                }
            };

            buildingClickSignal.Event += (btn, ctrl) =>
            {
                if (btn != 0)
                {
                    return;
                }

                if (ctrl.PlayerOwner != playerManager.HumanPlayer.ID)
                {
                    return;
                }

                clearSelection();

                ctrl.Select();
                SelectedBuilding = ctrl;
            };

            tileClickSignal.Event += (btn, x, y) =>
            {
                if (btn == 0 && !modifiers.WorkingWithAction)
                {
                    modifiers.DragStart = Input.mousePosition;
                    modifiers.Dragging  = true;
                }

                if (btn == 1)
                {
                    foreach (var unit in SelectedUnits)
                    {
                        if (map.Map[x, y].Minable && unit.CanHarvest)
                        {
                            unit.Harvest(x, y);
                            return;
                        }

                        unit.GetComponent <PathFinderFollower>().MoveTo(x, y);
                        unit.TargeType = TragetType.None;
                    }
                }
            };
        }