コード例 #1
0
 void Start()
 {
     if (npcBase == null)
     {
         npcBase = gameObject.GetComponent <NPC_Enemy> ();
     }
     StartSensor();
 }
コード例 #2
0
    public void CheckForIntercepts()
    {
        //Description: If arm is available, this method checks for (and identifies) any intercepts with enemies

        //Validations & Initializations:
        if (!equipped)
        {
            return;                                           //Do not check for intercepts if arm is not currently equipped
        }
        List <Collider2D> overlaps = new List <Collider2D>(); //Initialize list to store overlapping colliders
        ContactFilter2D   filter   = new ContactFilter2D();   //Initialize contact filter for intercept collider

        //Set Collider Filter:
        filter.useDepth    = false;                                   //Set filter to ignore depth
        filter.useTriggers = true;                                    //Set filter to use trigger colliders
        filter.SetLayerMask(LayerMask.GetMask("InterceptionFields")); //Set filter to mask out all colliders other than interception fields

        //Check Current Overlaps:
        reachField.OverlapCollider(filter, overlaps); //Populate list of current interception field overlaps

        //Compare With Known Intercepts:
        for (int x = 0; x < intercepts.Count; x++) //Iterate through list of known intercepts...
        {
            //Initializations & Validations:
            Intercept  thisIntercept     = intercepts[x];          //Get shorthand for this intercept
            Collider2D interceptCollider = thisIntercept.collider; //Get known collider from intercept
            bool       foundCollider     = false;                  //Initialize marker to track whether or not intercept could find its collider

            //Check for Continuity:
            for (int y = 0; y < overlaps.Count; y++)  //Iterate through list of found colliders...
            {
                if (overlaps[y] == interceptCollider) //If collider matches that of this intercept...
                {
                    foundCollider = true;             //Indicate that this intercept continues to be valid
                    overlaps.RemoveAt(y);             //Remove collider from list of potential new intercepts
                    break;                            //Break loop and continue to next task
                }
            }

            //Update or Remove Intercept:
            if (foundCollider)                  //UPDATE intercept, as collider was found successfully:
            {
                UpdateIntercept(thisIntercept); //Update intercept data
            }
            else //REMOVE intercept, as collider can no longer be found:
            {
                thisIntercept.enemy.intercepted = false; //Indicate that enemy is no longer intercepted
                player.intercepts.Remove(thisIntercept); //Remove this intercept from player list
                intercepts.RemoveAt(x);                  //Remove this intercept from this arm's list
            }
        }

        //Create New Intercepts:
        for (int x = 0; x < overlaps.Count; x++) //For each unclaimed overlap...
        {
            //Initializations & Validations:
            NPC_Enemy interceptedEnemy = overlaps[x].GetComponentInParent <NPC_Enemy>(); //Get enemy controller from overlap collider
            if (interceptedEnemy == null)
            {
                return;                               //Skip function if target does not have enemy controller
            }
            if (interceptedEnemy.intercepted)
            {
                return;                               //Skip function if target has already been intercepted by other arm
            }
            //Generate Intercept Core:
            Intercept newIntercept = new Intercept(); //Initialize new intercept
            newIntercept.enemy    = interceptedEnemy; //Set intercept enemy
            newIntercept.collider = overlaps[x];      //Set intercept collider
            newIntercept.arm      = this;             //Set intercept arm
            //Get Intercept Data:
            UpdateIntercept(newIntercept);            //Now that intercept has necessary core data, run an initial update
            //Intercept Generation Cleanup:
            intercepts.Add(newIntercept);             //Add new intercept to this arm's list of interceptions
            player.intercepts.Add(newIntercept);      //Add new intercept to player's list of interceptions
            interceptedEnemy.intercepted = true;      //Indicate to enemycontroller that it has been intercepted
        }

        //..Xx Sub-Methods xX...............................................................................
        void UpdateIntercept(Intercept intercept)
        {
            //Description: Updates intercept data based on relationship between player and intercepted enemy

            //Initializations & Validations:
            Vector2 enemyVel = intercept.enemy.velocity; //Get enemy velocity vector
            Vector2 armVel   = localVelocity;            //Get arm velocity vector

            //Find Intercept Vector:
            intercept.vector = -(enemyVel + armVel);                                        //Add velocity vectors to get vector of interception
            Vector2 correctedVector = intercept.vector.Rotate(player.transform.rotation.z); //Get interception vector relative to absolute rotation of player


            //Cleanup:
            intercept.duration += Time.fixedDeltaTime; //Update duration (CheckIntercepts should be run during FixedUpdate)

            //..Xx Debuggers xX.................................................................................
            //Debug.DrawLine(grabPoint.position, grabPoint.position + interceptVector.V3());
        }
    }