You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
210 lines
6.7 KiB
C#
210 lines
6.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Unity.BossRoom.Gameplay.GameplayObjects;
|
|
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
public class AbilitySystem : NetworkBehaviour
|
|
{
|
|
[Header("Assigned Abilities")]
|
|
public List<Ability> abilities = new List<Ability>();
|
|
|
|
private Ability activeAbility;
|
|
private bool isAbilityActive = false;
|
|
private HashSet<Ability> abilitiesOnCooldown = new HashSet<Ability>();
|
|
|
|
[SerializeField] private GameObject currentAbilityIndicator;
|
|
[SerializeField] private GameObject wallIndicator;
|
|
|
|
void Update()
|
|
{
|
|
HandleAbilityMode();
|
|
}
|
|
|
|
private void HandleAbilityMode()
|
|
{
|
|
if (isAbilityActive)
|
|
{
|
|
if (activeAbility.abilityKey == GameDataSource.Instance.VectorWallAbilityKey)
|
|
{
|
|
ManageVectorFenceIndicator();
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
PositionWallIndicator();
|
|
}
|
|
if (Input.GetMouseButton(0))
|
|
{
|
|
RotateWallIndicator();
|
|
}
|
|
if (Input.GetMouseButtonUp(0))
|
|
{
|
|
UseActiveAbility();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ManageStandardAbilityIndicator();
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
UseActiveAbility();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
DeactivateIndicators();
|
|
}
|
|
}
|
|
|
|
private void ManageVectorFenceIndicator()
|
|
{
|
|
wallIndicator.SetActive(true);
|
|
currentAbilityIndicator.SetActive(false);
|
|
}
|
|
|
|
private void PositionWallIndicator()
|
|
{
|
|
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit))
|
|
{
|
|
wallIndicator.transform.position = hit.point;
|
|
var vectorFence = (VectorFenceAbility)activeAbility;
|
|
wallIndicator.transform.localScale = new Vector3(vectorFence.wallLength, vectorFence.wallHeight, vectorFence.wallWidth);
|
|
}
|
|
}
|
|
|
|
private void RotateWallIndicator()
|
|
{
|
|
float mouseX = Input.GetAxis("Mouse X");
|
|
wallIndicator.transform.Rotate(Vector3.up, mouseX);
|
|
}
|
|
|
|
private void ManageStandardAbilityIndicator()
|
|
{
|
|
currentAbilityIndicator.SetActive(true);
|
|
wallIndicator.SetActive(false);
|
|
UpdateIndicatorPosition();
|
|
}
|
|
|
|
private void DeactivateIndicators()
|
|
{
|
|
currentAbilityIndicator.SetActive(false);
|
|
wallIndicator.SetActive(false);
|
|
}
|
|
|
|
public void ActivateAbilityByKey(string key)
|
|
{
|
|
var ability = abilities.FirstOrDefault(a => a.abilityKey == key);
|
|
if (ability == null)
|
|
{
|
|
Debug.LogWarning($"No ability assigned to key {key}.");
|
|
return;
|
|
}
|
|
|
|
if (abilitiesOnCooldown.Contains(ability))
|
|
{
|
|
Debug.Log($"{ability.abilityName} is on cooldown.");
|
|
}
|
|
else
|
|
{
|
|
ToggleAbilityMode(ability);
|
|
}
|
|
}
|
|
|
|
public bool IsAbilityModeActive() => isAbilityActive;
|
|
|
|
private void ToggleAbilityMode(Ability ability)
|
|
{
|
|
if (isAbilityActive && activeAbility == ability)
|
|
{
|
|
DeactivateAbilityMode();
|
|
}
|
|
else
|
|
{
|
|
ActivateAbilityMode(ability);
|
|
}
|
|
}
|
|
|
|
private void ActivateAbilityMode(Ability ability)
|
|
{
|
|
isAbilityActive = true;
|
|
activeAbility = ability;
|
|
Debug.Log($"Ability {ability.abilityName} activated! Click to use.");
|
|
}
|
|
|
|
private void DeactivateAbilityMode()
|
|
{
|
|
isAbilityActive = false;
|
|
activeAbility = null;
|
|
Debug.Log("Ability mode deactivated.");
|
|
}
|
|
|
|
public void UseActiveAbility()
|
|
{
|
|
if (activeAbility == null)
|
|
{
|
|
Debug.LogWarning("[AbilitySystem] No active ability to use.");
|
|
return;
|
|
}
|
|
|
|
Vector3 targetPosition = activeAbility.abilityKey == "VectorFence" ? wallIndicator.transform.position : currentAbilityIndicator.transform.position;
|
|
Vector3 targetRotation = activeAbility.abilityKey == "VectorFence" ? wallIndicator.transform.eulerAngles : currentAbilityIndicator.transform.eulerAngles;
|
|
|
|
Debug.Log($"[AbilitySystem] Using active ability {activeAbility.abilityName}.");
|
|
RequestAbilityActivationServerRpc(activeAbility.abilityKey, targetPosition, targetRotation);
|
|
StartCoroutine(StartCooldown(activeAbility));
|
|
DeactivateAbilityMode();
|
|
}
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
private void RequestAbilityActivationServerRpc(string abilityKey, Vector3 targetPosition, Vector3 targetRotation, ServerRpcParams rpcParams = default)
|
|
{
|
|
ulong ownerClientId = rpcParams.Receive.SenderClientId;
|
|
Debug.Log($"[AbilitySystem] Received activation request for ability '{abilityKey}' from client {ownerClientId}.");
|
|
ExecuteAbilityOnServer(abilityKey, ownerClientId, targetPosition, targetRotation);
|
|
}
|
|
|
|
private void ExecuteAbilityOnServer(string abilityKey, ulong ownerClientId, Vector3 targetPosition, Vector3 targetRotation)
|
|
{
|
|
var playerObject = NetworkManager.Singleton.SpawnManager.SpawnedObjectsList
|
|
.FirstOrDefault(obj => obj.OwnerClientId == ownerClientId && obj.GetComponent<ServerCharacter>());
|
|
|
|
if (playerObject == null || !playerObject.TryGetComponent(out ServerCharacter character))
|
|
{
|
|
Debug.LogError($"[AbilitySystem] No ServerCharacter component found for player {ownerClientId}.");
|
|
return;
|
|
}
|
|
|
|
var ability = abilities.FirstOrDefault(a => a.abilityKey == abilityKey);
|
|
if (ability == null)
|
|
{
|
|
Debug.LogError($"[AbilitySystem] Ability {abilityKey} not found.");
|
|
return;
|
|
}
|
|
|
|
Debug.Log($"[AbilitySystem] Activating ability {ability.abilityName} for player {ownerClientId}.");
|
|
ability.Execute(character, targetPosition, targetRotation);
|
|
}
|
|
|
|
private IEnumerator StartCooldown(Ability ability)
|
|
{
|
|
abilitiesOnCooldown.Add(ability);
|
|
Debug.Log($"{ability.abilityName} is now on cooldown for {ability.abilityCooldownTime} seconds.");
|
|
|
|
yield return new WaitForSeconds(ability.abilityCooldownTime);
|
|
|
|
abilitiesOnCooldown.Remove(ability);
|
|
Debug.Log($"{ability.abilityName} is off cooldown.");
|
|
}
|
|
|
|
private void UpdateIndicatorPosition()
|
|
{
|
|
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit))
|
|
{
|
|
currentAbilityIndicator.transform.position = hit.point;
|
|
currentAbilityIndicator.transform.localScale = Vector3.one * activeAbility.abilityRadius;
|
|
}
|
|
}
|
|
}
|