Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Assets.Scripts.Enemies;
- using Assets.Scripts.FactoryClasses;
- using Assets.Scripts.Weapons.Base;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- namespace Assets.Scripts.Weapons
- {
- [RequireComponent(typeof(ProjectileFactory))]
- internal class SingleProjectileWeapon : BaseWeapon
- {
- protected ProjectileFactory _objectPool;
- private readonly List<WeaponProjectile> _activeProjectiles = new();
- protected override void GetComponents()
- {
- base.GetComponents();
- _objectPool = GetComponent<ProjectileFactory>();
- }
- public override void Attack(EnemyCharacter target)
- {
- if (target == null)
- {
- _weaponCooldownTimer.StartTimer();
- return;
- }
- if (_weaponCooldownTimer.IsRunning) return;
- if (Properties.Amount == 1)
- {
- FireSingleProjectile(target);
- _weaponCooldownTimer.StartTimer();
- }
- else
- {
- StartCoroutine(AttackCoroutine(target));
- }
- }
- public override void AttackMany(EnemyCharacter[] targets)
- {
- if (targets.Length == 0)
- {
- _weaponCooldownTimer.StartTimer();
- return;
- }
- if (_weaponCooldownTimer.IsRunning) return;
- EnemyCharacter[] selection = targets.Take(Mathf.FloorToInt(Properties.Amount)).ToArray();
- foreach (var target in selection)
- {
- FireSingleProjectile(target);
- }
- _weaponCooldownTimer.StartTimer();
- }
- protected virtual IEnumerator AttackCoroutine(EnemyCharacter target)
- {
- float time;
- float duration = .5f; // delay between each shot...?
- // Behaviour: pew pew pew! waaaait... pew pew pew!
- for (int i = 0; i < Properties.Amount; i++)
- {
- // how to determine firing speed though?
- FireSingleProjectile(target);
- time = 0f;
- while (time < duration)
- {
- time += Time.deltaTime;
- yield return null;
- }
- yield return null;
- }
- _weaponCooldownTimer.StartTimer();
- }
- protected virtual void FireSingleProjectile(EnemyCharacter target)
- {
- var projectile = RequestProjectile();
- Vector3 projectileDirection = (target.transform.position - transform.position);
- projectile.MoveTowards(projectileDirection);
- }
- protected virtual WeaponProjectile RequestProjectile()
- {
- var projectile = _objectPool.RequestObject();
- projectile = SetProjectileStats(projectile);
- projectile.OnTargetHit += Projectile_OnTargetHit;
- projectile.OnExpired += Projectile_OnExpired;
- _activeProjectiles.Add(projectile);
- return projectile;
- }
- protected virtual void ReturnProjectile(WeaponProjectile projectile)
- {
- projectile.OnTargetHit -= Projectile_OnTargetHit;
- projectile.OnExpired -= Projectile_OnExpired;
- _activeProjectiles.Remove(projectile);
- _objectPool.ReturnToPool(projectile);
- }
- protected virtual WeaponProjectile SetProjectileStats(WeaponProjectile projectile)
- {
- projectile.transform.position = transform.position;
- if (Properties.Area > 1f) projectile.transform.localScale *= Properties.Area;
- projectile.SetPierceCount((int)Properties.Pierce);
- projectile.AdjustProjectileSpeed(Properties.Speed);
- return projectile;
- }
- private void Projectile_OnExpired(object sender, System.EventArgs e)
- {
- var projectile = (WeaponProjectile)sender;
- ReturnProjectile(projectile);
- }
- private void Projectile_OnTargetHit(object sender, EnemyCharacter e)
- {
- var projectile = (WeaponProjectile)sender;
- if (projectile.Health <= 0)
- {
- ReturnProjectile(projectile);
- }
- e.SubstractHealth(Properties.Damage);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement