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(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(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(Action action) => genericEvent.AddListener(action); public void Invoke(T value) => genericEvent.Invoke(value); public void RemoveListener(Action action) => genericEvent.RemoveListener(action); public void AddListener(string key, Action action) => genericEvent.AddListener(key, action); public void Invoke(string key, T value) => genericEvent.Invoke(key, value); public void RemoveListener(string key, Action action) => genericEvent.RemoveListener(key, action); public T Get(string key = default) => genericEvent.Get(key) ?? GetComponent(); public void Set(T value) => genericEvent.Set(value); public void Set(string key, T value) => genericEvent.Set(key, value); public T GetContext(T value = default) => processor.GetContext(value); public void AddProcessor(Func func) => processor.AddProcessor(func); public void RemoveProcessor(Func func) => processor.RemoveProcessor(func); public T GetContext(string key, T value) => processor.GetContext(value); public void AddProcessor(string key, Func func) => processor.AddProcessor(key, func); public void RemoveProcessor(string key, Func func) => processor.RemoveProcessor(key, func); } }