Progress character customization

This commit is contained in:
Lauchstange committed 2025-01-30 21:02:41 +01:00
1 parent bf545f3baf
commit 5708c0bbbd
20 files changed
+327 -191

No files matched your search

@@ -11,7 +11,6 @@ namespace Client
{
private EntityQuery _pendingNetworkIdQuery;
[BurstCompile]
public void OnCreate(ref SystemState state)
{
//Get all entities connected to the server (having networkID), but not in game yet (no NetworkStreamInGame)
@@ -23,42 +22,36 @@ namespace Client
state.RequireForUpdate<ClientTeamRequest>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
//Get Requested team and all network ids that want to join
var teamrequest = SystemAPI.GetSingleton<ClientTeamRequest>();
var ecb = new EntityCommandBuffer(Allocator.Temp);
var requestedTeam = SystemAPI.GetSingleton<ClientTeamRequest>().Value;
var requestedColor = SystemAPI.GetSingleton<ClientTeamRequest>().Color;
var pendingNetworkIds = _pendingNetworkIdQuery.ToEntityArray(Allocator.Temp);
foreach (var pendingNetworkId in pendingNetworkIds)
{
//mark network ID as joined in game - it will receive Snapshots
ecb.AddComponent<NetworkStreamInGame>(pendingNetworkId);
//create entity that holds request
var requestTeamEntity = ecb.CreateEntity();
//Give entity Rpc command request and team request so that the server can handle it
ecb.AddComponent(requestTeamEntity, new TdTeamRequest
{
Value = requestedTeam,
Color = requestedColor
Value = teamrequest.Value,
Color = teamrequest.Color
});
ecb.AddComponent(requestTeamEntity, new SendRpcCommandRequest()
{
TargetConnection = pendingNetworkId
});
//Broadcast Rpc
ecb.Playback(state.EntityManager);
}
}
[BurstCompile]
public void OnDestroy(ref SystemState state)
{
//Broadcast Rpc
ecb.Playback(state.EntityManager);
}
}
}
+2 -2
View File
@@ -6,7 +6,7 @@ namespace Client
{
public struct ClientTeamRequest : IComponentData
{
public TeamType Value;
public Color Color;
public TeamType Value;
public Color Color;
}
}
@@ -1,4 +1,6 @@
using System.Linq.Expressions;
using Unity.Entities;
using Unity.Rendering;
using UnityEngine;
namespace Common
@@ -13,6 +15,8 @@ namespace Common
AddComponent<CharacterTag>(entity);
AddComponent<NewCharacterTag>(entity);
AddComponent<TdTeam>(entity);
AddComponent<CharacterConfig>(entity);
}
}
}
+6 -6
View File
@@ -1,16 +1,16 @@
using Unity.Entities;
using Unity.NetCode;
using UnityEngine;
namespace Common
{
public struct CharacterTag : IComponentData
{
}
public struct CharacterTag : IComponentData { }
public struct NewCharacterTag : IComponentData
public struct NewCharacterTag : IComponentData { }
public struct CharacterConfig : IComponentData
{
[GhostField] public Color Color;
}
public struct TdTeam : IComponentData
@@ -1,23 +1,43 @@
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Rendering;
using UnityEngine;
using UnityEngine.Rendering;
partial struct InitializeCharacterSystem : ISystem
namespace Common
{
[BurstCompile]
public void OnCreate(ref SystemState state)
[UpdateInGroup(typeof(SimulationSystemGroup), OrderFirst = true)]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation | WorldSystemFilterFlags.ClientSimulation)]
public partial struct InitializeCharacterSystem : ISystem
{
}
public void OnCreate(ref SystemState state)
{
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
}
public void OnUpdate(ref SystemState state)
{
var ecb = new EntityCommandBuffer(Allocator.Temp);
foreach (var (physicsMass, config, newCharacterEntity) in SystemAPI.Query<RefRW<PhysicsMass>, RefRO<CharacterConfig>>().WithAny<NewCharacterTag>().WithEntityAccess())
{
physicsMass.ValueRW.InverseInertia[0] = 0;
physicsMass.ValueRW.InverseInertia[1] = 0;
physicsMass.ValueRW.InverseInertia[2] = 0;
[BurstCompile]
public void OnDestroy(ref SystemState state)
{
var requestedColor = config.ValueRO.Color;
var color = new float4(requestedColor.r, requestedColor.g, requestedColor.b, requestedColor.a);
ecb.AddComponent(newCharacterEntity, new URPMaterialPropertyBaseColor{Value = color});
ecb.RemoveComponent<NewCharacterTag>(newCharacterEntity);
}
ecb.Playback(state.EntityManager);
}
public void OnDestroy(ref SystemState state)
{
}
}
}
+1 -1
View File
@@ -3,7 +3,7 @@ using UnityEngine;
namespace Common
{
class TDPrefabsAuthoring : MonoBehaviour
public class TDPrefabsAuthoring : MonoBehaviour
{
// public GameObject PrefabController;
public GameObject PrefabCharacter;
@@ -1,5 +1,6 @@
using Common;
using RpcComponents;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
@@ -15,14 +16,15 @@ namespace Server
{
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<TdPrefabs>();
var builder = new EntityQueryBuilder(Allocator.Temp).WithAll<TdTeamRequest, ReceiveRpcCommandRequest>();
state.RequireForUpdate(state.GetEntityQuery(builder));
state.RequireForUpdate<TdPrefabs>();
}
public void OnUpdate(ref SystemState state)
{
var ecb = new EntityCommandBuffer(Allocator.Temp);
var ecb = new EntityCommandBuffer(Allocator.Temp);
var characterPrefab = SystemAPI.GetSingleton<TdPrefabs>().Character;
foreach (var (teamRequest, requestSource, requestEntity) in SystemAPI.Query<TdTeamRequest, ReceiveRpcCommandRequest>().WithEntityAccess())
@@ -35,20 +37,37 @@ namespace Server
var requestedTeam = teamRequest.Value;
var requestedColor = teamRequest.Color;
var clientId = SystemAPI.GetComponent<NetworkId>(requestSource.SourceConnection).Value;
Debug.Log($"Server is assigning ClientId {clientId} to team {requestedTeam}");
float3 spawnPosition = float3.zero;
var newCharacter = ecb.Instantiate(characterPrefab);
var spawnPosition = new float3(0f, 1f, 0f);
var newTransform = LocalTransform.FromPosition(spawnPosition);
var clientId = SystemAPI.GetComponent<NetworkId>(requestSource.SourceConnection).Value;
switch (clientId)
{
case 1:
spawnPosition = new float3(0f, 1f, 0f);
break;
case 2:
spawnPosition = new float3(2f, 1f, 0f);
break;
}
ecb.SetComponent(newCharacter, newTransform);
Debug.Log($"Server is assigning ClientId {clientId} to team {requestedTeam}");
var newCharacter = ecb.Instantiate(characterPrefab);
ecb.SetName(newCharacter, "Character");
var newTransform = LocalTransform.FromPosition(spawnPosition);
ecb.SetComponent(newCharacter, newTransform);
ecb.SetComponent(newCharacter, new TdTeam { Value = requestedTeam });
ecb.AddComponent(newCharacter, new GhostOwner { NetworkId = clientId});
ecb.SetComponent(newCharacter, new GhostOwner { NetworkId = clientId});
ecb.SetComponent(newCharacter, new TdTeam { Value = requestedTeam });
ecb.SetComponent(newCharacter, new CharacterConfig
{
Color = requestedColor
});
//Destroy character prefab, when client disconnects
ecb.AppendToBuffer(requestSource.SourceConnection, new LinkedEntityGroup{Value = newCharacter});
}
+7 -5
View File
@@ -100,6 +100,7 @@ namespace UI
}
}
private void StartServer()
{
//Create server world
@@ -108,13 +109,12 @@ namespace UI
//Define endpoint for world to listen to, and set network driver to listen to specified port
var serverEndPoint = NetworkEndpoint.AnyIpv4.WithPort(Port);
{
using var networkDriverQuery =
serverWorld.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
using var networkDriverQuery = serverWorld.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
networkDriverQuery.GetSingletonRW<NetworkStreamDriver>().ValueRW.Listen(serverEndPoint);
}
}
private void StartClient()
{
//Create server world
@@ -131,10 +131,12 @@ namespace UI
//create entity with join request component, so that the ClientRequestGameEntrySystem can take over
var teamRequestEntity = clientWorld.EntityManager.CreateEntity();
var color = _colorPicker.Color;
clientWorld.EntityManager.AddComponentData(teamRequestEntity, new ClientTeamRequest
{
Value = TeamType.Player,
Color = _colorPicker.Color
Value = TeamType.Player,
Color = color,
});
}
}