85 lines
3.4 KiB
C#
85 lines
3.4 KiB
C#
using UnityEngine;
|
||
using UnityEngine.InputSystem;
|
||
using RR.Game2D.Entities.Projectiles;
|
||
|
||
namespace RR.Game2D.Behaviour
|
||
{
|
||
[RequireComponent(typeof(Collider2D))]
|
||
public class PlayerFireScript : MonoBehaviour
|
||
{
|
||
[Header("Управление")]
|
||
[SerializeField] private InputActionReference fireButton;
|
||
|
||
[Header("Снаряд")]
|
||
[SerializeField] private PickableProjectile projectilePrefab;
|
||
public bool spawnIfMissing = false;
|
||
|
||
[Header("Бросок / Подбор")]
|
||
[SerializeField] private float throwSpeed = 20f;
|
||
[SerializeField] private Vector2 launchAxis;
|
||
[SerializeField] private Transform launchPoint;
|
||
[SerializeField] public Transform attachPoint;
|
||
|
||
private PickableProjectile pickableProjectile;
|
||
private Collider2D col;
|
||
|
||
void Awake()
|
||
{
|
||
if (!attachPoint)
|
||
Debug.LogWarning("PlayerFireScript: attachPoint не задан. Будет использован корень игрока.");
|
||
col = GetComponent<Collider2D>();
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
//if (!Input.GetButtonDown("Fire1")) return;
|
||
if (!fireButton.action.WasPressedThisFrame()) return;
|
||
|
||
if (pickableProjectile == null)
|
||
pickableProjectile = PickableProjectile.Instance;
|
||
|
||
if (pickableProjectile == null)
|
||
{
|
||
if (!spawnIfMissing || projectilePrefab == null)
|
||
{
|
||
Debug.LogError("PlayerFireScript: projectile missing; enable spawnIfMissing and assign projectilePrefab.");
|
||
return;
|
||
}
|
||
|
||
Vector3 spawnPos = attachPoint ? attachPoint.position : transform.position;
|
||
pickableProjectile = Instantiate(projectilePrefab, spawnPos, Quaternion.identity);
|
||
pickableProjectile.TryPickup(gameObject, attachPoint);
|
||
return;
|
||
}
|
||
// Бросок
|
||
if (pickableProjectile.CurrentState == PickableProjectile.State.Held && pickableProjectile.owner == gameObject) Throw();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Метод для броска снаряда (выстрела).
|
||
/// У снаряда обязательно должен быть метод Launch(Vector2 direction)
|
||
/// </summary>
|
||
private void Throw()
|
||
{
|
||
launchAxis = launchAxis.normalized;
|
||
|
||
float facing = Mathf.Abs(transform.localEulerAngles.y) >= 180 ? -1 : 1;
|
||
Vector2 directionX = facing > 0 ? Vector2.right : Vector2.left;
|
||
Vector2 launchPos = new (1.4f * facing + launchPoint.position.x, launchPoint.position.y);
|
||
//Debug.Log(col.bounds.center.y);
|
||
launchAxis.x = launchAxis.x == 0 ? launchAxis.x : directionX.x;
|
||
|
||
Vector3 projectileScale = pickableProjectile.transform.localScale;
|
||
pickableProjectile.transform.localScale = new Vector3(Mathf.Abs(projectileScale.x) * -facing, projectileScale.y, projectileScale.z);
|
||
|
||
pickableProjectile.transform.position = launchPos;
|
||
pickableProjectile.Launch(launchAxis, throwSpeed, gameObject);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Метод для привязки снаряда к игроку.
|
||
/// </summary>
|
||
/// <param name="proj">Снаряд</param>
|
||
public void BindProjectile(PickableProjectile proj) => pickableProjectile = proj;
|
||
}
|
||
} |