示例#1
0
    // Update is called once per frame
    void Update()
    {
        GameObject player = GameObject.Find("Player");

        if (Input.GetKey(KeyCode.L) && player.GetComponent <Player_Controls>().getMark() == this.gameObject)
        {
            return;
        }
        if (curState == basicStates.watch)
        {
            if (canSeePlayer() || base.getAlert())
            {
                lastSeen = player.transform.position;
                if (base.getAlert())
                {
                    lastSeen = base.getWhereToSearch();
                }
                curState = basicStates.flee;
            }
            if (Vector2.Distance(transform.position, home.position) > 1)
            {
                moveAI(home);
            }
        }
        else if (curState == basicStates.flee)
        {
            /*
             * GameObject flee = new GameObject();
             * Vector2 curPos = this.transform.position;
             * Vector2 fleeDir = player.GetComponent<Player_Controls>().grabSpellAim();
             * flee.transform.position = new Vector3(curPos.x+fleeDir.x,curPos.y+fleeDir.y,this.transform.position.z);
             * moveAI(flee.transform);
             */
            GameObject[] guards = GameObject.FindGameObjectsWithTag("enemy");

            //int pickIndex = Random.Range(0, guards.Length - 1);
            if (!searchDone)
            {
                foreach (GameObject g in guards)
                {
                    if (g.transform != this.transform && Vector2.Distance(g.transform.position, this.transform.position) < searchRadis)
                    {
                        currGuard  = g.transform;
                        searchDone = true;
                        break;
                    }
                }
                if (currGuard == null)
                {
                    int pickIndex = Random.Range(0, guards.Length - 1);
                    currGuard = guards[pickIndex].transform;
                }
            }
            moveAI(currGuard);
            if (canSeeGuards())
            {
                searchDone = false;
                curState   = basicStates.alert;
            }
        }
        else if (curState == basicStates.alert)
        {
            bool done = alertGuards();
            if (done)
            {
                base.setAlert(false);
                curState = basicStates.watch;
            }
        }
    }
示例#2
0
    /*
     * public void OnPathComplete(Path p)
     * {
     *  Debug.Log("A path was calculated. Did it fail with an error? " + p.error);
     *
     *  // Path pooling. To avoid unnecessary allocations paths are reference counted.
     *  // Calling Claim will increase the reference count by 1 and Release will reduce
     *  // it by one, when it reaches zero the path will be pooled and then it may be used
     *  // by other scripts. The ABPath.Construct and Seeker.StartPath methods will
     *  // take a path from the pool if possible. See also the documentation page about path pooling.
     *  p.Claim(this);
     *  if (!p.error)
     *  {
     *      if (path != null) path.Release(this);
     *      path = p;
     *      // Reset the waypoint counter so that we start to move towards the first point in the path
     *      currentWaypoint = 0;
     *  }
     *  else
     *  {
     *      p.Release(this);
     *  }
     * }
     */
    public void Update()
    {
        GameObject player = GameObject.Find("Player");

        if (Input.GetKey(KeyCode.L) && player.GetComponent <Player_Controls>().getMark() == this.gameObject)
        {
            return;
        }

        //print("Distance to target: " + Vector2.Distance(this.transform.position, targets[curIndex].position));
        //Patrol system needs work!
        if (base.getAlert())
        {
            curState = basicStates.alerted;
        }
        //Switch to seeing system with a wait timer before ignoring the player
        if (curState == basicStates.patrol)
        {
            base.moveAI(targets[curIndex]);
            if (Vector2.Distance(this.transform.position, targets[curIndex].position) < 2)
            {
                curIndex += direction;
                print("Current index: " + curIndex);
            }
            if ((curIndex > end && direction > 0) || (curIndex < end && direction < 0))
            {
                curIndex  = start;
                start     = end;
                end       = curIndex;
                curIndex  = start - direction;
                direction = -direction;
            }
            if (base.canSeePlayer())
            {
                curState = basicStates.chase;
            }

            /*
             * if (Vector2.Distance(playerPosition.position, this.transform.position) < 10)
             * {
             *  curState = basicStates.chase;
             * }
             */
            //moveAI(targets[index]);
        }
        else if (curState == basicStates.chase)
        {
            base.moveAI(playerPosition);
            if (Vector2.Distance(playerPosition.position, this.transform.position) > 10)
            {
                curState = basicStates.patrol;
            }
            base.attack();
        }
        else if (curState == basicStates.alerted)
        {
            moveAI(base.getWhereToSearch());
            if (canSeePlayer())
            {
                curState = basicStates.chase;
                base.setAlert(false);
            }
            if (Vector2.Distance(base.getWhereToSearch(), this.transform.position) < 1)
            {
                base.alertTimer += 1 * Time.deltaTime;
                if (alertTimer >= base.alertWait)
                {
                    base.alertTimer = 0;
                    base.setAlert(false);
                    curState = basicStates.patrol;
                }
            }
            //Attack state goes here! [Attack will no longer be its own state]
            //moveAI(playerPosition);
        }
    }
    // Update is called once per frame
    void Update()
    {
        GameObject player = GameObject.Find("Player");

        if (Input.GetKey(KeyCode.L) && player.GetComponent <Player_Controls>().getMark() == this.gameObject)
        {
            return;
        }
        Vector3 aimDir = gameObject.transform.GetChild(0).position;

        aimDir.z = transform.position.z;
        RaycastHit2D rayDir = Physics2D.Raycast(transform.position, aimDir, wallcheck);

        curdirction = currentDirection();
        if (base.getAlert())
        {
            curState = basicStates.alerted;
        }
        if (curState == basicStates.guard)
        {
            if (Vector2.Distance(guardLoc.position, this.transform.position) > 1f)
            {
                curState = basicStates.moveBack;
            }
            switch (curdirction)
            {
            case directions.east:

                if (rayDir && rayDir.collider.CompareTag("Wall"))
                {
                    updateFacing(90);
                }
                else
                {
                    if (base.canSeePlayer())
                    {
                        curState = basicStates.chase;
                    }
                }
                break;

            case directions.west:
                if (rayDir && rayDir.collider.CompareTag("Wall"))
                {
                    updateFacing(90);
                }
                else
                {
                    if (base.canSeePlayer())
                    {
                        curState = basicStates.chase;
                    }
                }
                break;

            case directions.north:
                if (rayDir && rayDir.collider.CompareTag("Wall"))
                {
                    updateFacing(90);
                }
                else
                {
                    if (base.canSeePlayer())
                    {
                        curState = basicStates.chase;
                    }
                }
                break;

            case directions.south:
                if (rayDir && rayDir.collider.CompareTag("Wall"))
                {
                    updateFacing(90);
                }
                else
                {
                    if (base.canSeePlayer())
                    {
                        curState = basicStates.chase;
                    }
                }
                break;
            }
            if (timer % 50 == 0)
            {
                updateFacing(90);
            }
        }
        else if (curState == basicStates.chase)
        {
            base.moveAI(playerPosition);
            if (!canSeePlayer())
            {
                curState = basicStates.guard;
            }
            base.attack();
        }
        else if (curState == basicStates.moveBack)
        {
            moveAI(guardLoc);
            if (Vector2.Distance(guardLoc.position, this.transform.position) < 1f)
            {
                curState = basicStates.guard;
                //Snap back into cardinal directions here
            }
        }
        else if (curState == basicStates.alerted)
        {
            moveAI(base.getWhereToSearch());
            if (canSeePlayer())
            {
                curState = basicStates.chase;
                base.setAlert(false);
            }
            if (Vector2.Distance(base.getWhereToSearch(), this.transform.position) < 1f)
            {
                base.alertTimer += 1 * Time.deltaTime;
                if (alertTimer >= base.alertWait)
                {
                    base.alertTimer = 0;
                    base.setAlert(false);
                    curState = basicStates.moveBack;
                }
            }
        }
        //Attack State goes here! (if close to player attack) [No longer its own state]
    }