Advertisement
ChibiPasting

SingleProjectileWeapon

May 31st, 2024 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.43 KB | None | 0 0
  1. using Assets.Scripts.Enemies;
  2. using Assets.Scripts.FactoryClasses;
  3. using Assets.Scripts.Weapons.Base;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using UnityEngine;
  8.  
  9. namespace Assets.Scripts.Weapons
  10. {
  11.     [RequireComponent(typeof(ProjectileFactory))]
  12.     internal class SingleProjectileWeapon : BaseWeapon
  13.     {
  14.         protected ProjectileFactory _objectPool;
  15.         private readonly List<WeaponProjectile> _activeProjectiles = new();
  16.  
  17.         protected override void GetComponents()
  18.         {
  19.             base.GetComponents();
  20.             _objectPool = GetComponent<ProjectileFactory>();
  21.         }
  22.  
  23.         public override void Attack(EnemyCharacter target)
  24.         {
  25.             if (target == null)
  26.             {
  27.                 _weaponCooldownTimer.StartTimer();
  28.                 return;
  29.             }
  30.  
  31.             if (_weaponCooldownTimer.IsRunning) return;
  32.  
  33.             if (Properties.Amount == 1)
  34.             {
  35.                 FireSingleProjectile(target);
  36.                 _weaponCooldownTimer.StartTimer();
  37.             }
  38.             else
  39.             {
  40.                 StartCoroutine(AttackCoroutine(target));
  41.             }
  42.         }
  43.  
  44.         public override void AttackMany(EnemyCharacter[] targets)
  45.         {
  46.             if (targets.Length == 0)
  47.             {
  48.                 _weaponCooldownTimer.StartTimer();
  49.                 return;
  50.             }
  51.  
  52.             if (_weaponCooldownTimer.IsRunning) return;
  53.  
  54.             EnemyCharacter[] selection = targets.Take(Mathf.FloorToInt(Properties.Amount)).ToArray();
  55.             foreach (var target in selection)
  56.             {
  57.                 FireSingleProjectile(target);
  58.             }
  59.  
  60.             _weaponCooldownTimer.StartTimer();
  61.         }
  62.  
  63.         protected virtual IEnumerator AttackCoroutine(EnemyCharacter target)
  64.         {
  65.             float time;
  66.             float duration = .5f; // delay between each shot...?
  67.  
  68.             // Behaviour: pew pew pew! waaaait... pew pew pew!
  69.             for (int i = 0; i < Properties.Amount; i++)
  70.             {
  71.                 // how to determine firing speed though?
  72.                 FireSingleProjectile(target);
  73.  
  74.                 time = 0f;
  75.                 while (time < duration)
  76.                 {
  77.                     time += Time.deltaTime;
  78.                     yield return null;
  79.                 }
  80.  
  81.                 yield return null;
  82.             }
  83.  
  84.             _weaponCooldownTimer.StartTimer();
  85.         }
  86.  
  87.         protected virtual void FireSingleProjectile(EnemyCharacter target)
  88.         {
  89.             var projectile = RequestProjectile();
  90.             Vector3 projectileDirection = (target.transform.position - transform.position);
  91.             projectile.MoveTowards(projectileDirection);
  92.         }
  93.  
  94.         protected virtual WeaponProjectile RequestProjectile()
  95.         {
  96.             var projectile = _objectPool.RequestObject();
  97.             projectile = SetProjectileStats(projectile);
  98.             projectile.OnTargetHit += Projectile_OnTargetHit;
  99.             projectile.OnExpired += Projectile_OnExpired;
  100.             _activeProjectiles.Add(projectile);
  101.  
  102.             return projectile;
  103.         }
  104.  
  105.         protected virtual void ReturnProjectile(WeaponProjectile projectile)
  106.         {
  107.             projectile.OnTargetHit -= Projectile_OnTargetHit;
  108.             projectile.OnExpired -= Projectile_OnExpired;
  109.             _activeProjectiles.Remove(projectile);
  110.             _objectPool.ReturnToPool(projectile);
  111.         }
  112.  
  113.         protected virtual WeaponProjectile SetProjectileStats(WeaponProjectile projectile)
  114.         {
  115.             projectile.transform.position = transform.position;
  116.             if (Properties.Area > 1f) projectile.transform.localScale *= Properties.Area;
  117.             projectile.SetPierceCount((int)Properties.Pierce);
  118.             projectile.AdjustProjectileSpeed(Properties.Speed);
  119.  
  120.             return projectile;
  121.         }
  122.  
  123.         private void Projectile_OnExpired(object sender, System.EventArgs e)
  124.         {
  125.             var projectile = (WeaponProjectile)sender;
  126.             ReturnProjectile(projectile);
  127.         }
  128.  
  129.         private void Projectile_OnTargetHit(object sender, EnemyCharacter e)
  130.         {
  131.             var projectile = (WeaponProjectile)sender;
  132.  
  133.             if (projectile.Health <= 0)
  134.             {
  135.                 ReturnProjectile(projectile);
  136.             }
  137.  
  138.             e.SubstractHealth(Properties.Damage);
  139.         }
  140.     }
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement