Each spawned entity should get a unique id. Rather than a clock or random source, the registry holds a counter starting at 1. Every create() assigns the current counter value to the clone's id, then increments the counter.
Update create() to stamp a fresh id on each clone. The prototype keeps its own id.
import copy
class Entity:
def __init__(self, name, tags=None, id=0):
self.name = name
self.tags = list(tags) if tags is not None else []
self.id = id
def clone(self):
"""Return an independent deep copy of this entity."""
return copy.deepcopy(self)
class PrototypeRegistry:
def __init__(self):
self._prototypes = {}
self._next_id = 1
def register(self, name, prototype):
self._prototypes[name] = prototype
def create(self, name):
"""Return a fresh clone with a unique id."""
if name not in self._prototypes:
raise KeyError(name)
obj = self._prototypes[name].clone()
# TODO: stamp a fresh id from self._next_id, then increment the counter
return obj
def spawn(self, name, /, **overrides):
obj = self.create(name)
for key, value in overrides.items():
setattr(obj, key, value)
return obj
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Each spawned entity should get a unique id. Rather than a clock or random source, the registry holds a counter starting at 1. Every create() assigns the current counter value to the clone's id, then increments the counter.
Update create() to stamp a fresh id on each clone. The prototype keeps its own id.
Press Run Tests or ββ© to check your solution.