Critical: - deactivate_insert() now called when selecting non-Insert spoke, cancelling with no selection, or pressing Escape while insert is active. Fixes simulation staying paused permanently after Insert. Warnings: - Checklist conditions with empty id excluded from get_results() and get_total_count() — prevents impossible-to-complete checklists. Warns at load time when empty-id conditions are found. - _content_base now checks res://content/ first (exported builds), falls back to ../content for editor/dev mode. - 26 new tests for D-054 functions: _angle_to_octant (8 octants), _snap_to_octant_dir (9 cases incl. zero/tiny), _wasd_to_world_dir (8 facing/movement combos). New test file: test_input_mapper_facing.gd. Suggestions: - Cached get_theme_default_font() in checklist overlay _ready(). - Documented InputMapper → GameState coupling as intentional. - Documented YAML parser # truncation limitation. - _insert_active reset on Escape dismiss (Tyre #3). - SimBridge test mode SetFacing reads action_data.facing instead of InputMapper global (Tyre #4). - Removed dead _facing_to_rotation() from entity_renderer.gd (Tyre #5). - Fixed 2 failing facing indicator tests to use InputMapper.facing_angle instead of GameState.player_facing. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
161 lines
6.0 KiB
GDScript
161 lines
6.0 KiB
GDScript
class_name EntityRenderer
|
|
extends Node2D
|
|
|
|
# Entity renderer — manages entity sprites under the Entities node
|
|
# Creates/updates/removes ColorRect children based on entity data
|
|
# Entity format (from Protocol v2): {entity_id, x, y, z, kind: {variant, data}, visibility}
|
|
#
|
|
# Position lerping: entity sprites smoothly slide between tiles instead of snapping.
|
|
# The server moves entities in discrete tile steps; the lerp makes this look fluid.
|
|
# Speed is tuned so Sprint feels snappy and Walk/Careful/Crouch feel deliberate.
|
|
#
|
|
# D-033 colors: Phase 1 defaults by entity kind. Phase 2 (#361) will derive
|
|
# color from RelationshipState via the knowledge graph.
|
|
|
|
const TILE_SIZE: int = Constants.TILE_SIZE
|
|
const ENTITY_SIZE: int = 24
|
|
const ENTITY_OFFSET: float = (TILE_SIZE - ENTITY_SIZE) / 2.0 # center within tile
|
|
|
|
# Lerp speed — framerate-independent exponential smoothing.
|
|
# At 12.0: ~70% there after 0.1s, ~95% after 0.25s.
|
|
# Fast enough for Sprint snappiness, slow enough for Walk to show sliding.
|
|
const LERP_SPEED: float = 12.0
|
|
|
|
var entity_nodes: Dictionary = {} # entity_id -> Node2D
|
|
var _entity_targets: Dictionary = {} # entity_id -> Vector2 (target pixel position)
|
|
|
|
func _ready() -> void:
|
|
print("EntityRenderer: Initialized")
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
# Lerp all entity visual positions toward their targets each frame.
|
|
# Uses framerate-independent exponential smoothing.
|
|
var weight := 1.0 - exp(-LERP_SPEED * delta)
|
|
for entity_id in entity_nodes.keys():
|
|
if not _entity_targets.has(entity_id):
|
|
continue
|
|
var node = entity_nodes[entity_id]
|
|
var target: Vector2 = _entity_targets[entity_id]
|
|
if not node.position.is_equal_approx(target):
|
|
node.position = node.position.lerp(target, weight)
|
|
|
|
|
|
# Update entities from snapshot data
|
|
func update_entities(entities: Array) -> void:
|
|
var active_ids: Array = []
|
|
|
|
# Create or update entities
|
|
for entity_data in entities:
|
|
if not entity_data.has("entity_id"):
|
|
continue
|
|
|
|
var entity_id = entity_data.entity_id
|
|
active_ids.append(entity_id)
|
|
|
|
# Create entity node if it doesn't exist
|
|
if not entity_nodes.has(entity_id):
|
|
_create_entity_node(entity_id, entity_data)
|
|
else:
|
|
_update_entity_node(entity_id, entity_data)
|
|
|
|
# Remove entities that are no longer visible
|
|
var ids_to_remove: Array = []
|
|
for entity_id in entity_nodes.keys():
|
|
if entity_id not in active_ids:
|
|
ids_to_remove.append(entity_id)
|
|
|
|
for entity_id in ids_to_remove:
|
|
_remove_entity_node(entity_id)
|
|
|
|
# Create a new entity node with D-033 color and optional facing indicator
|
|
func _create_entity_node(entity_id: int, entity_data: Dictionary) -> void:
|
|
var entity_node = ColorRect.new()
|
|
entity_node.name = "Entity_" + str(entity_id)
|
|
entity_node.size = Vector2(ENTITY_SIZE, ENTITY_SIZE)
|
|
entity_node.pivot_offset = Vector2(ENTITY_SIZE / 2.0, ENTITY_SIZE / 2.0)
|
|
|
|
# D-033 color by entity kind (Phase 1 default)
|
|
# TODO(#361): derive from RelationshipState via knowledge graph
|
|
entity_node.color = _color_for_kind(entity_data)
|
|
|
|
add_child(entity_node)
|
|
entity_nodes[entity_id] = entity_node
|
|
|
|
# Add facing indicator for the player entity
|
|
if entity_id == GameState.player_entity_id:
|
|
_add_facing_indicator(entity_node)
|
|
|
|
# Snap to initial position (no lerp on first appearance)
|
|
if entity_data.has("x") and entity_data.has("y"):
|
|
var target := Vector2(
|
|
floorf(entity_data.x) * TILE_SIZE + ENTITY_OFFSET,
|
|
floorf(entity_data.y) * TILE_SIZE + ENTITY_OFFSET
|
|
)
|
|
entity_node.position = target
|
|
_entity_targets[entity_id] = target
|
|
|
|
_update_entity_node(entity_id, entity_data)
|
|
|
|
# Update an existing entity node (target position, visibility dimming, facing)
|
|
func _update_entity_node(entity_id: int, entity_data: Dictionary) -> void:
|
|
if not entity_nodes.has(entity_id):
|
|
return
|
|
|
|
var entity_node = entity_nodes[entity_id]
|
|
|
|
# Update target position — the lerp in _process() will smoothly move there.
|
|
# Server sends tile-center coords (tile 16 → 16.5), floor to get tile index.
|
|
if entity_data.has("x") and entity_data.has("y"):
|
|
_entity_targets[entity_id] = Vector2(
|
|
floorf(entity_data.x) * TILE_SIZE + ENTITY_OFFSET,
|
|
floorf(entity_data.y) * TILE_SIZE + ENTITY_OFFSET
|
|
)
|
|
|
|
# v2: Peripheral vision dimming (D-015)
|
|
# null visibility (v1 backward compat) defaults to full alpha
|
|
var visibility: Variant = entity_data.get("visibility")
|
|
var target_alpha := Constants.PERIPHERAL_ALPHA if visibility == "Peripheral" else 1.0
|
|
if not is_equal_approx(entity_node.modulate.a, target_alpha):
|
|
entity_node.modulate.a = target_alpha
|
|
|
|
# D-054: Update facing indicator from client-side mouse angle (not server).
|
|
# InputMapper.facing_angle is a continuous float — smoother than octant snapping.
|
|
if entity_id == GameState.player_entity_id:
|
|
var indicator = entity_node.get_node_or_null("FacingIndicator")
|
|
if indicator != null:
|
|
# facing_angle: 0=East, -PI/2=North. Indicator: 0=North (up).
|
|
# Rotate from North basis: add PI/2 to convert.
|
|
indicator.rotation = InputMapper.facing_angle + PI / 2.0
|
|
|
|
# Remove an entity node
|
|
func _remove_entity_node(entity_id: int) -> void:
|
|
if not entity_nodes.has(entity_id):
|
|
return
|
|
|
|
var entity_node = entity_nodes[entity_id]
|
|
entity_node.queue_free()
|
|
entity_nodes.erase(entity_id)
|
|
_entity_targets.erase(entity_id)
|
|
|
|
# D-033 color by entity kind — delegates to Constants.color_for_entity_kind
|
|
static func _color_for_kind(entity_data: Dictionary) -> Color:
|
|
return Constants.color_for_entity_kind(entity_data)
|
|
|
|
# Add a facing direction indicator triangle to the player entity
|
|
func _add_facing_indicator(parent_node: Control) -> void:
|
|
var indicator := Polygon2D.new()
|
|
indicator.name = "FacingIndicator"
|
|
var s := Constants.FACING_INDICATOR_SIZE
|
|
var offset := Constants.FACING_INDICATOR_OFFSET
|
|
# Triangle pointing up (North), offset from center. Rotates around (0,0).
|
|
indicator.polygon = PackedVector2Array([
|
|
Vector2(0, -offset - s),
|
|
Vector2(-s * 0.6, -offset + s * 0.4),
|
|
Vector2(s * 0.6, -offset + s * 0.4),
|
|
])
|
|
indicator.color = Constants.ENTITY_COLOR_PLAYER
|
|
# Position at center of parent ColorRect — rotation around this point
|
|
indicator.position = Vector2(ENTITY_SIZE / 2.0, ENTITY_SIZE / 2.0)
|
|
parent_node.add_child(indicator)
|