Advertisement
Guest User

Untitled

a guest
Mar 1st, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.05 KB | None | 0 0
  1.  
  2. public class BulletManagerExample : MonoBehaviour{
  3.     public GameObject bulletExplosion;
  4.     public Material bulletLine;
  5.     public LayerMask bulletCollisionMask;
  6.     public Transform bulletEmitterTransform;
  7.     public Color bulletTrailColor1;
  8.     public Color bulletTrailColor2;
  9.  
  10.     void Start(){
  11.         BulletManager.Init(bulletCollisionMask, bulletLine, bulletExplosion);
  12.     }
  13.    
  14.     void Update(){
  15.         BulletManager.Shoot(bulletEmitterTransform.position, (bulletEmitterTransform.forward + (Random.onUnitSphere * 0.2f)).normalized, 200f);
  16.         BulletManager.UpdateBulletManager();
  17.     }
  18.  
  19.     void OnPostRender() {
  20.         BulletManager.RenderBulletTrails();
  21.     }
  22.  
  23.     void OnDrawGizmos() {
  24.         Gizmos.color = Color.red;
  25.         Gizmos.DrawCube(bulletEmitterTransform.position, bulletEmitterTransform.localScale);
  26.         Gizmos.DrawLine(bulletEmitterTransform.position, bulletEmitterTransform.position + bulletEmitterTransform.forward);
  27.     }
  28. }
  29.  
  30. [RequireComponent(typeof(ParticleSystem))]
  31. public class BulletExplosion : MonoBehaviour{
  32.     public ParticleSystem particles;
  33.  
  34.     private void Awake() {
  35.         var main = particles.main;
  36.         main.stopAction = ParticleSystemStopAction.Callback;
  37.     }
  38.  
  39.     void OnParticleSystemStopped() {
  40.         BulletManager.ReturnBulletExposionToPool(this);
  41.     }
  42. }
  43.  
  44. public static class BulletManager {
  45.     struct Bullet {
  46.         public Vector3 position;
  47.         public Vector3 lastRenderedPosition;
  48.         public Vector3 direction;
  49.         public float velocity;
  50.         public float aliveTime;
  51.  
  52.  
  53.         public Bullet(Vector3 pos, Vector3 dir, float vel, float time) {
  54.             position = lastRenderedPosition = pos;
  55.             direction = dir;
  56.             velocity = vel;
  57.             aliveTime = time;
  58.         }
  59.     }
  60.  
  61.     const float BULLET_LIVE_TIME = 10f;
  62.  
  63.     static int layerMask;
  64.     static Material material;
  65.     static GameObject explosion;
  66.  
  67.     static Stack<BulletExplosion> explosions;
  68.  
  69.     static Bullet[] bulletsArray;
  70.     static int bulletsCreated;
  71.  
  72.     static int[] freeIndexStack;
  73.     static int freeIndexStackLength;
  74.  
  75.     public static Color col1, col2;
  76.  
  77.     public static void Init(LayerMask bulletCollisionMask, Material bulletMaterial, GameObject bulletExplosion) {
  78.         layerMask = bulletCollisionMask.value;
  79.         material = bulletMaterial;
  80.         explosion = bulletExplosion;
  81.  
  82.         explosions = new Stack<BulletExplosion>();
  83.         bulletsArray = new Bullet[10];
  84.         bulletsCreated = 0;
  85.         freeIndexStack = new int[10];
  86.         freeIndexStackLength = 0;
  87.     }
  88.  
  89.     public static void Shoot(Vector3 position, Vector3 direction, float velocity) {
  90.         int index = freeIndexStackLength > 0 ? freeIndexStack[--freeIndexStackLength] : bulletsCreated++;
  91.         if (index == bulletsArray.Length)
  92.             System.Array.Resize(ref bulletsArray, bulletsArray.Length * 2);
  93.  
  94.         bulletsArray[index] = new Bullet(position, direction, velocity, BULLET_LIVE_TIME);
  95.     }
  96.  
  97.     public static void ReturnBulletExposionToPool(BulletExplosion explosion) {
  98.         explosions.Push(explosion);
  99.     }
  100.  
  101.     public static void RenderBulletTrails() {
  102.         GL.Begin(GL.LINES);
  103.         material.SetPass(0);
  104.  
  105.         for (int bulletIndex = 0; bulletIndex < bulletsCreated; bulletIndex++) {
  106.             Bullet bullet = bulletsArray[bulletIndex];
  107.  
  108.             if (bullet.aliveTime > 0f) {
  109.                 GL.Vertex(bullet.lastRenderedPosition);
  110.                 GL.Vertex(bullet.position);
  111.                 bulletsArray[bulletIndex].lastRenderedPosition = bullet.position;
  112.             }
  113.         }
  114.         GL.End();
  115.     }
  116.  
  117.     public static void UpdateBulletManager() {
  118.         float deltaTime = Time.deltaTime;
  119.  
  120.         for (int bulletIndex = 0; bulletIndex < bulletsCreated; bulletIndex++) {
  121.             Bullet bullet = bulletsArray[bulletIndex];
  122.  
  123.             if (bullet.aliveTime <= 0f)
  124.                 continue;
  125.  
  126.             float travelDistance = bullet.velocity * deltaTime;
  127.             Vector3 newPos = bullet.position + (bullet.direction * travelDistance);
  128.             float bulletLiveTime = bullet.aliveTime - deltaTime;
  129.  
  130.             RaycastHit hit;
  131.             if (Physics.Raycast(bullet.position, bullet.direction, out hit, travelDistance, layerMask)) {
  132.                 bulletLiveTime = 0f;
  133.                 BulletExplosion explosionEffect = explosions.Count > 0 ? explosions.Pop() : Object.Instantiate(explosion).GetComponent<BulletExplosion>();
  134.  
  135.                 explosionEffect.transform.position = hit.point;        
  136.                 explosionEffect.particles.Play();
  137.             }
  138.  
  139.             if (bulletLiveTime <= 0f) {  
  140.                 if (freeIndexStack.Length == freeIndexStackLength)
  141.                     System.Array.Resize(ref freeIndexStack, freeIndexStack.Length * 2);
  142.                 freeIndexStack[freeIndexStackLength++] = bulletIndex;
  143.             }
  144.  
  145.             bulletsArray[bulletIndex].aliveTime = bulletLiveTime;
  146.             bulletsArray[bulletIndex].position = newPos;
  147.         }
  148.  
  149.      
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement