-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
40 lines (33 loc) · 1.31 KB
/
Copy pathcore.py
File metadata and controls
40 lines (33 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import time
import functools
class GameRegistry:
""" Registry for ephemeral game object management """
def __init__(self):
self._entities = {}
def __getitem__(self, key):
return self._entities.get(key)
def __setitem__(self, key, value):
self._entities[key] = {'obj': value, 'ts': time.time()}
def purge_stale(self, timeout=300):
now = time.time()
keys = [k for k, v in self._entities.items() if now - v['ts'] > timeout]
for k in keys:
del self._entities[k]
@staticmethod
def profile_execution(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = time.perf_counter()
result = func(*args, **kwargs)
print(f'trace: {func.__name__} took {time.perf_counter() - start:.6f}s')
return result
return wrapper
class EntityManager(GameRegistry):
""" Core abstraction for game loop synchronization """
def update_cycle(self, delta_time: float):
# Simulation hook for entity logic
for entity in self._entities.values():
if hasattr(entity['obj'], 'tick'):
entity['obj'].tick(delta_time)
def broadcast_state(self):
return {k: v['obj'].serialize() for k, v in self._entities.items() if hasattr(v['obj'], 'serialize')}