Advertisement
ChibiPasting

MultipleProjectileWeapon

May 31st, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using Assets.Scripts.Enemies;
  2. using Assets.Scripts.Helpers;
  3. using System.Linq;
  4. using UnityEngine;
  5.  
  6. namespace Assets.Scripts.Weapons
  7. {
  8.     internal class MultipleProjectileWeapon : SingleProjectileWeapon
  9.     {
  10.         [SerializeField] protected float _fanAngle = 10f;
  11.        
  12.         public override void Attack(EnemyCharacter target)
  13.         {
  14.             if (target == null) return;
  15.  
  16.             float projectileAngle = 0f;
  17.             for (int i = 0; i < Properties.Amount; i++)
  18.             {
  19.                 var projectile = RequestProjectile();
  20.                 Vector3 projectileDirection = MyMathHelper.CalculateOffsetDirection(transform.position, target.transform.position, projectileAngle);
  21.                 projectile.MoveTowards(projectileDirection);
  22.  
  23.                 // Alternate projectile placement (above, below, above, below, ...) the first projectile
  24.                 if (i % 2 == 0) projectileAngle -= _fanAngle;
  25.                 projectileAngle *= -1;
  26.             }
  27.  
  28.             _weaponCooldownTimer.StartTimer();
  29.         }
  30.  
  31.         public override void AttackMany(EnemyCharacter[] targets)
  32.         {
  33.             if (targets.Length == 0) return;
  34.  
  35.             // how many targets *can* I aquire? (rounded down for safety)
  36.             var selection = targets.Take(Mathf.FloorToInt(Properties.Amount)).ToArray();
  37.             foreach (var target in selection)
  38.             {
  39.                 // send a projectile to each individual target:
  40.                 var projectile = RequestProjectile();
  41.                 Vector3 direction = target.transform.position - transform.position;
  42.                 projectile.MoveTowards(direction);
  43.             }
  44.  
  45.             _weaponCooldownTimer.StartTimer();
  46.         }
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement