一.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);