fast_ecs 0.0.1 fast_ecs: ^0.0.1 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
class RenderEcsSystem extends EcsSystem {
void render(Canvas canvas, SetEntity entities){};
}
var systems = ecs.systemManager.systems;
for (int id = 0; id < systems.length; id++) {
var system = systems[id];
if (system is RenderEcsSystem) {
system.render(canvas, systemManager.systemEntities[id]);
}
}