From f3793bf16e8084c95c021813452dfbe73fe2f3b7 Mon Sep 17 00:00:00 2001 From: Jeroen Schweitzer Date: Thu, 12 Feb 2026 00:53:28 +0100 Subject: [PATCH] feat(client): D-033 entity colors, peripheral dimming, facing indicator (#130) Replace hardcoded entity colors with D-033 relationship palette: Player=#e0e8ff, Npc=unknown teal #4a9ebb, Object=grey #8b8ba0. Phase 1 defaults by entity kind; Phase 2 (#361) will derive color from RelationshipState via knowledge graph. Entities in peripheral vision dimmed to 50% alpha (D-015). Player entity gets a Polygon2D triangle indicator showing facing direction, rotated from GameState.player_facing. 82 tests total, 0 failures. Co-Authored-By: Claude Opus 4.6 --- client/scripts/constants.gd | 17 ++++ client/scripts/rendering/entity_renderer.gd | 82 ++++++++++++++--- client/tests/test_rendering.gd | 98 +++++++++++++++++++++ 3 files changed, 184 insertions(+), 13 deletions(-) diff --git a/client/scripts/constants.gd b/client/scripts/constants.gd index 93eef2669..4940cb390 100644 --- a/client/scripts/constants.gd +++ b/client/scripts/constants.gd @@ -4,3 +4,20 @@ class_name Constants ## Tile size in pixels — all renderers and coordinate conversions use this. const TILE_SIZE: int = 32 + +# D-033: Entity relationship color palette +# Color represents the player's RELATIONSHIP to the entity, not an objective property. +# Phase 1: default colors by entity kind. Phase 2 (#361): driven by RelationshipState. +const ENTITY_COLOR_UNKNOWN: Color = Color("#4a9ebb") # Unknown/Neutral — cool teal +const ENTITY_COLOR_FRIENDLY: Color = Color("#6bc9a6") # Known/Friendly — soft green +const ENTITY_COLOR_POI: Color = Color("#e8c547") # Person of Interest — warm amber +const ENTITY_COLOR_HOSTILE: Color = Color("#d45d5d") # Hostile/Dangerous — muted red +const ENTITY_COLOR_OBJECT: Color = Color("#8b8ba0") # Static objects — muted grey +const ENTITY_COLOR_PLAYER: Color = Color("#e0e8ff") # Player character (detective) + +# D-015: Peripheral vision dimming +const PERIPHERAL_ALPHA: float = 0.5 + +# Facing direction indicator +const FACING_INDICATOR_SIZE: float = 6.0 +const FACING_INDICATOR_OFFSET: float = 14.0 diff --git a/client/scripts/rendering/entity_renderer.gd b/client/scripts/rendering/entity_renderer.gd index 8bf9847bc..8cb01224e 100644 --- a/client/scripts/rendering/entity_renderer.gd +++ b/client/scripts/rendering/entity_renderer.gd @@ -3,7 +3,10 @@ extends Node2D # Entity renderer — manages entity sprites under the Entities node # Creates/updates/removes ColorRect children based on entity data -# Entity format (from Protocol): {entity_id, x, y, z, kind: {variant, data}} +# Entity format (from Protocol v2): {entity_id, x, y, z, kind: {variant, data}, visibility} +# +# 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 @@ -41,30 +44,27 @@ func update_entities(entities: Array) -> void: for entity_id in ids_to_remove: _remove_entity_node(entity_id) -# Create a new entity node (placeholder visual) +# 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) - # Color based on entity kind (from Protocol decoded format). - # TODO(#130): replace with D-033 relationship colors (teal/green/amber/red). - var kind_variant: String = entity_data.get("kind", {}).get("variant", "") - match kind_variant: - "Npc": - entity_node.color = Color(0.3, 0.6, 0.9) # Placeholder blue - "Player": - entity_node.color = Color(0.88, 0.91, 1.0) # Placeholder light blue - _: - entity_node.color = Color(0.8, 0.8, 0.8) # Gray for unknown + # 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) + _update_entity_node(entity_id, entity_data) -# Update an existing entity node +# Update an existing entity node (position, visibility dimming, facing) func _update_entity_node(entity_id: int, entity_data: Dictionary) -> void: if not entity_nodes.has(entity_id): return @@ -78,6 +78,19 @@ func _update_entity_node(entity_id: int, entity_data: Dictionary) -> void: entity_data.y * TILE_SIZE + ENTITY_OFFSET ) + # v2: Peripheral vision dimming (D-015) + var visibility: Variant = entity_data.get("visibility") + if visibility == "Peripheral": + entity_node.modulate.a = Constants.PERIPHERAL_ALPHA + else: + entity_node.modulate.a = 1.0 + + # v2: Update facing indicator rotation (player entity only) + if entity_id == GameState.player_entity_id: + var indicator = entity_node.get_node_or_null("FacingIndicator") + if indicator != null: + indicator.rotation = _facing_to_rotation(GameState.player_facing) + # Remove an entity node func _remove_entity_node(entity_id: int) -> void: if not entity_nodes.has(entity_id): @@ -86,3 +99,46 @@ func _remove_entity_node(entity_id: int) -> void: var entity_node = entity_nodes[entity_id] entity_node.queue_free() entity_nodes.erase(entity_id) + +# D-033 color by entity kind (Phase 1: defaults by kind, not relationship) +static func _color_for_kind(entity_data: Dictionary) -> Color: + var kind_variant: String = entity_data.get("kind", {}).get("variant", "") + match kind_variant: + "Player": + return Constants.ENTITY_COLOR_PLAYER + "Npc": + return Constants.ENTITY_COLOR_UNKNOWN + "Object", "Terrain": + return Constants.ENTITY_COLOR_OBJECT + _: + return Constants.ENTITY_COLOR_OBJECT + +# 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) + +# Convert facing direction string to rotation in radians (0 = North/up) +static func _facing_to_rotation(facing: String) -> float: + match facing: + "North": return 0.0 + "Northeast": return PI / 4.0 + "East": return PI / 2.0 + "Southeast": return 3.0 * PI / 4.0 + "South": return PI + "Southwest": return 5.0 * PI / 4.0 + "West": return 3.0 * PI / 2.0 + "Northwest": return 7.0 * PI / 4.0 + _: return 0.0 diff --git a/client/tests/test_rendering.gd b/client/tests/test_rendering.gd index c51fa31d8..4f0eecbd4 100644 --- a/client/tests/test_rendering.gd +++ b/client/tests/test_rendering.gd @@ -22,6 +22,11 @@ var _test_entities: Array = [ {"entity_id": 2, "x": 7.0, "y": 5.0, "z": 0, "kind": {"variant": "Npc", "data": null}}, ] +var _test_entities_v2: Array = [ + {"entity_id": 1, "x": 5.0, "y": 5.0, "z": 0, "kind": {"variant": "Player", "data": null}, "visibility": "Forward"}, + {"entity_id": 2, "x": 7.0, "y": 5.0, "z": 0, "kind": {"variant": "Npc", "data": null}, "visibility": "Peripheral"}, +] + # -- Constants -- @@ -72,6 +77,27 @@ func test_game_state_same_count_different_visibility_new_tick() -> void: assert_that(GameState.visible_positions.has(Vector2i(1, 1))).is_false() assert_that(GameState.visible_positions.has(Vector2i(2, 2))).is_true() +func test_game_state_stores_game_time() -> void: + var gt := {"day": 1, "time_of_day": 720, "day_phase": "Evening", "paused": false} + GameState.apply_snapshot({"tick": 1, "game_time": gt}) + assert_that(GameState.game_time.day).is_equal(1) + assert_that(GameState.game_time.time_of_day).is_equal(720) + +func test_game_state_stores_player_facing() -> void: + GameState.apply_snapshot({"tick": 1, "player_facing": "Southeast"}) + assert_that(GameState.player_facing).is_equal("Southeast") + +func test_game_state_derives_visible_positions_from_visible_tiles() -> void: + var vtiles := [ + {"x": 5, "y": 5, "z": 0, "visibility": "Forward"}, + {"x": 6, "y": 5, "z": 0, "visibility": "Peripheral"}, + ] + GameState.apply_snapshot({"tick": 1, "visible_tiles": vtiles}) + assert_that(GameState.visible_positions.has(Vector2i(5, 5))).is_true() + assert_that(GameState.visible_positions.has(Vector2i(6, 5))).is_true() + assert_that(GameState.visibility_sectors[Vector2i(5, 5)]).is_equal("Forward") + assert_that(GameState.visibility_sectors[Vector2i(6, 5)]).is_equal("Peripheral") + func test_game_state_warns_on_missing_player() -> void: GameState.player_entity_id = 999 GameState.player_position = Vector2(5, 5) @@ -130,6 +156,20 @@ func test_sim_bridge_test_tiles_contain_all_types() -> void: assert_that(types.has("wall")).is_true() assert_that(types.has("door")).is_true() +func test_sim_bridge_test_snapshot_has_v2_fields() -> void: + SimBridge._test_tick = 0 + var snap = SimBridge._test_snapshot() + assert_that(snap.has("version")).is_true() + assert_that(snap.version).is_equal(2) + assert_that(snap.has("game_time")).is_true() + assert_that(snap.has("player_facing")).is_true() + assert_that(snap.has("visible_tiles")).is_true() + assert_that(snap.visible_tiles.size()).is_greater(0) + var vtile = snap.visible_tiles[0] + assert_that(vtile.has("visibility")).is_true() + # Entities should have visibility + assert_that(snap.entities[0].has("visibility")).is_true() + # -- EntityRenderer: lifecycle -- @@ -195,6 +235,64 @@ func test_entity_renderer_empty_entities_clears_all() -> void: renderer.queue_free() +# -- EntityRenderer: D-033 colors and v2 features -- + +func test_entity_renderer_player_uses_d033_color() -> void: + var renderer := _make_entity_renderer() + renderer.update_entities(_test_entities_v2) + var player_node = renderer.entity_nodes[1] as ColorRect + assert_that(player_node.color).is_equal(Constants.ENTITY_COLOR_PLAYER) + renderer.queue_free() + +func test_entity_renderer_npc_uses_unknown_teal() -> void: + var renderer := _make_entity_renderer() + renderer.update_entities(_test_entities_v2) + var npc_node = renderer.entity_nodes[2] as ColorRect + assert_that(npc_node.color).is_equal(Constants.ENTITY_COLOR_UNKNOWN) + renderer.queue_free() + +func test_entity_renderer_object_uses_grey() -> void: + var renderer := _make_entity_renderer() + var obj := [{"entity_id": 3, "x": 1.0, "y": 1.0, "z": 0, "kind": {"variant": "Object", "data": null}, "visibility": "Forward"}] + renderer.update_entities(obj) + var node = renderer.entity_nodes[3] as ColorRect + assert_that(node.color).is_equal(Constants.ENTITY_COLOR_OBJECT) + renderer.queue_free() + +func test_entity_renderer_peripheral_entity_dimmed() -> void: + var renderer := _make_entity_renderer() + renderer.update_entities(_test_entities_v2) + var npc_node = renderer.entity_nodes[2] + assert_that(npc_node.modulate.a).is_equal_approx(Constants.PERIPHERAL_ALPHA, 0.01) + renderer.queue_free() + +func test_entity_renderer_forward_entity_full_alpha() -> void: + var renderer := _make_entity_renderer() + renderer.update_entities(_test_entities_v2) + var player_node = renderer.entity_nodes[1] + assert_that(player_node.modulate.a).is_equal_approx(1.0, 0.01) + renderer.queue_free() + +func test_entity_renderer_player_has_facing_indicator() -> void: + GameState.player_entity_id = 1 + var renderer := _make_entity_renderer() + renderer.update_entities(_test_entities_v2) + var player_node = renderer.entity_nodes[1] + var indicator = player_node.get_node_or_null("FacingIndicator") + assert_that(indicator != null).is_true() + assert_that(indicator is Polygon2D).is_true() + renderer.queue_free() + +func test_entity_renderer_npc_has_no_facing_indicator() -> void: + GameState.player_entity_id = 1 + var renderer := _make_entity_renderer() + renderer.update_entities(_test_entities_v2) + var npc_node = renderer.entity_nodes[2] + var indicator = npc_node.get_node_or_null("FacingIndicator") + assert_that(indicator == null).is_true() + renderer.queue_free() + + # -- FogRenderer: position registration -- func _make_fog_renderer() -> TileMapLayer: