using System; using System.Collections.Generic; using OTGIntegrated.Core; using OTGIntegrated.Items; using OTGIntegrated.Player; using OTGIntegrated.Stats; using UnityEngine; using UnityEngine.EventSystems; namespace OTGIntegrated.Weapons { [Serializable] public sealed class WeaponUnlock { public ItemData item; public WeaponData weapon; } public sealed class WeaponManager : MonoBehaviour { public List availableWeapons = new List(); public List itemUnlocks = new List(); public WeaponData startingWeapon; public WeaponController weaponController; private PlayerStats playerStats; private PlayerController playerController; private OTGIntegrated.Inventory.Inventory inventory; private WeaponData currentWeapon; private float nextAttackTime; public WeaponData CurrentWeapon => currentWeapon; public float CurrentFinalDamage => currentWeapon == null || playerStats == null ? 0f : currentWeapon.damage + playerStats.Damage; public void Initialize(PlayerStats stats, OTGIntegrated.Inventory.Inventory inventory) { playerStats = stats; this.inventory = inventory; playerController = GetComponent(); if (weaponController == null) { weaponController = GetComponent(); } if (currentWeapon == null) { EquipWeapon(startingWeapon != null ? startingWeapon : availableWeapons.Count > 0 ? availableWeapons[0] : null); } } private void OnEnable() { GameEvents.OnItemAdded += HandleItemAdded; } private void OnDisable() { GameEvents.OnItemAdded -= HandleItemAdded; } private void Update() { if (Input.GetMouseButtonDown(0) && CanAcceptCombatInput()) { TryAttack(); } } public bool EquipWeapon(WeaponData weapon) { if (weapon == null) { return false; } currentWeapon = weapon; if (!availableWeapons.Contains(weapon)) { availableWeapons.Add(weapon); } GameEvents.RaiseWeaponChanged(currentWeapon); GameEvents.RaiseNotification($"Оружие: {currentWeapon.displayName}"); return true; } public bool TryAttack() { if (currentWeapon == null || weaponController == null || playerStats == null) { return false; } if (Time.time < nextAttackTime) { return false; } if (currentWeapon.manaCost > 0f && !playerStats.SpendMana(currentWeapon.manaCost)) { return false; } nextAttackTime = Time.time + currentWeapon.attackRate; bool hit = weaponController.Attack(currentWeapon, playerStats); if (!hit) { GameEvents.RaiseNotification("Атака"); } return hit; } public string SaveData() { return currentWeapon != null ? currentWeapon.weaponId : string.Empty; } public void LoadData(string weaponId) { if (string.IsNullOrWhiteSpace(weaponId)) { EquipWeapon(startingWeapon); return; } for (int i = 0; i < availableWeapons.Count; i++) { WeaponData weapon = availableWeapons[i]; if (weapon != null && weapon.weaponId == weaponId) { EquipWeapon(weapon); return; } } for (int i = 0; i < itemUnlocks.Count; i++) { WeaponUnlock unlock = itemUnlocks[i]; if (unlock != null && unlock.weapon != null && unlock.weapon.weaponId == weaponId) { EquipWeapon(unlock.weapon); return; } } } private void HandleItemAdded(ItemData item, int amount) { if (item == null || amount <= 0) { return; } for (int i = 0; i < itemUnlocks.Count; i++) { WeaponUnlock unlock = itemUnlocks[i]; if (unlock != null && unlock.item == item && unlock.weapon != null) { EquipWeapon(unlock.weapon); return; } } } private bool CanAcceptCombatInput() { if (playerController == null) { playerController = GetComponent(); } if (playerController != null && !playerController.InputEnabled) { return false; } if (GameManager.Instance != null && GameManager.Instance.uiManager != null && GameManager.Instance.uiManager.IsBlockingGameplay) { return false; } return EventSystem.current == null || !EventSystem.current.IsPointerOverGameObject(); } } }