Skip to content

Commit

Permalink
registSingleton
Browse files Browse the repository at this point in the history
  • Loading branch information
ActivePeter committed Feb 3, 2022
1 parent 1cc666f commit be8f4b0
Show file tree
Hide file tree
Showing 12 changed files with 448 additions and 505 deletions.
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ c++ implementation of ECS (Entity Component System)lib

c++的ecs库 (Entity Component System)

## Table of Contents (内容)

- [Record](./records/record.md)
- [Background](#Background)
- [Features](#Features)
- [Todo](#Todo)

## [Record (记录)](./records/record.md)

## Background (背景)
Expand Down Expand Up @@ -80,8 +73,10 @@ When I'm writing a minecraft like game, I knows that ecs is a way to improve gam
.addEmptyComponent<B>();
```

- **Random Access** Input pointer to access entity by entityID
- **Random Access/随机访问**

Input pointer to access entity by entityID

输入指针然后访问entity对应组件的数据,

```c++
Expand All @@ -97,9 +92,24 @@ When I'm writing a minecraft like game, I knows that ecs is a way to improve gam
cameraPtr->Position = pos;
```

- **Rigist singleton/注册单例资源**

```c++
ecs.scene->registSingleton<ContextId>(
[this](ContextId& cid)
{
cid.id = this->context_id.id;
});
```



## Todo

- [x] easy to add sys and entity

便捷注册 **系统****实体**

- [x] random access

随机访问
Expand All @@ -108,6 +118,10 @@ When I'm writing a minecraft like game, I knows that ecs is a way to improve gam

系统组概念

- [x] Rigist singleton

注册单例资源

- [ ] multiple threads

多线程支持
75 changes: 0 additions & 75 deletions README_CN.md

This file was deleted.

63 changes: 0 additions & 63 deletions example/example.cpp

This file was deleted.

45 changes: 23 additions & 22 deletions src/paecs/Archtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ namespace paecs
//archtype中某个插件在chunk中的offset
int compOffsetInOneChunk;
//archtype中某个插件的描述信息引用
ComponentDiscription *compDiscription;
ComponentDiscription* compDiscription;
// size_t compSize;
CompInfoInArchtype() {}
CompInfoInArchtype(int offset, ComponentDiscription &disc) : compOffsetInOneChunk(offset),
compDiscription(&disc) {}
CompInfoInArchtype(int offset, ComponentDiscription& disc) : compOffsetInOneChunk(offset),
compDiscription(&disc) {}
};

//class Archtype;
Expand All @@ -45,22 +45,22 @@ namespace paecs
public:
// variables /////////////////////
ComponentMask componentMask;
ArchtypeManager &archtypeManager;
ArchtypeManager& archtypeManager;
//////////////////////////////////

Archtype(ArchtypeManager &archtypeManager1, int index1,
const ComponentMask &componentMask1,
std::vector<BaseComponent::Id> componentsIds1,
std::vector<int> componentsOffsets1,
int maxCnt1);
Archtype(ArchtypeManager& archtypeManager1, int index1,
const ComponentMask& componentMask1,
std::vector<BaseComponent::Id> componentsIds1,
std::vector<int> componentsOffsets1,
int maxCnt1);

// : archtypeManager(archtypeManager1) {}
//用于通过插件id直接查找插件信息,也可用于遍历插件信息
// std::unordered_map<BaseComponent::Id, CompInfoInArchtype> compIds2InfoMap;
phmap::flat_hash_map<BaseComponent::Id, CompInfoInArchtype> compIds2InfoMap;

template <typename Comp>
CompInfoInArchtype &getCompInfo()
CompInfoInArchtype& getCompInfo()
{
return compIds2InfoMap[Component<Comp>::getId()];
}
Expand All @@ -81,8 +81,8 @@ namespace paecs
// std::list<Chunk> chunks;
//在创建entity时调用
// void registMemForAnEntity(EntityDataPos &entityDataPos);
void allocateMemForAnEntity(EntityDataPos &entityDataPos);
void deallocateMemForAnEntity(EntityDataPos &entityDataPos);
void allocateMemForAnEntity(EntityDataPos& entityDataPos);
void deallocateMemForAnEntity(EntityDataPos& entityDataPos);
};

// Archtype a;
Expand Down Expand Up @@ -110,35 +110,36 @@ namespace paecs
* @param id 插件id,需要这个id来获取在当前chunk中应该用的size和offset
* @param index entity在chunk中的位置
*/
void cpyComponentDataFromOldDatapos2ChunkIndex_ifContainId(uint8_t *oldDataPos, BaseComponent::Id id, uint32_t index)
void cpyComponentDataFromOldDatapos2ChunkIndex_ifContainId(uint8_t* oldDataPos, BaseComponent::Id id, uint32_t index)
{
if (archtypePtr->compIds2InfoMap.count(id))
{
// auto size = BaseComponent::getDiscriptionOfComponentById(id).componentSize;
auto &compInfo = archtypePtr->compIds2InfoMap[id];
auto &size = compInfo.compDiscription->componentSize;
auto &offset = compInfo.compOffsetInOneChunk;
auto& compInfo = archtypePtr->compIds2InfoMap[id];
auto& size = compInfo.compDiscription->componentSize;
auto& offset = compInfo.compOffsetInOneChunk;
// auto offset = .compOffsetInOneChunk;
// auto size=
memcpy(&storage[offset + index * size], oldDataPos, size);
// storage[offset+]
}
}
template <typename Comp>
Comp &getCompDataOfIndex(int index)
Comp& getCompDataOfIndex(int index)
{
CompInfoInArchtype &info = this->archtypePtr->getCompInfo<Comp>();
CompInfoInArchtype& info = this->archtypePtr->getCompInfo<Comp>();
assert(&info);
return
// (Comp &)
*((Comp *)&(storage[info.compOffsetInOneChunk + info.compDiscription->componentSize * index]));
*((Comp*)&(storage[info.compOffsetInOneChunk + info.compDiscription->componentSize * index]));
}
template <typename Comp>
Comp *getCompDataPtrOfIndex(int index)
Comp* getCompDataPtrOfIndex(int index)
{
CompInfoInArchtype &info = this->archtypePtr->getCompInfo<Comp>();

CompInfoInArchtype& info = this->archtypePtr->getCompInfo<Comp>();
assert(&info);
return (Comp *)(&(storage[info.compOffsetInOneChunk + info.compDiscription->componentSize * index]));
return (Comp*)(&(storage[info.compOffsetInOneChunk + info.compDiscription->componentSize * index]));
}
};

Expand Down
3 changes: 2 additions & 1 deletion src/paecs/ArchtypeManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//#include "Archtype.h"
#include "parallel_hashmap/phmap.h"
#include "ArchtypeManager.h"
#include "parallel_hashmap/phmap.h"


namespace paecs
{
Expand Down
Loading

0 comments on commit be8f4b0

Please sign in to comment.