[go: nahoru, domu]

Skip to content

Entity & Component System

Neil Richardson edited this page May 20, 2014 · 17 revisions

OUT OF DATE NEED TO UPDATE.

Entity & Component System

Overview

This system is designed for clean and efficient definition of game entities through composition. The idea is the entity is merely the container which you add to the scene, and the components are the actual logic that control everything.

ScnEntity is something you create directly using CsCore::createResource (see CsCore.h). You can then create components to add to the entity. Such components include:

  • ScnCanvasComponent: A canvas for drawing and sorting 2D sprites, lines, and other 2D geometry easily and efficiently. Limited support for batching of sprites (layer dependant).
  • ScnFontComponent: A child of ScnFont, used to render fonts to an ScnCanvasComponent. Batches to 1 draw call per render call.
  • ScnMaterialComponent: A child of ScnMaterial, used to allow multiple instances of the same material to be used on different rendering components (ScnCanvasComponent, ScnModelComponent, etc). May internally update shader parameters.
  • ScnModelComponent: A child on ScnModel, used to render multiple instances of the same ScnModel. Will update ScnMaterialComponent parameters as it requires.
  • ScnViewComponent: Used to define a view in which the scene is rendered. Effectively the camera, and can also have ScnRenderTarget resources attached to it (WIP). Registers itself with the scene when added to an entity that is part of the scene.

Most game logic should be derived from ScnComponent and attached to an ScnEntity. You choose the order in which to attach, but remember that ScnEntity's transform may be taken by subsequent component update stages, so choose the order wisely.

Certain components (ScnFontComponent & ScnModelComponent for example) will automatically attach any dependant components (ScnMaterialComponent for example) in the correct order relative to itself, you do not need to do this manually. If components are not attached correctly when used/updated, there will be an assertion triggered and you can break in code to find out which one is not set up correctly.

Examples

Basic example

// First create the entity.
ScnEntityRef Entity;
if( CsCore::pImpl()->createResource( "CanvasEntity_0", Entity ) )
{
	GaExampleComponentRef ExampleComponent;
	ScnCanvasComponentRef CanvasComponent;
	ScnMaterialComponentRef MaterialComponent;

	// Create component resources. (BcName::INVALID will autogenerate the names)
	CsCore::pImpl()->createResource( BcName::INVALID, ExampleComponent );
	CsCore::pImpl()->createResource( BcName::INVALID, MaterialComponent, ScnMaterial::Default, scnSPF_DEFAULT );
	CsCore::pImpl()->createResource( BcName::INVALID, CanvasComponent, 8192, MaterialComponent );

	// Attach material and canvas component to entity.
	Entity->attach( ExampleComponent );
	Entity->attach( MaterialComponent );
	Entity->attach( CanvasComponent );

	// Add entity to scene.
	ScnCore::pImpl()->addEntity( Entity );
}
Clone this wiki locally