39 lines
969 B
Dart
39 lines
969 B
Dart
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;
|
|
} |