http://www.gamedev.net/page/resources/_/technical/game-programming/understanding-component-entity-systems-r3013
The traditional way to implement game entities was to use object-oriented programming. Each entity was an object, which intuitively allowed for an instantiation system based on classes and enabled entities to extend others through polymorphism. This led to large, rigid class hierarchies. As the number of entities grew, it became increasingly difficult to place a new entity in the hierarchy, especially if the entity needed a lot of different types of functionality. Here, you can see a simple class hierarchy. A static enemy does not fit well into the tree.

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:

  1. It's easy to add new, complex entities
  2. It's easy to define new entities in data
  3. 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的更多相关文章

  1. 【转】Entity Systems

    “Favour composition over inheritance” If you haven’t already read my previous post on the problems o ...

  2. 【转】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 ...

  3. Programming Entity Framework 翻译(1)-目录

    1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...

  4. 为什么要在游戏开发中使用ECS模式

    http://www.richardlord.net/blog/why-use-an-entity-framework Why use an entity system framework for g ...

  5. 组件-实体-系统 Entiy-Compoent-System ECS架构整理

    继承体系的问题,为什么要用ECS 面向对象的问题 当一个新的类型需要多个老类型的不同功能的时候,不能很好的继承出来 游戏开发后期会有非常多的类,很难维护 游戏中子系统很多,它们对一个对象的关注点往往互 ...

  6. 中间件(middlebox)

    Middleboxes (also known as network functions) are systems that perform sophisticated and often state ...

  7. writing concurrent programs

    Computer Systems A Programmer's Perspective Second Edition To this point in our study of computer sy ...

  8. [Angular 2] Directive intro and exportAs

    First, What is directive, what is the difference between component and directive. For my understandi ...

  9. 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! ...

  10. uvc摄像头代码解析5

    8.初始化uvc控制 8.1 重要结构体 struct uvc_control { //uvc控制 struct uvc_entity *entity; //uvc实体 struct uvc_cont ...

随机推荐

  1. XX宝面试题——css部分

    1.<b></b>与<strong></strong>有什么不同? 1) <b>标签是一个实体标签,它所包围的字符将被设为bold(粗体), ...

  2. override和overload的区别

    override(重写,覆盖) 1.方法名.参数.返回值相同. 2.子类方法不能缩小父类方法的访问权限. 3.子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常). 4.存在于父类和子类之 ...

  3. css样式书写的问题

    经常遇到前端的朋友问及css样式书写的问题,结合自己实际的工作,自己总结了整理了一下,给大家分享: 一.顺序问题:显示属性-位置属性-元素自身属性-文本属性-其他属性 1.显示属性:z-index.d ...

  4. 配置coffeeScript

    1.安装好node.js后 在系统环境变量自动会设置好: 我安装在D:\Program Files文件夹中   也安装好了npm(node packges manager)   2.系统会自动配置np ...

  5. Mongodb Management Studio

    1.服务器管理功能添加服务器,删除服务器 2.服务器,数据库,表,列,索引,树形显示和状态信息查看 3.查询分析器功能.支持select,insert,Delete,update支持自定义分页函数 $ ...

  6. dx wpf的各种坑

    这篇随笔总结dx wpf使用中的各种坑,持续更新~ LookUpEdit里内嵌的DXGrid的名字必须是"PART_GridControl",不能不写.也不能写错.我对比了2个小时 ...

  7. Spring学习笔记之整合hibernate

    1.web.xml里边要配置好对应的springxml的路径 <context-param> <param-name>contextConfigLocation</par ...

  8. Notification通知栏

    Notification通知栏 首先实现的功能就是通知栏显示Notification,Notification是显示在系统的通知栏上面的,所以Notification 是属于进程之前的通讯.进程之间的 ...

  9. web api post传一个参数时 值永远是null

    这个问题纠结了我一个早上,不管用什么样的传参方法,走到控制器中,那个参数永远不变的等于null 在网上找了很多解决方案 上面这个是从网上截图的,第一:要将参数标记为[FromBody],变为简单参数 ...

  10. 12-27cell常用的属性

    1.创建cell //    创建一个cell并且设置cell的风格 UITableViewCell *cell  = [[UITableViewCell alloc]initWithStyle:UI ...