296 lines
7.0 KiB
C#
296 lines
7.0 KiB
C#
using UnityEngine;
|
|
|
|
namespace Artngame.SKYMASTER
|
|
{
|
|
[ExecuteInEditMode]
|
|
public class ParticleSheetOnGroundSKYMASTER : MonoBehaviour
|
|
{
|
|
private Transform Main_cam_transform;
|
|
|
|
public Vector3 start_pos;
|
|
|
|
public int particle_count = 100;
|
|
|
|
private Transform Cash_transform;
|
|
|
|
private int particle_count_init;
|
|
|
|
private bool got_positions;
|
|
|
|
private Vector3[] positions;
|
|
|
|
private int[] tile;
|
|
|
|
private ParticleSystem.Particle[] aaa;
|
|
|
|
public ParticleSystem nbb;
|
|
|
|
public bool letloose;
|
|
|
|
private bool let_loose;
|
|
|
|
private int place_start_pos;
|
|
|
|
public bool Gravity_Mode;
|
|
|
|
public int Tiles_X = 4;
|
|
|
|
public int Tiles_Y = 4;
|
|
|
|
private int KTiles_X = 4;
|
|
|
|
private int KTiles_Y = 4;
|
|
|
|
public float Y_offset;
|
|
|
|
public bool conform_to_terrain;
|
|
|
|
public float Grav_speed = 0.05f;
|
|
|
|
public float ext_rot = 90f;
|
|
|
|
public bool Use_formation;
|
|
|
|
public bool Use_mesh;
|
|
|
|
public bool Use_explicit;
|
|
|
|
public Vector3 Explicit_Axis = new Vector3(0f, 0f, 0f);
|
|
|
|
public bool keep_alive;
|
|
|
|
public bool auto_rot_mesh;
|
|
|
|
public bool use_cut_off;
|
|
|
|
public float cut_off_height = 20f;
|
|
|
|
private void Start()
|
|
{
|
|
nbb = base.gameObject.GetComponent("ParticleSystem") as ParticleSystem;
|
|
if (nbb != null)
|
|
{
|
|
nbb.Clear();
|
|
nbb.Emit(particle_count);
|
|
aaa = new ParticleSystem.Particle[nbb.particleCount];
|
|
}
|
|
start_pos = base.transform.position;
|
|
Cash_transform = base.transform;
|
|
KTiles_X = Tiles_X;
|
|
KTiles_Y = Tiles_Y;
|
|
got_positions = false;
|
|
Main_cam_transform = Camera.main.transform;
|
|
if (Application.isPlaying)
|
|
{
|
|
particle_count_init = particle_count;
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
nbb = base.gameObject.GetComponent("ParticleSystem") as ParticleSystem;
|
|
if (nbb == null)
|
|
{
|
|
Debug.Log("Please attach the script to a particle system");
|
|
}
|
|
if (nbb != null)
|
|
{
|
|
nbb.Clear();
|
|
nbb.Emit(particle_count);
|
|
aaa = new ParticleSystem.Particle[nbb.particleCount];
|
|
}
|
|
start_pos = base.transform.position;
|
|
Cash_transform = base.transform;
|
|
got_positions = false;
|
|
KTiles_X = Tiles_X;
|
|
KTiles_Y = Tiles_Y;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (nbb == null)
|
|
{
|
|
nbb = base.gameObject.GetComponent("ParticleSystem") as ParticleSystem;
|
|
return;
|
|
}
|
|
Main_cam_transform = Camera.main.transform;
|
|
if ((aaa.Length != particle_count) | (KTiles_X != Tiles_X) | (KTiles_Y != Tiles_Y))
|
|
{
|
|
if (nbb != null)
|
|
{
|
|
nbb.Clear();
|
|
nbb.Emit(particle_count);
|
|
aaa = new ParticleSystem.Particle[nbb.particleCount];
|
|
}
|
|
KTiles_X = Tiles_X;
|
|
KTiles_Y = Tiles_Y;
|
|
got_positions = false;
|
|
}
|
|
ParticleSystem.MainModule main = nbb.main;
|
|
if ((main.maxParticles < particle_count_init) & Application.isPlaying)
|
|
{
|
|
particle_count = main.maxParticles;
|
|
}
|
|
else if ((main.maxParticles > particle_count_init) & Application.isPlaying)
|
|
{
|
|
particle_count = particle_count_init;
|
|
}
|
|
let_loose = letloose;
|
|
if (!Application.isPlaying)
|
|
{
|
|
if (positions == null)
|
|
{
|
|
positions = new Vector3[particle_count];
|
|
got_positions = false;
|
|
}
|
|
else if (positions.Length != 0 && positions[0] == Vector3.zero)
|
|
{
|
|
got_positions = false;
|
|
}
|
|
let_loose = false;
|
|
aaa = new ParticleSystem.Particle[nbb.particleCount];
|
|
}
|
|
if (!let_loose)
|
|
{
|
|
nbb.Clear();
|
|
}
|
|
nbb.Emit(particle_count);
|
|
int num = Tiles_X * Tiles_Y - 1;
|
|
nbb.GetParticles(aaa);
|
|
if (!got_positions)
|
|
{
|
|
positions = new Vector3[aaa.Length];
|
|
tile = new int[aaa.Length];
|
|
got_positions = true;
|
|
for (int i = 0; i < aaa.Length; i++)
|
|
{
|
|
positions[i] = aaa[i].position;
|
|
tile[i] = Random.Range(0, 15);
|
|
}
|
|
}
|
|
Vector3 up = Camera.main.transform.up;
|
|
Vector3 right = Camera.main.transform.right;
|
|
int num2 = 0;
|
|
for (int j = 0; j < aaa.Length; j++)
|
|
{
|
|
if (j >= tile.Length)
|
|
{
|
|
continue;
|
|
}
|
|
if (keep_alive)
|
|
{
|
|
aaa[j].remainingLifetime = num + 1 - tile[j];
|
|
aaa[j].startLifetime = num;
|
|
}
|
|
if (!let_loose)
|
|
{
|
|
aaa[j].position = positions[j];
|
|
}
|
|
float x = aaa[j].position.x;
|
|
float z = aaa[j].position.z;
|
|
float y_offset = Y_offset;
|
|
Vector3 vector = Cash_transform.position - start_pos;
|
|
float num3 = Cash_transform.position.y;
|
|
if (Terrain.activeTerrain != null)
|
|
{
|
|
num3 = Terrain.activeTerrain.SampleHeight(new Vector3(x, 0f, z)) + y_offset + Terrain.activeTerrain.transform.position.y;
|
|
}
|
|
Vector3 position = new Vector3(x, num3, z) + new Vector3(vector.x, 0f, vector.z);
|
|
if (!let_loose | (place_start_pos < 1))
|
|
{
|
|
aaa[j].position = position;
|
|
}
|
|
if (let_loose & Gravity_Mode)
|
|
{
|
|
aaa[j].position = Vector3.Slerp(aaa[j].position, new Vector3(aaa[j].position.x, num3, aaa[j].position.z), Grav_speed * Time.deltaTime);
|
|
}
|
|
if (conform_to_terrain)
|
|
{
|
|
if (use_cut_off)
|
|
{
|
|
if (!(num3 > cut_off_height))
|
|
{
|
|
aaa[j].position = new Vector3(aaa[j].position.x, num3, aaa[j].position.z);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
aaa[j].position = new Vector3(aaa[j].position.x, num3, aaa[j].position.z);
|
|
}
|
|
aaa[j].angularVelocity = 0f;
|
|
if (Use_mesh)
|
|
{
|
|
aaa[j].axisOfRotation = up;
|
|
aaa[j].rotation = Vector3.Angle(Vector3.right, right);
|
|
if (Use_formation)
|
|
{
|
|
switch (num2)
|
|
{
|
|
case 0:
|
|
aaa[j].axisOfRotation = new Vector3(1 + j, 1f, 1f);
|
|
num2++;
|
|
break;
|
|
case 1:
|
|
aaa[j].axisOfRotation = new Vector3(1f, 1 + j, 1f);
|
|
num2++;
|
|
break;
|
|
case 2:
|
|
aaa[j].axisOfRotation = new Vector3(1f, 1f, 1 + j);
|
|
num2 = 0;
|
|
break;
|
|
}
|
|
aaa[j].rotation = (float)(1 + j) + ext_rot;
|
|
}
|
|
if (Use_explicit)
|
|
{
|
|
aaa[j].axisOfRotation = Explicit_Axis;
|
|
aaa[j].rotation = ext_rot;
|
|
}
|
|
if (auto_rot_mesh)
|
|
{
|
|
Vector3 rhs = -(Main_cam_transform.position - aaa[j].position).normalized;
|
|
float num4 = 90f;
|
|
if ((rhs.z >= 0f) & (rhs.x >= 0f))
|
|
{
|
|
num4 -= Vector3.Angle(new Vector3(1f, 0f, 0f), new Vector3(rhs.x, 0f, rhs.z));
|
|
}
|
|
if ((rhs.z < 0f) & (rhs.x >= 0f))
|
|
{
|
|
num4 += Vector3.Angle(new Vector3(1f, 0f, 0f), new Vector3(rhs.x, 0f, rhs.z));
|
|
}
|
|
if ((rhs.z >= 0f) & (rhs.x < 0f))
|
|
{
|
|
num4 = num4 + Vector3.Angle(new Vector3(1f, 0f, 0f), new Vector3(rhs.x, 0f, rhs.z)) + 180f;
|
|
}
|
|
if ((rhs.z < 0f) & (rhs.x < 0f))
|
|
{
|
|
num4 = num4 - Vector3.Angle(new Vector3(1f, 0f, 0f), new Vector3(rhs.x, 0f, rhs.z)) + 180f;
|
|
}
|
|
float num5 = Vector3.Dot(new Vector3(1E-07f, 1E-07f, 1f), rhs);
|
|
float num6 = 0f;
|
|
if (!(num5 > 1f || num5 < -1f))
|
|
{
|
|
num6 = Mathf.Acos(num5);
|
|
}
|
|
aaa[j].rotation = num6 + num4;
|
|
aaa[j].axisOfRotation = Vector3.Cross(new Vector3(1E-08f, 1E-08f, 1f), rhs);
|
|
}
|
|
}
|
|
aaa[j].velocity = Vector3.zero;
|
|
}
|
|
if (!let_loose)
|
|
{
|
|
aaa[j].angularVelocity = 0f;
|
|
aaa[j].rotation = 0f;
|
|
aaa[j].velocity = Vector3.zero;
|
|
}
|
|
}
|
|
if (place_start_pos < 1)
|
|
{
|
|
place_start_pos++;
|
|
}
|
|
nbb.SetParticles(aaa, aaa.Length);
|
|
}
|
|
}
|
|
}
|