335 lines
10 KiB
GDScript
335 lines
10 KiB
GDScript
extends Node2D
|
|
|
|
# Tile atlas coordinates in your tileset (adjust these to match your tileset)
|
|
const FLOOR_ATLAS_COORD := Vector2i(1, 0) # Brown floor tile
|
|
const WALL_ATLAS_COORD := Vector2i(3, 2) # Purple wall tile
|
|
|
|
const TILE_SIZE := 16 # Tileset tile size
|
|
const TILE_SCALE := 2 # Scale up for better visibility
|
|
const WORLD_TILE_SIZE := TILE_SIZE * TILE_SCALE # 32 pixels per tile in world
|
|
|
|
@onready var player_node = $Player
|
|
@onready var camera = $Camera2D
|
|
@onready var players_container = $Entities/Players
|
|
@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 loot_chests_container = $Entities/LootChests
|
|
@onready var tilemap: TileMapLayer = $TileMapLayer
|
|
@onready var hud = $Hud
|
|
@onready var inventory_panel = $Inventory
|
|
@onready var chat_panel = $Chat
|
|
|
|
# Scenes for entities
|
|
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 loot_chest_scene: PackedScene = null
|
|
|
|
var aim_angle: float = 0.0
|
|
|
|
func _ready():
|
|
# Load entity scenes
|
|
if ResourceLoader.exists("res://scenes/dungeon/other_player.tscn"):
|
|
player_scene = load("res://scenes/dungeon/other_player.tscn")
|
|
if ResourceLoader.exists("res://scenes/dungeon/mob.tscn"):
|
|
mob_scene = load("res://scenes/dungeon/mob.tscn")
|
|
if ResourceLoader.exists("res://scenes/dungeon/chest.tscn"):
|
|
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")
|
|
if ResourceLoader.exists("res://scenes/dungeon/loot_chest.tscn"):
|
|
loot_chest_scene = load("res://scenes/dungeon/loot_chest.tscn")
|
|
|
|
# Defer GameState connections
|
|
call_deferred("_connect_gamestate")
|
|
|
|
# Initially hidden
|
|
visible = false
|
|
|
|
func _connect_gamestate():
|
|
if GameState:
|
|
GameState.dungeon_entered.connect(_on_dungeon_entered)
|
|
GameState.dungeon_state_updated.connect(_on_state_updated)
|
|
GameState.state_changed.connect(_on_game_state_changed)
|
|
|
|
func _on_game_state_changed(state: String):
|
|
var is_playing = (state == "PLAYING")
|
|
visible = is_playing
|
|
set_process(is_playing)
|
|
set_physics_process(is_playing)
|
|
|
|
# Show/hide HUD with dungeon
|
|
if hud:
|
|
hud.visible = is_playing
|
|
|
|
# Hide inventory when leaving dungeon
|
|
if not is_playing and inventory_panel:
|
|
inventory_panel.visible = false
|
|
|
|
func _on_dungeon_entered():
|
|
# Scale the tilemap to match world coordinates
|
|
if tilemap:
|
|
tilemap.scale = Vector2(TILE_SCALE, TILE_SCALE)
|
|
|
|
_build_tiles()
|
|
|
|
# Position player at spawn
|
|
var spawn_x = GameState.self_player.get("x", 0)
|
|
var spawn_y = GameState.self_player.get("y", 0)
|
|
var world_pos = Vector2(spawn_x, spawn_y)
|
|
|
|
if player_node:
|
|
player_node.position = world_pos
|
|
if camera:
|
|
camera.position = world_pos
|
|
|
|
# Setup player texture
|
|
if player_node and player_node.has_node("Sprite") and SpriteGenerator:
|
|
player_node.get_node("Sprite").texture = SpriteGenerator.get_circle()
|
|
|
|
# Request initial inventory
|
|
if Network:
|
|
Network.send_message(OpCodes.INVENTORY_REQUEST, {})
|
|
|
|
func _build_tiles():
|
|
if not tilemap:
|
|
push_error("[DungeonManager] TileMapLayer not found!")
|
|
return
|
|
|
|
tilemap.clear()
|
|
|
|
if not GameState:
|
|
return
|
|
|
|
var tiles = GameState.dungeon.tiles
|
|
var width = GameState.dungeon.width
|
|
var height = GameState.dungeon.height
|
|
|
|
if tiles.is_empty():
|
|
print("[DungeonManager] No tiles to render")
|
|
return
|
|
|
|
if tilemap.tile_set == null:
|
|
push_error("[DungeonManager] TileSet is null!")
|
|
return
|
|
|
|
var source_count = tilemap.tile_set.get_source_count()
|
|
if source_count == 0:
|
|
push_error("[DungeonManager] TileSet has no sources!")
|
|
return
|
|
|
|
var source_id = tilemap.tile_set.get_source_id(0)
|
|
|
|
# Build the tilemap
|
|
for x in range(width):
|
|
if x >= tiles.size():
|
|
continue
|
|
for y in range(height):
|
|
if y >= tiles[x].size():
|
|
continue
|
|
|
|
var tile_type = tiles[x][y]
|
|
if tile_type == 1:
|
|
tilemap.set_cell(Vector2i(x, y), source_id, WALL_ATLAS_COORD)
|
|
else:
|
|
tilemap.set_cell(Vector2i(x, y), source_id, FLOOR_ATLAS_COORD)
|
|
|
|
print("[DungeonManager] Tilemap built: %dx%d" % [width, height])
|
|
|
|
func _process(delta):
|
|
if not GameState or GameState.current_state != GameState.STATE_PLAYING:
|
|
return
|
|
|
|
_handle_input()
|
|
_update_camera(delta)
|
|
|
|
func _physics_process(_delta):
|
|
if not GameState or GameState.current_state != GameState.STATE_PLAYING:
|
|
return
|
|
|
|
_send_movement()
|
|
|
|
func _handle_input():
|
|
if not camera:
|
|
return
|
|
|
|
# Don't process game input if inventory or chat is open
|
|
var ui_open = (inventory_panel and inventory_panel.visible) or (chat_panel and chat_panel.visible)
|
|
|
|
# Calculate aim angle from mouse
|
|
var mouse_pos = get_global_mouse_position()
|
|
var player_screen_pos = camera.get_screen_center_position()
|
|
aim_angle = (mouse_pos - player_screen_pos).angle()
|
|
|
|
# Update player aim visual
|
|
if player_node and player_node.has_method("set_aim_angle"):
|
|
player_node.set_aim_angle(aim_angle)
|
|
|
|
# Attack on click (only if UI not open)
|
|
if not ui_open and Input.is_action_just_pressed("attack"):
|
|
GameState.send_attack(aim_angle)
|
|
|
|
# Inventory toggle (I key)
|
|
if Input.is_action_just_pressed("inventory"):
|
|
if chat_panel.visible != true:
|
|
_toggle_inventory()
|
|
|
|
# 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
|
|
else:
|
|
# 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"):
|
|
_open_chat()
|
|
|
|
# Hotbar keys 1-8
|
|
if not ui_open:
|
|
for i in range(1, 9):
|
|
if Input.is_action_just_pressed("hotbar_%d" % i):
|
|
_use_hotbar_slot(i - 1)
|
|
|
|
func _send_movement():
|
|
# Don't move if UI is open
|
|
var ui_open = (inventory_panel and inventory_panel.visible) or (chat_panel and chat_panel.visible)
|
|
if ui_open:
|
|
return
|
|
|
|
var dx = Input.get_axis("move_left", "move_right")
|
|
var dy = Input.get_axis("move_up", "move_down")
|
|
|
|
var move_vec = Vector2(dx, dy)
|
|
if move_vec.length() > 1:
|
|
move_vec = move_vec.normalized()
|
|
|
|
GameState.send_move(move_vec.x, move_vec.y, aim_angle)
|
|
|
|
func _update_camera(delta):
|
|
if not camera or not GameState:
|
|
return
|
|
var target_x = GameState.self_player.get("x", camera.position.x)
|
|
var target_y = GameState.self_player.get("y", camera.position.y)
|
|
var target_pos = Vector2(target_x, target_y)
|
|
camera.position = camera.position.lerp(target_pos, 5.0 * delta)
|
|
|
|
func _on_state_updated():
|
|
if not GameState:
|
|
return
|
|
|
|
# Update player position
|
|
if player_node:
|
|
var px = GameState.self_player.get("x", player_node.position.x)
|
|
var py = GameState.self_player.get("y", player_node.position.y)
|
|
player_node.position = Vector2(px, py)
|
|
|
|
if player_node.has_method("update_health"):
|
|
var hp = GameState.self_player.get("health", 100)
|
|
var max_hp = GameState.self_player.get("max_health", 100)
|
|
player_node.update_health(hp, max_hp)
|
|
|
|
# Update entities
|
|
if player_scene and players_container:
|
|
_update_entities(players_container, GameState.players, player_scene)
|
|
if mob_scene and mobs_container:
|
|
_update_entities(mobs_container, GameState.mobs, mob_scene)
|
|
if chest_scene and chests_container:
|
|
_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)
|
|
if loot_chest_scene and loot_chests_container:
|
|
_update_loot_chests()
|
|
|
|
func _update_entities(container: Node2D, data: Dictionary, scene: PackedScene):
|
|
if scene == null or container == null:
|
|
return
|
|
|
|
var seen_ids = []
|
|
|
|
for id in data:
|
|
seen_ids.append(id)
|
|
var entity_data = data[id]
|
|
var entity_node = _find_entity_by_id(container, id)
|
|
|
|
if entity_node == null:
|
|
entity_node = scene.instantiate()
|
|
entity_node.set_meta("entity_id", id)
|
|
container.add_child(entity_node)
|
|
|
|
var x = entity_data.get("x", entity_data.get("X", 0))
|
|
var y = entity_data.get("y", entity_data.get("Y", 0))
|
|
entity_node.position = Vector2(x, y)
|
|
|
|
if entity_node.has_method("update_data"):
|
|
entity_node.update_data(entity_data)
|
|
|
|
for child in container.get_children():
|
|
if child.has_meta("entity_id"):
|
|
var id = child.get_meta("entity_id")
|
|
if id not in seen_ids:
|
|
child.queue_free()
|
|
|
|
func _find_entity_by_id(container: Node2D, id) -> Node2D:
|
|
for child in container.get_children():
|
|
if child.has_meta("entity_id") and child.get_meta("entity_id") == id:
|
|
return child
|
|
return null
|
|
|
|
func _toggle_inventory():
|
|
if inventory_panel:
|
|
inventory_panel.visible = not inventory_panel.visible
|
|
if inventory_panel.visible and Network:
|
|
Network.send_message(OpCodes.INVENTORY_REQUEST, {})
|
|
|
|
func _open_chat():
|
|
if chat_panel:
|
|
chat_panel.visible = true
|
|
if chat_panel.has_method("focus_input"):
|
|
chat_panel.focus_input()
|
|
|
|
func _use_hotbar_slot(index: int):
|
|
if not GameState:
|
|
return
|
|
var hotbar_entry = GameState.hotbar[index]
|
|
if hotbar_entry and hotbar_entry.has("inventory_slot"):
|
|
GameState.send_use_item(hotbar_entry.inventory_slot)
|
|
|
|
func _update_loot_chests():
|
|
if loot_chest_scene == null or loot_chests_container == null:
|
|
return
|
|
|
|
var seen_ids = []
|
|
|
|
for id in GameState.loot_chests:
|
|
seen_ids.append(id)
|
|
var chest_data = GameState.loot_chests[id]
|
|
var chest_node = _find_entity_by_id(loot_chests_container, id)
|
|
|
|
if chest_node == null:
|
|
chest_node = loot_chest_scene.instantiate()
|
|
chest_node.set_meta("entity_id", id)
|
|
loot_chests_container.add_child(chest_node)
|
|
|
|
if chest_node.has_method("setup"):
|
|
chest_node.setup(chest_data)
|
|
|
|
# Remove chests that no longer exist
|
|
for child in loot_chests_container.get_children():
|
|
if child.has_meta("entity_id"):
|
|
var id = child.get_meta("entity_id")
|
|
if id not in seen_ids:
|
|
child.queue_free()
|