提交完美

This commit is contained in:
PC-20230316NUNE\Administrator
2024-10-17 20:36:24 +08:00
parent 0d600a2786
commit b0a2e4a900
1522 changed files with 40000 additions and 15615 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 35611e159b144494beb3985bb0aa884a
timeCreated: 1720700493

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0cc4a62c9f984472a75d9dcce3e117bb
timeCreated: 1720864778

View File

@@ -0,0 +1,19 @@
using Entitas;
using JNGame.Math;
using JNGame.Sync.Entity.Component;
using JNGame.Sync.Frame.Entity.Components;
namespace JNGame.Sync.Frame.Entity.Component.Components
{
public class JNTransformComponent : JNComponent
{
public LVector3 Position = new();
public bool IsRange(LVector3 target,LFloat len)
{
return LVector3.Distance(Position, target) <= len;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: de8c1bdf52c14049b969b7d4ec7052cd
timeCreated: 1720700573

View File

@@ -0,0 +1,30 @@
using Entitas;
using JNGame.Sync.System;
using NotImplementedException = System.NotImplementedException;
namespace JNGame.Sync.Entity.Component
{
/// <summary>
/// 组件
/// </summary>
public class JNComponent : IComponent,IJNSyncCycle
{
public IJNEntity Entity;
public void OnInit(IJNEntity entity)
{
this.Entity = entity;
}
public T GetSystem<T>() where T : SLogicSystem
{
return Entity.GetContext().GetSync().GetSystem<T>();
}
//生命周期
public virtual void OnSyncStart(){}
public virtual void OnSyncUpdate(int dt){}
public virtual void OnSyncDestroy(){}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1d7ede39ee1a4ac6a40ea7e0b5334765
timeCreated: 1720864842

View File

@@ -0,0 +1,34 @@
using JNGame.Runtime.Entitas;
using JNGame.Sync.Frame.Entity.Component.Components;
using System;
using Entitas;
using JNGame.Runtime.Util.Types;
namespace JNGame.Sync.Frame.Entity.Components
{
public class JNEntityLookup : JNLookup
{
//位置组件
public int Transform { get; set; }
//绑定索引
protected override void BindIndex()
{
Transform = Next();
}
//绑定类型
protected override void BindType(KeyValue<int, Type> types)
{
types.Add(Transform,typeof(JNTransformComponent));
}
//查询组件
public T Query<T>(Entitas.Entity entity) where T : class, IComponent
{
return entity.GetComponent(GetIndex<T>()) as T;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 76bb4b74188d47beb992777c9fc19d8c
timeCreated: 1720749419

View File

@@ -0,0 +1,144 @@
using System;
using System.Collections.Generic;
using Entitas;
using JNGame.Runtime.Sync;
using JNGame.Sync.Entity;
using JNGame.Sync.Frame.Entity.Component.Components;
using JNGame.Sync.Frame.Entity.Components;
using JNGame.Sync.System;
namespace JNGame.Sync.Frame.Entity
{
public abstract class JNContext<T> : Entitas.Context<T>,IJNContext where T : JNEntity, new()
{
public JNSyncService Sync { get; private set; }
public JNEntityLookup CLookup;
//方便查抄的实体Map
public Dictionary<ulong, T> Entities = new ();
public JNContext()
: this((new T()).NewCLookup().Count, () => new T())
{
}
public JNContext(int totalComponents, Func<T> entityFactory) : this(totalComponents, 0, null, null, entityFactory)
{
}
public JNContext(int totalComponents, int startCreationIndex, ContextInfo contextInfo, Func<IEntity, IAERC> aercFactory, Func<T> entityFactory)
: base(totalComponents, startCreationIndex, contextInfo, aercFactory, entityFactory)
{
CLookup = (new T()).NewCLookup();
}
public JNSyncService GetSync()
{
return Sync;
}
public void InitReference(JNSyncService data)
{
Sync = data;
}
public T Query(ulong id)
{
Entities.TryGetValue(id, out var entity);
return entity;
}
public void AddEntity(ulong id, JNEntity entity)
{
Entities[id] = entity as T;
}
public void RemoveEntity(ulong id)
{
Entities.Remove(id);
}
public T GetService<T>() where T : SLogicSystem
{
return Sync.GetSystem<T>();
}
protected T NewEntity()
{
T entity = base.CreateEntity();
//绑定组件查询器
entity.CLookup = CLookup;
return entity;
}
protected virtual T BindInitialize(T entity)
{
entity.OnInit(this);
return entity;
}
protected virtual T BindComponent(T entity)
{
entity.AddComponent<JNTransformComponent>();
return entity;
}
protected virtual T BindLifeCycle(T entity)
{
entity.OnSyncStart();
return entity;
}
public override T CreateEntity()
{
var entity = NewEntity();
BindInitialize(entity);
BindComponent(entity);
BindLifeCycle(entity);
return entity;
}
//生效延迟销毁
public void DelayDestroy()
{
foreach (var child in GetEntities())
{
if (child.IsDelayDestroy)
{
child.Destroy();
}
}
}
//生命周期
public virtual void OnSyncStart(){}
public virtual void OnSyncUpdate(int dt)
{
//给实体推帧
foreach (var entity in GetEntities())
{
if (entity.isEnabled)
{
entity.OnSyncUpdate(dt);
}
}
}
public virtual void OnSyncDestroy()
{}
}
public interface IJNContext : IJNSyncCycle
{
public abstract JNSyncService GetSync();
public abstract void InitReference(JNSyncService data);
public void AddEntity(ulong id,JNEntity entity);
public void RemoveEntity(ulong id);
public void DelayDestroy();
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f3ea713c89fe42fd98c9b84f2ec623f8
timeCreated: 1721187670

View File

@@ -0,0 +1,60 @@
using System;
using Entitas;
using JNGame.Runtime.Sync;
using JNGame.Sync.Frame.Entity;
namespace JNGame.Sync.Entity
{
public class JNContexts : Entitas.IContexts
{
public virtual IContext[] allContexts => Array.Empty<IContext>();
public JNSyncService Sync;
public JNContexts()
{
for (var i = 0; i < allContexts.Length; i++)
{
(allContexts[i] as IJNContext)?.OnSyncStart();
}
}
//给所有实体推帧
public virtual void Simulate()
{
for (var i = 0; i < allContexts.Length; i++)
{
(allContexts[i] as IJNContext)?.OnSyncUpdate(Sync.DeltaTime);
}
//延迟销毁
for (var i = 0; i < allContexts.Length; i++)
{
(allContexts[i] as IJNContext)?.DelayDestroy();
}
}
public T GetContext<T>() where T : IContext
{
for (int i = 0; i < allContexts.Length; i++)
{
if (allContexts[i] is T) return (T)allContexts[i];
}
throw new InvalidOperationException($"No context of type {typeof(T).Name} was found.");
}
//注入 JNSyncFrameService
public void InitReference(JNSyncService data)
{
Sync = data;
foreach (var context in allContexts)
{
(context as IJNContext)?.InitReference(data);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f17062daa45a431a9ff4377391dfa708
timeCreated: 1721187474

View File

@@ -0,0 +1,165 @@
using System;
using JNGame.Math;
using JNGame.Sync.Entity.Component;
using JNGame.Sync.Frame.Entity;
using JNGame.Sync.Frame.Entity.Component.Components;
using JNGame.Sync.Frame.Entity.Components;
using JNGame.Sync.Frame.Service;
using JNGame.Sync.System;
namespace JNGame.Sync.Entity
{
public interface IJNEntity : IJNSyncCycle,IJNSyncId
{
/// <summary>
/// 坐标
/// </summary>
public JNTransformComponent Transform { get; }
/// <summary>
/// 位置
/// </summary>
public LVector3 Position { get; }
public IJNContext GetContext();
public T GetComponent<T>() where T : JNComponent;
}
public abstract class JNEntity : Entitas.Entity,IJNEntity,IComparable
{
private ulong _id;
public ulong Id => _id;
public IJNContext Context { get; private set; }
//组件Lookup
public JNEntityLookup CLookup{ get; set; }
/// <summary>
/// 坐标
/// </summary>
public JNTransformComponent Transform => CLookup.Query<JNTransformComponent>(this);
/// <summary>
/// 位置
/// </summary>
public LVector3 Position => Transform.Position;
public bool IsDelayDestroy { get; private set; } = false;
public virtual void OnInit(IJNContext context,ulong id = 0)
{
Context = context;
_id = id;
if (_id <= 0)
{
_id = GetSystem<JNRandomSystem>().NextId();
}
Context.AddEntity(Id,this);
}
public abstract JNEntityLookup NewCLookup();
public T GetSystem<T>() where T : SLogicSystem
{
return Context.GetSync().GetSystem<T>();
}
/// <summary>
/// 获取组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetComponent<T>() where T : JNComponent
{
return CLookup.Query<T>(this);
}
/// <summary>
/// 添加组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public JNComponent AddComponent<T>() where T : JNComponent,new()
{
int index = CLookup.GetIndex<T>();
var temp = CreateComponent<T>(index);
temp.OnInit(this);
base.AddComponent(index,temp);
temp.OnSyncStart();
return temp;
}
public void RemoveComponent<T>() where T : JNComponent,new()
{
int index = CLookup.GetIndex<T>();
var component = CLookup.Query<T>(this);
component.OnSyncDestroy();
base.RemoveComponent(index);
}
public override void RemoveAllComponents()
{
foreach (var child in GetComponents())
{
(child as JNComponent)?.OnSyncDestroy();
}
base.RemoveAllComponents();
}
//实现排序实现
public int CompareTo(object obj)
{
if (obj is IJNEntity entity)
{
return Id.CompareTo(entity.Id);
}
return 1;
}
public IJNContext GetContext()
{
return Context;
}
public override void Destroy()
{
//清理缓存
Context.RemoveEntity(Id);
OnSyncDestroy();
RemoveAllComponents();
base.Destroy();
}
public void DelayDestroy()
{
IsDelayDestroy = true;
}
//生命周期
public virtual void OnSyncStart(){}
public virtual void OnSyncUpdate(int dt)
{
//给组件推帧
foreach (var component in GetComponents())
{
(component as JNComponent)?.OnSyncUpdate(dt);
}
}
public virtual void OnSyncDestroy()
{
_id = 0;
IsDelayDestroy = false;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a8180ef801954d1e90f4d203c632063e
timeCreated: 1721187721