diff --git a/Assets/Scripts/Gameplay/AbilitySystem.cs b/Assets/Scripts/Gameplay/AbilitySystem.cs index 7f4c938..45a97b1 100644 --- a/Assets/Scripts/Gameplay/AbilitySystem.cs +++ b/Assets/Scripts/Gameplay/AbilitySystem.cs @@ -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 abilitiesOnCooldown = new HashSet(); - [SerializeField] private GameObject currentAbilityIndicator; - [SerializeField] private GameObject wallIndicator; // Separate indicator for the wall + [SerializeField] private GameObject currentAbilityIndicator; + [SerializeField] private GameObject wallIndicator; void Update() + { + HandleAbilityMode(); + } + + private void HandleAbilityMode() { if (isAbilityActive) { - if (activeAbility.abilityKey == "VectorFence") + if (activeAbility.abilityKey == GameDataSource.Instance.VectorWallAbilityKey) { - // Use wallIndicator for VectorFence - if (!wallIndicator.activeSelf) - { - 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,47 +54,66 @@ public class AbilitySystem : NetworkBehaviour } else { - // Deactivate both indicators when ability mode is inactive - currentAbilityIndicator.SetActive(false); - wallIndicator.SetActive(false); + DeactivateIndicators(); } } - private void UpdateIndicatorForVectorFence(Vector3 center, Vector3 direction, float length) + private void ManageVectorFenceIndicator() { - if (wallIndicator != null) + wallIndicator.SetActive(true); + currentAbilityIndicator.SetActive(false); + } + + private void PositionWallIndicator() + { + 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); } } + 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) { - foreach (var ability in abilities) + var ability = abilities.FirstOrDefault(a => a.abilityKey == key); + if (ability == null) { - if (ability.abilityKey == key) - { - if (!abilitiesOnCooldown.Contains(ability)) - { - ToggleAbilityMode(ability); - } - else - { - Debug.Log($"{ability.abilityName} is on cooldown."); - } - return; - } + Debug.LogWarning($"No ability assigned to key {key}."); + return; } - Debug.LogWarning($"No ability assigned to key {key}."); - } - public bool IsAbilityModeActive() - { - return isAbilityActive; + 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) @@ -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 - { - targetPosition = currentAbilityIndicator.transform.position; - targetRotation = currentAbilityIndicator.transform.eulerAngles; // Optional, depending on other abilities - } - - RequestAbilityActivationServerRpc(activeAbility.abilityKey, targetPosition, targetRotation); - StartCoroutine(StartCooldown(activeAbility)); - DeactivateAbilityMode(); - } - else + 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} 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()); @@ -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 (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit)) { - if (currentAbilityIndicator != null) - { - currentAbilityIndicator.transform.position = hit.point; - currentAbilityIndicator.transform.localScale = Vector3.one * activeAbility.abilityRadius; - } + currentAbilityIndicator.transform.position = hit.point; + currentAbilityIndicator.transform.localScale = Vector3.one * activeAbility.abilityRadius; } } } diff --git a/Assets/Scripts/Gameplay/Platform.cs b/Assets/Scripts/Gameplay/Platform.cs index e05f818..a948a41 100644 --- a/Assets/Scripts/Gameplay/Platform.cs +++ b/Assets/Scripts/Gameplay/Platform.cs @@ -17,26 +17,6 @@ namespace Unity.Multiplayer.Samples.BossRoom private Collider m_PlatformCollider; private Animator animator; private NetworkAnimator networkAnimator; - public override void OnNetworkSpawn() - { - if (!IsOwner) return; - - Debug.Log("OnNetworkSpawn"); - // Example: Start an idle animation when spawned - PlayAnimation("Rotating"); - } - - public void PlayAnimation(string animationTrigger) - { - if (IsOwner) - { - // Set the trigger on the local Animator - animator.SetTrigger(animationTrigger); - - // Synchronize animation across the network - networkAnimator.SetTrigger(animationTrigger); - } - } public void PauseAnimation() {