Advertisement
CassataGames

Untitled

Feb 21st, 2023
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Sirenix.OdinInspector;
  4. using UnityEngine;
  5.  
  6. public class ModuleControl : MonoBehaviour
  7. {
  8.     public List<ModuleBehavior> modules;
  9.     public List<BulletBehavior> bulletBehaviorsApplied;
  10.  
  11.     [Tooltip("These behaviors are detached from all other logic and will be added after module behaviors were added.")]
  12.     public List<BulletBehavior> testBehaviorsApplied;
  13.    
  14.     // Prefab used to create a new cached bullet for the player
  15.     [SerializeField] private GameObject prefabProjectile;
  16.     // Copy of the actual prefab with all behaviors attached
  17.     [SerializeField, Unity.Collections.ReadOnly] private GameObject cachedProjectile;
  18.    
  19.     [Button, HorizontalGroup("Split", 0.75f)]
  20.     private void GenerateProjectile()
  21.     {
  22.         ClearCache();
  23.         InitiateModules(modules);
  24.         SetupCachedBullet();
  25.         AddBehaviors(bulletBehaviorsApplied);
  26.         GetComponent<DroneFire>().projectilePrefab = cachedProjectile;
  27.     }
  28.    
  29.     [Button, HorizontalGroup("Split/right")]
  30.     private void ClearCache()
  31.     {
  32.         bulletBehaviorsApplied.Clear();
  33.         if(cachedProjectile!= null) DestroyImmediate(cachedProjectile);
  34.         cachedProjectile = null;
  35.     }
  36.    
  37.     private void SetupCachedBullet()
  38.     {
  39.         cachedProjectile = Instantiate(prefabProjectile, transform, false);
  40.         cachedProjectile.name = "[test] Projectile";
  41.         cachedProjectile.SetActive(false);
  42.     }
  43.    
  44.     private void InitiateModules(List<ModuleBehavior> moduleBehaviors)
  45.     {
  46.         foreach (var moduleBehavior in moduleBehaviors)
  47.             moduleBehavior.InitiateModule(this);
  48.     }
  49.    
  50.     private void AddBehaviors(List<BulletBehavior> bulletBehaviors)
  51.     {
  52.         foreach (var behavior in bulletBehaviors)
  53.         {
  54.             cachedProjectile.AddComponent(behavior.GetType());
  55.         }
  56.  
  57.         foreach (var behavior in testBehaviorsApplied)
  58.         {
  59.             cachedProjectile.AddComponent(behavior.GetType());
  60.         }
  61.     }
  62.  
  63.     private void Start()
  64.     {
  65.         GenerateProjectile();
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement