/// <summary> /// Return the closest object in the array. /// </summary> /// <returns></returns> public JCS_DetectAreaObject FindTheClosest() { int closestIndex = -1; float closestDistance = -1.0f; bool theFirstAssign = true; for (int index = 0; index < mDetectedObjects.length; ++index) { JCS_DetectAreaObject obj = mDetectedObjects.at(index); if (mDetectedObjects.at(index) == null) { // remove from the list, // the object could be dead for some reason. mDetectedObjects.slice(index); return(null); } // check to see if the object is live object. JCS_2DLiveObject liveObj = obj.GetComponent <JCS_2DLiveObject>(); if (liveObj != null) { // cannot target object that cannot be damage. if (!liveObj.CanDamage) { continue; } } Vector3 objectPos = obj.transform.position; Vector3 areaPos = this.transform.position; float distance = Vector3.Distance(objectPos, areaPos); // assign the first value! if (theFirstAssign) { closestDistance = distance; closestIndex = index; theFirstAssign = false; continue; } if (distance < closestDistance) { closestDistance = distance; closestIndex = index; } } // nothing found or nothing in the list(nothing detected)! if (theFirstAssign) { return(null); } // return result return(mDetectedObjects.at(closestIndex)); }
//======================================== // Self-Define //------------------------------ //---------------------- // Public Functions /// <summary> /// Shoot bullets multiple times in times. (not in per frame) /// </summary> public void Shoots(int hit, Vector3 pos) { if (mShootAction.Bullet == null) { JCS_Debug.LogReminders( "There is no bullet assign to \"JCS_ShootAction\", so we cannot shoot a sequence..."); return; } if (hit <= 0) { JCS_Debug.LogReminders( "Cannot shoot sequence of bullet with lower than 0 hit..."); return; } // after finding once is still null, // try it agian! if (mDetectedObject == null) { // find the target switch (mShootAction.GetTrackType()) { case JCS_ShootAction.TrackType.CLOSEST: mDetectedObject = mShootAction.GetDetectAreaAction().FindTheClosest(); break; case JCS_ShootAction.TrackType.FURTHEST: mDetectedObject = mShootAction.GetDetectAreaAction().FindTheFurthest(); break; } } // does not found the target to damage if (mDetectedObject == null) { mTargetsPerSequence.push(null); } else { // found target to damage, add in to data segment mTargetsPerSequence.push(mDetectedObject.transform); JCS_2DLiveObject liveObj = mDetectedObject.GetComponent <JCS_2DLiveObject>(); if (liveObj != null) { int defenseVal = 0; // get the targeting defense value if (liveObj.AbilityFormat != null) { defenseVal = liveObj.AbilityFormat.GetDefenseValue(); } // calculate the damage we are going to apply to // the target object. mDamageApplying = PreCalculateSequenceDamage( mAbilityFormat.GetMinDamage(), mAbilityFormat.GetMaxDamage(), hit, defenseVal); // pre calculate the damage before the // actual bullet hit the object, // so it could decide what object are dead already. // and other object or this object wont target the // object is going die. liveObj.ReceivePreCalDamage(mDamageApplying); // start targeting object to hit liveObj.BeenTarget = true; } } // thread itself mThread.push(mThread.length); // needed data mTimers.push(0); // timer to calculate between each shoot. mShootCount.push(hit); // hit per sequence. mShootCounter.push(0); // counter to count how many shoot left? mShootPos.push(pos); // position to spawn the bullet implements the position stay effect! bool isLeft = true; if (this.transform.localScale.x < 0) { isLeft = false; } // shoot direction mShootDirection.push(isLeft); // decide which direction should the bullet goes? (right/left) }