F5: Simulation core and player command stream
Add the fixed-tick Simulation, the EntityRegistry with never-reused IDs, SimRng's named seeded streams, and the 14-byte PlayerCommand used by humans, bots and the network. state_hash is a SHA-256 over a canonical encoding of the state. Movement is a flat placeholder until the v0.2 character controller. Tests replay a 24-player command log over 10,000 ticks and scan src/sim for unseeded randomness and presentation code. Closes #5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
## Owns every entity. IDs count up from 1 and are never reused within a match, and
|
||||
## iteration is always in ascending ID order.
|
||||
class_name EntityRegistry
|
||||
extends RefCounted
|
||||
|
||||
## Reserved: no entity has this ID.
|
||||
const NONE: int = 0
|
||||
|
||||
var _next_id: int = 1
|
||||
var _by_id: Dictionary = {}
|
||||
|
||||
|
||||
func create(archetype: int) -> SimEntity:
|
||||
var entity: SimEntity = SimEntity.new(_next_id, archetype)
|
||||
_next_id += 1
|
||||
# Dictionaries keep insertion order, and IDs only grow, so this stays sorted.
|
||||
_by_id[entity.id] = entity
|
||||
return entity
|
||||
|
||||
|
||||
## Returns the entity, or null if it doesn't exist or has been removed.
|
||||
func find(id: int) -> SimEntity:
|
||||
return _by_id.get(id)
|
||||
|
||||
|
||||
func remove(id: int) -> bool:
|
||||
return _by_id.erase(id)
|
||||
|
||||
|
||||
func contains(id: int) -> bool:
|
||||
return _by_id.has(id)
|
||||
|
||||
|
||||
func count() -> int:
|
||||
return _by_id.size()
|
||||
|
||||
|
||||
func next_id() -> int:
|
||||
return _next_id
|
||||
|
||||
|
||||
## All entities in ascending ID order. Safe to modify the registry while looping over this.
|
||||
func entities() -> Array[SimEntity]:
|
||||
var all: Array[SimEntity] = []
|
||||
for entity: SimEntity in _by_id.values():
|
||||
all.append(entity)
|
||||
return all
|
||||
Reference in New Issue
Block a user