Add new meta files and interfaces for project structure
This commit is contained in:
19
Assets/Scripts/Weapons/AutoWeapon.cs
Normal file
19
Assets/Scripts/Weapons/AutoWeapon.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Interfaces;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Weapons
|
||||
{
|
||||
public class AutoWeapon : Weapon, IWeapon
|
||||
{
|
||||
[SerializeField] private GameObject projectilePrefab;
|
||||
[SerializeField] private Transform firePoint;
|
||||
|
||||
public override void Fire()
|
||||
{
|
||||
var projectile = Instantiate(projectilePrefab, firePoint.position, firePoint.rotation);
|
||||
projectile.TryGetComponent<IDamageInflectorSetup>(out var inflector);
|
||||
|
||||
inflector?.Setup(character);
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Weapons/AutoWeapon.cs.meta
Normal file
3
Assets/Scripts/Weapons/AutoWeapon.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fd9cdb55272f45ab9c60a948a56e01f5
|
||||
timeCreated: 1752259218
|
27
Assets/Scripts/Weapons/MeleeAttack.cs
Normal file
27
Assets/Scripts/Weapons/MeleeAttack.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using Data;
|
||||
using Interfaces;
|
||||
using Systems;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Weapons
|
||||
{
|
||||
public class MeleeAttack : Weapon, IWeapon
|
||||
{
|
||||
[SerializeField] private float range = 1f;
|
||||
[SerializeField] private LayerMask targetMask;
|
||||
|
||||
public override void Fire()
|
||||
{
|
||||
var hits = Physics2D.OverlapCircleAll(transform.position, range, targetMask);
|
||||
foreach (var hit in hits)
|
||||
{
|
||||
hit.TryGetComponent<Health>(out var health);
|
||||
if (hit.gameObject == character.gameObject) continue;
|
||||
|
||||
var damage = character.attributes.Damage * character.attributes.MeleeDamage;
|
||||
health.TakeDamage(damage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Weapons/MeleeAttack.cs.meta
Normal file
3
Assets/Scripts/Weapons/MeleeAttack.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e4ff53f671604189b15fac28115b2b70
|
||||
timeCreated: 1752259665
|
47
Assets/Scripts/Weapons/Projectile.cs
Normal file
47
Assets/Scripts/Weapons/Projectile.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using Data;
|
||||
using Interfaces;
|
||||
using KBCore.Refs;
|
||||
using Systems;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Weapons
|
||||
{
|
||||
public class Projectile : MonoBehaviour, IDamageInflector, IDamageInflectorSetup
|
||||
{
|
||||
[Self, SerializeField] private Rigidbody2D rb;
|
||||
[SerializeField] private float speed = 10f;
|
||||
[SerializeField] private float lifeTime = 5f;
|
||||
|
||||
public float Damage { get; private set; }
|
||||
public GameObject Owner { get; private set; }
|
||||
public DamageType Type => DamageType.Ranged;
|
||||
|
||||
public void Setup(Character attacker)
|
||||
{
|
||||
Damage = attacker.attributes.Damage * attacker.attributes.RangedDamage;
|
||||
Owner = attacker.gameObject;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
other.TryGetComponent<Health>(out var health);
|
||||
if (other.gameObject == Owner) return;
|
||||
|
||||
health.TakeDamage(Damage);
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Weapons/Projectile.cs.meta
Normal file
3
Assets/Scripts/Weapons/Projectile.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d5fcee922b049d1b9db7b49711ce562
|
||||
timeCreated: 1752259511
|
27
Assets/Scripts/Weapons/Weapon.cs
Normal file
27
Assets/Scripts/Weapons/Weapon.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using Data;
|
||||
using Interfaces;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Weapons
|
||||
{
|
||||
public abstract class Weapon : MonoBehaviour, IWeapon
|
||||
{
|
||||
private float timer;
|
||||
|
||||
[SerializeField] private float cooldown = 1f;
|
||||
[SerializeField] protected Character character;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
timer -= Time.deltaTime;
|
||||
|
||||
if (!(timer <= 0f)) return;
|
||||
|
||||
Fire();
|
||||
timer = 1f / character.attributes.AttackSpeed;
|
||||
}
|
||||
|
||||
public abstract void Fire();
|
||||
}
|
||||
}
|
3
Assets/Scripts/Weapons/Weapon.cs.meta
Normal file
3
Assets/Scripts/Weapons/Weapon.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a504bad3918744d7bbbf3065678bb96a
|
||||
timeCreated: 1752259779
|
Reference in New Issue
Block a user