Added intervene mechanic. not fully developed needs testing

dev-ali
Hazim Bin Ijaz 2 days ago
parent 9c03acdd07
commit 955e59bf04

@ -142,8 +142,37 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character
NetworkAvatarGuidState m_State; NetworkAvatarGuidState m_State;
public ulong? PendingSwapRequest { get; set; } public ulong? PendingSwapRequest { get; set; }
public int? TargetPlatformId { get; private set; } = null;
public void SetTargetPlatform(int platformId)
{
TargetPlatformId = platformId;
}
public void ClearTargetPlatform()
{
TargetPlatformId = null;
}
private void OnTriggerEnter(Collider other)
{
if (TargetPlatformId.HasValue && other.TryGetComponent<Platform>(out var platform))
{
if (platform.PlatformID == TargetPlatformId.Value)
{
if (!platform.IsOccupied)
{
// platform.Occupy(this);
Debug.Log($"{name} successfully occupied Platform {platform.PlatformID}.");
}
else
{
Debug.Log($"{name} couldn't occupy Platform {platform.PlatformID}. Becoming a crow!");
}
ClearTargetPlatform();
}
}
}
void Awake() void Awake()
{ {
@ -194,14 +223,10 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character
if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(initiatingPlayerId, out var initiatingPlayerObj) && if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(initiatingPlayerId, out var initiatingPlayerObj) &&
initiatingPlayerObj.TryGetComponent(out ServerCharacter initiatingPlayer)) initiatingPlayerObj.TryGetComponent(out ServerCharacter initiatingPlayer))
{ {
Vector3 initiatingPlayerPosition = initiatingPlayer.physicsWrapper.Transform.position; // Call InitiateSwap directly
Vector3 targetPlayerPosition = this.physicsWrapper.Transform.position; InitiateSwap(initiatingPlayer, this);
// Execute the swap
initiatingPlayer.ServerSendCharacterInputRpc(targetPlayerPosition);
this.ServerSendCharacterInputRpc(initiatingPlayerPosition);
Debug.Log($"Swap confirmed: {initiatingPlayer.name} swapped with {this.name}."); Debug.Log($"Swap confirmed: {initiatingPlayer.name} and {this.name} are swapping.");
} }
} }
else else
@ -209,10 +234,27 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character
Debug.Log($"Swap request denied by {this.name}."); Debug.Log($"Swap request denied by {this.name}.");
} }
// Clear pending request
PendingSwapRequest = null; PendingSwapRequest = null;
} }
public void InitiateSwap(ServerCharacter initiatingPlayer, ServerCharacter targetPlayer)
{
int initiatingPlatformId = PlatformManager.Instance.GetPlatformOccupiedByPlayer(initiatingPlayer).PlatformID;
int targetPlatformId = PlatformManager.Instance.GetPlatformOccupiedByPlayer(targetPlayer).PlatformID;
initiatingPlayer.SetTargetPlatform(targetPlatformId);
targetPlayer.SetTargetPlatform(initiatingPlatformId);
Vector3 initiatingPlayerPosition = initiatingPlayer.physicsWrapper.Transform.position;
Vector3 targetPlayerPosition = this.physicsWrapper.Transform.position;
// Execute the swap
initiatingPlayer.ServerSendCharacterInputRpc(targetPlayerPosition);
this.ServerSendCharacterInputRpc(initiatingPlayerPosition);
Debug.Log($"Swap initiated: {initiatingPlayer.name} -> Platform {targetPlatformId}, {targetPlayer.name} -> Platform {initiatingPlatformId}.");
}
public override void OnNetworkSpawn() public override void OnNetworkSpawn()
{ {

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Unity.BossRoom.Gameplay.GameplayObjects.Character; using Unity.BossRoom.Gameplay.GameplayObjects.Character;
@ -7,8 +8,19 @@ namespace Unity.Multiplayer.Samples.BossRoom
{ {
public class PlatformManager : MonoBehaviour public class PlatformManager : MonoBehaviour
{ {
public static PlatformManager Instance = null;
private List<Platform> m_Platforms; private List<Platform> m_Platforms;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
} else {
Instance = this;
}
}
private void Start() private void Start()
{ {
m_Platforms = GetComponentsInChildren<Platform>().ToList(); m_Platforms = GetComponentsInChildren<Platform>().ToList();
@ -58,5 +70,18 @@ namespace Unity.Multiplayer.Samples.BossRoom
{ {
return platform.IsOccupied; return platform.IsOccupied;
} }
public Vector3 GetPlatformPosition(int platformId)
{
var platform = m_Platforms.FirstOrDefault(p => p.PlatformID == platformId);
return platform ? platform.transform.position : Vector3.zero;
}
public Platform GetPlatformById(int platformId)
{
return m_Platforms.FirstOrDefault(p => p.PlatformID == platformId);
}
} }
} }

@ -1,6 +1,7 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using Unity.BossRoom.Gameplay.GameplayObjects.Character; using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using Unity.Multiplayer.Samples.BossRoom;
using Unity.Netcode; using Unity.Netcode;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@ -38,4 +39,6 @@ public class SwapConfirmationPanel : MonoBehaviour
} }
confirmationPanel.SetActive(false); confirmationPanel.SetActive(false);
} }
} }

@ -529,7 +529,7 @@ namespace Unity.BossRoom.Gameplay.UserInput
if (Input.GetMouseButtonDown(0)) // Left-click to request swap if (Input.GetMouseButtonDown(0)) // Left-click to request swap
{ {
var ray = m_MainCamera.ScreenPointToRay(UnityEngine.Input.mousePosition); var ray = m_MainCamera.ScreenPointToRay(UnityEngine.Input.mousePosition);
int hits = Physics.RaycastNonAlloc(ray, k_CachedHit, k_MouseInputRaycastDistance, m_PlatformLayerMask); int hits = Physics.RaycastNonAlloc(ray, k_CachedHit, k_MouseInputRaycastDistance);
if (hits > 0) if (hits > 0)
{ {
for (int i = 0; i < hits; i++) for (int i = 0; i < hits; i++)

Loading…
Cancel
Save