Advertisement
ChibiPasting

BaseWeapon

May 31st, 2024 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | Source Code | 0 0
  1. using Assets.Scripts.Enemies;
  2. using Assets.Scripts.Enums;
  3. using Assets.Scripts.GameSystems.Leveling;
  4. using Assets.Scripts.Interfaces;
  5. using Assets.Scripts.Interfaces.Weapons;
  6. using Assets.Scripts.ScriptableObjects;
  7. using Assets.Scripts.ScriptableObjects.Stats;
  8. using Assets.Scripts.Weapons.Components;
  9. using System;
  10. using UnityEngine;
  11.  
  12. namespace Assets.Scripts.Weapons.Base
  13. {  
  14.     [RequireComponent(typeof(WeaponCooldownTimer))]
  15.     [RequireComponent(typeof(WeaponLevelSystem))]
  16.     internal abstract class BaseWeapon : MonoBehaviour, IWeapon
  17.     {
  18.         public event EventHandler CooldownFinished;
  19.  
  20.         [SerializeField] protected WeaponType _weaponType;
  21.         [SerializeField] protected WeaponTargetingPriority _targetingPriority;
  22.         [SerializeField] protected WeaponAttackMode _attackMode;
  23.         [SerializeField] protected BaseWeaponProperties _properties;
  24.         [SerializeField] protected WeaponCooldownTimer _weaponCooldownTimer;
  25.         [SerializeField] protected WeaponLevelSystem _levelSystem;
  26.  
  27.         public string Name => gameObject.name.Replace("(Clone)", string.Empty);
  28.         public Vector3 Position => transform.position;
  29.         public WeaponType Type => _weaponType;
  30.         public WeaponTargetingPriority TargettingPriority => _targetingPriority;
  31.         public WeaponAttackMode AttackMode => _attackMode;
  32.         public IWeaponProperties Properties => _properties;
  33.         public ILevelSystem LevelSystem => _levelSystem;
  34.  
  35.         protected virtual void Awake()
  36.         {
  37.             GetComponents();
  38.             _weaponCooldownTimer.Duration = Properties.Cooldown;
  39.         }
  40.  
  41.         protected virtual void OnEnable()
  42.         {
  43.             _weaponCooldownTimer.Completed += WeaponCooldownTimer_OnCompleted;
  44.             _weaponCooldownTimer.StartTimer();
  45.         }
  46.  
  47.         protected virtual void OnDisable()
  48.         {
  49.             _weaponCooldownTimer.StopTimer();
  50.             _weaponCooldownTimer.Completed -= WeaponCooldownTimer_OnCompleted;
  51.         }
  52.  
  53.         protected virtual void GetComponents()
  54.         {
  55.             _weaponCooldownTimer ??= GetComponent<WeaponCooldownTimer>();
  56.             _levelSystem ??= GetComponent<WeaponLevelSystem>();
  57.         }
  58.  
  59.         private void WeaponCooldownTimer_OnCompleted(object sender, EventArgs e)
  60.         {  
  61.             CooldownFinished?.Invoke(this, e);
  62.         }
  63.  
  64.         public abstract void Attack(EnemyCharacter target);
  65.         public abstract void AttackMany(EnemyCharacter[] targets);
  66.  
  67.         public virtual void AddAttributeDependency(CharacterStats stats)
  68.         {
  69.             _properties.Damage.AddAttribute(stats.Might);
  70.             _properties.Cooldown.AddAttribute(stats.WeaponCooldown);
  71.             _properties.Speed.AddAttribute(stats.ProjectileSpeed);
  72.             _properties.Amount.AddAttribute(stats.Amount);
  73.             _properties.Area.AddAttribute(stats.Area);
  74.             _properties.Duration.AddAttribute(stats.Duration);
  75.         }
  76.         public virtual void RemoveAttributeDependency(CharacterStats stats)
  77.         {
  78.             _properties.Damage.RemoveAttribute(stats.Might);
  79.             _properties.Cooldown.RemoveAttribute(stats.WeaponCooldown);
  80.             _properties.Speed.RemoveAttribute(stats.ProjectileSpeed);
  81.             _properties.Amount.RemoveAttribute(stats.Amount);
  82.             _properties.Area.RemoveAttribute(stats.Area);
  83.             _properties.Duration.RemoveAttribute(stats.Duration);
  84.         }
  85.         public virtual void AddAttributeModifier(WeaponProperties properties)
  86.         {
  87.             _properties.Damage.AddRawBonus(properties.Damage);
  88.             _properties.Cooldown.AddRawBonus(properties.Cooldown);
  89.             _properties.Speed.AddRawBonus(properties.Speed);
  90.             _properties.Amount.AddRawBonus(properties.Amount);
  91.             _properties.Area.AddRawBonus(properties.Area);
  92.             _properties.Pierce.AddRawBonus(properties.Pierce);
  93.             _properties.Duration.AddRawBonus(properties.Duration);
  94.         }
  95.  
  96.         public ITimer GetCooldownTimer()
  97.         {
  98.             return _weaponCooldownTimer;
  99.         }
  100.     }
  101. }
  102.  
Tags: C# Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement