【转】 Understanding Component-Entity-Systems

To solve this, game programmers started to build entities
through composition instead of inheritance. An entity is simply an
aggregation (technically a composition) of components. This has some
major benefits over the object-oriented architecture described above:
- It's easy to add new, complex entities
- It's easy to define new entities in data
- It's more efficient
Here's
how a few of the entities above would be implemented. Notice that the
components are all pure data - no methods. This will be explained in
detail below.
![]()
The Component
A component can be likened to a C
struct. It has no methods and is only capable of storing data, not
acting upon it. In a typical implementation, each different component
type will derive from an abstract Component class, which
provides facilities for getting a component's type and containing entity
at runtime. Each component describes a certain aspect of an entity and
its parameters. By themselves, components are practically meaningless,
but when used in conjunction with entities and systems, they become
extremely powerful. Empty components are useful for tagging entities.
Examples
- Position (x, y)
- Velocity (x, y)
- Physics (body)
- Sprite (images, animations)
- Health (value)
- Character (name, level)
- Player (empty)
The Entity
An
entity is something that exists in your game world. Again, an entity is
little more than a list of components. Because they are so simple, most
implementations won't define an entity as a concrete piece of data.
Instead, an entity is a unique ID, and all components that make up an
entity will be tagged with that ID. The entity is an implicit
aggregation of the components tagged with its ID. If you want, you can
allow components to be dynamically added to and removed from entities.
This allows you to "mutate" entities on the fly. For example, you could
have a spell that makes its target freeze. To do this, you could simply
remove the Velocity component.
Examples
- Rock (Position, Sprite)
- Crate (Position, Sprite, Health)
- Sign (Position, Sprite, Text)
- Ball (Position, Velocity, Physics, Sprite)
- Enemy (Position, Velocity, Sprite, Character, Input, AI)
- Player (Position, Velocity, Sprite, Character, Input, Player)
The System
Notice
that I've neglected to mention any form of game logic. This is the job
of the systems. A system operates on related groups of components, i.e.
components that belong to the same entity. For example, the character
movement system might operate on a Position, a Velocity, a Collider, and an Input. Each system will be updated once per frame in a logical order. To make a character jump, first the keyJump field of the Input data is checked. If it is true, the system will look through the contacts contained in the Collider data and check if there is one with the ground. If so, it will set the Velocity's y field to make the character jump.
Because
a system only operates on components if the whole group is present,
components implicitly define the behaviour an entity will have. For
example, an entity with a Position component but not a Velocity component will be static. Since the Movement system uses a Position and a Velocity, it won't operate on the Position contained within that entity. Adding a Velocity component will make the Movement
system work on that entity, thus making the entity dynamic and affected
by gravity. This behaviour can be exploited with "tag components"
(explained above) to reuse components in different contexts. For
example, the Input component defines generic flags for jumping, moving, and shooting. Adding an empty Player component will tag the entity for the PlayerControl system so that the Input data will be populated based on controller inputs.
Examples
- Movement (Position, Velocity) - Adds velocity to position
- Gravity (Velocity) - Accelerates velocity due to gravity
- Render (Position, Sprite) - Draws sprites
- PlayerControl (Input, Player) - Sets the player-controlled entity's input according to a controller
- BotControl (Input, AI) - Sets a bot-controlled entity's input according to an AI agent
Conclusion
To
wrap up, OOP-based entity hierarchies need to be left behind in favour
of Component-Entity-Systems. Entities are your game objects, which are
implicitly defined by a collection of components. These components are
pure data and are operated on in functional groups by the systems.
I
hope I've managed to help you to understand how
Component-Entity-Systems work, and to convince you that they are better
than traditional OOP. If you have any questions about the article, I'd
appreciate a comment or message.
A follow-up article has been posted, which provides a sample C implementation and solves some design problems. Implementing Component-Entity-Systems
Article Update Log
1 April 2013 - Initial submission
2 April 2013 - Initial publication; cleaned up formatting
29 September 2013 - Added notice of follow-up article; changed some formatting)
【转】 Understanding Component-Entity-Systems的更多相关文章
- 【转】Entity Systems
“Favour composition over inheritance” If you haven’t already read my previous post on the problems o ...
- 【转】What is an entity system framework for game development?
What is an entity system framework for game development? Posted on 19 January 2012 Last week I relea ...
- Programming Entity Framework 翻译(1)-目录
1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...
- 为什么要在游戏开发中使用ECS模式
http://www.richardlord.net/blog/why-use-an-entity-framework Why use an entity system framework for g ...
- 组件-实体-系统 Entiy-Compoent-System ECS架构整理
继承体系的问题,为什么要用ECS 面向对象的问题 当一个新的类型需要多个老类型的不同功能的时候,不能很好的继承出来 游戏开发后期会有非常多的类,很难维护 游戏中子系统很多,它们对一个对象的关注点往往互 ...
- 中间件(middlebox)
Middleboxes (also known as network functions) are systems that perform sophisticated and often state ...
- writing concurrent programs
Computer Systems A Programmer's Perspective Second Edition To this point in our study of computer sy ...
- [Angular 2] Directive intro and exportAs
First, What is directive, what is the difference between component and directive. For my understandi ...
- Why your Games are Unfinished, and What To Do About It (转)
So, you've got a new game idea, and it's going to change what everyone knows about the genre! Great! ...
- uvc摄像头代码解析5
8.初始化uvc控制 8.1 重要结构体 struct uvc_control { //uvc控制 struct uvc_entity *entity; //uvc实体 struct uvc_cont ...
随机推荐
- C++中颜色的设置
1.改变整个控制台的颜色用 system("color 0A"); 其中color后面的0是背景色代号,A是前景色代号.各颜色代码如下: 0=黑色 1=蓝色 2=绿色 3=湖蓝色 ...
- 各种浏览器hack
Hack是针对不同的浏览器去写不同的CSS样式,从而让各浏览器能达到一致的渲染效果,那么针对不同的浏览器写不同的CSS CODE的过程,就叫CSS HACK,同时也叫写CSS Hack.然后将Hack ...
- css+div如何解决文字溢出
看到标题你一定很轻易就会想到截断文字加“...”的做法.哈哈,就是这样.其实写这篇日志也只是把这样方法做个记录,因为似乎还有很多人不记得碰到这样的情况该如何处理. 首先,先解释一下,一般用div+cs ...
- vue.js——初体验
看到最近很火的vue.js,于是开启了自己人生中首篇翻译之路,才意识到这个纯英文版的的确没有中文的通俗易懂~~~~~~不过, 还是硬着头皮把这篇英文版的博客给翻译完了,希望可以帮助自己的同时也方便别人 ...
- [转]Java程序员们最常犯的10个错误
1.将数组转化为列表 将数组转化为一个列表时,程序员们经常这样做: List<String> list = Arrays.asList(arr); Arrays.asList()会返回一个 ...
- asp.net 错误跳转
每当用户访问错误页面时,会出现不友好的404错误,所以为了防止这种不友好,我们在web.config中的<system.web>节点下配置 <customErrors>,在出现 ...
- js判断手机系统和微信
//判断手机浏览器 var ua = navigator.userAgent; var ipad = ua.match(/(iPad).*OS\s([\d_]+)/), isIphone = !ipa ...
- 转 velocity 模板使用总结
Velocity是一个基于java的模板引擎.它允许任何人仅仅简单的使用模板语言来引用由java代码定义的对象. 当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一 ...
- mysqldump使用语法
复制代码 代码如下: mysqldump -u user -p db tab1 tab2 > db.sql 恢复 复制代码 代码如下: mysql -u user -p db < db ...
- cf731E
题意:一个游戏,由n张贴纸组成.贴纸排成一排,并且纸条上标有数字,玩家轮流揭下m张从左到右连续的纸条(m大等2),揭下后玩家得分累加这些纸条的sum,并且在剩下纸条最左边贴上新的纸条,数值为揭下纸条的 ...