GServerECS is an open-source ECS framework designed and developed specifically for game servers, implemented in Java. This framework provides complete ECS architecture support, including runtime component addition/removal, system execution order control, on-the-fly and deferred loading of entities/components, and other key features.
This framework is designed for game server scenarios. A single process can create multiple EcsWorld instances, each corresponding to a game room (Room) or scene (Scene). Each EcsWorld is designed to be non-thread-safe and should only be used within a single thread. Cross-thread calls are not supported.
If this project has helped you, please feel free to give it a starโญ to show your support~ This will help more people discover it ๐
- Entity Management: Entity creation, destruction, and lifecycle management
- Component System: Support for dynamic component addition/removal with type safety
- System Execution: Flexible system update mechanism with multiple execution modes
- Entity Prototypes: Component-based entity prototype system
- System Groups: Support for system group management, facilitating complex logic organization
- Execution Order Control: Precise control of system execution order through annotations
- Deferred Commands: Support for deferred execution of entity operation commands
- Entity Factories: Factory pattern for entity creation, simplifying entity instantiation
- Auto-scanning: Automatic discovery and registration of systems, components, and factories based on package scanning
- Java: Version 21 or higher
- Maven: Version 3.6 or higher
- Dependencies:
- Log4j2 (2.25.3)
- Disruptor (3.4.4)
- JUnit 5 (for testing)
<dependency>
<groupId>top.kgame</groupId>
<artifactId>kgame-lib-ecs</artifactId>
<version>1.0.1</version>
</dependency>public class PositionComponent implements EcsComponent {
public float x, y, z;
}
public class HealthComponent implements EcsComponent {
public int currentHealth;
public int maxHealth;
}@SystemGroup(GameSystemGroup.class)
// Systems without @SystemGroup annotation belong to top-level systems,
// at the same level as SystemGroups, all scheduled directly by EcsWorld
public class MovementSystem extends EcsOneComponentUpdateSystem<PositionComponent> {
@Override
protected void update(Entity entity, PositionComponent position) {
// Update position logic
position.x += 1.0f;
}
}// EntityFactory implementations are automatically scanned and registered
public class PlayerFactory extends BaseEntityFactory {
@Override
protected Collection<EcsComponent> generateComponent() {
return List.of(new PositionComponent(), new HealthComponent());
}
@Override
public int typeId() {
return 1; // Factory type ID, must be unique within the same EcsWorld
}
}public class GameSystemGroup extends EcsSystemGroup {
// System group implementation
@Override
protected void onStart() {
}
@Override
protected void onStop() {
}
}public class Game {
private EcsWorld world;
public void init() {
// Create ECS world, specify package names to scan
world = EcsWorld.generateInstance("com.example.game");
// Can set custom context
world.setContext(this);
}
public void update(long currentTime) {
// Update ECS world
// Note: Timestamp must be strictly increasing, must be greater than the last passed time
world.update(currentTime);
}
public void createPlayer() {
// Create player entity through factory
Entity player = world.createEntity(PlayerFactory.class);
}
public void cleanup() {
world.close();
}
}// Get component
PositionComponent position = entity.getComponent(PositionComponent.class);
// Check component
if (entity.hasComponent(HealthComponent.class)) {
// Handle logic
}
// Add component
entity.addComponent(new HealthComponent());
// Remove component
entity.removeComponent(PositionComponent.class);
// Destroy entity
world.requestDestroyEntity(entity);GServerECS provides rich annotations to control system behavior:
- Purpose: Marks an EcsSystem to execute updates within a specified EcsSystemGroup
- Target: EcsSystem classes
- Parameters:
Class<? extends EcsSystemGroup> value()- System group type - Description: EcsSystem marked with this annotation will execute updates within the specified EcsSystemGroup. EcsSystem not marked with this annotation belong to top-level systems at the same level as EcsSystemGroup, scheduled by EcsWorld. Note: This annotation cannot be used on EcsSystemGroup classes, and nested SystemGroups are not currently supported.
- Purpose: Marks system update interval time
- Target: EcsSystem classes
- Parameters:
int value()- Update interval time (milliseconds) - Description: Systems marked with this annotation will execute updates after the specified time interval. Systems not marked with this annotation will execute every update cycle.
- Purpose: Marks EcsSystem to always execute updates, regardless of whether there are matching entities
- Target: EcsSystem classes
- Parameters: None
- Description: EcsSystem marked with this annotation will execute in every update cycle, even if no entities contain the components required by this EcsSystem. EcsSystem not marked with this annotation will only execute updates in each update cycle when there are entities containing the required components.
- Purpose: Marks EcsSystem to execute updates after specified EcsSystem within the same group
- Target: EcsSystem classes
- Parameters:
Class<? extends EcsSystem>[] value()- Target system type array - Description: EcsSystem marked with this annotation will execute updates after the specified EcsSystem completes. EcsSystem with the same conditions will execute in dictionary order. Can be used in SystemGroup.
- Purpose: Marks EcsSystem to execute updates before specified EcsSystem within the same group
- Target: EcsSystem classes
- Parameters:
Class<? extends EcsSystem>[] value()- Target system type array - Description: EcsSystem marked with this annotation will execute updates before the specified EcsSystem. EcsSystem with the same conditions will execute in dictionary order. Can be used in SystemGroup.
EntityFactory implementations are automatically scanned and registered by EcsWorld. No annotation is required. Simply implement the EntityFactory interface or extend BaseEntityFactory, and the framework will automatically discover and register your factory classes.
GServerECS provides various predefined system base classes:
EcsOneComponentUpdateSystem<T>: System handling entities with a single specified componentEcsTwoComponentUpdateSystem<T1, T2>: System handling entities with two specified componentsEcsThreeComponentUpdateSystem<T1, T2, T3>: System handling entities with three specified componentsEcsFourComponentUpdateSystem<T1, T2, T3, T4>: System handling entities with four specified componentsEcsFiveComponentUpdateSystem<T1, T2, T3, T4, T5>: System handling entities with five specified componentsEcsStandaloneUpdateSystem: Singleton update system, not bound to entities, executes once per world updateEcsExcludeComponentUpdateSystem<T>: System handling entities that do not contain the specified component TEcsInitializeSystem<T>: Entity initialization system, automatically adds initialization completion markerEcsDestroySystem<T>: Entity destruction system, handles entities marked for destructionEcsLogicSystem: Logic system base class, provides component filtering and entity query functionality
System Groups (EcsSystemGroup) are an important mechanism in GServerECS for organizing and managing system execution. A system group is itself a system that can contain multiple subsystems and execute them in a specific order.
- Automatic Management: System groups automatically scan and manage all systems marked with the
@SystemGroupannotation - Execution Order: Systems within a system group execute according to the order defined by
@Afterand@Beforeannotations - Lifecycle: System groups have complete lifecycle management, including initialization, updates, and destruction
- Dynamic Management: Support for adding and removing Systems at runtime
EcsWorld
โโโ Top-level Systems (without @SystemGroup annotation)
โ โโโ SystemA
โ โโโ SystemB
โโโ System Groups
โโโ GameSystemGroup
โ โโโ InputSystem
โ โโโ LogicSystem
โ โโโ RenderSystem
โโโ PhysicsSystemGroup
โโโ CollisionSystem
โโโ MovementSystem
GServerECS provides a complete deferred command system that allows safe execution of entity and component operations during system execution. Deferred commands execute within specified scopes, ensuring atomicity and consistency of operations.
public class MySystem extends EcsOneComponentUpdateSystem<MyComponent> {
@Override
protected void update(Entity entity, MyComponent component) {
// Add deferred command
addDelayCommand(new EcsCommandAddComponent(entity, new NewComponent()),
EcsCommandScope.SYSTEM);
}
}GServerECS provides the following four deferred commands:
- EcsCommandCreateEntity: Deferred entity creation
- EcsCommandDestroyEntity: Deferred entity destruction
- EcsCommandAddComponent: Deferred component addition
- EcsCommandRemoveComponent: Deferred component removal
Deferred commands support three scopes that control command execution timing:
SYSTEM: System scope, commands execute after the current System completesSYSTEM_GROUP: System group scope, commands execute after the current system group completesWORLD: World scope, commands execute after the current world update completes
GServerECS divides entity operations into immediate effect and deferred effect modes:
- Entity Addition: Through
ecsworld.createEntity()calls - Component Addition/Removal: Through direct calls to
entity.addComponent()andentity.removeComponent() - Effect Timing: Operations take effect immediately, accessible by other Systems after the current System completes
- Operation Method: Request destruction through
world.requestDestroyEntity() - Effect Timing: Executes after the current world update completes, ensuring all Systems can process the entity
- All Operations: Execute through the deferred command system (EcsCommandCreateEntity, EcsCommandDestroyEntity, EcsCommandAddComponent, EcsCommandRemoveComponent)
- Effect Timing: Refer to the Deferred Command System section
The project includes comprehensive test cases demonstrating various functionality usage:
- Component Operation Tests: Demonstrates component addition and removal operations (immediate and deferred)
- Entity Operation Tests: Demonstrates entity creation and destruction operations (immediate and deferred)
- System Tests: Demonstrates system execution order control, update interval functionality, and complex system combination usage
- Resource Cleanup Tests: Demonstrates ECS world destruction and resource cleanup functionality
src/
โโโ main/java/top/kgame/lib/ecs/
โ โโโ annotation/ # Annotation definitions
โ โ โโโ After.java # System execution order control (after)
โ โ โโโ Before.java # System execution order control (before)
โ โ โโโ Standalone.java # Standalone system marker
โ โ โโโ SystemGroup.java # System group marker
โ โ โโโ TickRate.java # System update interval
โ โโโ command/ # Deferred command system
โ โ โโโ EcsCommand.java # Command interface
โ โ โโโ EcsCommandBuffer.java # Command buffer
โ โ โโโ EcsCommandScope.java # Command scope
โ โ โโโ EcsCommandAddComponent.java
โ โ โโโ EcsCommandCreateEntity.java
โ โ โโโ EcsCommandDestroyEntity.java
โ โ โโโ EcsCommandRemoveComponent.java
โ โโโ core/ # Core implementation
โ โ โโโ ComponentFilter.java # Component filter
โ โ โโโ ComponentFilterMode.java # Filter mode
โ โ โโโ ComponentFilterParam.java # Filter parameter
โ โ โโโ EcsComponentManager.java # Component manager
โ โ โโโ EcsEntityManager.java # Entity manager
โ โ โโโ EcsSystemManager.java # System manager
โ โ โโโ EntityArchetype.java # Entity archetype
โ โ โโโ EntityFactory.java # Entity factory interface
โ โ โโโ EntityQuery.java # Entity query
โ โ โโโ SystemScheduler.java # System scheduler
โ โโโ exception/ # Exception definitions
โ โโโ extensions/ # Extension functionality
โ โ โโโ component/ # Extension components
โ โ โโโ entity/ # Extension entity factories
โ โ โโโ system/ # Extension system base classes
โ โโโ tools/ # Utility classes
โ โโโ EcsComponent.java # Component interface
โ โโโ EcsEntity.java # Entity class
โ โโโ EcsSystem.java # System base class
โ โโโ EcsSystemGroup.java # System group base class
โ โโโ EcsWorld.java # ECS world
โโโ test/java/top/kgame/lib/ecstest/
โโโ component/ # Component tests
โ โโโ add/ # Component addition tests
โ โ โโโ immediately/ # Immediate addition
โ โ โโโ delay/ # Deferred addition
โ โโโ remove/ # Component removal tests
โ โโโ immediately/ # Immediate removal
โ โโโ delay/ # Deferred removal
โโโ entity/ # Entity tests
โ โโโ add/ # Entity addition tests
โ โโโ remove/ # Entity removal tests
โโโ schedule/ # System scheduling tests
โโโ system/ # System tests
โโโ core/ # Core functionality tests
โโโ performance/ # Performance tests
โโโ dispose/ # Resource cleanup tests
โโโ util/ # Test utility classes
- Multi-thread Support (In Progress)
- Entity-Component Framework Independent of System (Not Started)
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
For questions or suggestions, please contact us through:
- Submit Issue: GitHub Issues
- Email: chinazhangk@gmail.com