@ -1,6 +1,7 @@
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 ;
@ -13,61 +14,38 @@ public class AbilitySystem : NetworkBehaviour
private Ability activeAbility ;
private bool isAbilityActive = false ;
private HashSet < Ability > abilitiesOnCooldown = new HashSet < Ability > ( ) ;
[SerializeField] private GameObject currentAbilityIndicator ;
[SerializeField] private GameObject wallIndicator ; // Separate indicator for the wall
[SerializeField] private GameObject currentAbilityIndicator ;
[SerializeField] private GameObject wallIndicator ;
void Update ( )
{
if ( isAbilityActive )
HandleAbilityMode ( ) ;
}
private void HandleAbilityMode ( )
{
if ( activeAbility . abilityKey = = "VectorFence" )
if ( isAbilityActive )
{
// Use wallIndicator for VectorFence
if ( ! wallIndicator . activeSelf )
if ( activeAbility . abilityKey = = GameDataSource . Instance . VectorWallAbilityKey )
{
wallIndicator . SetActive ( true ) ;
currentAbilityIndicator . SetActive ( false ) ;
}
ManageVectorFenceIndicator ( ) ;
if ( Input . GetMouseButtonDown ( 0 ) )
{
// Save the spawn position for the wall
Ray ray = Camera . main . ScreenPointToRay ( Input . mousePosition ) ;
if ( Physics . Raycast ( ray , out RaycastHit hit ) )
{
wallIndicator . transform . position = hit . point ;
// wallIndicator.transform.rotation = Quaternion.identity; // Reset rotation
wallIndicator . transform . localScale = new Vector3 ( ( ( VectorFenceAbility ) activeAbility ) . wallLength ,
( ( VectorFenceAbility ) activeAbility ) . wallHeight ,
( ( VectorFenceAbility ) activeAbility ) . wallWidth ) ;
}
PositionWallIndicator ( ) ;
}
if ( Input . GetMouseButton ( 0 ) )
{
// Rotate the wall using Mouse X for Y-axis rotation
float mouseX = Input . GetAxis ( "Mouse X" ) ;
wallIndicator . transform . Rotate ( Vector3 . up , mouseX ) ; // Adjust rotation speed if needed
RotateWallIndicator ( ) ;
}
if ( Input . GetMouseButtonUp ( 0 ) )
{
// Place the wall at the saved position and rotation
UseActiveAbility ( ) ;
}
}
else
{
// Use currentAbilityIndicator for all other abilities
if ( ! currentAbilityIndicator . activeSelf )
{
currentAbilityIndicator . SetActive ( true ) ;
wallIndicator . SetActive ( false ) ;
}
UpdateIndicatorPosition ( ) ;
ManageStandardAbilityIndicator ( ) ;
if ( Input . GetMouseButtonDown ( 0 ) )
{
UseActiveAbility ( ) ;
@ -76,46 +54,65 @@ public class AbilitySystem : NetworkBehaviour
}
else
{
// Deactivate both indicators when ability mode is inactive
currentAbilityIndicator . SetActive ( false ) ;
wallIndicator . SetActive ( false ) ;
DeactivateIndicators ( ) ;
}
}
private void ManageVectorFenceIndicator ( )
{
wallIndicator . SetActive ( true ) ;
currentAbilityIndicator . SetActive ( false ) ;
}
private void UpdateIndicatorForVectorFence ( Vector3 center , Vector3 direction , float length )
private void PositionWallIndicator( )
{
if ( wallIndicator ! = null )
if ( Physics. Raycast ( Camera . main . ScreenPointToRay ( Input . mousePosition ) , out RaycastHit hit ) )
{
wallIndicator . transform . position = center ;
wallIndicator . transform . rotation = Quaternion . LookRotation ( direction ) ;
wallIndicator . transform . localScale = new Vector3 ( length, 1 , 0.2f ) ; // Adjust wall width & height
wallIndicator . transform . position = hit. point ;
var vectorFence = ( VectorFenceAbility ) activeAbility ;
wallIndicator . transform . localScale = new Vector3 ( vectorFence. wallLength , vectorFence . wallHeight , vectorFence . wallWidth ) ;
}
}
p ublic void ActivateAbilityByKey ( string key )
p rivate void RotateWallIndicator ( )
{
foreach ( var ability in abilities )
float mouseX = Input . GetAxis ( "Mouse X" ) ;
wallIndicator . transform . Rotate ( Vector3 . up , mouseX ) ;
}
private void ManageStandardAbilityIndicator ( )
{
if ( ability . abilityKey = = key )
currentAbilityIndicator . SetActive ( true ) ;
wallIndicator . SetActive ( false ) ;
UpdateIndicatorPosition ( ) ;
}
private void DeactivateIndicators ( )
{
if ( ! abilitiesOnCooldown . Contains ( ability ) )
currentAbilityIndicator . SetActive ( false ) ;
wallIndicator . SetActive ( false ) ;
}
public void ActivateAbilityByKey ( string key )
{
ToggleAbilityMode ( ability ) ;
var ability = abilities . FirstOrDefault ( a = > a . abilityKey = = key ) ;
if ( ability = = null )
{
Debug . LogWarning ( $"No ability assigned to key {key}." ) ;
return ;
}
else
if ( abilitiesOnCooldown . Contains ( ability ) )
{
Debug . Log ( $"{ability.abilityName} is on cooldown." ) ;
}
return ;
}
else
{
ToggleAbilityMode ( ability ) ;
}
Debug . LogWarning ( $"No ability assigned to key {key}." ) ;
}
public bool IsAbilityModeActive ( )
{
return isAbilityActive ;
}
public bool IsAbilityModeActive ( ) = > isAbilityActive ;
private void ToggleAbilityMode ( Ability ability )
{
@ -145,48 +142,31 @@ public class AbilitySystem : NetworkBehaviour
public void UseActiveAbility ( )
{
if ( activeAbility ! = null )
{
Debug . Log ( $"[AbilitySystem] Using active ability {activeAbility.abilityName}." ) ;
Vector3 targetPosition ;
Vector3 targetRotation ;
if ( activeAbility . abilityKey = = "VectorFence" )
{
targetPosition = wallIndicator . transform . position ;
targetRotation = wallIndicator . transform . eulerAngles ;
}
else
if ( activeAbility = = null )
{
targetPosition = currentAbilityIndicator . transform . position ;
targetRotation = currentAbilityIndicator . transform . eulerAngles ; // Optional, depending on other abilities
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 ( ) ;
}
else
{
Debug . LogWarning ( "[AbilitySystem] No active ability to use." ) ;
}
}
[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} at position {targetPosition} with rotation {targetRotation}." ) ;
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 )
{
// Find the player's ServerCharacter
var playerObject = NetworkManager . Singleton . SpawnManager . SpawnedObjectsList
. FirstOrDefault ( obj = > obj . OwnerClientId = = ownerClientId & & obj . GetComponent < ServerCharacter > ( ) ) ;
@ -196,16 +176,14 @@ public class AbilitySystem : NetworkBehaviour
return ;
}
// Find the ability
var ability = abilities . Find ( a = > a . abilityKey = = abilityKey ) ;
var ability = abilities . FirstOrDefault ( a = > a . abilityKey = = abilityKey ) ;
if ( ability = = null )
{
Debug . LogError ( $"[AbilitySystem] Ability {abilityKey} not found in the Ability System .") ;
Debug . LogError ( $"[AbilitySystem] Ability {abilityKey} not found .") ;
return ;
}
// Activate the ability
Debug . Log ( $"[AbilitySystem] Activating ability {ability.abilityName} for player {ownerClientId} at {targetPosition} with rotation {targetRotation}." ) ;
Debug . Log ( $"[AbilitySystem] Activating ability {ability.abilityName} for player {ownerClientId}." ) ;
ability . Execute ( character , targetPosition , targetRotation ) ;
}
@ -222,15 +200,10 @@ public class AbilitySystem : NetworkBehaviour
private void UpdateIndicatorPosition ( )
{
// Raycast to get the position of the cursor on the ground
Ray ray = Camera . main . ScreenPointToRay ( Input . mousePosition ) ;
if ( Physics . Raycast ( ray , out RaycastHit hit ) )
{
if ( currentAbilityIndicator ! = null )
if ( Physics . Raycast ( Camera . main . ScreenPointToRay ( Input . mousePosition ) , out RaycastHit hit ) )
{
currentAbilityIndicator . transform . position = hit . point ;
currentAbilityIndicator . transform . localScale = Vector3 . one * activeAbility . abilityRadius ;
}
}
}
}