예제 #1
0
    private void FixedUpdate()
    {
        // pathfinding
        if (pathfindingActive)
        {
            pathfindingActive = !myPathfindingAlgorithm.PathfindingStep();
            if (SettingsMenuCtrl.Instance.DebugEnabled)
            {
                // draw search pattern gizmos
                GameMenuCtrl.Instance.DrawNodeWidgets(myNum, myPathfindingAlgorithm.GetClosedSet());
                GameMenuCtrl.Instance.DrawNodeWidgets(myNum, myPathfindingAlgorithm.GetOpenSet());
            }
        }
        if (!pathfindingActive && pathfindingStarted)
        {
            myPath = myPathfindingAlgorithm.GetPath();
            if (myPath != null)
            {
                if (SettingsMenuCtrl.Instance.DebugEnabled)
                {
                    LevelGenerator.Instance.DrawPath(myNum, myPath, myStartNode);
                }
                // two iteration moves
                distancePonder = speed * Time.fixedDeltaTime;
                startMoving    = true;
            }
            pathfindingStarted = false;
        }

        // shooting part
        int raycastMask = LayerMask.GetMask("Enemy");

        raycastMask = ~raycastMask;
        // Cast a ray straight down.
        RaycastHit2D hit = Physics2D.Raycast(transform.position + Vector3.one / 2f, Vector2.down, 100, raycastMask);

        if (hit.collider != null)
        {
            if (hit.collider.tag == "Player" || hit.collider.tag == "Base")
            {
                if (canShoot)
                {
                    canShoot = false;
                    StartCoroutine(Shoot(Vector3.down));
                }
            }
        }
        // Cast a ray straight right.
        hit = Physics2D.Raycast(transform.position + Vector3.one / 2f, Vector2.right, 100, raycastMask);
        if (hit.collider != null)
        {
            if (hit.collider.tag == "Player" || hit.collider.tag == "Base")
            {
                if (canShoot)
                {
                    canShoot = false;
                    StartCoroutine(Shoot(Vector3.right));
                }
            }
        }
        // Cast a ray straight left.
        hit = Physics2D.Raycast(transform.position + Vector3.one / 2f, Vector2.left, 100, raycastMask);
        if (hit.collider != null)
        {
            if (hit.collider.tag == "Player" || hit.collider.tag == "Base")
            {
                if (canShoot)
                {
                    canShoot = false;
                    StartCoroutine(Shoot(Vector3.left));
                }
            }
        }

        // moving part
        if (!startMoving)
        {
            return;
        }

        if (nextNodeIter >= myPath.Count)
        {
            return;
        }

        Vector3 direction = myPath[nextNodeIter].GetWorldPos() - transform.localPosition;

        if (direction.magnitude < distancePonder)
        {
            //you are there, snap to pos and move to next point
            transform.localPosition = myPath[nextNodeIter].GetWorldPos();
            nextNodeIter++;
        }
        else
        {
            transform.localPosition += direction.normalized * speed * Time.fixedDeltaTime;
        }
    }