Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Sirenix.OdinInspector;
- using UnityEngine;
- public class ModuleControl : MonoBehaviour
- {
- public List<ModuleBehavior> modules;
- public List<BulletBehavior> bulletBehaviorsApplied;
- [Tooltip("These behaviors are detached from all other logic and will be added after module behaviors were added.")]
- public List<BulletBehavior> testBehaviorsApplied;
- // Prefab used to create a new cached bullet for the player
- [SerializeField] private GameObject prefabProjectile;
- // Copy of the actual prefab with all behaviors attached
- [SerializeField, Unity.Collections.ReadOnly] private GameObject cachedProjectile;
- [Button, HorizontalGroup("Split", 0.75f)]
- private void GenerateProjectile()
- {
- ClearCache();
- InitiateModules(modules);
- SetupCachedBullet();
- AddBehaviors(bulletBehaviorsApplied);
- GetComponent<DroneFire>().projectilePrefab = cachedProjectile;
- }
- [Button, HorizontalGroup("Split/right")]
- private void ClearCache()
- {
- bulletBehaviorsApplied.Clear();
- if(cachedProjectile!= null) DestroyImmediate(cachedProjectile);
- cachedProjectile = null;
- }
- private void SetupCachedBullet()
- {
- cachedProjectile = Instantiate(prefabProjectile, transform, false);
- cachedProjectile.name = "[test] Projectile";
- cachedProjectile.SetActive(false);
- }
- private void InitiateModules(List<ModuleBehavior> moduleBehaviors)
- {
- foreach (var moduleBehavior in moduleBehaviors)
- moduleBehavior.InitiateModule(this);
- }
- private void AddBehaviors(List<BulletBehavior> bulletBehaviors)
- {
- foreach (var behavior in bulletBehaviors)
- {
- cachedProjectile.AddComponent(behavior.GetType());
- }
- foreach (var behavior in testBehaviorsApplied)
- {
- cachedProjectile.AddComponent(behavior.GetType());
- }
- }
- private void Start()
- {
- GenerateProjectile();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement