Added Crow functionality. Not server authenticated yet

main
Hazim Bin Ijaz 1 month ago
parent 955e59bf04
commit 0bd0070054

@ -1075,6 +1075,50 @@ GameObject:
m_CorrespondingSourceObject: {fileID: 5277713031684110771, guid: 523332e67f2466e4f818161850bfb207, type: 3} m_CorrespondingSourceObject: {fileID: 5277713031684110771, guid: 523332e67f2466e4f818161850bfb207, type: 3}
m_PrefabInstance: {fileID: 222782633} m_PrefabInstance: {fileID: 222782633}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
--- !u!1 &396364066
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 396364068}
- component: {fileID: 396364067}
m_Layer: 0
m_Name: CrowManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &396364067
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 396364066}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 501460ae984b4e44d991e4b43e36afee, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &396364068
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 396364066}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &451210155 --- !u!1 &451210155
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -8575,11 +8619,11 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1676734516695783279, guid: 0193228de87741d40a42e561901c9083, type: 3} - target: {fileID: 1676734516695783279, guid: 0193228de87741d40a42e561901c9083, type: 3}
propertyPath: m_LocalRotation.y propertyPath: m_LocalRotation.y
value: 0.30972984 value: 0.3097298
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1676734516695783279, guid: 0193228de87741d40a42e561901c9083, type: 3} - target: {fileID: 1676734516695783279, guid: 0193228de87741d40a42e561901c9083, type: 3}
propertyPath: m_LocalRotation.z propertyPath: m_LocalRotation.z
value: -0.1450697 value: -0.14506969
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1676734516724634599, guid: 0193228de87741d40a42e561901c9083, type: 3} - target: {fileID: 1676734516724634599, guid: 0193228de87741d40a42e561901c9083, type: 3}
propertyPath: m_LocalPosition.z propertyPath: m_LocalPosition.z
@ -10602,3 +10646,4 @@ SceneRoots:
- {fileID: 1716415238} - {fileID: 1716415238}
- {fileID: 1690610726} - {fileID: 1690610726}
- {fileID: 1486746210} - {fileID: 1486746210}
- {fileID: 396364068}

@ -143,7 +143,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!4 &240007729 --- !u!4 &240007729
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -474,7 +474,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!4 &849256359 --- !u!4 &849256359
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -732,7 +732,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!4 &997499840 --- !u!4 &997499840
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1118,7 +1118,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!4 &1463337823 --- !u!4 &1463337823
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1258,7 +1258,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!4 &1620155725 --- !u!4 &1620155725
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

@ -664,6 +664,70 @@ PrefabInstance:
propertyPath: m_Name propertyPath: m_Name
value: NetworkSimulator value: NetworkSimulator
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 5634176411869099529, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: m_PresetReference
value: 2982271614721982465
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982464]
value: Unity.Multiplayer.Tools.NetworkSimulator.Runtime Unity.Multiplayer.Tools.NetworkSimulator.Runtime.NetworkSimulatorPreset
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982465]
value: Unity.Multiplayer.Tools.NetworkSimulator.Runtime Unity.Multiplayer.Tools.NetworkSimulator.Runtime.NetworkSimulatorPreset
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982464].<Name>k__BackingField
value: None
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982465].<Name>k__BackingField
value: None
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982464].<Description>k__BackingField
value:
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982465].<Description>k__BackingField
value:
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982464].<PacketDelayMs>k__BackingField
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982465].<PacketDelayMs>k__BackingField
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982464].<PacketJitterMs>k__BackingField
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982465].<PacketJitterMs>k__BackingField
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982464].<PacketLossPercent>k__BackingField
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982465].<PacketLossPercent>k__BackingField
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982464].<PacketLossInterval>k__BackingField
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6032759632252749615, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: managedReferences[2982271614721982465].<PacketLossInterval>k__BackingField
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6220772510846579177, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3} - target: {fileID: 6220772510846579177, guid: 545b16e53789f354abf0d6515ca4f3e1, type: 3}
propertyPath: m_RootOrder propertyPath: m_RootOrder
value: 14 value: 14

@ -0,0 +1,96 @@
using System.Collections.Generic;
using System.Linq;
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using UnityEngine;
public class CrowManager : MonoBehaviour
{
public static CrowManager Instance { get; private set; }
private List<ServerCharacter> players = new List<ServerCharacter>();
private ServerCharacter currentCrow;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(this);
return;
}
Instance = this;
}
private void Start()
{
// players = FindObjectsOfType<ServerCharacter>().ToList();
}
private void Update()
{
DetermineCrow();
}
private void DetermineCrow()
{
int unoccupiedPlayers = 0;
ServerCharacter potentialCrow = null;
foreach (var player in players)
{
if (!player.IsOnAPlatform)
{
unoccupiedPlayers++;
potentialCrow = player;
}
}
// If exactly one player is not on a platform, assign as crow
if (unoccupiedPlayers == 1)
{
AssignCrow(potentialCrow);
}
else if (unoccupiedPlayers == 0 && currentCrow != null)
{
// All players are on platforms, clear crow
ClearCrow();
}
}
private void AssignCrow(ServerCharacter player)
{
if (currentCrow == player) return; // Already the crow
if (currentCrow != null)
{
// Clear old crow
currentCrow.SetAsCrow(false);
}
currentCrow = player;
currentCrow.SetAsCrow(true);
Debug.Log($"{currentCrow.name} is now the Crow.");
}
private void ClearCrow()
{
if (currentCrow != null)
{
currentCrow.SetAsCrow(false);
Debug.Log($"{currentCrow.name} is no longer the Crow.");
currentCrow = null;
}
}
public void AddCharacterToList(ServerCharacter character)
{
players.Add(character);
}
public void RemoveCharacterFromList(ServerCharacter character)
{
players.Remove(character);
}
}

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 501460ae984b4e44d991e4b43e36afee
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -143,36 +143,10 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character
public ulong? PendingSwapRequest { get; set; } public ulong? PendingSwapRequest { get; set; }
public int? TargetPlatformId { get; private set; } = null; public int? TargetPlatformId { get; private set; } = null;
public bool IsOnAPlatform { get; private set; } = false;
public bool IsCrow { get; private set; } = false;
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()
{ {
@ -199,7 +173,95 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character
} }
} }
//Hazim
public void SetAsCrow(bool status)
{
IsCrow = status;
}
public void SetTargetPlatform(int platformId)
{
TargetPlatformId = platformId;
}
public void ClearTargetPlatform()
{
TargetPlatformId = null;
}
public void OnArrivalOnPlatform()
{
ClearTargetPlatform();
SetOnPlatform(true);
}
public void OnLeavingPlatform()
{
SetOnPlatform(false);
}
public void SetOnPlatform(bool status)
{
IsOnAPlatform = status;
}
// private void OnTriggerEnter(Collider other)
// {
// if (TargetPlatformId == null) return;
//
// if (other.TryGetComponent(out Platform platform) && platform.PlatformID == TargetPlatformId)
// {
// if (platform.IsOccupied && platform.Occupier != this)
// {
// Debug.Log($"Platform {platform.PlatformID} was occupied. Now looking for others");
// // Platform is occupied, handle intervention
// // HandleOccupiedPlatform(platform);
// }
// else
// {
// // Occupy the platform
// Debug.Log($"Platform {platform.PlatformID} is being occupied");
// platform.Occupy(this);
// ClearTargetPlatform();
// }
// }
// }
private void HandleOccupiedPlatform(Platform currentPlatform)
{
var nearestPlatform = PlatformManager.Instance.FindNearestUnoccupiedPlatform(transform.position);
if (nearestPlatform != null)
{
Debug.Log($"Platform {currentPlatform.PlatformID} is occupied. Moving to nearest Platform {nearestPlatform.PlatformID}.");
MoveToPlatform(nearestPlatform);
}
else
{
Debug.Log($"No unoccupied platforms available. Becoming the Crow.");
BecomeCrow();
}
}
private void MoveToPlatform(Platform platform)
{
SetTargetPlatform(platform.PlatformID);
var pltpos = PlatformManager.Instance.GetPlatformPosition(platform.PlatformID);
pltpos.y = 0;
Debug.Log($"Platform position: {pltpos}.");
ServerSendCharacterInputRpc(pltpos);
}
private void BecomeCrow()
{
ClearTargetPlatform();
Debug.Log($"{name} is now the Crow.");
// Add additional logic for Crow role if needed
}
[Rpc(SendTo.Server, RequireOwnership = false)] [Rpc(SendTo.Server, RequireOwnership = false)]
public void NotifySwapRequestRpc(ulong initiatingPlayerId) public void NotifySwapRequestRpc(ulong initiatingPlayerId)
{ {
@ -223,9 +285,7 @@ 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))
{ {
// Call InitiateSwap directly
InitiateSwap(initiatingPlayer, this); InitiateSwap(initiatingPlayer, this);
Debug.Log($"Swap confirmed: {initiatingPlayer.name} and {this.name} are swapping."); Debug.Log($"Swap confirmed: {initiatingPlayer.name} and {this.name} are swapping.");
} }
} }
@ -237,30 +297,32 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character
PendingSwapRequest = null; PendingSwapRequest = null;
} }
public void InitiateSwap(ServerCharacter initiatingPlayer, ServerCharacter targetPlayer) public void InitiateSwap(ServerCharacter initiatingPlayer, ServerCharacter targetPlayer)
{ {
int initiatingPlatformId = PlatformManager.Instance.GetPlatformOccupiedByPlayer(initiatingPlayer).PlatformID; var initiatingPlatform = PlatformManager.Instance.GetPlatformOccupiedByPlayer(initiatingPlayer);
int targetPlatformId = PlatformManager.Instance.GetPlatformOccupiedByPlayer(targetPlayer).PlatformID; var targetPlatform = PlatformManager.Instance.GetPlatformOccupiedByPlayer(targetPlayer);
initiatingPlayer.SetTargetPlatform(targetPlatformId);
targetPlayer.SetTargetPlatform(initiatingPlatformId);
Vector3 initiatingPlayerPosition = initiatingPlayer.physicsWrapper.Transform.position; if (initiatingPlatform == null || targetPlatform == null)
Vector3 targetPlayerPosition = this.physicsWrapper.Transform.position; {
Debug.LogError("One or both players are not on a platform!");
// Execute the swap return;
initiatingPlayer.ServerSendCharacterInputRpc(targetPlayerPosition); }
this.ServerSendCharacterInputRpc(initiatingPlayerPosition); // Use the MoveToPlatform function for movement
initiatingPlayer.MoveToPlatform(targetPlatform);
targetPlayer.MoveToPlatform(initiatingPlatform);
Debug.Log($"Swap initiated: {initiatingPlayer.name} -> Platform {targetPlatformId}, {targetPlayer.name} -> Platform {initiatingPlatformId}."); Debug.Log($"Swap initiated: {initiatingPlayer.name} -> Platform {targetPlatform.PlatformID}, {targetPlayer.name} -> Platform {initiatingPlatform.PlatformID}.");
} }
//Hazim
public override void OnNetworkSpawn() public override void OnNetworkSpawn()
{ {
if (!IsServer) { enabled = false; } if (!IsServer) { enabled = false; }
else else
{ {
CrowManager.Instance.AddCharacterToList(this);
NetLifeState.LifeState.OnValueChanged += OnLifeStateChanged; NetLifeState.LifeState.OnValueChanged += OnLifeStateChanged;
m_DamageReceiver.DamageReceived += ReceiveHP; m_DamageReceiver.DamageReceived += ReceiveHP;
m_DamageReceiver.CollisionEntered += CollisionEntered; m_DamageReceiver.CollisionEntered += CollisionEntered;
@ -282,7 +344,7 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character
public override void OnNetworkDespawn() public override void OnNetworkDespawn()
{ {
NetLifeState.LifeState.OnValueChanged -= OnLifeStateChanged; NetLifeState.LifeState.OnValueChanged -= OnLifeStateChanged;
CrowManager.Instance.RemoveCharacterFromList(this);
if (m_DamageReceiver) if (m_DamageReceiver)
{ {
m_DamageReceiver.DamageReceived -= ReceiveHP; m_DamageReceiver.DamageReceived -= ReceiveHP;

@ -69,7 +69,8 @@ namespace Unity.Multiplayer.Samples.BossRoom
if (!IsOccupied && other.TryGetComponent<ServerCharacter>(out var player)) if (!IsOccupied && other.TryGetComponent<ServerCharacter>(out var player))
{ {
Occupy(player); Occupy(player);
Debug.Log($"{player.name} has entered Platform {name}."); player.OnArrivalOnPlatform();
// Debug.Log($"{player.name} has entered Platform {name}.");
} }
} }
@ -78,6 +79,7 @@ namespace Unity.Multiplayer.Samples.BossRoom
if (IsOccupied && other.TryGetComponent<ServerCharacter>(out var player) && player == Occupier) if (IsOccupied && other.TryGetComponent<ServerCharacter>(out var player) && player == Occupier)
{ {
Vacate(); Vacate();
player.OnLeavingPlatform();
Debug.Log($"{player.name} has exited Platform {name}."); Debug.Log($"{player.name} has exited Platform {name}.");
} }
} }

@ -33,6 +33,20 @@ namespace Unity.Multiplayer.Samples.BossRoom
Debug.Log("All platforms have been assigned unique IDs."); Debug.Log("All platforms have been assigned unique IDs.");
} }
public Platform FindNearestUnoccupiedPlatform(Vector3 position)
{
return m_Platforms
.Where(platform => !platform.IsOccupied)
.OrderBy(platform => Vector3.Distance(position, platform.transform.position))
.FirstOrDefault();
}
public bool AreAllPlatformsOccupied()
{
return m_Platforms.All(platform => platform.IsOccupied);
}
/// <summary> /// <summary>
/// Finds an unoccupied platform and assigns the player to it. /// Finds an unoccupied platform and assigns the player to it.
/// </summary> /// </summary>

Loading…
Cancel
Save