Fixed a few things

dev-hazim
Hazim Bin Ijaz 2 weeks ago
parent 89cc5f60e3
commit 3576ce23af

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

@ -17,26 +17,6 @@ namespace Unity.Multiplayer.Samples.BossRoom
private Collider m_PlatformCollider; private Collider m_PlatformCollider;
private Animator animator; private Animator animator;
private NetworkAnimator networkAnimator; 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() public void PauseAnimation()
{ {

Loading…
Cancel
Save