SlideShare a Scribd company logo
1 of 33
Download to read offline
A Data-Driven
Game Object System
CDC 2013
Soul Game
http://elvisqin.me
Scott Bilas, GDC 2002
Unity Cocos2d-x ...
Cocos2d-x 3.0 alpha
Warrior,Goblin,Player...
Scene
MVC
Entity Component System
Game Object
GameObject
AI
tree bullet monster player hero
GameObject
GameObject
GameObject
GameObject ID, GameObject.Find()
GameObject
( )
getChildByTag(), GameObject.Find()
Game Object System
?
( )
boss
helper
root-heavy blob pattern
hard coding database
Collision
Animation
AI
Render
Path finding
Input
ID
ID
Render
Collision
AI
Path
finding
Animation
Input
Entity Component System
Entity
Entity ID
UI
class Entity:public Object
{
public:
typedef std::string entity_id_type;
static Entity* createWithId(const entity_id_type& id);
const entity_id_type& getId(){return _id;}
bool operator==(const Entity& entity){return _id==entity._id;}
private:
Entity();
Entity* initWithId(const entity_id_type& id);
private:
entity_id_type _id;
};
Component
Component Entity
health, hero
move
, render, collision,AI, move, gun, input...
class ECSComponent:public Object
{
protected:
ECSComponent(const std::string& type);
public:
inline const std::string& getType() const{return _type;}
private:
std::string _type;
};
ship
monster
bullet
helper
hero
level
healthcollisiongunscriptrendermovement ....
Entity Component Grid
EntityManager/** Manage all entity ids, and it's components */
class EntityManager:public Object
{
public:
EntityManager();
~EntityManager();
entity_id_type generateNewEid();
Entity* createEntity();
void removeEntity(Entity* entity);
void addComponentToEntity(ECSComponent* component,Entity* entity);
/** an entity only can own one instane of some kind of component */
ECSComponent* getComponentForEntity(const std::string & eId,Entity* entity);
/** get all entities which contain some kind of component. */
const std::vector<Entity*>* getAllEntitiesPosessingComponent(const std::string& cId);
private:
std::vector<Entity*> _entities;
std::map<std::string, std::map<Entity*, ECSComponent*>*> _componentsByType;
//for quick find entities every frame.
std::map<std::string, std::vector<Entity*>*> _componentEntities;
int _lowestUnassignedEid;
};
System
System
System
System Entity Entity
CollisionSystem, AISystem, MoveSystem, InputSystem, BattleSystem,
CameraSystem...
Code...
class ECSSystem:public Object
{
protected:
ECSSystem();
void initWithManager(EntityManager*);
public:
virtual void configure(){};
virtual void update(float dt){};
protected:
EntityManager* _entityManager;
};
ECS
(Byte56 stackoverflow)
System ( )
Calculations collision detection
Collision
Render
CollisionSystem
Position
Entity ( )
Entity ID: 88
Entity
Collision
Render
CollisionSystem Id 88 Entity
Position
void CollisionSystem::update(float dt)
{
Array* entities=_entityManager->getAllEntitiesPosessingComponent(CollisionComponent::COLLISION_TYPE);
if(!entities) return;
int count=entities->count();
//记录两个Entity之间是否已执⾏碰撞检测
int haveCollision[count][count];
for (int i=0; i<count; i++){
//碰撞主体
Entity* entity=(Entity*)entities->getObjectAtIndex(i);
RenderComponent* render=(RenderComponent*)_entityManager->getComponentForEntity(RenderComponent::RENDER_TYPE, entity);
if(!render) continue;
//可能与之发⽣碰撞的主体
for (int j=0; j<count; j++) {
if (haveCollision[j][i]==1||i==j) {
continue;
}
Entity* collisionEntity=(Entity*)entities->getObjectAtIndex(j);
RenderComponent* collisionRender=(RenderComponent*)_entityManager->getComponentForEntity(RenderComponent::RENDER_TYPE,
collisionEntity);
if(!collisionRender) continue;
//发⽣碰撞
if (collide(render->getNode(), collisionRender->getNode())) {
//标记已处理两个特定Entity
haveCollision[i][j]=1;
CollisionEvent* event=new CollisionEvent(entity,collisionEntity);
EventDispatcher::getInstance()->dispatchEvent(event);
}
}
}
}
Collision
Render
Position
System
Render
entity1
entity2
entity3
Collision
entity1
entity2
entity3
Move
entity1
entity2
entity3
Move
Health
Render
Move
Health
EntityManager ComponentSystem
GameEngine
Loop
Entity Sprite
Entity System
getChildByTag GameObject.Find
System Component Sprite tag name
ID Name
Entity UI UI
UI Sprite UI
System
EventDispatcher
GunEvent, DeadEvent,CollisionEvent…
System
configure EventDispatcher
this
ECS
Cocos2d-x 3.0 alpha
System
System
this
Entity
CCCalFuncN
C++11 lambda
Demo
Entity( Render ship ghost)
ghost MoveComponent
ghost GunComponent
girl
Cocosd-x 3.0 alpha
http://github.com/elvisqin/entitysystem
ECS
System
UI
mouse touch
ECS EventDispatcher
Unity GameObject name
( )
prefab
UI
Unity
<?xml version="1.0"?>
<entity>
<Render>
<Sprite>Ghost/ghost.png</Sprite>
</Render>
<Health>
<curHP>20</curHP>
<maxHP>20</maxHP>
</Health>
<Move></Move>
<Collision/>
<Ghost/>
<Gun>
<distance>100</distance>
<targetType>ship</targetType>
</Gun>
</entity>
Entity* unit = _entityManager->createEntity();
_entityManager->addComponentToEntity(RenderComponent::create(unitSprite), unit); //UI
_entityManager->addComponentToEntity(HealthComponent::create(20,20),unit); //⽣命值
_entityManager->addComponentToEntity(MoveComponent::cretae(ship->getPosition(),1), unit); //可移动
_entityManager->addComponentToEntity(CollisionComponent::create(), unit); //可碰撞
_entityManager->addComponentToEntity(HitComponent::create(ShipComponent::SHIP_TYPE, 10), unit); //碰撞攻击伤害及⺫标
_entityManager->addComponentToEntity(GhostComponent::create(), unit); //单位类型
_entityManager->addComponentToEntity(GunComponent::create(ShipComponent::SHIP_TYPE, 1000, 0.2), unit);
System Entity
std::map size
EntityManager
System Entity(Unity )
Cocos2d-x 2.1.2 CCNode
EventDispatcher
Entity System
Sprite Kit , …
2.5D
C++ Style STL lamda
GUI
Label
CocoStudio
more…
Cocos2d-x 3.0
thanks&enjoy

More Related Content

Similar to Entity System

IntroToEngineDevelopment.pdf
IntroToEngineDevelopment.pdfIntroToEngineDevelopment.pdf
IntroToEngineDevelopment.pdfzakest1
 
Web Game Development
Web Game DevelopmentWeb Game Development
Web Game DevelopmentSabin Buraga
 
Game development via_sprite_kit
Game development via_sprite_kitGame development via_sprite_kit
Game development via_sprite_kitBuşra Deniz, CSM
 
WaveEngine 2D components
WaveEngine 2D componentsWaveEngine 2D components
WaveEngine 2D componentswaveengineteam
 
Snake in the DOM!
Snake in the DOM!Snake in the DOM!
Snake in the DOM!Gil Steiner
 
Owasp.meet up.2017.ppt
Owasp.meet up.2017.pptOwasp.meet up.2017.ppt
Owasp.meet up.2017.pptSul Haedir
 
Android game engine
Android game engineAndroid game engine
Android game engineJulian Chu
 
Useful Tools for Making Video Games - Irrlicht (2008)
Useful Tools for Making Video Games - Irrlicht (2008)Useful Tools for Making Video Games - Irrlicht (2008)
Useful Tools for Making Video Games - Irrlicht (2008)Korhan Bircan
 
Academy PRO: Unity 3D. Scripting
Academy PRO: Unity 3D. ScriptingAcademy PRO: Unity 3D. Scripting
Academy PRO: Unity 3D. ScriptingBinary Studio
 
The current state of web on mobile - Have smartphone browsers gotten smarter?
The current state of web on mobile - Have smartphone browsers gotten smarter?The current state of web on mobile - Have smartphone browsers gotten smarter?
The current state of web on mobile - Have smartphone browsers gotten smarter?Tomomi Imura
 
飛び道具ではないMetal #iOSDC
飛び道具ではないMetal #iOSDC飛び道具ではないMetal #iOSDC
飛び道具ではないMetal #iOSDCShuichi Tsutsumi
 
The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185Mahmoud Samir Fayed
 
Academy PRO: Unity 3D. Environment
Academy PRO: Unity 3D. EnvironmentAcademy PRO: Unity 3D. Environment
Academy PRO: Unity 3D. EnvironmentBinary Studio
 
Understanding Reflection
Understanding ReflectionUnderstanding Reflection
Understanding ReflectionTamir Khason
 
Game development with Cocos2d-x Engine
Game development with Cocos2d-x EngineGame development with Cocos2d-x Engine
Game development with Cocos2d-x EngineDuy Tan Geek
 
Introduction to Game Development
Introduction to Game DevelopmentIntroduction to Game Development
Introduction to Game DevelopmentShaan Alam
 
Funky game engines
Funky game enginesFunky game engines
Funky game engineseduriez
 

Similar to Entity System (20)

Unity workshop
Unity workshopUnity workshop
Unity workshop
 
IntroToEngineDevelopment.pdf
IntroToEngineDevelopment.pdfIntroToEngineDevelopment.pdf
IntroToEngineDevelopment.pdf
 
Web Game Development
Web Game DevelopmentWeb Game Development
Web Game Development
 
Game development via_sprite_kit
Game development via_sprite_kitGame development via_sprite_kit
Game development via_sprite_kit
 
WaveEngine 2D components
WaveEngine 2D componentsWaveEngine 2D components
WaveEngine 2D components
 
Snake in the DOM!
Snake in the DOM!Snake in the DOM!
Snake in the DOM!
 
Owasp.meet up.2017.ppt
Owasp.meet up.2017.pptOwasp.meet up.2017.ppt
Owasp.meet up.2017.ppt
 
Android game engine
Android game engineAndroid game engine
Android game engine
 
Useful Tools for Making Video Games - Irrlicht (2008)
Useful Tools for Making Video Games - Irrlicht (2008)Useful Tools for Making Video Games - Irrlicht (2008)
Useful Tools for Making Video Games - Irrlicht (2008)
 
Academy PRO: Unity 3D. Scripting
Academy PRO: Unity 3D. ScriptingAcademy PRO: Unity 3D. Scripting
Academy PRO: Unity 3D. Scripting
 
The current state of web on mobile - Have smartphone browsers gotten smarter?
The current state of web on mobile - Have smartphone browsers gotten smarter?The current state of web on mobile - Have smartphone browsers gotten smarter?
The current state of web on mobile - Have smartphone browsers gotten smarter?
 
Cocos2d for beginners
Cocos2d for beginnersCocos2d for beginners
Cocos2d for beginners
 
飛び道具ではないMetal #iOSDC
飛び道具ではないMetal #iOSDC飛び道具ではないMetal #iOSDC
飛び道具ではないMetal #iOSDC
 
The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185
 
Academy PRO: Unity 3D. Environment
Academy PRO: Unity 3D. EnvironmentAcademy PRO: Unity 3D. Environment
Academy PRO: Unity 3D. Environment
 
Understanding Reflection
Understanding ReflectionUnderstanding Reflection
Understanding Reflection
 
Flappy bird
Flappy birdFlappy bird
Flappy bird
 
Game development with Cocos2d-x Engine
Game development with Cocos2d-x EngineGame development with Cocos2d-x Engine
Game development with Cocos2d-x Engine
 
Introduction to Game Development
Introduction to Game DevelopmentIntroduction to Game Development
Introduction to Game Development
 
Funky game engines
Funky game enginesFunky game engines
Funky game engines
 

Entity System