The client program now turns a connection into a match someone can play. - ClientSession: a ClientGame, the keyboard and mouse as commands (ClientInput), and a MatchView of the client's own copy of the world. - ClientMirror writes what the client is showing into that world every frame: other players from the recent past, its own player where it was predicted, items, the barge, the clock and the sea. The existing views draw it unchanged. - LocalServer: offline play is the same ServerLoop and NetGame in the same process, with packets handed across in memory, so there is one path. - main.gd: --connect=HOST[:PORT] joins a server, otherwise an offline match; --autoplay and --client-seconds=N for scripts and checks. - The camera aboard the barge follows the player's own place on the deck. Tests drive a scripted client through a whole match on a real server over loopback UDP, lobby to results, and through an offline match. `make client-check` does it with two real processes, and `make client-captures` renders what the client sees. Closes #77 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
65 lines
1.6 KiB
GDScript
65 lines
1.6 KiB
GDScript
## 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
|
|
|
|
|
|
## Creates an entity under an ID chosen elsewhere: a network client's copy of one the
|
|
## server made. Iteration stays in ascending ID order.
|
|
func restore(id: int, archetype: int) -> SimEntity:
|
|
var entity: SimEntity = SimEntity.new(id, archetype)
|
|
var newest: bool = id >= _next_id
|
|
_by_id[id] = entity
|
|
_next_id = maxi(_next_id, id + 1)
|
|
if not newest:
|
|
var ids: Array = _by_id.keys()
|
|
ids.sort()
|
|
var sorted: Dictionary = {}
|
|
for key: int in ids:
|
|
sorted[key] = _by_id[key]
|
|
_by_id = sorted
|
|
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
|