地图组件和ECS框架完成
This commit is contained in:
39
lib/ecs/entity.dart
Normal file
39
lib/ecs/entity.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'component.dart';
|
||||
/// 实体ID生成器
|
||||
class EntityIdGenerator {
|
||||
static int _id = 0;
|
||||
static int nextId() => _id++;
|
||||
}
|
||||
|
||||
/// 实体类 - 代表地图上的一个对象
|
||||
class Entity {
|
||||
final int id;
|
||||
final String name;
|
||||
int entityType;
|
||||
final Map<String, Component> _components = {};
|
||||
|
||||
Entity({int? id, required this.name, this.entityType = 0}) : id = id ?? EntityIdGenerator.nextId();
|
||||
|
||||
/// 添加组件
|
||||
void addComponent<T extends Component>(T component) {
|
||||
_components[T.toString()] = component;
|
||||
}
|
||||
|
||||
/// 获取组件
|
||||
T? getComponent<T extends Component>() {
|
||||
return _components[T.toString()] as T?;
|
||||
}
|
||||
|
||||
/// 检查是否有特定类型的组件
|
||||
bool hasComponent<T extends Component>() {
|
||||
return _components.containsKey(T.toString());
|
||||
}
|
||||
|
||||
/// 移除组件
|
||||
void removeComponent<T extends Component>() {
|
||||
_components.remove(T.toString());
|
||||
}
|
||||
|
||||
/// 获取所有组件
|
||||
Map<String, Component> get components => _components;
|
||||
}
|
||||
Reference in New Issue
Block a user