final class MyNpcNameplateSystem extends EntityTickingSystem<EntityStore> {
private final ComponentType<EntityStore, NPCEntity> npcType;
private final ComponentType<EntityStore, NameplateData> nameplateDataType;
MyNpcNameplateSystem(ComponentType<EntityStore, NameplateData> nameplateDataType) {
this.npcType = NPCEntity.getComponentType();
this.nameplateDataType = nameplateDataType;
}
@Override
public Archetype<EntityStore> getQuery() {
// Only tick on entities that have an NPCEntity component
return Archetype.of(npcType);
}
@Override
public SystemGroup<EntityStore> getGroup() {
return EntityTrackerSystems.QUEUE_UPDATE_GROUP;
}
@Override
public void tick(float deltaTime, int index, ArchetypeChunk<EntityStore> chunk,
Store<EntityStore> store, CommandBuffer<EntityStore> commandBuffer) {
// Check if this is the right type of NPC
NPCEntity npc = chunk.getComponent(index, npcType);
if (npc == null || !"Kweebec".equals(npc.getRoleName())) {
return;
}
Ref<EntityStore> entityRef = chunk.getReferenceTo(index);
// Skip if this entity already has nameplate data (already initialized)
if (store.getComponent(entityRef, nameplateDataType) != null) {
return;
}
// First time seeing this entity - create the component manually
NameplateData data = new NameplateData();
data.setText("faction", "<Forest>");
data.setText("level", "Lv. 10");
// Use commandBuffer.putComponent() instead of store.addComponent()
// The CommandBuffer queues the change and applies it after the tick finishes
commandBuffer.putComponent(entityRef, nameplateDataType, data);
}
}