mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 03:14:47 +00:00
150 lines
3.7 KiB
C#
150 lines
3.7 KiB
C#
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 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 void OnInit(IJNContext context,ulong 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()
|
|
{}
|
|
}
|
|
|
|
} |