Esempio n. 1
0
 // Could maybe only be available to enemies with low intelligence since this dosnt take into account dangerous terrains.
 IEnumerator DirectMoveToTarget()
 {
     directlyMovingtoTarget = true;
     transform.forward      = new Vector3(target.position.x - thisEnemyTrans.position.x, target.position.y - thisEnemyTrans.position.y, myZ);
     directTargetPos        = target.position;
     if (!useOldAnimSystem)
     {
         StartWalkAnim();
     }
     while (directlyMovingtoTarget && eRefs.DistToTarget(thisEnemyTrans.position, directTargetPos) > stopRangeToTarget)
     {
         transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, myZ);
         transform.Translate(Vector3.forward * Time.deltaTime * enemy.moveSpeed * speedModifier, Space.Self);
         flip.Flip();
         if (useOldAnimSystem)
         {
             eRefs.eWalkAnim.UpdateWalkCycleAnim();
         }
         //eRefs.eSpriteR.sprite = eRefs.eSO.spriteIdle;
         yield return(null);
     }
     //print("just stopped moving, taking a lil break b4 more murder xD");
     if (!useOldAnimSystem)
     {
         StopWalkAnim();
     }
     if (useOldAnimSystem)
     {
         if (useAlternateIdle)
         {
             eRefs.eSpriteR.sprite = eRefs.alternateIdleSprite;
         }
         else
         {
             eRefs.eSpriteR.sprite = eRefs.eSO.spriteIdle;
         }
     }
     directlyMovingtoTarget = false;
 }
 public bool CheckToBash()
 {
     // Check if im currently attacking and if its off cooldown.
     if (!inBash && cdTimer >= cooldown)
     {
         // Check if the target is within attack range.
         if (eRefs.SqrDistToTarget(eRefs.PlayerCenterPos, attackDirPoint.position) <= sqrAtkRange)
         {
             // Check to see if there are obstacles in the way.
             if (!Physics2D.Raycast(attackDirPoint.position, (Vector2)eRefs.PlayerCenterPos - (Vector2)attackDirPoint.position, eRefs.DistToTarget(attackDirPoint.position, eRefs.PlayerCenterPos), eRefs.losLayerMask))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
    // public void DisableAggro() {
    //     checkingAggro = false;
    // }
    IEnumerator CheckAggro()
    {
        coroutineRunning = true;
        yield return(null);

        if (aggroDebugs)
        {
            Debug.Log("Enemy: " + this.name + " CheckAggro coroutine has started.");
        }
        float aggroRangeSqr = 0f;

        aggroRangeSqr = eRefs.eSO.aggroRange * eRefs.eSO.aggroRange;
        while (checkingAggro)
        {
            if (aggroDebugs)
            {
                Debug.Log("Enemy: " + this.name + " just checked aggro.");
            }
            float distToTargetSqr = eRefs.SqrDistToTarget(this.transform.position, eRefs.PlayerShadowPos);
            // Check if the Player is within aggro range.
            if (distToTargetSqr < aggroRangeSqr)
            {
                if (aggroDebugs)
                {
                    Debug.DrawRay(this.transform.position, eRefs.NormDirToTargetV2(this.transform.position, eRefs.PlayerShadowPos) * eRefs.DistToTarget(this.transform.position, eRefs.PlayerShadowPos), Color.green, 0.5f);
                }
                // If I dont hit anything that blocks line of sight, aggro.
                if (!Physics2D.Raycast(this.transform.position, eRefs.NormDirToTargetV2(this.transform.position, eRefs.PlayerShadowPos), eRefs.DistToTarget(this.transform.position, eRefs.PlayerShadowPos), blockLOSLayers))
                {
                    eRefs.eAction.StartChecks();
                    //Enemy has aggroed its target, request first path.
                    eRefs.eFollowPath.StartCoroutine(eRefs.eFollowPath.UpdatePath());
                    checkingAggro    = false;
                    coroutineRunning = false;
                    aggroed          = true;
                    break;
                }
            }
            // Stop the aggro checking coroutine if the enemy is outside of the target's aggro check range. Only works if the enemy has not aggroed yet.
            else if (distToTargetSqr > targCheckRadiusSqr)
            {
                checkingAggro    = false;
                coroutineRunning = false;
                break;
            }
            // Wait a certain amount of time before checking again. (for more precision, increase rate based on Player distance)
            yield return(new WaitForSeconds(checkAggroDelay));
        }
        coroutineRunning = false;
        yield return(null);
    }
    public void SetupRunAwayPos()
    {
        Stopwatch sw = new Stopwatch();

        if (runAwayDebugs)
        {
            sw.Start();
        }
        Vector2      targetPos = Vector2.zero;
        Vector2      prevDir = Vector2.zero;
        Vector2      plyrPos = eRefs.PlayerShadowPos;
        Vector2      oppositeDirNorm = (plyrPos - (Vector2)this.transform.position).normalized * -1;
        RaycastHit2D hit, lastHit;

        float distLeft = eRefs.eSO.atkRange;

        hit = Physics2D.Raycast(this.transform.position, oppositeDirNorm, distLeft, eRefs.losLayerMask);

        if (hit)
        {
            lastHit       = hit;
            targetPos     = hit.point - (hit.normal * -1) * (circleCastRadius + 0.1f);
            distLeft     -= (hit.distance - circleCastRadius);
            surfaceNormal = hit.normal;
            loopCount     = 0;
            while (loopCount < maxLoops)
            {
                loopCount++;
                RaycastHit2D loopHit = new RaycastHit2D();
                // Get the perpendicular direction the furtest away from the player that is not inside an obstacle and apply the remaining direction that way until it has been fully used.
                cWisePerpenDir        = new Vector2(surfaceNormal.y, -surfaceNormal.x);
                counterCWisePerpenDir = new Vector2(-surfaceNormal.y, surfaceNormal.x);
                cWiseTestPos          = targetPos + cWisePerpenDir;
                counterCWiseTestPos   = targetPos + counterCWisePerpenDir;
                // Try clockwise.
                if (eRefs.DistToTarget(cWiseTestPos, eRefs.PlayerShadowPos) > eRefs.DistToTarget(counterCWiseTestPos, eRefs.PlayerShadowPos) && !Physics2D.Raycast(targetPos, cWisePerpenDir, aGridNodeDiam, eRefs.losLayerMask))
                {
                    if (runAwayDebugs)
                    {
                        UnityEngine.Debug.DrawRay(targetPos, cWisePerpenDir * aGridNodeDiam, Color.cyan, 5f); print("Chose to shoot clockwise.");
                    }
                    loopHit = Physics2D.Raycast(targetPos, cWisePerpenDir, distLeft, eRefs.losLayerMask);
                    prevDir = cWisePerpenDir;
                }
                // Try counter-clockwise.
                else if (!Physics2D.Raycast(targetPos, counterCWisePerpenDir, aGridNodeDiam, eRefs.losLayerMask))
                {
                    if (runAwayDebugs)
                    {
                        UnityEngine.Debug.DrawRay(targetPos, counterCWisePerpenDir * aGridNodeDiam, Color.cyan, 5f); print("Chose to shoot counter-clockwise.");
                    }
                    loopHit = Physics2D.Raycast(targetPos, counterCWisePerpenDir, distLeft, eRefs.losLayerMask);
                    prevDir = counterCWisePerpenDir;
                }
                else
                {
                    break;
                }
                if (loopHit)
                {
                    targetPos     = loopHit.point - (loopHit.normal * -1) * (circleCastRadius + 0.1f);
                    distLeft     -= (loopHit.distance - circleCastRadius);
                    surfaceNormal = loopHit.normal;
                }
                // If nothing was hit, set the position to move to, to the last direction plus the distance left.
                else
                {
                    targetPos = targetPos + (prevDir * distLeft);
                    break;
                }
            }
        }
        else
        {
            targetPos = (Vector2)this.transform.position + oppositeDirNorm * distLeft;
        }
        movementToken.position   = targetPos;
        eRefs.eFollowPath.target = movementToken;
        if (runAwayDebugs)
        {
            sw.Stop(); print("The run away target postion took: " + sw.ElapsedMilliseconds + "ms to determine.");
        }
    }
 public bool CheckThrowProj()
 {
     if (!inProjThrow && throwProjReady)
     {
         // Check if the target is within attack range.
         if (eRefs.SqrDistToTarget(eRefs.PlayerCenterPos, this.transform.position) <= sqrAtkRange)
         {
             // Check to see if there are obstacles in the way. // Maybe switch to circle cas to make sure there is space to fire the projectile.
             if (!Physics2D.Raycast(attackDirPoint.position, eRefs.PlayerCenterPos - attackDirPoint.position, eRefs.DistToTarget(attackDirPoint.position, eRefs.PlayerCenterPos), blockLOSLayers))
             {
                 return(true);
             }
         }
     }
     return(false);
 }