first commit
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
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<WeaponData> availableWeapons = new List<WeaponData>();
|
||||
public List<WeaponUnlock> itemUnlocks = new List<WeaponUnlock>();
|
||||
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<PlayerController>();
|
||||
if (weaponController == null)
|
||||
{
|
||||
weaponController = GetComponent<WeaponController>();
|
||||
}
|
||||
|
||||
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<PlayerController>();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user