This is an ECS archetecture in Game implemented by C++

Related tags

Game ECS
Overview

ECS

This is an implement of the ECS archetecture in Game by C++17

Star us on GitHub — it helps!

ECS

Sun Entity-Component-System

Compiler compatibility

c++17

Tested platforms:

  • Windows 7 : VS2019 16.10.2

Helper Document

ECSMgr 管理所有的实体和系统,通常定义为唯一的全局变量,其模板参数为支持的原型

Archetype 原型,表示组件的组合,一个原型管理着Chunk的数组,Chunk中以SOA形式存储的这些组件。其模板参数为该原型所包含的组件

TEntity 实体,模板参数为其对应的原型,内部数据其实是表示一个句柄。

ArchetypeIter 原型迭代器,可以遍历相同原型的所有实体的,内部实现为一级索引;虽然通过entity也可以访问到组件,但是entity是2级索引,对于遍历,用迭代器效率更高

如果想给entity添加组件,由于本框架采用强类型实现,一旦entity添加组件,其原型就改变了,相当于其类型改变了。 因此统一通过createEntity接口,添加组件等效为删除再创建。详情见Example中的createEntity(std::move(...)

Example

#include <iostream>
#include "ECSMgr.h"

using namespace Sun;

struct AAA {
	int mass;
	AAA() {
		mass = 0;
	}
	AAA(const AAA& other) {
		this->mass = other.mass;
	}
};
int main()
{
	using ArchetypeA = Archetype<int, float, AAA>;
	using ArchetypeB = Archetype<int, float, AAA, char>;
	using ArchetypeC = Archetype<float, AAA>;

	ECSMgr<ArchetypeA, ArchetypeB, ArchetypeC> mgr;
	static_assert(mgr.getArchetypeNum() == 3);

	TEntity<ArchetypeA> ea1 = mgr.createEntity< ArchetypeA>();
	mgr.get<AAA>(ea1).mass = 100;
	TEntity<ArchetypeA> ea2 = mgr.createEntity< ArchetypeA>();
	mgr.get<AAA>(ea2).mass = 100;
	TEntity<ArchetypeA> ea3 = mgr.createEntity< ArchetypeA>();
	mgr.get<AAA>(ea3).mass = 100;

	TEntity<ArchetypeB> eb1 = mgr.createEntity< ArchetypeB>();
	mgr.get<AAA>(eb1).mass = 50;
	TEntity<ArchetypeB> eb2 = mgr.createEntity< ArchetypeB>();
	mgr.get<AAA>(eb2).mass = 50;
	TEntity<ArchetypeB> eb3 = mgr.createEntity< ArchetypeB>();
	mgr.get<AAA>(eb3).mass = 50;

	TEntity<ArchetypeC> ec1 = mgr.createEntity< ArchetypeC>();
	mgr.get<AAA>(ec1).mass = 20;
	TEntity<ArchetypeC> ec2 = mgr.createEntity< ArchetypeC>();
	mgr.get<AAA>(ec2).mass = 20;
	TEntity<ArchetypeC> ec3 = mgr.createEntity< ArchetypeC>();
	mgr.get<AAA>(ec3).mass = 20;

	ArchetypeIter<ArchetypeB> iter = mgr.begin<ArchetypeB>();
	ArchetypeIter<ArchetypeB> iter2 = mgr.end<ArchetypeB>();
	for (iter; iter != iter2; ++iter) {
		iter->get<AAA>().mass -= 50;
	}
	int k = mgr.get<AAA>(eb2).mass;
	assert(mgr.get<AAA>(eb2).mass == 0);

	std::function<void(AAA& aaa, float& f, char c)> func = [](AAA& aaa, float& f, char c) {
		aaa.mass += 50;
	};
	mgr.entity_for_each(func);
	mgr.runAllTasks();
	assert(mgr.get<AAA>(eb2).mass == 50);

	//移动entity(由于强类型,移动等价于删除再创建,但是组件会拷贝过去)
	auto eb4 = mgr.createEntity<ArchetypeB>(std::move(ea2));
	assert(mgr.exist(ea2) == false);
	assert(mgr.get<AAA>(eb4).mass == 100);

    std::cout << "All test passed!\n";
}
You might also like...
Improved version of the X-Ray Engine, the game engine used in the world-famous S.T.A.L.K.E.R. game series by GSC Game World.
Improved version of the X-Ray Engine, the game engine used in the world-famous S.T.A.L.K.E.R. game series by GSC Game World.

OpenXRay OpenXRay is an improved version of the X-Ray Engine, the game engine used in the world-famous S.T.A.L.K.E.R. game series by GSC Game World. S

Stealthy way to hijack the existing game process handle within the game launcher (currently supports Steam and Battle.net). Achieve external game process read/write with minimum footprint.
Stealthy way to hijack the existing game process handle within the game launcher (currently supports Steam and Battle.net). Achieve external game process read/write with minimum footprint.

Launcher Abuser Stealthy way to hijack the existing game process handle within the game launcher (currently supports Steam and Battle.net). Achieve ex

Game Boy, Game Boy Color, and Game Boy Advanced Emulator
Game Boy, Game Boy Color, and Game Boy Advanced Emulator

SkyEmu SkyEmu is low level cycle accurate GameBoy, GameBoy Color and Game Boy Advance emulator that I have been developing in my spare time. Its prima

A simple Unreal 5 plugin with demo content that shows how audio DSP can be implemented as Metasound, SourceEffect and SubmixEffect
A simple Unreal 5 plugin with demo content that shows how audio DSP can be implemented as Metasound, SourceEffect and SubmixEffect

A simple Unreal 5 plugin with demo content that shows how audio DSP can be implemented as Metasound, SourceEffect and SubmixEffect. The intention is to use it as a template to create more complex audio DSP effects.

A cycle-accurate Game Boy and Game Boy Color Emulator, with rewind feature.
A cycle-accurate Game Boy and Game Boy Color Emulator, with rewind feature.

Azayaka is a free and open-source Game Boy and Game Boy Color emulator written in C++. Features Cycle-Accurate emulation. Console based Debugg

The Game Boy ROM of the Game Boy bitcoin miner!

game-boy-bitcoin-miner The Game Boy ROM of the Game Boy bitcoin miner! To build this, currently this patch needs to be applied to GBDK: https://gist.g

CLUSEK-RT is a complex game engine written in C++ and the successor of the CLUSEK game engine
CLUSEK-RT is a complex game engine written in C++ and the successor of the CLUSEK game engine

CLUSEK-RT is a complex game engine written in C++ and the successor of the CLUSEK game engine. This engine has been designed with a cross-platform design in mind. Thanks to Vulkan API it delivers a next-gen experience with ray tracing to both Linux and Windows platforms

Ground Engine is an easy to use Game Engine for 3D Game Development written in C++
Ground Engine is an easy to use Game Engine for 3D Game Development written in C++

Ground Engine is an easy to use Game Engine Framework for 3D Game Development written in C++. It's currently under development and its creation will b

Minetest is an open source voxel game engine with easy modding and game creation

Minetest is an open source voxel game engine with easy modding and game creation

Owner
yao tang
yao tang
Gaming meets modern C++ - a fast and reliable entity component system (ECS) and much more

EnTT is a header-only, tiny and easy to use library for game programming and much more written in modern C++. Among others, it's used in Minecraft by

Michele Caini 7.6k Jan 4, 2023
A fast entity component system (ECS) for C & C++

Flecs is a fast and lightweight Entity Component System with a focus on high performance game development (join the Discord!). Highlights of the frame

Sander Mertens 3.5k Jan 8, 2023
Godex is a Godot Engine ECS library.

Godex Godex is a Godot Engine ecs library. Disclaimer: this module is still in development, open an issues to report any problem or a new discussion i

GodotECS 767 Jan 9, 2023
A lightweight but complete ECS implementation for modern C++.

ECS A lightweight but complete ECS implementation for modern C++. Features Cache friendly design implemented on top of an EnTT-like sparse set. Clean,

null 7 Sep 3, 2022
Gaming meets modern C++ - a fast and reliable entity-component system (ECS) and much more

EnTT is a header-only, tiny and easy to use entity-component system (and much more) written in modern C++. Among others, it's also used in Minecraft by Mojang and The Forge by Confetti.

Sean Middleditch 8 Feb 16, 2021
Conway's Game of Life implemented in C++

Conway's Game of Life A brute force implementation in C++ What's this? Conway's Game of Life is a zero-player game defined by a state of cells either

Ishan Dubey 4 Dec 27, 2021
Data Structures concepts being implemented to build the Game of Life

The Game of Life Data Structures concepts being implemented to build the Game of Life which is a cellular automation devised by the mathematician Jame

Aleezeh Usman 3 Sep 5, 2021
An implemented GUI of Conway's Game of Life created using QT Creator.

Conways-Game-of-Life This is my newly implemented GUI of Conway's Game of Life created using QT Creator! Interactions: -The 'Step' button allows you t

null 1 Nov 16, 2021
Do you have what it takes? - 2-bit Dungeon Escape game implemented on the Arduino Platform

This game was created as part of the Introduction to Robotics course I took during my 3rd year of studying Computer Science @ University of Bucharest,

Nicoleta Ciausu 6 Feb 28, 2022
Lisp-in-life - A Lisp interpreter implemented in Conway's Game of Life

Lisp in Conway's Game of Life Lisp in Life is a Lisp interpreter implemented in Conway's Game of Life. The entire pattern is viewable on the browser h

Hikaru Ikuta 399 Dec 17, 2022