This commit is contained in:
CortexCore
2023-06-05 16:25:06 +08:00
parent 9027120bb8
commit 4565ff2e35
2947 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
{
"name": "BITKits.Extensions.Netcode",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [
"NetCode"
],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,70 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.VisualScripting;
using UnityEngine.Events;
using System.Linq;
using Unity.Netcode;
namespace BITKit.Entities
{
public class EntityNet : NetworkBehaviour, IEntity
{
GenericEvent genericEvent = new();
Processor processor = new();
public IEntityComponent[] entityComponents { get; set; }
protected virtual void Awake()
{
entityComponents = GetComponentsInChildren<IEntityComponent>(true);
entityComponents.ForEach(x => x.Initialize(this));
}
protected virtual void Start()
{
entityComponents.ForEach(x => x.OnAwake());
entityComponents.ForEach(x => x.OnStart());
}
public override void OnDestroy()
{
base.OnDestroy();
entityComponents.ForEach(x => x.OnDestroyComponent());
}
protected virtual void Update()
{
entityComponents.ForEach(x => x.OnUpdate(Time.deltaTime));
}
protected virtual void FixedUpdate()
{
entityComponents.ForEach(x => x.OnFixedUpdate(Time.fixedDeltaTime));
}
protected virtual void LateUpdate()
{
entityComponents.ForEach(x => x.OnLateUpdate(Time.deltaTime));
}
public override void OnNetworkSpawn()
{
Set<bool>(nameof(EntityComponent.isLocalPlayer), IsLocalPlayer);
IEntity.LocalPlayer = this;
entityComponents.ForEach(x => x.OnSpawn());
}
public override void OnNetworkDespawn()
{
entityComponents.ForEach(x => x.OnDespawn());
IEntity.LocalPlayer = null;
}
public void AddListener<T>(Action<T> action) => genericEvent.AddListener<T>(action);
public void Invoke<T>(T value) => genericEvent.Invoke<T>(value);
public void RemoveListener<T>(Action<T> action) => genericEvent.RemoveListener<T>(action);
public void AddListener<T>(string key, Action<T> action) => genericEvent.AddListener<T>(key, action);
public void Invoke<T>(string key, T value) => genericEvent.Invoke<T>(key, value);
public void RemoveListener<T>(string key, Action<T> action) => genericEvent.RemoveListener<T>(key, action);
public T Get<T>(string key = default) => genericEvent.Get<T>(key) ?? GetComponent<T>();
public void Set<T>(T value) => genericEvent.Set<T>(value);
public void Set<T>(string key, T value) => genericEvent.Set<T>(key, value);
public T GetContext<T>(T value = default) => processor.GetContext<T>(value);
public void AddProcessor<T>(Func<T, T> func) => processor.AddProcessor<T>(func);
public void RemoveProcessor<T>(Func<T, T> func) => processor.RemoveProcessor<T>(func);
public T GetContext<T>(string key, T value) => processor.GetContext<T>(value);
public void AddProcessor<T>(string key, Func<T, T> func) => processor.AddProcessor<T>(key, func);
public void RemoveProcessor<T>(string key, Func<T, T> func) => processor.RemoveProcessor<T>(key, func);
}
}

View File

@@ -0,0 +1,51 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BITKit;
using Unity.Netcode;
namespace BITKit.Entities
{
public class EntityNetComponent : NetworkBehaviour, IEntityComponent
{
public IEntity entity { get; set; }
public bool isLocalPlayer => IsLocalPlayer;
public virtual void Initialize(IEntity entity)
{
this.entity = entity;
}
public virtual void OnAwake()
{
}
public virtual void OnDestroyComponent()
{
}
public virtual void OnFixedUpdate(float deltaTime)
{
}
public virtual void OnLateUpdate(float deltaTime)
{
}
public virtual void OnSetOverride(bool value)
{
}
public virtual void OnSpawn()
{
}
public virtual void OnDespawn()
{
}
public virtual void OnStart()
{
}
public virtual void OnUpdate(float deltaTime)
{
}
}
}

View File

@@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
using BITKit;
namespace BITKit.Entities
{
public static partial class NetcodeExtensions
{
}
}

View File

@@ -0,0 +1,34 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
using UnityEngine.Events;
namespace BITKit
{
public class NetcodeManager : MonoBehaviour
{
public NetworkManager networkManager;
public UnityEvent onServerStarted = new();
public UnityEvent<ulong> onClientConnectedCallback = new();
public UnityEvent<ulong> onClientDisconnectCallback = new();
void Start()
{
networkManager.OnServerStarted += onServerStarted.Invoke;
networkManager.OnClientConnectedCallback += onClientConnectedCallback.Invoke;
networkManager.OnClientDisconnectCallback += onClientDisconnectCallback.Invoke;
}
public void StartHost()
{
networkManager.StartHost();
}
public void StartClient()
{
networkManager.StartClient();
}
public void Disconnect()
{
}
}
}