85 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
public class PlayerWeapon : Weapon
{
[SerializeField] private List<GameObject> Projectiles;
public event Action<PlayerWeaponType> OnWeaponChanged;
private int weaponIndex;
public void Initialize()
{
weaponIndex = 0;
SetProjectile(Projectiles[weaponIndex]);
RefreshWeapon();
}
public void NextWeapon()
{
weaponIndex = (weaponIndex + 1) % Projectiles.Count;
RefreshWeapon();
}
public override void Shoot()
{
ProjectilePrefab.TryGetComponent<AbstractProjectile>(out var projectile);
if (projectile == null)
{
Debug.LogError("Снаряд не найден", this);
return;
}
if (projectile is Torpedo)
{
var go = Instantiate(ProjectilePrefab, ShootPoint.position, ShootPoint.rotation);
go.GetComponent<Torpedo>().Setup();
Debug.Log("Торпеда пущена");
}
else if (projectile is Rocket)
{
Debug.LogWarning("Логика выстрела ракетой не определена", this);
}
}
private void Update()
{
if (In.GetButtonDown(InSignal.DownRight))
{
Shoot();
}
}
private void RefreshWeapon()
{
SetProjectile(Projectiles[weaponIndex]);
OnWeaponChanged?.Invoke(GetCurrentWeaponType());
}
private void SetProjectile(GameObject projectileGO) => ProjectilePrefab = projectileGO;
private PlayerWeaponType GetCurrentWeaponType()
{
if(ProjectilePrefab.TryGetComponent<AbstractProjectile>(out var projectile))
{
if (projectile is Torpedo) return PlayerWeaponType.Torpedo;
else if (projectile is Rocket) return PlayerWeaponType.Rocket;
else return PlayerWeaponType.Error;
}
else
return PlayerWeaponType.Error;
}
public enum PlayerWeaponType
{
Torpedo,
Rocket,
Error
}
}