fast_ecs 0.1.0 fast_ecs: ^0.1.0 copied to clipboard
Simple and fast Entity-Component-System (ECS) library written in Dart.
Fast ECS Examples #
Simple and fast Entity-Component-System (ECS) library written in Dart.
Components #
Creating a VelocityComponent
class
class VelocityComponent extends Component {
double velocity = 2;
}
Creating a TransformComponent
class
class TransformComponent extends Component {
double rotation = 0.0;
double scale = 0.5;
}
Systems #
Creating a RotationSystem
class
class RotationSystem extends UpdateEcsSystem {
late List<TransformComponent> transformComponents; // quick link
late List<VelocityComponent> velocityComponents; // quick link
@override
void init(ecs, Signature signature) {
transformComponents = ecs.getComponentList<TransformComponent>();
velocityComponents = ecs.getComponentList<VelocityComponent>();
}
@override
void update(double deltaTime, SetEntity entities) {
for (var i = 0; i < entities.size; i++) {
Entity entity = entities.get(i);
TransformComponent transform = transformComponents[entity];
VelocityComponent velocity = velocityComponents[entity];
transform.rotation += velocity.velocity * deltaTime;
}
}
}
Entity Component System #
Creating a Ecs
class
Ecs ecs = Ecs(maxEntity: maxEntity, maxComponents: 8);
// register components
ComponentId transformId = ecs.registerComponent<TransformComponent>((index) => TransformComponent(), maxEntity);
ComponentId velocityId = ecs.registerComponent<VelocityComponent>((index) => VelocityComponent(), maxEntity);
var rotationSystemSignature = ecs.createSignature([transformId, velocityId]);
// register RotationSystem with signature
ecs.registerSystem<RotationSystem>(() => RotationSystem(), signature: rotationSystemSignature);
Entity #
Creating a Entity
Entity entity = ecs.createEntity();
ecs.addComponent(transformId, entity);
ecs.addComponent(velocityId, entity);
Usage #
update ECS
ecs.update(deltaTime);
render ECS
void _render(EcsSystem system, Uint16Set entities) {
if (system is DrawSystem) {
system.draw(batch, entities);
}
}
void render() {
ecs.forEach(_render);
}