一.ECS

     E:Entity-游戏世界中的人,房子等实际物体,这些物体可能由不同的MetaMesh,ParticleSys组成

     C:Component-组成实际物体的MetaMesh,ParticleSys,也可以是一个实际物体

     S:System-游戏引擎,负责完成实际物体的初始化,内存管理,帧同步,线程同步等核心功能

二.ECS内存布局

     1.创建GameSystem,维护Entities列表

public class GameSystem
{
    public List<GameEntity> entities = new List<GameEntity>;
}

     2.创建GameSystem引擎回调接口,帧渲染回调,实例创建回调,物理检测回调等

public abstract class GameSystemCallback
{
     public virtual void OnEntityInit();

     public virtual void OnGameSystemTick();

     public virtual void OnEntityDeInit();
}


int main()
{
   while(true)
   {

       for(GameEntity entity: entities)
       {
          #FPS游戏帧同步回调
          GameEntityComponent scriptComponent = entity.getScript()
          scriptComponent.OnGameSystemTick();
       }
   }
}

     3.创建实际的Entity,可能是一个人

public class Agent: GameEntity
{
    public int age;

    public int health;

    public int height;
}

     4.创建对应Entity下的Component,人具备技能,人具备盔甲,人具备AI三种不同的Component

public SkillComponent : GameEntityComponent
{

   public override void OnFrameTick()
   {
       //释放技能按键,业务逻辑
   }
}

public AIComponent : GameEntityComponent
{

   public override void OnFrameTick()
   {
       //人物AI行为
   }
}

public ArmorComponent : GameEntityComponent
{

   public override void OnFrameTick()
   {
       //人物盔甲挂载和卸下
   }
}

     5.将三种不同的Component添加至Entity中,检测人是否具备了技能,盔甲等特性

Agent agent = new GameEntity();
agent.addComponent(skillComponent);
agent.addComponent(armorComponent);
agent.addComponent(aiComponent);

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐