using System.Collections.Generic; using UnityEngine; namespace Artngame.SKYMASTER { public class ChainLightning_SKYMASTER : MonoBehaviour { public Transform target; private GameObject[] target1; public int zigs = 100; public float speed = 1f; public float scale = 1f; public Light startLight; public Light endLight; private PerlinSKYMASTER noise; public bool Energized; public bool is_parent; public int current_depth; public int max_depth; private int current_target_count; public int max_target_count = 3; private Transform this_transform; public bool Random_target; public float Affect_dist = 10f; private float Time_count; public float Change_target_delay = 0.5f; public bool Random_delay; public float Delay_offset = 1f; private float Target_delay; public bool Moving_Source; public float Particle_energy = 1f; public int optimize_factor = 5; public float ParticleSize = 5f; public bool Branches = true; public Vector3 Branch_Offset; public Vector3 offset_bias; private Vector3 Target_pos; private Vector3 Saved_start_pos; public float Downward_speed = 4f; public float Stop_dist = 1f; private bool cast_branches; public Color Branch_color = Color.white; [Range(1f, 6f)] public float Zigs_branching_divider = 2f; public bool offset_noise; public bool reset_noise; public bool Slow_on_end; public float Slow_down_height = 20f; public float Slow_divider = 40f; public Vector2 Light_delay = new Vector2(0f, 0.3f); private bool particle_on = true; public bool line_on; private LineRenderer Liner; private float stop_time; public Vector2 Line_delay = new Vector2(0.1f, 0.4f); public int Vertex_count = 30; public float Deviation = 10.2f; public Vector2 Line_width = new Vector2(10f, 10f); public float min_y_disp = 10f; public bool line_glow_on; private LineRenderer LinerGlow; public Color Glow_color = Color.blue; public Material Line_Lightning_mat; public List LineBranches = new List(); public List LineBranchesGlow = new List(); public int max_branches = 10; public bool SmoothLights; public float max_end_light_intensity = 1f; public float max_start_light_intensity = 8f; public float LightFadeSpeed = 5f; public float LightSpeed = 100f; public float LightRipple = 3f; public bool updateOnPositionchange; public Vector3 prev_pos; public int randomizeFactor = 30; public bool alignEndToStartLight; private void Start() { if (zigs == 0) { zigs = 1; } target1 = GameObject.FindGameObjectsWithTag("Conductor"); if ((bool)endLight) { endLight.enabled = false; endLight.gameObject.SetActive(value: false); } if ((bool)startLight) { startLight.enabled = false; startLight.gameObject.SetActive(value: false); } Target_pos = base.transform.position; Saved_start_pos = Target_pos; this_transform = base.transform; Target_delay = Change_target_delay; if (Random_delay) { Target_delay = Random.Range(Change_target_delay - Delay_offset, Change_target_delay + Delay_offset); } } private void Update() { if (((target == null) & (Random.Range(0, 2) == 1)) || (updateOnPositionchange && prev_pos == this_transform.position && Random.Range(0, randomizeFactor) != 1)) { return; } prev_pos = this_transform.position; if (line_on) { if (Liner == null) { Liner = GetComponent(); if (Liner == null) { base.gameObject.AddComponent(); Liner = GetComponent(); Liner.startWidth = 20f; Liner.endWidth = 10f; Liner.startColor = Color.white; Liner.endColor = Color.white; Liner.material = Line_Lightning_mat; } } if (line_glow_on && LinerGlow == null) { GameObject gameObject = new GameObject(); gameObject.name = "GlowHolder"; gameObject.transform.parent = base.transform; gameObject.transform.localPosition = Vector3.zero; LinerGlow = gameObject.AddComponent(); LinerGlow.startWidth = 17f; LinerGlow.endWidth = 30f; LinerGlow.startColor = Color.black; LinerGlow.endColor = Glow_color; LinerGlow.material = Line_Lightning_mat; } if (Branches) { if (LineBranches.Count < max_branches) { for (int i = 0; i < max_branches; i++) { GameObject obj = new GameObject(); obj.name = "Lineholder" + i; obj.transform.parent = base.transform; obj.transform.localPosition = Vector3.zero; LineRenderer lineRenderer = obj.AddComponent(); lineRenderer.startWidth = 5f; lineRenderer.endWidth = 10f; lineRenderer.startColor = Color.white; lineRenderer.endColor = Color.white; lineRenderer.material = Line_Lightning_mat; LineBranches.Add(lineRenderer); } } if (line_glow_on && LineBranchesGlow.Count < max_branches) { for (int j = 0; j < max_branches; j++) { GameObject obj2 = new GameObject(); obj2.name = "Glowholder" + j; obj2.transform.parent = base.transform; obj2.transform.localPosition = Vector3.zero; LineRenderer lineRenderer2 = obj2.AddComponent(); lineRenderer2.startWidth = 9f; lineRenderer2.endWidth = 20f; lineRenderer2.startColor = Color.black; lineRenderer2.endColor = Glow_color; lineRenderer2.material = Line_Lightning_mat; LineBranchesGlow.Add(lineRenderer2); } } } } if (!particle_on) { return; } if (noise == null) { noise = new PerlinSKYMASTER(); } if (!(is_parent | (!is_parent & Energized & (current_depth < max_depth)))) { return; } target1 = GameObject.FindGameObjectsWithTag("Conductor"); if (target1 == null || target1.Length == 0) { return; } int num = Random.Range(0, target1.Length); if (Random_target) { if (Time.fixedTime - Time_count > Target_delay) { if (Vector3.Distance(target1[num].transform.position, base.transform.position) < Affect_dist) { target = target1[num].transform; Saved_start_pos = this_transform.position; } Time_count = Time.fixedTime; if (!is_parent & Energized & (current_depth < max_depth) & (current_target_count > max_target_count)) { Energized = false; } current_target_count++; } if (target != null && Vector3.Distance(target.position, base.transform.position) > Affect_dist) { target = null; } } else { target = null; int num2 = 0; GameObject[] array = target1; foreach (GameObject gameObject2 in array) { if (Vector3.Distance(gameObject2.transform.position, base.transform.position) < Affect_dist) { target = gameObject2.transform; } num2++; } if (!is_parent & Energized & (current_depth < max_depth) & (current_target_count > max_target_count)) { Energized = false; } current_target_count++; } float num3 = Time.time * speed * 0.1365143f; if (target != null) { float num4 = Vector3.Distance(Target_pos, target.position); if (num4 < Vector3.Distance(base.transform.position, target.position) / 2f && Branches) { cast_branches = true; } if ((Target_pos == target.position) | (num4 < Stop_dist)) { target = null; Target_pos = this_transform.position; cast_branches = false; } } if (target != null) { Vector3 vector = offset_bias; if (offset_noise) { if (reset_noise) { vector.x = offset_bias.x + Mathf.Clamp(0.1f * noise.Noise(num3 + offset_bias.x), -0.15f, 0.15f); vector.x = Mathf.Clamp(offset_bias.x, -10.25f, 10.25f); } else { vector.x = offset_bias.x + Mathf.Clamp(0.1f * noise.Noise(num3 + offset_bias.x), -0.15f, 0.15f); vector.x = Mathf.Clamp(offset_bias.x, -0.25f, 0.25f); offset_bias = vector; } } float num5 = Downward_speed; if (Slow_on_end && Vector3.Distance(Target_pos, target.position) < Stop_dist + Slow_down_height) { num5 = Downward_speed / Slow_divider; } if (Moving_Source) { Target_pos = Vector3.Lerp(this_transform.position, target.position, num5 * Time.deltaTime); } else { Target_pos += num5 * (target.position - Target_pos).normalized; } if (line_on && Liner != null) { if (LinerGlow != null) { LinerGlow.enabled = true; } Liner.enabled = true; if (Branches) { for (int l = 0; l < LineBranches.Count; l++) { LineBranches[l].enabled = true; if (line_glow_on) { LineBranchesGlow[l].enabled = true; } } } int num6 = 30; float num7 = 10.2f; if (line_glow_on) { LinerGlow.positionCount = num6; LinerGlow.startWidth = 17f; LinerGlow.endWidth = 11f; LinerGlow.startColor = Color.black; LinerGlow.endColor = Glow_color; } Liner.startWidth = Line_width.x; Liner.endWidth = Line_width.y; List list = new List(); List list2 = new List(); if (Branches) { Liner.positionCount = num6; } else { Liner.positionCount = num6; } Vector3 item = Saved_start_pos; Vector3 zero = Vector3.zero; Vector3 vector2 = (target.position - this_transform.position) / num6; Liner.SetPosition(0, this_transform.position); for (int m = 0; m < num6; m++) { zero.x = Random.Range(item.x - num7 + vector2.x, item.x + num7 + vector2.x); zero.y = item.y + vector2.y; zero.z = Random.Range(item.z - num7 + vector2.z, item.z + num7 + vector2.z); if (Mathf.Abs(zero.y - item.y) < min_y_disp) { zero.y -= min_y_disp; } Liner.SetPosition(m, zero); if (line_glow_on) { LinerGlow.SetPosition(m, zero); } item = zero; if (((m / 5 - Mathf.RoundToInt(m / 5) == 0) & (m > num6 / 3)) && list.Count < max_branches / 3) { list.Add(item); } } Liner.SetPosition(num6 - 1, new Vector3(zero.x, target.position.y, zero.z)); if (Branches) { for (int n = 0; n < list.Count; n++) { Vector3 item2 = list[n]; Vector3 zero2 = Vector3.zero; LineBranches[n].positionCount = num6; LineBranches[n].startWidth = 5f; LineBranches[n].endWidth = 1f; LineBranches[n].SetPosition(0, list[n]); if (line_glow_on) { LineBranchesGlow[n].positionCount = num6; LineBranchesGlow[n].startWidth = 10f; LineBranchesGlow[n].endWidth = 5f; LineBranchesGlow[n].SetPosition(0, list[n]); } for (int num8 = 0; num8 < num6; num8++) { zero2.x = Random.Range(item2.x - num7 * 1f + vector2.x, item2.x + num7 * 2f + vector2.x); zero2.y = item2.y + vector2.y / (float)Random.Range(1, 2); zero2.z = Random.Range(item2.z - num7 * 1f + vector2.z, item2.z + num7 * 2f + vector2.z); LineBranches[n].SetPosition(num8, zero2); if (line_glow_on) { LineBranchesGlow[n].SetPosition(num8, zero2); } item2 = zero2; if (((num8 / 15 - Mathf.RoundToInt(num8 / 15) == 0) & (num8 > num6 / 3)) && list2.Count < max_branches / 3) { list2.Add(item2); } } } int count = list.Count; for (int num9 = 0; num9 < list2.Count; num9++) { Vector3 vector3 = list2[num9]; Vector3 zero3 = Vector3.zero; LineBranches[num9 + count].positionCount = num6; LineBranches[num9 + count].startWidth = 3f; LineBranches[num9 + count].endWidth = 1f; LineBranches[num9 + count].SetPosition(0, list2[num9]); LineBranches[num9 + count].startColor = Color.white; LineBranches[num9 + count].endColor = new Color(0f, 0f, 0f, 0f); if (line_glow_on) { LineBranchesGlow[num9 + count].positionCount = num6; LineBranchesGlow[num9 + count].startWidth = 6f; LineBranchesGlow[num9 + count].endWidth = 4f; LineBranchesGlow[num9 + count].SetPosition(0, list2[num9]); LineBranchesGlow[num9 + count].startColor = Glow_color; LineBranchesGlow[num9 + count].endColor = new Color(0f, 0f, 0f, 0f); } for (int num10 = 0; num10 < num6; num10++) { zero3.x = (float)Random.Range(11, 12) + Random.Range(vector3.x - num7 * 1f + vector2.x, vector3.x + num7 * 2f + vector2.x); zero3.y = vector3.y + vector2.y / (float)Random.Range(1, 2); zero3.z = Random.Range(vector3.z - num7 * 1f + vector2.z, vector3.z + num7 * 2f + vector2.z); LineBranches[num9 + count].SetPosition(num10, zero3); if (line_glow_on) { LineBranchesGlow[num9 + count].SetPosition(num10, zero3); } vector3 = zero3; } } } } stop_time = Time.fixedTime; _ = Branches; if (cast_branches) { if (Random.Range(0, 2) != 1) { } } else { _ = Branches; } ChainLightning_SKYMASTER chainLightning_SKYMASTER = target.gameObject.GetComponent(typeof(ChainLightning_SKYMASTER)) as ChainLightning_SKYMASTER; if (chainLightning_SKYMASTER == null) { chainLightning_SKYMASTER = target.gameObject.GetComponentInChildren(typeof(ChainLightning_SKYMASTER)) as ChainLightning_SKYMASTER; } if (chainLightning_SKYMASTER != null && !chainLightning_SKYMASTER.is_parent) { chainLightning_SKYMASTER.Energized = true; chainLightning_SKYMASTER.max_depth = max_depth; chainLightning_SKYMASTER.current_depth = current_depth + 1; } int num11 = 1; num11 = Random.Range(1, optimize_factor); if ((bool)endLight) { if ((num11 == 1) & (target != null)) { if (!SmoothLights) { endLight.enabled = true; endLight.gameObject.SetActive(value: true); if (alignEndToStartLight) { endLight.transform.position = new Vector3(Saved_start_pos.x, target.transform.position.y, Saved_start_pos.z); } else { endLight.transform.position = target.transform.position; } } else { endLight.enabled = true; endLight.gameObject.SetActive(value: true); endLight.intensity = Mathf.Lerp(endLight.intensity, max_end_light_intensity, Time.deltaTime * LightSpeed); if (alignEndToStartLight) { endLight.transform.position = new Vector3(Saved_start_pos.x, target.transform.position.y, Saved_start_pos.z); } else { endLight.transform.position = target.transform.position; } } } else if (!SmoothLights) { endLight.enabled = false; } else { endLight.intensity = Mathf.Lerp(endLight.intensity, 0f, Time.deltaTime * LightFadeSpeed * Random.Range(1f, LightRipple)); } } if ((bool)startLight) { if ((num11 == 1) & (target != null)) { if (!SmoothLights) { startLight.enabled = true; startLight.gameObject.SetActive(value: true); startLight.transform.position = Saved_start_pos; } else { startLight.enabled = true; startLight.gameObject.SetActive(value: true); startLight.intensity = Mathf.Lerp(startLight.intensity, max_start_light_intensity, Time.deltaTime * LightSpeed); startLight.transform.position = Saved_start_pos; } } else if (!SmoothLights) { startLight.enabled = false; } else { startLight.intensity = Mathf.Lerp(startLight.intensity, 0f, Time.deltaTime * LightFadeSpeed * Random.Range(1f, LightRipple)); } } } else if (line_on && Liner != null && Time.fixedTime - stop_time > Random.Range(Line_delay.x, Line_delay.y)) { Liner.startWidth = 0f; Liner.endWidth = 0f; if (line_glow_on) { LinerGlow.startWidth = 0f; LinerGlow.endWidth = 0f; LinerGlow.enabled = false; } Liner.enabled = false; if (Branches) { for (int num12 = 0; num12 < LineBranches.Count; num12++) { LineBranches[num12].startWidth = 0f; LineBranches[num12].endWidth = 0f; if (line_glow_on) { LineBranchesGlow[num12].startWidth = 0f; LineBranchesGlow[num12].endWidth = 0f; LineBranchesGlow[num12].enabled = false; } LineBranches[num12].enabled = false; } } } if ((bool)startLight & (target == null) & (Time.fixedTime - stop_time > Random.Range(Light_delay.x, Light_delay.y))) { if (!SmoothLights) { startLight.enabled = false; startLight.gameObject.SetActive(value: false); } else { startLight.intensity = Mathf.Lerp(startLight.intensity, 0f, Time.deltaTime * LightFadeSpeed * Random.Range(1f, LightRipple)); } } if ((bool)endLight & (target == null)) { if (!SmoothLights) { endLight.enabled = false; endLight.gameObject.SetActive(value: false); } else { endLight.intensity = Mathf.Lerp(endLight.intensity, 0f, Time.deltaTime * LightFadeSpeed * Random.Range(1f, LightRipple)); } } } } }