Add weapon prefabs for Grenade, Rifle, and Sword; set attributes including names, descriptions, prices, and projectile behaviors
This commit is contained in:
66
Assets/Scripts/Weapons/ExplosiveProjectile.cs
Normal file
66
Assets/Scripts/Weapons/ExplosiveProjectile.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using Data;
|
||||
using Interfaces;
|
||||
using KBCore.Refs;
|
||||
using Systems;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Weapons
|
||||
{
|
||||
public class ExplosiveProjectile : MonoBehaviour, IDamageInflector, IDamageInflectorSetup
|
||||
{
|
||||
[Self, SerializeField] private Rigidbody2D rb;
|
||||
[SerializeField] private float speed = 10f;
|
||||
[SerializeField] private float lifeTime = 5f;
|
||||
[SerializeField] private WeaponStats stats;
|
||||
|
||||
public float Damage { get; private set; }
|
||||
public GameObject Owner { get; private set; }
|
||||
public DamageType Type => DamageType.Ranged;
|
||||
|
||||
public void Setup(Character attacker, float damage, WeaponStats weaponStats = null)
|
||||
{
|
||||
Damage = damage;
|
||||
Owner = attacker.gameObject;
|
||||
if (weaponStats != null)
|
||||
{
|
||||
stats = weaponStats;
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Destroy(gameObject, lifeTime);
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
var direction = transform.up.normalized;
|
||||
var movement = direction * (speed * Time.fixedDeltaTime);
|
||||
rb.MovePosition(rb.position + (Vector2)movement);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
Explode();
|
||||
}
|
||||
|
||||
private void Explode()
|
||||
{
|
||||
var hitColliders = Physics2D.OverlapCircleAll(transform.position, stats.range);
|
||||
foreach (var hitCollider in hitColliders)
|
||||
{
|
||||
if (hitCollider.gameObject == Owner) continue;
|
||||
|
||||
hitCollider.TryGetComponent<Health>(out var health);
|
||||
health?.TakeDamage(Damage, Owner);
|
||||
}
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawWireSphere(transform.position, stats.range);
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Weapons/ExplosiveProjectile.cs.meta
Normal file
3
Assets/Scripts/Weapons/ExplosiveProjectile.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 23b6d668eacd4bffa5ec3772523d2b35
|
||||
timeCreated: 1752353915
|
Reference in New Issue
Block a user