- Добавил систему диалогов - Создал классы Sequencer.cs и SequenceAction.cs для воссоздания последовательности действий - Добавил иеханику последовательности действий в Message.cs и MessageController.cs
104 lines
4.2 KiB
C#
104 lines
4.2 KiB
C#
using System;
|
||
using UnityEngine;
|
||
using UnityEngine.InputSystem;
|
||
|
||
using RR.Game2D.Core.Managers;
|
||
using RR.Game2D.Behaviour.ScriptableObjects;
|
||
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;
|
||
|
||
[Header("FX")]
|
||
[SerializeField] private SFXAsset throwSounds;
|
||
|
||
// Включение/отключение возможности броска
|
||
[HideInInspector] public bool enableFire = true;
|
||
|
||
private PickableProjectile _pickableProjectile;
|
||
private Collider2D _col;
|
||
|
||
private void Awake()
|
||
{
|
||
if (!attachPoint)
|
||
Debug.LogWarning("PlayerFireScript: attachPoint не задан. Будет использован корень игрока.");
|
||
_col = GetComponent<Collider2D>();
|
||
fireButton.action.performed += ThrowProjectile;
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
fireButton.action.performed -= ThrowProjectile;
|
||
}
|
||
|
||
private void ThrowProjectile(InputAction.CallbackContext obj)
|
||
{
|
||
//if (!Input.GetButtonDown("Fire1")) return;
|
||
// if (!fireButton.action.WasPressedThisFrame()) return;
|
||
if (!enableFire) 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;
|
||
// Debug.Log(transform.localEulerAngles.y);
|
||
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;
|
||
|
||
if (throwSounds.soundEffects.Length > 0)
|
||
GameManager.Instance.Audio.PlayRandomSfxAt(throwSounds.soundEffects, transform.position);
|
||
|
||
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;
|
||
}
|
||
} |