From fef118b27784e175da7066280e6ec2799270e2fa Mon Sep 17 00:00:00 2001 From: Alexandru Gheorghe Date: Mon, 18 Mar 2024 19:03:13 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Adds=20WeaponAutoDestroyWhenEmpty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../WeaponAutoDestroyWhenEmpty.cs | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 WeaponAutoDestroyWhenEmpty/WeaponAutoDestroyWhenEmpty.cs diff --git a/README.md b/README.md index 6b7da56..ed27ab4 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ This repository contains community-created extensions for the TopDown Engine, Mo * **TwinStickShooterHandleWeapon**, _by AlexanderGheorghe_ : an extension of CharacterHandleWeapon that gives you the option to shoot on receiving SecondaryMovement input, with configurable minimum magnitude of the input * **TypedDamage**, _by AlexanderGheorghe_ : a collection of extension scripts that implement typed damage with scriptable objects, like explained in [this video](https://youtu.be/_q21rEaSlAs). * **Vehicle**, _by AlexanderGheorghe_ : allows creating vehicles that the player character can enter, drive and exit. uses [Hayden Donnelly's Vehicle-Physics](https://github.com/hayden-donnelly/Vehicle-Physics). +* **WeaponAutoDestroyWhenEmpty**, _by AlexanderGheorghe_ : component that enables the Auto Destroy When Empty option to work without the Weapon Ammo component. goes on your magazine based, Auto Destroy When Empty weapon * **WeaponCooldownProgressBar**, _by AlexanderGheorghe_ : put this component on a MMProgressBar and it will be updated with the weapon's cooldown progress * **WeaponLaserSightEx**, _by Velsthinez_ : an extended version of the base WeaponLaserSight to create a widening laser when player turns diff --git a/WeaponAutoDestroyWhenEmpty/WeaponAutoDestroyWhenEmpty.cs b/WeaponAutoDestroyWhenEmpty/WeaponAutoDestroyWhenEmpty.cs new file mode 100644 index 0000000..59dc4fc --- /dev/null +++ b/WeaponAutoDestroyWhenEmpty/WeaponAutoDestroyWhenEmpty.cs @@ -0,0 +1,32 @@ +using MoreMountains.Feedbacks; +using MoreMountains.TopDownEngine; +using UnityEngine; + +[RequireComponent(typeof(Weapon))] +public class WeaponAutoDestroyWhenEmpty : MonoBehaviour +{ + private Weapon[] _weapons; + [SerializeField] private MMFeedbacks DestructionStartFeedbacks; + private void Start() + { + _weapons = GetComponents(); + if (!DestructionStartFeedbacks) return; + DestructionStartFeedbacks.Initialization(); + #if UNITY_EDITOR + foreach (var weapon in _weapons) + if (weapon.AutoDestroyWhenEmpty && weapon.AutoDestroyWhenEmptyDelay < DestructionStartFeedbacks.TotalDuration) + Debug.LogWarning($"{name} {weapon.GetType().Name}'s Auto Destroy When Empty Delay is less than the Destruction Start Feedbacks total duration, {DestructionStartFeedbacks.TotalDuration:F}. Feedbacks may not play to the end", weapon); + #endif + } + private void Update() + { + foreach (var weapon in _weapons) + if (weapon.AutoDestroyWhenEmpty && weapon.CurrentAmmoLoaded < weapon.AmmoConsumedPerShot) + { + enabled = false; + DestructionStartFeedbacks?.PlayFeedbacks(transform.position); + StartCoroutine(weapon.WeaponDestruction()); + break; + } + } +}