public void UpdateTargetsList() { //update bolts for each target list, fill up for missing int boltsMissing = targets.Count - allBolts.Count; if (boltsMissing > 0) { for (int j = 0; j < boltsMissing; j++) { GameObject newBolt = (GameObject)Instantiate(boltPrefab, transform.position, transform.rotation); newBolt.name = "BoltPart" + j; newBolt.transform.parent = transform; allBolts.Add(newBolt.GetComponent <X_LB_Bolt> ()); newBolt.GetComponent <X_LB_Bolt>().Initialize(this); } } //clear segments from redundant bolts if (boltsMissing < 0) { for (int e = allBolts.Count - 1; e > targets.Count - 1; e--) { X_LB_Bolt current = (X_LB_Bolt)allBolts[e]; current.ClearSegments(); } } }
public void StrikeAction() { //first: update targets //it's a realtime parameter! UpdateTargetsList(); //The action striking = true; if (lightSize > 0) { mainLight.enabled = true; mainLight.color = color; mainLight.range = lightSize; } else { mainLight.enabled = false; } if (targets.Count == 0) { return; } startingPoint = transform; for (int h = 0; h < targets.Count; h++) { //create one bolt with several segments from one target to another currentTarget = targets[h]; currentBolt = (X_LB_Bolt)allBolts[h]; if (currentTarget == null) { currentBolt.ClearSegments(); continue; } //Call method on impact, that's what a lightning is for if (callMethodOnImpactBool[h] == true) { targets[h].gameObject.SendMessage(callMethodOnImpact[h], SendMessageOptions.DontRequireReceiver); } //get number of segments for bolt part distanceToTarget = Calculator.X_LB_Math.Sqrt2((startingPoint.position - currentTarget.position).sqrMagnitude); numberOfSegmentsFloat = distanceToTarget / segmentSize; //catch no segment case if (numberOfSegmentsFloat < minNumberOfSegments) { numberOfSegmentsFloat = minNumberOfSegments; } numberOfSegments = (int)Mathf.Round(numberOfSegmentsFloat); numberOfSegmentsActive = currentBolt.allSegmentsInBolt.Count; numberOfSegmentsMissing = numberOfSegments - numberOfSegmentsActive; if (numberOfSegmentsMissing > 0) { for (int i = 0; i < numberOfSegmentsMissing; i++) { //too little segments active, put some into the bolt if (allSegmentsInPool.Count > 0) { currentSegmentOutOfPool = (X_LB_Segment)allSegmentsInPool [0]; currentSegmentOutOfPool.FromPoolInBolt(currentBolt); } } } if (numberOfSegmentsMissing < 0) { for (int j = 0; j < -numberOfSegmentsMissing; j++) { //too many segments active, put them back in pool currentSegmentOutOfBolt = (X_LB_Segment)currentBolt.allSegmentsInBolt [0]; currentSegmentOutOfBolt.FromBoltInPool(currentBolt); } } //before positioning the segments: set an array of chain points for this bolt and flickering offsets chainPoints = new Vector3[currentBolt.allSegmentsInBolt.Count + 1]; // +1 because is has 5 chain points per bolt with 4 segments for (int e = 0; e < chainPoints.Length; e++) { segmentCount = currentBolt.allSegmentsInBolt.Count; chainSpace = (currentTarget.position - startingPoint.position) / segmentCount; chainPoint = startingPoint.position + e * chainSpace; chainPoints[e] = chainPoint; } if ((flickeringOffsets.Length == 0 && freezeShape == true) || freezeShape == false) { flickeringOffsets = new Vector3[currentBolt.allSegmentsInBolt.Count + 1]; for (int f = 0; f < flickeringOffsets.Length; f++) { flickerOffset = GetFlickerOffset(); if (f == 0 || f == chainPoints.Length - 1) { flickerOffset = Vector3.zero; } flickeringOffsets[f] = flickerOffset; } } //now positioning all segments of one bolt regarding the chain points for (int k = 0; k < currentBolt.allSegmentsInBolt.Count; k++) { currentSegment = (X_LB_Segment)currentBolt.allSegmentsInBolt [k]; //position start currentSegment.SetChainPositioner(chainPoints[k] + flickeringOffsets[k]); //position end currentSegment.SetEndingPoint(chainPoints[k + 1] + flickeringOffsets[k + 1]); currentSegment.target = currentTarget; currentSegment.point2.localScale = Vector3.one; } //end point is starting point for next bolt/target startingPoint = currentTarget; } //end cap of the whole lightning if (allBolts.Count > 0) { lastBoltForEndCap = (X_LB_Bolt)allBolts [allBolts.Count - 1]; if (lastBoltForEndCap.allSegmentsInBolt.Count > 0) { lastSegmentForEndCap = (X_LB_Segment)lastBoltForEndCap.allSegmentsInBolt [lastBoltForEndCap.allSegmentsInBolt.Count - 1]; lastSegmentForEndCap.point2.transform.localScale = new Vector3(.5f, .5f, .5f); } } }