Example #1
0
    void UpdateNewAI()
    {
        currentMoveDirection = 0;

        Vector2 targetPos;
        Vector2 myPos = rigidbody2D.position;

        if (aiPhase == AIPhase.PathToPickup)
        {
            if (targetObject == null)
            {
                GetClosestTarget();
                targetPos = enemyControl.transform.position;
            }
            else
            {
                targetPos = targetObject.position;
            }
        }
        else if (aiPhase == AIPhase.FreePath)
        {
            if (destinationNode == null)
            {
                GetClosestTarget();
                targetPos = enemyControl.transform.position;
            }
            else
            {
                targetPos = destinationNode.node.transform.position;
            }
        }
        else
        {
            targetPos = enemyControl.transform.position;
        }

        Vector2 distanceBetween = new Vector2(Mathf.Abs(targetPos.x - myPos.x), Mathf.Abs(targetPos.y - myPos.y));

        switch (aiPhase)
        {
        case AIPhase.FreePath:
        case AIPhase.PathToPickup: UpdatePathToTarget(distanceBetween, 0.2f);
            break;

        case AIPhase.PathToEnemy: UpdatePathToTarget(distanceBetween, punchDistance - 0.2f);
            break;

        case AIPhase.InCombat: UpdateCombat(targetPos, myPos, distanceBetween);
            break;

        case AIPhase.BreakingCrate: UpdateCrateBreaking(targetPos, myPos, distanceBetween);
            break;
        }

        if (aiPhase != AIPhase.PathToPickup && pickupTimer == 0 && pickupSpawner.PickupSpawned())
        {
            if (Random.Range(0, 100) < 30)
            {
                GetClosestPickup();
            }

            pickupTimer = Random.Range(1.0f, 5.0f);
        }

        if (destinationNode == null && navPath.Count == 0)
        {
            GetClosestTarget();
        }

        if (blockingTimer > 0)
        {
            blockingTimer -= Time.deltaTime;
            if (blockingTimer <= 0)
            {
                blockingTimer = 0;
                block         = false;
            }
        }

        if (freerunCounter > 0)
        {
            freerunCounter -= Time.deltaTime;
            if (freerunCounter <= 0)
            {
                freerunCounter = 0;
            }
        }

        if (attackCooldown > 0)
        {
            attackCooldown -= Time.deltaTime;
            if (attackCooldown <= 0)
            {
                attackCooldown = 0;
            }
        }

        if (pickupTimer > 0)
        {
            pickupTimer -= Time.deltaTime;
            if (pickupTimer <= 0)
            {
                pickupTimer = 0;
            }
        }

        if (!playerControl.CanBlock)
        {
            block = false;
        }

        playerControl.MoveDirection = currentMoveDirection;
        playerControl.Blocking      = block;
    }