Mobile Controls

This commit is contained in:
dingo
2026-01-19 08:49:10 -05:00
parent 718a8ecb3b
commit 5e8f9d383e
3 changed files with 376 additions and 42 deletions
+94 -41
View File
@@ -22,6 +22,9 @@ const WORLD_TILE_SIZE := TILE_SIZE * TILE_SCALE # 32 pixels per tile in world
@onready var inventory_panel = $Inventory
@onready var chat_panel = $Chat
# Crafting panel (loaded dynamically)
var crafting_panel = null
# Visual tile renderer (for V2 dungeons)
var visual_renderer: VisualTileMapRenderer = null
@@ -41,6 +44,9 @@ var projectiles: Dictionary = {} # id -> projectile node
var aim_angle: float = 0.0
func _ready():
# Add to group for HUD to access
add_to_group("dungeon_manager")
# Load entity scenes
if ResourceLoader.exists("res://scenes/dungeon/other_player.tscn"):
player_scene = load("res://scenes/dungeon/other_player.tscn")
@@ -59,16 +65,23 @@ func _ready():
if ResourceLoader.exists("res://scenes/dungeon/damage_number.tscn"):
damage_number_scene = load("res://scenes/dungeon/damage_number.tscn")
# Load crafting UI
if ResourceLoader.exists("res://scenes/ui/crafting_ui.tscn"):
var crafting_scene = load("res://scenes/ui/crafting_ui.tscn")
crafting_panel = crafting_scene.instantiate()
crafting_panel.visible = false
add_child(crafting_panel)
# Create visual tile renderer
visual_renderer = VisualTileMapRenderer.new()
visual_renderer.name = "VisualTileRenderer"
add_child(visual_renderer)
# Move it behind entities
move_child(visual_renderer, 0)
# Defer GameState connections
call_deferred("_connect_gamestate")
# Initially hidden
visible = false
@@ -93,10 +106,13 @@ func _on_game_state_changed(state: String):
if hud:
hud.visible = is_playing
# Hide inventory when leaving dungeon
if not is_playing and inventory_panel:
inventory_panel.visible = false
# Hide inventory and crafting when leaving dungeon
if not is_playing:
if inventory_panel:
inventory_panel.visible = false
if crafting_panel:
crafting_panel.visible = false
# Clear visual tiles when leaving dungeon
if not is_playing and visual_renderer:
visual_renderer.clear_tiles()
@@ -185,10 +201,32 @@ func _build_tiles():
print("[DungeonManager] Tilemap built: %dx%d" % [width, height])
func _input(event: InputEvent):
# Handle touch screen taps for attacking
if event is InputEventScreenTouch and event.pressed:
var ui_open = (inventory_panel and inventory_panel.visible) or (chat_panel and chat_panel.visible)
# Don't attack if UI is open
if ui_open:
return
# Calculate attack direction from touch position to player
var touch_world_pos = camera.get_global_mouse_position()
var player_pos = Vector2(GameState.self_player.get("x", 0), GameState.self_player.get("y", 0))
var attack_angle = (touch_world_pos - player_pos).angle()
# Update aim angle for visual feedback
aim_angle = attack_angle
if player_node and player_node.has_method("set_aim_angle"):
player_node.set_aim_angle(aim_angle)
# Perform attack
_perform_attack(attack_angle)
func _process(delta):
if not GameState or GameState.current_state != GameState.STATE_PLAYING:
return
_handle_input()
_update_camera(delta)
@@ -201,47 +239,23 @@ func _physics_process(_delta):
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
# Calculate aim angle from mouse/touch position
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)
# Attack on click/touch (only if UI not open)
# This handles both mouse clicks and touch taps
if not ui_open and Input.is_action_just_pressed("attack"):
# Check if player has a ranged weapon equipped
var equipped_weapon = GameState.equipment.get("weapon", null)
if equipped_weapon != null and not equipped_weapon.is_empty():
# Server sends camelCase properties
var requires_ammo = equipped_weapon.get("requiresAmmunition", false)
var is_throwable = equipped_weapon.get("isThrowable", false)
var projectile_speed = equipped_weapon.get("projectileSpeed", 0)
print("[DungeonManager] Attack - Weapon: ", equipped_weapon.get("name", "Unknown"))
print("[DungeonManager] RequiresAmmo: ", requires_ammo, ", IsThrowable: ", is_throwable, ", Speed: ", projectile_speed)
# If weapon fires projectiles (bow, crossbow, throwable), send projectile fire
if (requires_ammo or is_throwable) and projectile_speed > 0:
print("[DungeonManager] Firing projectile!")
# Calculate direction vector from aim angle
var direction_x = cos(aim_angle)
var direction_y = sin(aim_angle)
GameState.send_fire_projectile(direction_x, direction_y)
else:
print("[DungeonManager] Using melee attack")
# Melee weapon - use normal attack
GameState.send_attack(aim_angle)
else:
print("[DungeonManager] No weapon equipped, using fists")
# No weapon or empty slot - use normal attack (fists)
GameState.send_attack(aim_angle)
_perform_attack(aim_angle)
# Inventory toggle (I key)
if Input.is_action_just_pressed("inventory"):
@@ -250,8 +264,10 @@ func _handle_input():
# 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:
# Close UI first (in priority order)
if crafting_panel and crafting_panel.visible:
crafting_panel.visible = false
elif inventory_panel and inventory_panel.visible:
inventory_panel.visible = false
elif chat_panel and chat_panel.visible:
chat_panel.visible = false
@@ -269,6 +285,34 @@ func _handle_input():
if Input.is_action_just_pressed("hotbar_%d" % i):
_use_hotbar_slot(i - 1)
func _perform_attack(attack_angle: float):
# Check if player has a ranged weapon equipped
var equipped_weapon = GameState.equipment.get("weapon", null)
if equipped_weapon != null and not equipped_weapon.is_empty():
# Server sends camelCase properties
var requires_ammo = equipped_weapon.get("requiresAmmunition", false)
var is_throwable = equipped_weapon.get("isThrowable", false)
var projectile_speed = equipped_weapon.get("projectileSpeed", 0)
print("[DungeonManager] Attack - Weapon: ", equipped_weapon.get("name", "Unknown"))
print("[DungeonManager] RequiresAmmo: ", requires_ammo, ", IsThrowable: ", is_throwable, ", Speed: ", projectile_speed)
# If weapon fires projectiles (bow, crossbow, throwable), send projectile fire
if (requires_ammo or is_throwable) and projectile_speed > 0:
print("[DungeonManager] Firing projectile!")
# Calculate direction vector from aim angle
var direction_x = cos(attack_angle)
var direction_y = sin(attack_angle)
GameState.send_fire_projectile(direction_x, direction_y)
else:
print("[DungeonManager] Using melee attack")
# Melee weapon - use normal attack
GameState.send_attack(attack_angle)
else:
print("[DungeonManager] No weapon equipped, using fists")
# No weapon or empty slot - use normal attack (fists)
GameState.send_attack(attack_angle)
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)
@@ -362,6 +406,15 @@ func _toggle_inventory():
if inventory_panel.visible and Network:
Network.send_message(OpCodes.INVENTORY_REQUEST, {})
func _toggle_crafting():
if crafting_panel:
if crafting_panel.visible:
crafting_panel.visible = false
else:
crafting_panel.show_crafting()
else:
print("[DungeonManager] Crafting UI not loaded")
func _open_chat():
if chat_panel:
chat_panel.visible = true