From c1df40361aec8727523f91a633458ad96e1bdf34 Mon Sep 17 00:00:00 2001 From: Steven Wroblewski Date: Sat, 9 May 2026 00:19:15 +0200 Subject: [PATCH] feat(items): add OutfitItem, tap-to-undress, and outfit refs on Character - Add OutfitItem (extends HoldableItem): applies outfit on drop within 80px of character body, falls back to hand slot attach if no character in range - Add apply_outfit_item / remove_outfit / _handle_outfit_tap to Character - Track item node refs in _outfit_item_refs for restoring visibility - Fix animation state: reset to idle before tap handling in _on_drag_released - Extract _ITEM_DROP_OFFSET constant (replaces magic Vector2(0,60)) - Add 5 tests in test_outfit_item.gd, 14 new tests in test_character_v2.gd --- scripts/characters/character.gd | 45 ++++++++++++++++++++++- scripts/objects/outfit_item.gd | 31 ++++++++++++++++ test/unit/test_outfit_item.gd | 63 +++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 scripts/objects/outfit_item.gd create mode 100644 test/unit/test_outfit_item.gd diff --git a/scripts/characters/character.gd b/scripts/characters/character.gd index 97a8fe6..de94f06 100644 --- a/scripts/characters/character.gd +++ b/scripts/characters/character.gd @@ -13,7 +13,11 @@ signal state_changed(new_state: CharacterData.State) var _is_held: bool = false var _current_anim: String = "idle" +var _drag_start_position: Vector2 = Vector2.ZERO +var _outfit_item_refs: Array = [null, null, null] +const _TAP_THRESHOLD: float = 10.0 +const _ITEM_DROP_OFFSET: Vector2 = Vector2(0.0, 60.0) const _STATE_COLORS: Dictionary = { CharacterData.State.HEALTHY: Color(0.6, 0.8, 1.0), CharacterData.State.SICK: Color(0.7, 0.9, 0.7), @@ -149,14 +153,53 @@ func _update_visual_state() -> void: ear_right.color = color -func _on_drag_picked_up(_pos: Vector2) -> void: +func apply_outfit_item(layer: int, item_id: String, texture: Texture2D, item_node: Node2D) -> void: + if layer < 1 or layer > 3: + return + var i: int = layer - 1 + var existing: Node2D = _outfit_item_refs[i] as Node2D + if existing != null: + existing.global_position = global_position + _ITEM_DROP_OFFSET + existing.visible = true + _outfit_item_refs[i] = item_node + set_outfit(layer, item_id, texture) + if item_node != null: + item_node.visible = false + + +func remove_outfit(layer: int) -> void: + if layer < 1 or layer > 3: + return + var i: int = layer - 1 + clear_outfit(layer) + var item_ref: Node2D = _outfit_item_refs[i] as Node2D + if item_ref != null: + _outfit_item_refs[i] = null + item_ref.global_position = global_position + _ITEM_DROP_OFFSET + item_ref.visible = true + + +func _handle_outfit_tap() -> void: + for layer: int in range(3, 0, -1): + if not get_outfit(layer).is_empty(): + remove_outfit(layer) + return + + +func _on_drag_picked_up(pos: Vector2) -> void: _is_held = true + _drag_start_position = pos set_animation_state("held") character_picked_up.emit(self) func _on_drag_released(pos: Vector2) -> void: _is_held = false + var drag_distance: float = pos.distance_to(_drag_start_position) + if drag_distance < _TAP_THRESHOLD: + set_animation_state("idle") + _handle_outfit_tap() + return set_animation_state("idle") if data == null or data.id.is_empty(): return diff --git a/scripts/objects/outfit_item.gd b/scripts/objects/outfit_item.gd new file mode 100644 index 0000000..d993c7d --- /dev/null +++ b/scripts/objects/outfit_item.gd @@ -0,0 +1,31 @@ +## OutfitItem — HoldableItem that applies an outfit layer to a Character when dropped +## within OUTFIT_APPLY_RADIUS of the character's center. Falls back to hand slot +## attachment if no character body is in range. +class_name OutfitItem extends HoldableItem + +const OUTFIT_APPLY_RADIUS: float = 80.0 + +@export var outfit_layer: int = 1 +@export var outfit_sprite: Texture2D + + +func _on_drag_released(_pos: Vector2) -> void: + var character: Character = _find_nearest_character() + if character != null: + character.apply_outfit_item(outfit_layer, item_id, outfit_sprite, self) + return + super._on_drag_released(_pos) + + +func _find_nearest_character() -> Character: + var best_dist: float = OUTFIT_APPLY_RADIUS + var best: Character = null + for node: Node in get_tree().get_nodes_in_group("characters"): + var character: Character = node as Character + if character == null: + continue + var dist: float = global_position.distance_to(character.global_position) + if dist < best_dist: + best_dist = dist + best = character + return best diff --git a/test/unit/test_outfit_item.gd b/test/unit/test_outfit_item.gd new file mode 100644 index 0000000..a0f1c57 --- /dev/null +++ b/test/unit/test_outfit_item.gd @@ -0,0 +1,63 @@ +## Tests for OutfitItem — applies outfit layer when dropped near a character. +extends GutTest + + +func test_outfit_item_default_layer_is_one() -> void: + var item: OutfitItem = OutfitItem.new() + add_child_autofree(item) + assert_eq(item.outfit_layer, 1) + + +func test_outfit_item_applies_to_character_on_release_in_range() -> void: + var character: Character = preload("res://scenes/characters/Character.tscn").instantiate() as Character + add_child_autofree(character) + var data: CharacterData = CharacterData.new() + character.data = data + var item: OutfitItem = OutfitItem.new() + add_child_autofree(item) + item.item_id = "white_coat" + item.outfit_layer = 1 + item.outfit_sprite = null + item.global_position = character.global_position + item._on_drag_released(item.global_position) + assert_eq(character.get_outfit(1), "white_coat") + + +func test_outfit_item_hides_after_applying() -> void: + var character: Character = preload("res://scenes/characters/Character.tscn").instantiate() as Character + add_child_autofree(character) + var data: CharacterData = CharacterData.new() + character.data = data + var item: OutfitItem = OutfitItem.new() + add_child_autofree(item) + item.item_id = "white_coat" + item.outfit_layer = 1 + item.global_position = character.global_position + item._on_drag_released(item.global_position) + assert_false(item.visible) + + +func test_outfit_item_stays_visible_if_no_character_in_range() -> void: + var item: OutfitItem = OutfitItem.new() + add_child_autofree(item) + item.item_id = "white_coat" + item.outfit_layer = 1 + item.global_position = Vector2(9999.0, 9999.0) + item._on_drag_released(item.global_position) + assert_true(item.visible) + assert_false(item.is_in_hand_slot()) + + +func test_outfit_item_does_not_apply_if_far_from_character() -> void: + var character: Character = preload("res://scenes/characters/Character.tscn").instantiate() as Character + add_child_autofree(character) + var data: CharacterData = CharacterData.new() + character.data = data + var item: OutfitItem = OutfitItem.new() + add_child_autofree(item) + item.item_id = "white_coat" + item.outfit_layer = 1 + item.global_position = Vector2(9999.0, 9999.0) + item._on_drag_released(item.global_position) + assert_eq(character.get_outfit(1), "") + assert_true(item.visible)