mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-12-08 05:49:08 +00:00
提交Unity 联机Pro
This commit is contained in:
150
JNFrame2/Assets/JNGame/Sync/Entity/JNEntity.cs
Normal file
150
JNFrame2/Assets/JNGame/Sync/Entity/JNEntity.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using DotRecast.Core.Collections;
|
||||
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 long _id;
|
||||
public long 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 void OnInit(IJNContext context,long id = 0)
|
||||
{
|
||||
Context = context;
|
||||
_id = id;
|
||||
if (_id <= 0)
|
||||
{
|
||||
_id = GetSystem<JNRandomSystem>().NextId();
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
GetComponents().ForEach(child => (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()
|
||||
{
|
||||
OnSyncDestroy();
|
||||
RemoveAllComponents();
|
||||
base.Destroy();
|
||||
}
|
||||
|
||||
//生命周期
|
||||
public virtual void OnSyncStart(){}
|
||||
|
||||
public virtual void OnSyncUpdate()
|
||||
{
|
||||
//给组件推帧
|
||||
foreach (var component in GetComponents())
|
||||
{
|
||||
(component as JNComponent)?.OnSyncUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void OnSyncDestroy()
|
||||
{}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user