Upload project
This commit is contained in:
1 parent
6bbeab16d1
commit
bf545f3baf
371 files changed
+91095
No files matched your search
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8792f172af35d6742a4215f9476eba5b
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,64 @@
|
||||
using RpcComponents;
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using Unity.NetCode;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
|
||||
partial struct ClientRequestGameEntrySystem : ISystem
|
||||
{
|
||||
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)
|
||||
var builder = new EntityQueryBuilder(Allocator.Temp).WithAll<NetworkId>().WithNone<NetworkStreamInGame>();
|
||||
_pendingNetworkIdQuery = state.GetEntityQuery(builder);
|
||||
|
||||
//only run system when entities with these components exist
|
||||
state.RequireForUpdate(_pendingNetworkIdQuery);
|
||||
state.RequireForUpdate<ClientTeamRequest>();
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
public void OnUpdate(ref SystemState state)
|
||||
{
|
||||
//Get Requested team and all network ids that want to join
|
||||
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);
|
||||
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
|
||||
});
|
||||
|
||||
ecb.AddComponent(requestTeamEntity, new SendRpcCommandRequest()
|
||||
{
|
||||
TargetConnection = pendingNetworkId
|
||||
});
|
||||
|
||||
//Broadcast Rpc
|
||||
ecb.Playback(state.EntityManager);
|
||||
}
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
public void OnDestroy(ref SystemState state)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b0d42479451fa44dba6c499faf3ad02
|
||||
@@ -0,0 +1,12 @@
|
||||
using Common;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
public struct ClientTeamRequest : IComponentData
|
||||
{
|
||||
public TeamType Value;
|
||||
public Color Color;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 02d9a6f5146a3da409cad66253e5af62
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e1de4271d5744db2ac4a8b9b2f551d07
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,20 @@
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
class CharacterAuthoring : MonoBehaviour
|
||||
{
|
||||
public class CharacterAuthoringBaker : Baker<CharacterAuthoring>
|
||||
{
|
||||
public override void Bake(CharacterAuthoring authoring)
|
||||
{
|
||||
var entity = GetEntity(TransformUsageFlags.Dynamic);
|
||||
AddComponent<CharacterTag>(entity);
|
||||
AddComponent<NewCharacterTag>(entity);
|
||||
AddComponent<TdTeam>(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 544609009a6605d44a92eca2ad0145ad
|
||||
@@ -0,0 +1,20 @@
|
||||
using Unity.Entities;
|
||||
using Unity.NetCode;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
public struct CharacterTag : IComponentData
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public struct NewCharacterTag : IComponentData
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public struct TdTeam : IComponentData
|
||||
{
|
||||
[GhostField] public TeamType Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5f6fcd509124404f90aa1251fdc5c9f9
|
||||
timeCreated: 1738175723
|
||||
@@ -0,0 +1,23 @@
|
||||
using Unity.Burst;
|
||||
using Unity.Entities;
|
||||
|
||||
partial struct InitializeCharacterSystem : ISystem
|
||||
{
|
||||
[BurstCompile]
|
||||
public void OnCreate(ref SystemState state)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
public void OnUpdate(ref SystemState state)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
public void OnDestroy(ref SystemState state)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 85635081d8036b3449c6c39f27ba3734
|
||||
@@ -0,0 +1,26 @@
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
class TDPrefabsAuthoring : MonoBehaviour
|
||||
{
|
||||
// public GameObject PrefabController;
|
||||
public GameObject PrefabCharacter;
|
||||
|
||||
public class TdPrefabsAuthoringBaker : Baker<TDPrefabsAuthoring>
|
||||
{
|
||||
public override void Bake(TDPrefabsAuthoring authoring)
|
||||
{
|
||||
var entity = GetEntity(TransformUsageFlags.None);
|
||||
AddComponent(entity, new TdPrefabs()
|
||||
{
|
||||
Character = GetEntity(authoring.PrefabCharacter, TransformUsageFlags.Dynamic),
|
||||
// Controller = GetEntity(authoring.PrefabController, TransformUsageFlags.Dynamic)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c277d7b8d296f0418bcdc6075f1e394
|
||||
@@ -0,0 +1,10 @@
|
||||
using Unity.Entities;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
public struct TdPrefabs : IComponentData
|
||||
{
|
||||
public Entity Character;
|
||||
// public Entity Controller;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: df870d4496392d54886c4c0f0751c272
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Common
|
||||
{
|
||||
public enum TeamType : byte
|
||||
{
|
||||
None = 0,
|
||||
Player = 1,
|
||||
Enemy = 2,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: efb76c252d0141d69c139984b61ab289
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 86b8029522a13a347a654b5cf536c63a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,28 @@
|
||||
#if UNITY_EDITOR
|
||||
using Unity.Entities;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Helpers
|
||||
{
|
||||
partial class LoadConnectionSceneSystem : SystemBase
|
||||
{
|
||||
protected override void OnCreate()
|
||||
{
|
||||
Enabled = false;
|
||||
|
||||
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByBuildIndex(0))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SceneManager.LoadScene(0);
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f4d973db9670e0f42907e508bebd2f0d
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 18006239b7b76544eb3fd383fb31a3e6
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,20 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Rotating : MonoBehaviour
|
||||
{
|
||||
private float _rotationSpeed = 20f;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
var rotation = transform.rotation;
|
||||
rotation *= Quaternion.Euler(0, _rotationSpeed * Time.deltaTime, 0);
|
||||
transform.rotation = rotation;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 81cbba94ac85d184a95e7d5b5d71b965
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 33a5dc4ccb349d545bd075e6b3ccd9c7
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,12 @@
|
||||
using Common;
|
||||
using Unity.NetCode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RpcComponents
|
||||
{
|
||||
public struct TdTeamRequest : IRpcCommand
|
||||
{
|
||||
public TeamType Value;
|
||||
public Color Color;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6e93550397832f4418b37fb7734936f0
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a9157def7ac4006ba00429f1635844b
|
||||
timeCreated: 1738084609
|
||||
@@ -0,0 +1,59 @@
|
||||
using Common;
|
||||
using RpcComponents;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using Unity.Mathematics;
|
||||
using Unity.NetCode;
|
||||
using Unity.Rendering;
|
||||
using Unity.Transforms;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
||||
public partial struct ServerProcessGameEntryRequestSystem : ISystem
|
||||
{
|
||||
public void OnCreate(ref SystemState state)
|
||||
{
|
||||
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 characterPrefab = SystemAPI.GetSingleton<TdPrefabs>().Character;
|
||||
|
||||
foreach (var (teamRequest, requestSource, requestEntity) in SystemAPI.Query<TdTeamRequest, ReceiveRpcCommandRequest>().WithEntityAccess())
|
||||
{
|
||||
//destroy request entity that only holds request
|
||||
ecb.DestroyEntity(requestEntity);
|
||||
|
||||
//sync server connection entity with client connection entity
|
||||
ecb.AddComponent<NetworkStreamInGame>(requestSource.SourceConnection);
|
||||
|
||||
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}");
|
||||
|
||||
var newCharacter = ecb.Instantiate(characterPrefab);
|
||||
var spawnPosition = new float3(0f, 1f, 0f);
|
||||
var newTransform = LocalTransform.FromPosition(spawnPosition);
|
||||
|
||||
ecb.SetComponent(newCharacter, newTransform);
|
||||
ecb.SetName(newCharacter, "Character");
|
||||
|
||||
ecb.SetComponent(newCharacter, new TdTeam { Value = requestedTeam });
|
||||
ecb.AddComponent(newCharacter, new GhostOwner { NetworkId = clientId});
|
||||
|
||||
//Destroy character prefab, when client disconnects
|
||||
ecb.AppendToBuffer(requestSource.SourceConnection, new LinkedEntityGroup{Value = newCharacter});
|
||||
}
|
||||
|
||||
ecb.Playback(state.EntityManager);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e714f5d70bd94d9294b3324c62136b64
|
||||
timeCreated: 1738084632
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10b929f7bf13b884f8145addf7238245
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,141 @@
|
||||
using Client;
|
||||
using Common;
|
||||
using TMPro;
|
||||
using Unity.Entities;
|
||||
using Unity.NetCode;
|
||||
using Unity.Networking.Transport;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class ClientConnectionManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private TMP_InputField _addressField;
|
||||
[SerializeField] private TMP_InputField _portField;
|
||||
[SerializeField] private TMP_Dropdown _connectionModeDropdown;
|
||||
[SerializeField] private Button _connectButton;
|
||||
|
||||
[SerializeField] private ColorPicker _colorPicker;
|
||||
|
||||
private ushort Port => ushort.Parse(_portField.text);
|
||||
private string Address => _addressField.text;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_addressField.text = "127.0.0.1";
|
||||
_portField.text = "7979";
|
||||
|
||||
_connectionModeDropdown.onValueChanged.AddListener(OnConnectionModeChanged);
|
||||
_connectButton.onClick.AddListener(OnButtonConnect);
|
||||
OnConnectionModeChanged(_connectionModeDropdown.value);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
_connectionModeDropdown.onValueChanged.RemoveAllListeners();
|
||||
_connectButton.onClick.RemoveAllListeners();
|
||||
}
|
||||
|
||||
private void OnConnectionModeChanged(int connectionMode)
|
||||
{
|
||||
string buttonLabel;
|
||||
_connectButton.enabled = true;
|
||||
|
||||
switch (connectionMode)
|
||||
{
|
||||
case 0:
|
||||
buttonLabel = "Start Host";
|
||||
break;
|
||||
case 1 :
|
||||
buttonLabel = "Start Server";
|
||||
break;
|
||||
case 2:
|
||||
buttonLabel = "Start Client";
|
||||
break;
|
||||
default:
|
||||
buttonLabel = "<ERROR>";
|
||||
_connectButton.enabled = false;
|
||||
break;
|
||||
}
|
||||
|
||||
var buttonText = _connectButton.GetComponentInChildren<TextMeshProUGUI>();
|
||||
buttonText.text = buttonLabel;
|
||||
}
|
||||
|
||||
private void OnButtonConnect()
|
||||
{
|
||||
DestroyLocalSimulationWorld();
|
||||
SceneManager.LoadScene(1);
|
||||
|
||||
switch (_connectionModeDropdown.value)
|
||||
{
|
||||
case 0:
|
||||
StartServer();
|
||||
StartClient();
|
||||
break;
|
||||
case 1:
|
||||
StartServer();
|
||||
break;
|
||||
case 2:
|
||||
StartClient();
|
||||
break;
|
||||
default:
|
||||
Debug.LogError("Error: Unknown connection mode", gameObject);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void DestroyLocalSimulationWorld()
|
||||
{
|
||||
//Destroy default world, we will create a new one
|
||||
foreach (var world in World.All)
|
||||
{
|
||||
if (world.Flags == WorldFlags.Game)
|
||||
{
|
||||
world.Dispose();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void StartServer()
|
||||
{
|
||||
//Create server world
|
||||
var serverWorld = ClientServerBootstrap.CreateServerWorld("TD Server World");
|
||||
|
||||
//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>());
|
||||
|
||||
networkDriverQuery.GetSingletonRW<NetworkStreamDriver>().ValueRW.Listen(serverEndPoint);
|
||||
}
|
||||
}
|
||||
|
||||
private void StartClient()
|
||||
{
|
||||
//Create server world
|
||||
var clientWorld = ClientServerBootstrap.CreateClientWorld("TD Client World");
|
||||
|
||||
//Set IP adress and port for server to host on
|
||||
var connectionEndpoint = NetworkEndpoint.Parse(Address, Port);
|
||||
{
|
||||
using var networkDriverQuery = clientWorld.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
|
||||
networkDriverQuery.GetSingletonRW<NetworkStreamDriver>().ValueRW.Connect(clientWorld.EntityManager, connectionEndpoint);
|
||||
}
|
||||
|
||||
World.DefaultGameObjectInjectionWorld = clientWorld;
|
||||
|
||||
//create entity with join request component, so that the ClientRequestGameEntrySystem can take over
|
||||
var teamRequestEntity = clientWorld.EntityManager.CreateEntity();
|
||||
clientWorld.EntityManager.AddComponentData(teamRequestEntity, new ClientTeamRequest
|
||||
{
|
||||
Value = TeamType.Player,
|
||||
Color = _colorPicker.Color
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dd0b0c7ce7b8fc643af547eca631b1d4
|
||||
@@ -0,0 +1,63 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class ColorPicker : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Renderer previewRenderer;
|
||||
|
||||
[SerializeField] private Button buttonNext;
|
||||
[SerializeField] private Button buttonPrev;
|
||||
[SerializeField] private Image previewImage;
|
||||
|
||||
private int _currentIndex = 0;
|
||||
private Color _currentColor;
|
||||
private Color[] _colors = new Color[] { Color.green, Color.magenta, Color.yellow, Color.cyan };
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
|
||||
public Color Color => _currentColor;
|
||||
|
||||
void Start()
|
||||
{
|
||||
_currentColor = _colors[_currentIndex];
|
||||
buttonNext.onClick.AddListener(NextColor);
|
||||
buttonPrev.onClick.AddListener(PrevColor);
|
||||
|
||||
var newMaterial = new Material(previewRenderer.sharedMaterial);
|
||||
newMaterial.color = _currentColor;
|
||||
previewRenderer.sharedMaterial = newMaterial;
|
||||
|
||||
UpdateMaterialColor();
|
||||
}
|
||||
|
||||
private void UpdateMaterialColor()
|
||||
{
|
||||
_currentColor = _colors[_currentIndex];
|
||||
previewImage.color = _currentColor;
|
||||
previewRenderer.sharedMaterial.color = _currentColor;
|
||||
}
|
||||
|
||||
private void PrevColor()
|
||||
{
|
||||
_currentIndex--;
|
||||
if (_currentIndex < 0)
|
||||
{
|
||||
_currentIndex = _colors.Length - 1;
|
||||
}
|
||||
|
||||
UpdateMaterialColor();
|
||||
}
|
||||
|
||||
private void NextColor()
|
||||
{
|
||||
_currentIndex++;
|
||||
if (_currentIndex == _colors.Length)
|
||||
{
|
||||
_currentIndex = 0;
|
||||
}
|
||||
|
||||
UpdateMaterialColor();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fb59c4864abd9254aa540ee9f2230239
|
||||
Reference in new issue
Block a user