RiftMayhem/Assets/Scripts/Player/AbilityKeyBinder.cs
Pedro Gomes 9c136817b3 Updates to rogue-lite mechanics (WIP)
- visual update on riftraids
- ability unlock system through ability tomes
- (possible) bugfix sprite issue
- player reputation level
2025-01-18 23:14:12 +00:00

189 lines
6.2 KiB
C#

using Kryz.CharacterStats.Examples;
using Photon.Pun;
using System.Collections;
using UnityEngine;
public class AbilityKeyBinder : MonoBehaviour
{
[Header("Listeners:")]
[SerializeField] private GameEventListener onLocalPlayerFainted;
[SerializeField] private GameEventListener onLocalPlayerPermaDeath;
[SerializeField] private GameEventListener onLocalPlayerRevived;
[Space]
[SerializeField] private BaseAbility ability;
[SerializeField] private GameKey key;
[SerializeField] private CastingStateController castingStateController;
[SerializeField] private GameEvent_AbilityKeyBinder onAbilityKeyBinderSpawned;
[SerializeField] private GameInputBinding gamepadBinding; // New field for gamepad binding
private PhotonView user;
private Taggable userTag;
private Mana mana;
private Health health;
public Health Health => health;
public Mana Mana => mana;
public BaseAbility Ability => ability;
public GameKey GameKey => key;
Coroutine currentChanneling;
NetworkedChanneling networkedChanneling;
AbilityBindInstance abilityBindInstance;
AbilityCooldownTracker cooldownTracker;
bool isDead = false;
bool abilitySlotUnlocked = false;
float finalHealthCost;
float finalManaCost;
private void Awake()
{
user = GetComponentInParent<PhotonView>();
userTag = GetComponentInParent<Taggable>();
mana = GetComponentInParent<Mana>();
health = GetComponentInParent<Health>();
cooldownTracker = user.GetComponentInChildren<AbilityCooldownTracker>();
}
private void Start()
{
if (!user.IsMine)
{
this.enabled = false;
return;
}
onLocalPlayerFainted.Response.AddListener(() =>
{
isDead = true;
});
onLocalPlayerPermaDeath.Response.AddListener(() =>
{
isDead = true;
});
onLocalPlayerRevived.Response.AddListener(() =>
{
isDead = false;
});
onAbilityKeyBinderSpawned.Raise(this);
}
bool isInputPressed;
bool isInputReleased;
void Update()
{
if (isDead) return;
if (!abilitySlotUnlocked) return;
// Check for input using both keyboard and gamepad
isInputPressed =
(key != null && Input.GetKeyDown(key.keyCode)) ||
(!string.IsNullOrEmpty(gamepadBinding.bindingName) && GameInputManager.Instance.GetButtonDown(gamepadBinding.bindingName));
isInputReleased =
(key != null && Input.GetKeyUp(key.keyCode)) ||
(!string.IsNullOrEmpty(gamepadBinding.bindingName) && GameInputManager.Instance.GetButtonUp(gamepadBinding.bindingName));
if (isInputPressed)
{
if (abilityBindInstance != null)
abilityBindInstance.pressed.SetActive(true);
if (IsAbilityOffCooldown() && mana.EnoughMana(ability.GetFinalManaCost(mana)) && health.EnoughHealth(ability.GetFinalHealthCost(health)))
{
if (ability is ChanneledAbility)
{
castingStateController.RequestAbilityChannel(ability, () =>
{
networkedChanneling = ((ChanneledAbility)ability).ExecuteChannel(user, userTag, ref currentChanneling);
if (ability.cooldown > 0)
{
cooldownTracker.StartAbilityCooldown(ability);
abilityBindInstance.StartCooldownTrackerUI();
}
});
}
else
{
castingStateController.RequestAbilityCast(ability, () =>
{
ability.Execute(user, userTag);
if (ability.cooldown > 0)
{
cooldownTracker.StartAbilityCooldown(ability);
abilityBindInstance.StartCooldownTrackerUI();
}
});
}
}
}
if (isInputReleased)
{
if (abilityBindInstance != null)
abilityBindInstance.pressed.SetActive(false);
if (currentChanneling != null)
{
if (networkedChanneling != null)
{
networkedChanneling.StopCoroutine(currentChanneling);
}
else
{
StopCoroutine(currentChanneling);
}
castingStateController.ResetChannelingCast();
CastBarHandler.Instance.CancelChannelingOnButtonReleased();
}
if (networkedChanneling != null)
{
networkedChanneling.channeling = false;
networkedChanneling.DisableVisuals();
}
}
}
public void SetupAbilityBindInstance(AbilityBindInstance abilityBindInstance)
{
this.abilityBindInstance = abilityBindInstance;
mana.onResourceChanged.AddListener(OnManaChanged);
health.onResourceChanged.AddListener(OnHealthChanged);
}
public void OnManaChanged(float currentMana)
{
finalManaCost = ability.GetFinalManaCost(mana);
abilityBindInstance.manaCost.text = finalManaCost.ToString("F0");
abilityBindInstance.noMana.SetActive(!mana.EnoughMana(finalManaCost));
}
public void OnHealthChanged(float currentHealth)
{
finalHealthCost = ability.GetFinalHealthCost(health);
abilityBindInstance.healthCost.text = finalHealthCost.ToString("F0");
abilityBindInstance.healthCostGO.SetActive(finalHealthCost > 0);
abilityBindInstance.noHealth.SetActive(!health.EnoughHealth(finalHealthCost));
}
public bool IsAbilityOffCooldown()
{
return ability.cooldown <= 0 || !cooldownTracker.OnCooldown(ability);
}
public void BindAbility(BaseAbility ability)
{
this.ability = ability;
onAbilityKeyBinderSpawned.Raise(this);
}
public void SetUnlockAbilitySlot(bool unlocked)
{
abilitySlotUnlocked = unlocked;
abilityBindInstance.SetUnlocked(unlocked);
}
}