From 2c9d3200f840938184d66596b175fa2df29e9831 Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 16 Dec 2025 22:39:50 -0500 Subject: [PATCH] Changed Escape, portal spawns at spawn to exit. --- scenes/dungeon/dungeon.tscn | 2 + scenes/dungeon/player.tscn | 4 +- scenes/dungeon/portal.tscn | 88 ++++++++++++++++++++++++++++++ scripts/autoload/game_state.gd | 47 +++++++++++++++- scripts/autoload/opcodes.gd | 5 +- scripts/dungeon/dungeon_manager.gd | 16 ++++-- scripts/ui/hud.gd | 2 +- 7 files changed, 155 insertions(+), 9 deletions(-) create mode 100644 scenes/dungeon/portal.tscn diff --git a/scenes/dungeon/dungeon.tscn b/scenes/dungeon/dungeon.tscn index 106faa2..1727842 100644 --- a/scenes/dungeon/dungeon.tscn +++ b/scenes/dungeon/dungeon.tscn @@ -23,6 +23,8 @@ tile_set = ExtResource("2_frwky") [node name="DroppedItems" type="Node2D" parent="Entities"] +[node name="Portals" type="Node2D" parent="Entities"] + [node name="Player" parent="." instance=ExtResource("2_player")] [node name="Hud" parent="." instance=ExtResource("2_4f38h")] diff --git a/scenes/dungeon/player.tscn b/scenes/dungeon/player.tscn index dfac11b..ca4048b 100644 --- a/scenes/dungeon/player.tscn +++ b/scenes/dungeon/player.tscn @@ -70,5 +70,7 @@ offset_bottom = 25.0 value = 100.0 show_percentage = false -[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +[node name="CharacterBody2D" type="CharacterBody2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] shape = SubResource("CircleShape2D_collision") diff --git a/scenes/dungeon/portal.tscn b/scenes/dungeon/portal.tscn new file mode 100644 index 0000000..0fbf133 --- /dev/null +++ b/scenes/dungeon/portal.tscn @@ -0,0 +1,88 @@ +[gd_scene load_steps=3 format=3 uid="uid://brn46teujr2e6"] + +[sub_resource type="GDScript" id="GDScript_portal"] +script/source = "extends Node2D + +@onready var sprite: Sprite2D = $Sprite +@onready var area: Area2D = $Area2D +@onready var collision: CollisionShape2D = $Area2D/CollisionShape2D + +var portal_data: Dictionary = {} +var portal_id: int = 0 +var portal_type: String = \"exit\" + +# Pulsing animation +var pulse_time: float = 0.0 + +func _ready(): + # Generate portal sprite + if SpriteGenerator: + sprite.texture = SpriteGenerator.get_circle() + + # Set portal color (purple/blue swirl effect via modulate) + sprite.modulate = Color(0.6, 0.4, 1.0) # Purple tint + + # Connect area signal for player interaction + if area: + area.body_entered.connect(_on_body_entered) + area.input_event.connect(_on_input_event) + +func _process(delta): + # Pulsing effect + pulse_time += delta * 3.0 + var pulse = 0.9 + sin(pulse_time) * 0.1 + sprite.scale = Vector2(pulse, pulse) + + # Color cycling for magical effect + var hue_shift = sin(pulse_time * 0.5) * 0.1 + sprite.modulate = Color.from_hsv(0.75 + hue_shift, 0.6, 1.0) + +func update_data(data: Dictionary): + portal_data = data + portal_id = data.get(\"id\", data.get(\"Id\", 0)) + portal_type = data.get(\"type\", data.get(\"Type\", \"exit\")) + + # Color based on type + match portal_type: + \"exit\": + sprite.modulate = Color(0.6, 0.4, 1.0) # Purple + \"nextfloor\": + sprite.modulate = Color(0.2, 0.8, 0.2) # Green + \"secret\": + sprite.modulate = Color(1.0, 0.8, 0.2) # Gold + +func _on_body_entered(body): + # Auto-use portal when player walks into it + print(\"Player Entered Portal \",body.name) + if body.name == \"CharacterBody2D\": + _use_portal() + +func _on_input_event(_viewport, event, _shape_idx): + # Also allow clicking the portal + if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT: + # Check if player is close enough + var player_pos = GameState.self_player.get(\"x\", 0) + var player_y = GameState.self_player.get(\"y\", 0) + var dist = Vector2(player_pos, player_y).distance_to(position) + if dist < 64: + _use_portal() + +func _use_portal(): + print(\"[Portal] Using portal %d (type: %s)\" % [portal_id, portal_type]) + GameState.send_use_portal(portal_id) +" + +[sub_resource type="CircleShape2D" id="CircleShape2D_portal"] +radius = 35.128338 + +[node name="Portal" type="Node2D"] +script = SubResource("GDScript_portal") + +[node name="Sprite" type="Sprite2D" parent="."] +modulate = Color(0.6, 0.4, 1, 1) +scale = Vector2(1.2, 1.2) + +[node name="Area2D" type="Area2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] +shape = SubResource("CircleShape2D_portal") diff --git a/scripts/autoload/game_state.gd b/scripts/autoload/game_state.gd index 20abe88..5ee7ec6 100644 --- a/scripts/autoload/game_state.gd +++ b/scripts/autoload/game_state.gd @@ -56,6 +56,7 @@ var players: Dictionary = {} var mobs: Dictionary = {} var chests: Dictionary = {} var dropped_items: Dictionary = {} +var portals: Dictionary = {} # Inventory var inventory: Array = [] @@ -107,10 +108,14 @@ func _on_message_received(opcode: int, data: Dictionary): print("[GameState] >>> CHAT_MESSAGE opcode matched! <<<") _handle_chat_message(data) OpCodes.ESCAPE_INTERRUPTED: + print("[OpCodes]PlayerEscape] Interrupted.") is_escaping = false OpCodes.ESCAPE_SUCCESS: print("[GameState] >>> ESCAPE_SUCCESS - Returning to hub <<<") _handle_escape_success(data) + OpCodes.PORTAL_USED: + print("[GameState] >>> PORTAL_USED - Extracting via portal <<<") + _handle_portal_used(data) OpCodes.EVENT_DAMAGE: _handle_damage(data) OpCodes.EVENT_DEATH: @@ -178,6 +183,13 @@ func _handle_dungeon_enter(data: Dictionary): mobs.clear() chests.clear() dropped_items.clear() + portals.clear() + + # Parse initial portals + var portal_list = data.get("portals", data.get("Portals", [])) + for p in portal_list: + var pid = p.get("id", p.get("Id")) + portals[pid] = p set_state(STATE_PLAYING) dungeon_entered.emit() @@ -250,6 +262,12 @@ func _handle_dungeon_state(data: Dictionary): var iid = i.get("id", i.get("Id")) dropped_items[iid] = i + # Parse portals + var ports = data.get("portals", data.get("Portals", [])) + for p in ports: + var pid = p.get("id", p.get("Id")) + portals[pid] = p + dungeon_state_updated.emit() func _handle_inventory_state(data: Dictionary): @@ -313,12 +331,14 @@ func _handle_escape_success(_data: Dictionary): mobs.clear() chests.clear() dropped_items.clear() + portals.clear() dungeon = { "id": "", "tier": 1, "width": 0, "height": 0, - "tiles": [] + "tiles": [], + "phase": "active" } escaped.emit() @@ -372,3 +392,28 @@ func cancel_escape(): func return_to_hub(): Network.send_message(OpCodes.DUNGEON_LEAVE, {}) set_state(STATE_HUB) + +func send_use_portal(portal_id: int): + Network.send_message(OpCodes.INPUT_PORTAL, {"PortalId": portal_id}) + +func _handle_portal_used(_data: Dictionary): + print("[GameState] Portal used! Returning to hub...") + is_escaping = false + + # Clear dungeon data + players.clear() + mobs.clear() + chests.clear() + dropped_items.clear() + portals.clear() + dungeon = { + "id": "", + "tier": 1, + "width": 0, + "height": 0, + "tiles": [], + "phase": "active" + } + + escaped.emit() + # Server will send HUB_STATE next which will call set_state(STATE_HUB) diff --git a/scripts/autoload/opcodes.gd b/scripts/autoload/opcodes.gd index 9c2b075..9c79987 100644 --- a/scripts/autoload/opcodes.gd +++ b/scripts/autoload/opcodes.gd @@ -28,18 +28,21 @@ const DUNGEON_ENTER = 300 const DUNGEON_ENTER_RESULT = 301 const DUNGEON_STATE = 302 const DUNGEON_LEAVE = 303 +const DUNGEON_CLEARED = 304 # Input const INPUT_MOVE = 400 const INPUT_ATTACK = 401 const INPUT_PICKUP = 402 const INPUT_CHEST = 403 +const INPUT_PORTAL = 404 -# Escape +# Escape / Portal const ESCAPE_START = 450 const ESCAPE_CANCEL = 451 const ESCAPE_INTERRUPTED = 452 const ESCAPE_SUCCESS = 453 +const PORTAL_USED = 454 # Events const EVENT_DAMAGE = 500 diff --git a/scripts/dungeon/dungeon_manager.gd b/scripts/dungeon/dungeon_manager.gd index 42b7a7e..130fab1 100644 --- a/scripts/dungeon/dungeon_manager.gd +++ b/scripts/dungeon/dungeon_manager.gd @@ -14,6 +14,7 @@ const WORLD_TILE_SIZE := TILE_SIZE * TILE_SCALE # 32 pixels per tile in world @onready var mobs_container = $Entities/Mobs @onready var chests_container = $Entities/Chests @onready var items_container = $Entities/DroppedItems +@onready var portals_container = $Entities/Portals @onready var tilemap: TileMapLayer = $TileMapLayer @onready var hud = $Hud @onready var inventory_panel = $Inventory @@ -24,6 +25,7 @@ var player_scene: PackedScene = null var mob_scene: PackedScene = null var chest_scene: PackedScene = null var item_scene: PackedScene = null +var portal_scene: PackedScene = null var aim_angle: float = 0.0 @@ -37,6 +39,8 @@ func _ready(): chest_scene = load("res://scenes/dungeon/chest.tscn") if ResourceLoader.exists("res://scenes/dungeon/dropped_item.tscn"): item_scene = load("res://scenes/dungeon/dropped_item.tscn") + if ResourceLoader.exists("res://scenes/dungeon/portal.tscn"): + portal_scene = load("res://scenes/dungeon/portal.tscn") # Defer GameState connections call_deferred("_connect_gamestate") @@ -169,19 +173,19 @@ func _handle_input(): # Inventory toggle (I key) if Input.is_action_just_pressed("inventory"): - _toggle_inventory() + if chat_panel.visible != true: + _toggle_inventory() - # Escape handling + # Escape handling - close UI panels, or show hint about portals if Input.is_action_just_pressed("escape_menu"): # Close UI first if inventory_panel and inventory_panel.visible: inventory_panel.visible = false elif chat_panel and chat_panel.visible: chat_panel.visible = false - elif GameState.is_escaping: - GameState.cancel_escape() else: - GameState.start_escape() + # Player pressed escape but no UI open - hint about portals + print("[Dungeon] To exit, walk into the purple exit portal near spawn") # Chat (Enter key) if Input.is_action_just_pressed("chat"): @@ -240,6 +244,8 @@ func _on_state_updated(): _update_entities(chests_container, GameState.chests, chest_scene) if item_scene and items_container: _update_entities(items_container, GameState.dropped_items, item_scene) + if portal_scene and portals_container: + _update_entities(portals_container, GameState.portals, portal_scene) func _update_entities(container: Node2D, data: Dictionary, scene: PackedScene): if scene == null or container == null: diff --git a/scripts/ui/hud.gd b/scripts/ui/hud.gd index 839ec2d..181412f 100644 --- a/scripts/ui/hud.gd +++ b/scripts/ui/hud.gd @@ -211,7 +211,7 @@ func _update_escape_indicator(): escape_indicator.visible = GameState.is_escaping if GameState.is_escaping: var elapsed = (Time.get_ticks_msec() - GameState.escape_start_time) / 1000.0 - var remaining = max(0, 5 - elapsed) + var remaining = max(0, 20 - elapsed) if escape_countdown: escape_countdown.text = "%.1f" % remaining