using RR.Game2D.Behaviour.ScriptableObjects; using RR.Game2D.Core.Managers; 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; [Header("FX")] [SerializeField] private SFXAsset throwSounds; private PickableProjectile _pickableProjectile; private Collider2D _col; void Awake() { if (!attachPoint) Debug.LogWarning("PlayerFireScript: attachPoint не задан. Будет использован корень игрока."); _col = GetComponent(); } 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(); } /// /// Метод для броска снаряда (выстрела). /// У снаряда обязательно должен быть метод Launch(Vector2 direction) /// 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; 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); } /// /// Метод для привязки снаряда к игроку. /// /// Снаряд public void BindProjectile(PickableProjectile proj) => _pickableProjectile = proj; } }