Skip to content

Commit

Permalink
feat(mmextensions): added the weapons class
Browse files Browse the repository at this point in the history
Co-authored-by: Mike Evans <[email protected]>
  • Loading branch information
h0lybyte and JigSawPsycho committed Jan 4, 2025
1 parent 28811b9 commit df25d70
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 27 deletions.
56 changes: 29 additions & 27 deletions packages/mmextensions/mmextensions/Abilities/DisableOnDeath.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
using UnityEngine;
using MoreMountains.TopDownEngine;
using Cysharp.Threading.Tasks;
using System;
using Cysharp.Threading.Tasks;
using MoreMountains.TopDownEngine;
using UnityEngine;

public class DisableOnDeath : MonoBehaviour
{
[SerializeField] Health health;
[SerializeField]
Health health;

private void Start()
{
if (health == null)
health = GetComponent<Health>();
}

private void OnEnable()
{
health.OnDeath += Health_OnDeath;
}

private void Start()
{
if(health == null) health = GetComponent<Health>();
}
private void OnDisable()
{
health.OnDeath -= Health_OnDeath;
}

private void OnEnable()
{
health.OnDeath += Health_OnDeath;
}
private void Health_OnDeath()
{
OnDeathDisableAsync();
}

private void OnDisable()
{
health.OnDeath -= Health_OnDeath;
}

private void Health_OnDeath()
{
OnDeathDisableAsync();
}
private async UniTask OnDeathDisableAsync()
{
await UniTask.Delay(500, DelayType.DeltaTime);

private async UniTask OnDeathDisableAsync()
{
await UniTask.Delay(500, DelayType.DeltaTime);

gameObject.SetActive(false);
}
gameObject.SetActive(false);
}
}
207 changes: 207 additions & 0 deletions packages/mmextensions/mmextensions/Weapons/AOEWeapon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
using System;
using System.Collections;
using System.Collections.Generic;
using MoreMountains.Feedbacks;
using MoreMountains.Tools;
using UnityEngine;
using Random = UnityEngine.Random;

// Originally by Sargorath - From the repo https://github.com/reunono/TopDownEngineExtensions/tree/ef687a03b7dad8fcb486c7e4db3f7d5f5113b0ec

namespace KBVE.MMExtensions.Weapons
{
/// <summary>
/// A weapon class aimed specifically at allowing the creation of various projectile weapons, from shotgun to machine gun, via plasma gun or rocket launcher
/// </summary>
[AddComponentMenu("KBVE/MMExtensions/Weapons/AOE Weapon")]
public class AOEWeapon : Weapon, MMEventListener<TopDownEngineEvent>
{
[MMInspectorGroup("AOESpell", true, 22)]
[MMReadOnly]
[Tooltip("the projectile's spawn position")]
public Vector3 SpawnPosition = Vector3.zero;

/// the object pooler used to spawn projectiles, if left empty, this component will try to find one on its game object
[Tooltip(
"the object pooler used to spawn projectiles, if left empty, this component will try to find one on its game object"
)]
public MMObjectPooler ObjectPooler;

public LayerMask groundLayerMask;

protected Vector3 _flippedProjectileSpawnOffset;
protected Vector3 _randomSpreadDirection;
protected bool _poolInitialized = false;
protected Transform _projectileSpawnTransform;
protected int _spawnArrayIndex = 0;

[MMInspectorButton("TestShoot")]
/// a button to test the shoot method
public bool TestShootButton;

/// <summary>
/// A test method that triggers the weapon
/// </summary>
protected virtual void TestShoot()
{
if (WeaponState.CurrentState == WeaponStates.WeaponIdle)
{
WeaponInputStart();
}
else
{
WeaponInputStop();
}
}

/// <summary>
/// Initialize this weapon
/// </summary>
public override void Initialization()
{
base.Initialization();
_weaponAim = GetComponent<WeaponAim>();

if (!_poolInitialized)
{
if (ObjectPooler == null)
{
ObjectPooler = GetComponent<MMObjectPooler>();
}
if (ObjectPooler == null)
{
Debug.LogWarning(
this.name
+ " : no object pooler (simple or multiple) is attached to this Projectile Weapon, it won't be able to shoot anything."
);
return;
}
_poolInitialized = true;
}
}

/// <summary>
/// Called everytime the weapon is used
/// </summary>
public override void WeaponUse()
{
base.WeaponUse();

DetermineSpawnPosition();

SpawnProjectile(SpawnPosition, true);
}

/// <summary>
/// Spawns a new object and positions/resizes it
/// </summary>
public virtual GameObject SpawnProjectile(
Vector3 spawnPosition,
bool triggerObjectActivation = true
)
{
/// we get the next object in the pool and make sure it's not null
GameObject nextGameObject = ObjectPooler.GetPooledGameObject();

// mandatory checks
if (nextGameObject == null)
{
return null;
}
if (nextGameObject.GetComponent<MMPoolableObject>() == null)
{
throw new Exception(
gameObject.name
+ " is trying to spawn objects that don't have a PoolableObject component."
);
}
// we position the object
nextGameObject.transform.position = spawnPosition;
if (_projectileSpawnTransform != null)
{
nextGameObject.transform.position = _projectileSpawnTransform.position;
}
// we set its direction

Projectile projectile = nextGameObject.GetComponent<Projectile>();
if (projectile != null)
{
projectile.SetWeapon(this);
if (Owner != null)
{
projectile.SetOwner(Owner.gameObject);
}
}
// we activate the object
nextGameObject.gameObject.SetActive(true);

if (triggerObjectActivation)
{
if (nextGameObject.GetComponent<MMPoolableObject>() != null)
{
nextGameObject.GetComponent<MMPoolableObject>().TriggerOnSpawnComplete();
}
}
return (nextGameObject);
}

/// <summary>
/// Determines the spawn position
/// </summary>
public virtual void DetermineSpawnPosition()
{
// Cast a ray from the cursor position into the scene
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;

// Check if the ray hits any object on the ground layer
if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, groundLayerMask))
{
// Set the spawn position to the point where the ray hits the ground
SpawnPosition = hitInfo.point;
}
else
{
Debug.LogWarning("No ground found for spawning projectile.");
}
}

/// <summary>
/// When the weapon is selected, draws a circle at the spawn's position
/// </summary>
protected virtual void OnDrawGizmosSelected()
{
DetermineSpawnPosition();

Gizmos.color = Color.white;
Gizmos.DrawWireSphere(SpawnPosition, 0.2f);
}

public void OnMMEvent(TopDownEngineEvent engineEvent)
{
switch (engineEvent.EventType)
{
case TopDownEngineEventTypes.LevelStart:
_poolInitialized = false;
Initialization();
break;
}
}

/// <summary>
/// On enable we start listening for events
/// </summary>
protected virtual void OnEnable()
{
this.MMEventStartListening<TopDownEngineEvent>();
}

/// <summary>
/// On disable we stop listening for events
/// </summary>
protected virtual void OnDisable()
{
this.MMEventStopListening<TopDownEngineEvent>();
}
}
}
11 changes: 11 additions & 0 deletions packages/mmextensions/mmextensions/Weapons/AOEWeapon.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit df25d70

Please sign in to comment.