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.
HighGroundRoyaleNetcode/Assets/Scripts/Gameplay/AbilitySystem.cs

253 lines
8.2 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;
[Header("Wall Placement Settings")]
[SerializeField] private float wallRotationSpeed = 2f;
private Vector3 wallSpawnPosition;
private bool isWallPlacementStarted = false;
void Update()
{
HandleAbilityMode();
}
private void HandleAbilityMode()
{
if (isAbilityActive)
{
if (activeAbility.abilityKey == "VectorFence")
{
ManageVectorFenceIndicator();
if (!isWallPlacementStarted)
{
UpdateWallIndicatorPosition(); // Follow the mouse when ability is activated
}
if (Input.GetMouseButtonDown(0))
{
StartWallPlacement();
}
if (Input.GetMouseButton(0) && isWallPlacementStarted)
{
RotateWallIndicator(); // Rotate while holding LMB
}
if (Input.GetMouseButtonUp(0) && isWallPlacementStarted)
{
UseActiveAbility(); // Place the wall when LMB is released
isWallPlacementStarted = false;
}
}
else
{
ManageStandardAbilityIndicator();
if (Input.GetMouseButtonDown(0))
{
UseActiveAbility();
}
}
}
else
{
DeactivateIndicators();
}
}
private void ManageVectorFenceIndicator()
{
if (wallIndicator != null)
{
wallIndicator.SetActive(true);
currentAbilityIndicator.SetActive(false);
// Apply ability-specific scale to the wall indicator
if (activeAbility is VectorFenceAbility vectorFence)
{
wallIndicator.transform.localScale = new Vector3(vectorFence.wallLength, vectorFence.wallHeight, vectorFence.wallWidth);
}
}
}
private void UpdateWallIndicatorPosition()
{
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit))
{
wallIndicator.transform.position = hit.point; // Update position to follow the mouse
}
}
private void StartWallPlacement()
{
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit))
{
wallSpawnPosition = hit.point; // Save spawn position
isWallPlacementStarted = true;
Debug.Log($"[AbilitySystem] Wall placement started at {wallSpawnPosition}");
}
}
private void RotateWallIndicator()
{
if (isWallPlacementStarted && wallIndicator != null)
{
float mouseX = Input.GetAxis("Mouse X");
wallIndicator.transform.Rotate(Vector3.up, mouseX * wallRotationSpeed); // Rotate Y-axis based on mouse movement
}
}
private void ManageStandardAbilityIndicator()
{
if (currentAbilityIndicator != null)
{
currentAbilityIndicator.SetActive(true);
}
if (wallIndicator != null)
{
wallIndicator.SetActive(false);
}
UpdateIndicatorPosition();
}
private void DeactivateIndicators()
{
if (currentAbilityIndicator != null) currentAbilityIndicator.SetActive(false);
if (wallIndicator != null) 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.");
}
public void DeactivateAbilityMode()
{
isAbilityActive = false;
activeAbility = null;
isWallPlacementStarted = false;
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;
}
}
}