Skip to content

Commit b67f4f0

Browse files
committed
docs(copilot-instructions): enhance project documentation and structure
- Improved project overview for clarity and performance emphasis - Added detailed sections on development workflow, architecture patterns, and key design patterns - Included specific examples for component creation, deferred operations, and system implementation - Updated testing conventions and integration points for better guidance
1 parent f4ca814 commit b67f4f0

1 file changed

Lines changed: 125 additions & 13 deletions

File tree

.github/copilot-instructions.md

Lines changed: 125 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,146 @@
22

33
## Project Overview
44

5-
This is an Entity Component System (ECS) library built with TypeScript and Bun runtime. The project follows modern TypeScript practices with strict type checking and ESNext features.
5+
This is a high-performance Entity Component System (ECS) library built with TypeScript and Bun runtime. The library implements an archetype-based architecture for optimal memory layout and query performance, with support for entity relationships, lifecycle hooks, and deferred command execution.
66

77
## Runtime & Environment
88

99
- **Runtime**: Bun (not Node.js) - use `bun` commands instead of `npm`/`yarn`
1010
- **Language**: TypeScript with ESNext target and strict mode enabled
1111
- **Module System**: ES modules with `"module": "Preserve"` and bundler resolution
12+
- **Type Checking**: Strict mode with `noUncheckedIndexedAccess`, `noImplicitOverride`, and other advanced checks
1213

1314
## Development Workflow
1415

1516
- **Install**: `bun install` (not `npm install`)
16-
- **Run**: `bun run src/index.ts` (direct execution, no build step)
17-
- **TypeScript**: `bunx tsc --noEmit` (Configured for modern features with strict checking - noEmit mode for bundler compatibility)
18-
- **Testing**: `bun test` (set up tests as needed)
17+
- **Run**: `bun run src/index.ts` (direct execution, no build step required)
18+
- **TypeScript**: `bunx tsc --noEmit` (validates code with modern TypeScript features)
19+
- **Testing**: `bun test` (runs all `*.test.ts` files)
20+
- **Demo**: `bun run demo` (runs `examples/simple/demo.ts`)
21+
- **Build**: `bun run scripts/build.ts` (generates dist/ with bundled JS and .d.ts files)
22+
- **Release**: `bun run scripts/release.ts` (automated versioning from git tags)
23+
24+
## Architecture Patterns
25+
26+
### Core ECS Components
27+
28+
- **World**: Central coordinator managing entities, components, archetypes, and systems
29+
- **Archetype**: Groups entities with identical component combinations for contiguous memory access
30+
- **Entity**: Unique identifiers (starting from 1024) representing game objects
31+
- **Component**: Data structures attached to entities (IDs 1-1023)
32+
- **Query**: Cached entity queries with `forEach()` and `getEntitiesWithComponents()` methods
33+
- **System**: Update logic implementing `System` interface with `update(world, deltaTime)` method
34+
- **CommandBuffer**: Deferred execution system for batched structural changes
35+
36+
### Key Design Patterns
37+
38+
**Component Creation**:
39+
40+
```typescript
41+
const PositionId = component<Position>(1);
42+
const VelocityId = component<Velocity>(2);
43+
```
44+
45+
**Deferred Operations** (always call `world.flushCommands()` after):
46+
47+
```typescript
48+
world.addComponent(entity, PositionId, { x: 0, y: 0 });
49+
world.removeComponent(entity, VelocityId);
50+
world.flushCommands(); // Execute queued changes
51+
```
52+
53+
**Query Usage**:
54+
55+
```typescript
56+
const query = world.createQuery([PositionId, VelocityId]);
57+
query.forEach([PositionId, VelocityId], (entity, position, velocity) => {
58+
// Direct component access - no undefined checks needed
59+
});
60+
```
61+
62+
**System Implementation**:
63+
64+
```typescript
65+
class MovementSystem implements System {
66+
private query: Query;
67+
68+
constructor(world: World) {
69+
this.query = world.createQuery([PositionId, VelocityId]); // Cache query
70+
}
71+
72+
update(world: World, deltaTime: number): void {
73+
this.query.forEach([PositionId, VelocityId], (entity, position, velocity) => {
74+
position.x += velocity.x * deltaTime;
75+
});
76+
}
77+
}
78+
```
79+
80+
**Entity Relationships**:
81+
82+
```typescript
83+
// Direct relation: Position component targeting entity2
84+
const positionRelation = relation(PositionId, entity2);
85+
world.addComponent(entity1, positionRelation, { x: 10, y: 20 });
86+
87+
// Wildcard relation: Listen to all Position relations
88+
const wildcardPosition = relation(PositionId, "*");
89+
world.registerLifecycleHook(wildcardPosition, { onAdded: callback });
90+
```
91+
92+
### Component ID Constraints
93+
94+
- Component IDs: 1-1023 (allocated via `component<T>(id)`)
95+
- Entity IDs: 1024+ (auto-generated)
96+
- Relation IDs: Negative encoded values (created via `relation()`)
97+
98+
### Memory Optimization
99+
100+
- Archetypes group entities by component signatures for cache-friendly iteration
101+
- Command buffer batches structural changes to minimize archetype transitions
102+
- Queries automatically cache matching archetypes and update on world changes
19103

20104
## Code Patterns
21105

22-
- **Imports**: Use ES module syntax with `.ts` extensions allowed due to `"allowImportingTsExtensions": true`
23-
- **Type Checking**: Strict mode with additional checks like `noUncheckedIndexedAccess` and `noImplicitOverride`
106+
### Imports & Exports
107+
108+
- Use ES module syntax with `.ts` extensions allowed due to `"allowImportingTsExtensions": true`
109+
- Main entry point: `src/index.ts` re-exports all public APIs
110+
- Import from source: `import { World, component } from "@codehz/ecs"` (library consumers)
111+
112+
### Error Handling
113+
114+
- Entity operations validate existence before modification
115+
- Component operations check type validity and entity existence
116+
- Command buffer prevents infinite loops with iteration limits
117+
118+
### Testing Conventions
119+
120+
- Use Bun's test framework: `import { describe, expect, it } from "bun:test"`
121+
- Test files: `*.test.ts` alongside implementation files
122+
- Performance tests: `*.perf.test.ts` for benchmark validation
123+
124+
## Integration Points
125+
126+
### Build System
127+
128+
- **Primary Build**: `scripts/build.ts` uses `Bun.build()` for bundling
129+
- **Type Generation**: `tsconfig.build.json` generates `.d.ts` files only
130+
- **Release Process**: `scripts/release.ts` handles versioning and packaging
24131

25-
## Architecture Notes
132+
### Development Tools
26133

27-
- **Entry Point**: `src/index.ts` - single file currently, expand with ECS components/systems
28-
- **Dependencies**: Minimal setup - add peer dependencies for TypeScript integration
134+
- **Formatting**: Prettier with lint-staged (auto-formats on commit)
135+
- **Git Hooks**: Husky manages pre-commit validation
136+
- **Type Validation**: Typia transform plugin for runtime type checking
29137

30-
## Key Files
138+
## Key Files Reference
31139

140+
- `src/world.ts`: Core World class with archetype management and command execution
141+
- `src/archetype.ts`: Archetype implementation for contiguous component storage
142+
- `src/entity.ts`: Entity/component ID system with relation encoding
143+
- `src/query.ts`: Query caching and iteration over matching entities
144+
- `src/command-buffer.ts`: Deferred execution for structural changes
145+
- `examples/simple/demo.ts`: Basic usage example with movement system
32146
- `tsconfig.json`: Modern TypeScript config with bundler mode
33-
- `package.json`: Module configuration with Bun-specific settings
34-
- `src/index.ts`: Main entry point (currently minimal)</content>
35-
<parameter name="filePath">d:\Developer\ecs\.github\copilot-instructions.md
147+
- `package.json`: Library configuration with Bun-specific settings

0 commit comments

Comments
 (0)