/// <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)); }
private void OnTriggerEnter(Collider other) { // check if anything we target are in there JCS_DetectAreaObject jcsDo = other.GetComponent <JCS_DetectAreaObject>(); if (jcsDo == null) { return; } // if cannot damage if (!jcsDo.GetLiveObject().CanDamage) { return; } if (!JCS_GameSettings.instance.TRIBE_DAMAGE_EACH_OTHER) { // if both player does not need to add in to list. // or if both enemy does not need to add in to list. if (jcsDo.GetLiveObject().IsPlayer == mDetectAreaAction.GetLiveObject().IsPlayer) { return; } } // add it to the list mDetectAreaAction.AddDetectedObject(jcsDo); }
/// <summary> /// If detected target leave the area, /// remove from list. /// </summary> /// <param name="jcsDo"></param> public void RemoveDetectedObject(JCS_DetectAreaObject jcsDo) { if (jcsDo == null) { return; } mDetectedObjects.slice(jcsDo); }
//======================================== // Self-Define //------------------------------ //---------------------- // Public Functions /// <summary> /// If detect a target add it to list. /// </summary> /// <param name="jcsDo"></param> public void AddDetectedObject(JCS_DetectAreaObject jcsDo) { if (jcsDo == null) { return; } mDetectedObjects.push(jcsDo); }
private void OnTriggerExit(Collider other) { // if the target thing left JCS_DetectAreaObject jcsDo = other.GetComponent <JCS_DetectAreaObject>(); if (jcsDo == null) { return; } // remove from the list. mDetectAreaAction.RemoveDetectedObject(jcsDo); }
/// <summary> /// Process/Handle the inputs from the user. /// </summary> private void ProcessInput() { if (!mAction) { if (JCS_Input.GetKey(mShootAction.ShootKeyCode) || JCS_Input.GetMouseByAction(mShootAction.KeyAct, mShootAction.MouseButton)) { // 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; } mAction = true; } } if (mAfterDelay) { mActionTimer += Time.deltaTime; if (mTimeDelayAfterShoot < mActionTimer) { // reset timer mActionTimer = 0; // can do the next shoot mAction = false; // exit delay process mAfterDelay = false; } } if (mAction && !mAfterDelay) { mActionTimer += Time.deltaTime; if (mTimeBeforeShoot < mActionTimer) { if (mShootAction.GetShootCallback() != null) { // do call back mShootAction.GetShootCallback().Invoke(); } // Do shooting effect Shoots(mHit, this.transform.position); // start after delay timer. mAfterDelay = true; // reset timer for "mAfterDelay" Trigger. mActionTimer = 0; } } }
//======================================== // 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) }