Exemplo n.º 1
0
    public IEnumerator MeleeAttack(Unit unit, Vector2Int direction, int damage)
    {
        Vector3 startinPos = unit.GetUnitWorldPosition();

        // Get new direction based on if obstacles are hit
        Vector2Int moveDirection   = GetClearDirection(direction, true);  //Stops one tile before target
        Vector2Int attackDirection = GetClearDirection(direction, false); //Stops at target
        Vector2Int attackPosition  = new Vector2Int(gridPositioin.x + attackDirection.x, gridPositioin.y + attackDirection.y);
        Vector2Int endPosition     = new Vector2Int(gridPositioin.x + moveDirection.x, gridPositioin.y + moveDirection.y);

        World.WorldObject hitObject = World.instance.GetObjectAtPosition(attackPosition.x, attackPosition.y);
        bool absorbed = false;

        if (hitObject == World.WorldObject.Empty || hitObject == World.WorldObject.Enemy || hitObject == World.WorldObject.Friend)
        {
            if (World.instance.DamageObjectAtPoint(attackPosition, damage))
            {
                // Move to unit position if unit is killed
                endPosition = attackPosition;
            }
        }
        else if (hitObject == World.WorldObject.Hut && unitType == World.WorldObject.Enemy)
        {
            if (World.instance.AbsorbHutAtPoint(attackPosition, data))
            {
                endPosition = attackPosition;
                World.instance.DamageObjectAtPoint(gridPositioin, 100);
                absorbed = true;
            }
        }

        Debug.Log("Attacking from: " + gridPositioin + ", to: " + endPosition + " With attack direction: " + direction + " and fixed direction: " + attackDirection + ", woth damage: " + damage);

        Vector3 endPos = GetUnitWorldPosition(endPosition);

        //Update position in world
        if (!absorbed)
        {
            World.instance.MarkWorldPosition(gridPositioin.x, gridPositioin.y, World.WorldObject.Empty);
            unit.gridPositioin = endPosition;
            World.instance.MarkWorldPosition(gridPositioin.x, gridPositioin.y, unitType);
        }

        float elapsedTime       = 0;
        float durationInSeconds = ATTACK_DURATION;

        while (elapsedTime < durationInSeconds)
        {
            unit.transform.position = Vector2.Lerp(startinPos, endPos, Mathf.SmoothStep(0.0f, 1.0f, elapsedTime / durationInSeconds));
            elapsedTime            += Time.deltaTime;
            yield return(new WaitForEndOfFrame());
        }
        unit.transform.position = endPos;
    }
Exemplo n.º 2
0
    public void PlanAttack()
    {
        hasPlannedAttack = true;

        // chance to target cell or friendly unit
        World.WorldObject target = (Random.Range(0, 100) < GameData.instance.attackCellChance) ? World.WorldObject.Hut : World.WorldObject.Friend;

        Vector2Int closestTarget = World.instance.GetClosestObjectFromPosition(target, gridPositioin, true);

        // No target exsist if vector is null
        if (closestTarget.x < 0 && closestTarget.y < 0)
        {
            hasPlannedAttack = false;
            return;
        }

        plannedAction = new UnitAction();
        Vector2Int directionToTarget = closestTarget - gridPositioin;

        plannedAction.type      = (attackType == EnemyData.AttackType.Melee) ? UnitAction.ActionType.MeleeAttack : UnitAction.ActionType.RangedAttack;
        plannedAction.damage    = damage;
        plannedAction.direction = GetRandomClampedDirection(directionToTarget, attackRange);
    }