Ping/Pong
This commit is contained in:
@@ -93,12 +93,17 @@ var _network_connected: bool = false
|
||||
|
||||
func _ready():
|
||||
call_deferred("_connect_to_network")
|
||||
Network.disconnected.connect(_on_disconnected)
|
||||
|
||||
|
||||
func _connect_to_network():
|
||||
if Network:
|
||||
Network.message_received.connect(_on_message_received)
|
||||
_network_connected = true
|
||||
|
||||
func _on_disconnected():
|
||||
set_state(0)
|
||||
|
||||
func set_state(new_state: int):
|
||||
current_state = new_state
|
||||
var state_name = STATE_NAMES[new_state] if new_state < STATE_NAMES.size() else "UNKNOWN"
|
||||
|
||||
+159
-107
@@ -1,5 +1,13 @@
|
||||
extends Node
|
||||
|
||||
## Network Connection Manager with Ping/Pong Keepalive Support
|
||||
##
|
||||
## This script handles WebSocket connection to the game server and implements
|
||||
## automatic ping/pong keepalive mechanism to prevent idle timeout disconnections.
|
||||
##
|
||||
## The server will send PING messages every 30 seconds (configurable server-side).
|
||||
## This client automatically responds with PONG to reset the server's idle timeout.
|
||||
|
||||
signal connected
|
||||
signal disconnected
|
||||
signal message_received(opcode: int, data: Dictionary)
|
||||
@@ -7,13 +15,24 @@ signal connection_error(error: String)
|
||||
|
||||
const SERVER_URL = "wss://srs.aeternalisgames.com/ws"
|
||||
|
||||
# WebSocket connection instance
|
||||
var socket: WebSocketPeer = WebSocketPeer.new()
|
||||
|
||||
# Buffer for handling fragmented messages
|
||||
var _receive_buffer: PackedByteArray = PackedByteArray()
|
||||
|
||||
# Connection state tracking
|
||||
var _is_connected: bool = false
|
||||
|
||||
# Statistics for monitoring connection health
|
||||
var _last_ping_received_time: float = 0.0
|
||||
var _ping_count: int = 0
|
||||
var _pong_sent_count: int = 0
|
||||
|
||||
func _ready():
|
||||
set_process(false)
|
||||
|
||||
## Main processing loop - polls WebSocket and handles incoming messages
|
||||
func _process(_delta):
|
||||
socket.poll()
|
||||
|
||||
@@ -26,7 +45,7 @@ func _process(_delta):
|
||||
print("[Network] WebSocket connected successfully")
|
||||
connected.emit()
|
||||
|
||||
# Receive data
|
||||
# Receive and process all available packets
|
||||
while socket.get_available_packet_count() > 0:
|
||||
var packet = socket.get_packet()
|
||||
print("[Network] Received packet, size: ", packet.size())
|
||||
@@ -36,24 +55,30 @@ func _process(_delta):
|
||||
print("[Network] WebSocket closing...")
|
||||
|
||||
WebSocketPeer.STATE_CLOSED:
|
||||
var code = socket.get_close_code()
|
||||
var reason = socket.get_close_reason()
|
||||
print("[Network] WebSocket closed - code: %d, reason: %s" % [code, reason])
|
||||
var close_code = socket.get_close_code()
|
||||
var close_reason = socket.get_close_reason()
|
||||
print("[Network] WebSocket closed - code: %d, reason: %s" % [close_code, close_reason])
|
||||
|
||||
if _is_connected:
|
||||
_is_connected = false
|
||||
disconnected.emit()
|
||||
|
||||
set_process(false)
|
||||
|
||||
## Establish connection to game server
|
||||
func connect_to_server() -> Error:
|
||||
_receive_buffer.clear()
|
||||
_ping_count = 0
|
||||
_pong_sent_count = 0
|
||||
_last_ping_received_time = 0.0
|
||||
|
||||
# Reset socket for fresh connection
|
||||
socket = WebSocketPeer.new()
|
||||
|
||||
# Increase buffer sizes for large dungeon data
|
||||
socket.inbound_buffer_size = 8 * 1024 * 1024 # 8MB
|
||||
socket.outbound_buffer_size = 1024 * 1024 # 1MB
|
||||
socket.max_queued_packets = 4096
|
||||
# Increase buffer sizes for large dungeon data transfers
|
||||
socket.inbound_buffer_size = 8 * 1024 * 1024 # 8MB for large state updates
|
||||
socket.outbound_buffer_size = 1024 * 1024 # 1MB for outgoing messages
|
||||
socket.max_queued_packets = 4096 # Handle burst traffic
|
||||
|
||||
print("[Network] Attempting connection to: ", SERVER_URL)
|
||||
var err = socket.connect_to_url(SERVER_URL)
|
||||
@@ -67,124 +92,151 @@ func connect_to_server() -> Error:
|
||||
|
||||
return err
|
||||
|
||||
## Gracefully disconnect from server
|
||||
func disconnect_from_server():
|
||||
if _is_connected:
|
||||
socket.close()
|
||||
print("[Network] Disconnecting from server...")
|
||||
|
||||
func send_message(opcode: int, payload: Dictionary = {}):
|
||||
if socket.get_ready_state() != WebSocketPeer.STATE_OPEN:
|
||||
push_warning("[Network] Cannot send - socket not open (state: %d)" % socket.get_ready_state())
|
||||
## Send a message to the server
|
||||
## @param opcode: Message type identifier (see opcodes.gd)
|
||||
## @param data: Dictionary of message data to send
|
||||
func send_message(opcode: int, data: Dictionary = {}):
|
||||
if not _is_connected:
|
||||
push_warning("[Network] Cannot send message - not connected")
|
||||
return
|
||||
|
||||
# Encode payload to JSON bytes
|
||||
var payload_json = JSON.stringify(payload)
|
||||
var payload_bytes = payload_json.to_utf8_buffer()
|
||||
# Serialize message to JSON
|
||||
var json_string = JSON.stringify(data)
|
||||
var payload = json_string.to_utf8_buffer()
|
||||
|
||||
# Create message envelope - matching JS client exactly
|
||||
var message = {
|
||||
"OpCode": opcode,
|
||||
"Payload": Array(payload_bytes) # Array of ints, same as JS Array.from()
|
||||
}
|
||||
var message_json = JSON.stringify(message)
|
||||
var message_bytes = message_json.to_utf8_buffer()
|
||||
# Create frame: [4 bytes length][1 byte opcode][payload]
|
||||
var frame_length = 1 + payload.size()
|
||||
var frame = PackedByteArray()
|
||||
|
||||
# Create framed message with 4-byte big-endian length prefix
|
||||
var length = message_bytes.size()
|
||||
var framed = PackedByteArray()
|
||||
framed.resize(4 + length)
|
||||
# Write length as big-endian 32-bit integer
|
||||
frame.append((frame_length >> 24) & 0xFF)
|
||||
frame.append((frame_length >> 16) & 0xFF)
|
||||
frame.append((frame_length >> 8) & 0xFF)
|
||||
frame.append(frame_length & 0xFF)
|
||||
|
||||
# Big-endian length prefix
|
||||
framed[0] = (length >> 24) & 0xFF
|
||||
framed[1] = (length >> 16) & 0xFF
|
||||
framed[2] = (length >> 8) & 0xFF
|
||||
framed[3] = length & 0xFF
|
||||
# Write opcode
|
||||
frame.append(opcode)
|
||||
|
||||
# Copy message bytes
|
||||
for i in range(length):
|
||||
framed[4 + i] = message_bytes[i]
|
||||
# Write payload
|
||||
frame.append_array(payload)
|
||||
|
||||
print("[Network] Sending opcode %d, payload: %s" % [opcode, payload_json])
|
||||
print("[Network] Framed message size: %d bytes" % framed.size())
|
||||
|
||||
# Send as binary (default)
|
||||
var send_err = socket.send(framed)
|
||||
if send_err != OK:
|
||||
print("[Network] Send error: ", send_err)
|
||||
else:
|
||||
print("[Network] Message sent successfully")
|
||||
# Send to server
|
||||
var err = socket.send(frame)
|
||||
if err != OK:
|
||||
push_error("[Network] Failed to send message, error: ", err)
|
||||
|
||||
func _handle_received_data(data: PackedByteArray):
|
||||
# Append to buffer
|
||||
_receive_buffer.append_array(data)
|
||||
print("[Network] Buffer size after append: ", _receive_buffer.size())
|
||||
## Handle incoming data packets from server
|
||||
func _handle_received_data(packet: PackedByteArray):
|
||||
# Add to buffer
|
||||
_receive_buffer.append_array(packet)
|
||||
|
||||
# Process complete messages
|
||||
while _receive_buffer.size() >= 4:
|
||||
# Read length (big-endian)
|
||||
var length = (_receive_buffer[0] << 24) | \
|
||||
(_receive_buffer[1] << 16) | \
|
||||
(_receive_buffer[2] << 8) | \
|
||||
_receive_buffer[3]
|
||||
# Process all complete messages in buffer
|
||||
while _receive_buffer.size() >= 5: # Minimum: 4 bytes length + 1 byte opcode
|
||||
# Read length (big-endian 32-bit integer)
|
||||
var message_length = (
|
||||
(_receive_buffer[0] << 24) |
|
||||
(_receive_buffer[1] << 16) |
|
||||
(_receive_buffer[2] << 8) |
|
||||
_receive_buffer[3]
|
||||
)
|
||||
|
||||
print("[Network] Message length header: ", length)
|
||||
|
||||
if length <= 0 or length > 4194304:
|
||||
push_error("[Network] Invalid message length: %d" % length)
|
||||
# Validate message length
|
||||
if message_length < 1 or message_length > 4 * 1024 * 1024:
|
||||
push_error("[Network] Invalid message length: ", message_length)
|
||||
_receive_buffer.clear()
|
||||
disconnect_from_server()
|
||||
return
|
||||
|
||||
if _receive_buffer.size() < 4 + length:
|
||||
print("[Network] Waiting for more data, have %d, need %d" % [_receive_buffer.size(), 4 + length])
|
||||
return # Wait for more data
|
||||
# Check if we have the complete message
|
||||
var total_frame_size = 4 + message_length
|
||||
if _receive_buffer.size() < total_frame_size:
|
||||
# Incomplete message, wait for more data
|
||||
break
|
||||
|
||||
# Extract message
|
||||
var message_bytes = _receive_buffer.slice(4, 4 + length)
|
||||
_receive_buffer = _receive_buffer.slice(4 + length)
|
||||
# Extract opcode
|
||||
var opcode = _receive_buffer[4]
|
||||
|
||||
# Parse JSON
|
||||
var message_text = message_bytes.get_string_from_utf8()
|
||||
print("[Network] Raw message: ", message_text.substr(0, 200)) # First 200 chars
|
||||
# Extract payload (if any)
|
||||
var payload_size = message_length - 1
|
||||
var payload_bytes = PackedByteArray()
|
||||
if payload_size > 0:
|
||||
payload_bytes = _receive_buffer.slice(5, 5 + payload_size)
|
||||
|
||||
var json = JSON.new()
|
||||
var parse_result = json.parse(message_text)
|
||||
# Remove processed message from buffer
|
||||
_receive_buffer = _receive_buffer.slice(total_frame_size)
|
||||
|
||||
if parse_result != OK:
|
||||
push_error("[Network] Failed to parse message JSON: " + json.get_error_message())
|
||||
continue
|
||||
|
||||
var msg = json.data
|
||||
var opcode: int = msg.get("opCode", msg.get("OpCode", 0))
|
||||
print("[Network] Parsed opcode: ", opcode)
|
||||
|
||||
# Decode payload
|
||||
var payload_data = msg.get("payload", msg.get("Payload", null))
|
||||
var decoded_data = {}
|
||||
|
||||
if payload_data != null:
|
||||
var payload_string: String
|
||||
if payload_data is String:
|
||||
# Base64 encoded
|
||||
print("[Network] Payload is base64 string")
|
||||
payload_string = Marshalls.base64_to_utf8(payload_data)
|
||||
elif payload_data is Array:
|
||||
# Array of bytes
|
||||
print("[Network] Payload is byte array, size: ", payload_data.size())
|
||||
var byte_array = PackedByteArray(payload_data)
|
||||
payload_string = byte_array.get_string_from_utf8()
|
||||
else:
|
||||
print("[Network] Payload is other type: ", typeof(payload_data))
|
||||
payload_string = JSON.stringify(payload_data)
|
||||
|
||||
print("[Network] Decoded payload string: ", payload_string.substr(0, 200))
|
||||
|
||||
var payload_json = JSON.new()
|
||||
if payload_json.parse(payload_string) == OK:
|
||||
decoded_data = payload_json.data
|
||||
else:
|
||||
decoded_data = {"raw": payload_string}
|
||||
|
||||
print("[Network] Emitting message_received with opcode: ", opcode)
|
||||
message_received.emit(opcode, decoded_data)
|
||||
# Handle the message
|
||||
_handle_message(opcode, payload_bytes)
|
||||
|
||||
func is_socket_connected() -> bool:
|
||||
return _is_connected and socket.get_ready_state() == WebSocketPeer.STATE_OPEN
|
||||
## Process a single decoded message
|
||||
func _handle_message(opcode: int, payload: PackedByteArray):
|
||||
# Handle ping/pong messages internally (don't emit to game logic)
|
||||
match opcode:
|
||||
2: # PING from server
|
||||
_handle_ping_message()
|
||||
return
|
||||
|
||||
3: # PONG from server (shouldn't happen, but handle gracefully)
|
||||
print("[Network] Unexpected PONG received from server")
|
||||
return
|
||||
|
||||
# For all other messages, parse JSON payload and emit to game
|
||||
var data = {}
|
||||
|
||||
if payload.size() > 0:
|
||||
var json_string = payload.get_string_from_utf8()
|
||||
var json = JSON.new()
|
||||
var parse_result = json.parse(json_string)
|
||||
|
||||
if parse_result == OK:
|
||||
data = json.data
|
||||
else:
|
||||
push_warning("[Network] Failed to parse JSON for opcode ", opcode)
|
||||
print("[Network] JSON parse error: ", json.get_error_message())
|
||||
print("[Network] Invalid JSON: ", json_string)
|
||||
|
||||
# Emit message to game logic
|
||||
message_received.emit(opcode, data)
|
||||
|
||||
## Handle PING message from server - respond with PONG
|
||||
func _handle_ping_message():
|
||||
_ping_count += 1
|
||||
_last_ping_received_time = Time.get_ticks_msec() / 1000.0
|
||||
|
||||
print("[Network] PING received (count: %d), sending PONG..." % _ping_count)
|
||||
|
||||
# Send PONG response (OpCode 3, empty payload)
|
||||
var pong_frame = PackedByteArray()
|
||||
|
||||
# Length: 1 byte (just the opcode)
|
||||
pong_frame.append(0) # High bytes of length
|
||||
pong_frame.append(0)
|
||||
pong_frame.append(0)
|
||||
pong_frame.append(1) # Low byte of length = 1
|
||||
|
||||
# OpCode: 3 (PONG)
|
||||
pong_frame.append(3)
|
||||
|
||||
# Send directly without going through send_message to avoid JSON overhead
|
||||
var err = socket.send(pong_frame)
|
||||
if err == OK:
|
||||
_pong_sent_count += 1
|
||||
print("[Network] PONG sent successfully (count: %d)" % _pong_sent_count)
|
||||
else:
|
||||
push_error("[Network] Failed to send PONG, error: ", err)
|
||||
|
||||
## Get connection statistics for debugging
|
||||
func get_connection_stats() -> Dictionary:
|
||||
return {
|
||||
"is_connected": _is_connected,
|
||||
"ping_count": _ping_count,
|
||||
"pong_sent_count": _pong_sent_count,
|
||||
"last_ping_seconds_ago": (Time.get_ticks_msec() / 1000.0) - _last_ping_received_time if _last_ping_received_time > 0 else -1,
|
||||
"socket_state": socket.get_ready_state()
|
||||
}
|
||||
|
||||
+192
-25
@@ -1,97 +1,264 @@
|
||||
extends Node
|
||||
|
||||
# Auth
|
||||
## Network OpCodes - Message Type Identifiers
|
||||
##
|
||||
## This file defines all message types (opcodes) used for client-server communication.
|
||||
## Each opcode represents a specific type of message or action.
|
||||
|
||||
# ===== Keepalive / Connection Management (1-9) =====
|
||||
|
||||
## Echo test message - server echoes back the received payload
|
||||
const ECHO = 1
|
||||
|
||||
## Server -> Client: Keepalive ping to prevent idle timeout
|
||||
## Client should automatically respond with PONG
|
||||
const PING = 2
|
||||
|
||||
## Client -> Server: Response to PING message
|
||||
## Handled automatically by network.gd
|
||||
const PONG = 3
|
||||
|
||||
# ===== Authentication (100-119) =====
|
||||
|
||||
## Client -> Server: Register new account
|
||||
const AUTH_REGISTER = 100
|
||||
|
||||
## Server -> Client: Registration result
|
||||
const AUTH_REGISTER_RESULT = 101
|
||||
|
||||
## Client -> Server: Login with username and password
|
||||
const AUTH_LOGIN = 110
|
||||
|
||||
## Server -> Client: Login result
|
||||
const AUTH_LOGIN_RESULT = 111
|
||||
|
||||
# Hub
|
||||
## Client -> Server: Authenticate using stored session token
|
||||
const AUTH_WITH_TOKEN = 112
|
||||
|
||||
## Server -> Client: Token authentication result
|
||||
const AUTH_WITH_TOKEN_RESULT = 113
|
||||
|
||||
# ===== Hub / Character Selection (200-249) =====
|
||||
|
||||
## Server -> Client: Hub state with character info
|
||||
const HUB_STATE = 200
|
||||
|
||||
## Client -> Server: Allocate stat points
|
||||
const STAT_ALLOCATE = 201
|
||||
|
||||
# Inventory
|
||||
## Client -> Server: Create new character
|
||||
const CHAR_CREATE = 202
|
||||
|
||||
## Server -> Client: Character creation result
|
||||
const CHAR_CREATE_RESULT = 203
|
||||
|
||||
## Client -> Server: Select character to play
|
||||
const CHAR_SELECT = 204
|
||||
|
||||
## Server -> Client: Character selection result
|
||||
const CHAR_SELECT_RESULT = 205
|
||||
|
||||
# ===== Inventory Management (250-259) =====
|
||||
|
||||
## Client -> Server: Request current inventory state
|
||||
const INVENTORY_REQUEST = 250
|
||||
|
||||
## Server -> Client: Current inventory state
|
||||
const INVENTORY_STATE = 251
|
||||
|
||||
## Client -> Server: Equip an item
|
||||
const EQUIP_ITEM = 252
|
||||
|
||||
## Client -> Server: Unequip an item
|
||||
const UNEQUIP_ITEM = 253
|
||||
|
||||
## Client -> Server: Use a consumable item
|
||||
const USE_ITEM = 254
|
||||
|
||||
## Client -> Server: Drop an item from inventory
|
||||
const DROP_ITEM = 255
|
||||
|
||||
## Client -> Server: Assign item to hotbar slot
|
||||
const HOTBAR_ASSIGN = 256
|
||||
|
||||
# Chat
|
||||
# ===== Chat (260-269) =====
|
||||
|
||||
## Client -> Server: Send chat message
|
||||
const CHAT_SEND = 260
|
||||
|
||||
## Server -> Client: Chat message from player
|
||||
const CHAT_MESSAGE = 261
|
||||
|
||||
# Loot Chests
|
||||
# ===== Loot Chests (270-279) =====
|
||||
|
||||
## Client -> Server: Open a loot chest
|
||||
const LOOT_CHEST_OPEN = 270
|
||||
|
||||
## Server -> Client: Loot chest contents
|
||||
const LOOT_CHEST_STATE = 271
|
||||
|
||||
## Client -> Server: Take specific item from chest
|
||||
const LOOT_CHEST_TAKE_ITEM = 272
|
||||
|
||||
## Client -> Server: Take all items from chest
|
||||
const LOOT_CHEST_TAKE_ALL = 273
|
||||
|
||||
## Client -> Server: Close loot chest UI
|
||||
const LOOT_CHEST_CLOSE = 274
|
||||
|
||||
## Server -> Client: New loot chest spawned in dungeon
|
||||
const LOOT_CHEST_SPAWNED = 275
|
||||
|
||||
# Player Death
|
||||
## Server -> Client: Loot chest removed/destroyed
|
||||
const LOOT_CHEST_REMOVED = 276
|
||||
|
||||
# ===== Player Death (280-289) =====
|
||||
|
||||
## Server -> Client: Notify player what loot they lost on death
|
||||
const PLAYER_DEATH_LOOT_LOST = 280
|
||||
|
||||
# Crafting
|
||||
const CRAFTING_SKILLS_REQUEST = 330
|
||||
const CRAFTING_SKILLS_STATE = 331
|
||||
const CRAFTING_RECIPES_REQUEST = 332
|
||||
const CRAFTING_RECIPES_STATE = 333
|
||||
const CRAFTING_ATTEMPT = 334
|
||||
const CRAFTING_RESULT = 335
|
||||
const CRAFTING_SKILL_LEVELUP = 336
|
||||
const RECIPE_DISCOVERED = 337
|
||||
const RECIPE_SCROLL_USE = 338
|
||||
# ===== Dungeon Core (300-309) =====
|
||||
|
||||
# Dungeon
|
||||
## Client -> Server: Request to enter a dungeon
|
||||
const DUNGEON_ENTER = 300
|
||||
|
||||
## Server -> Client: Dungeon entry result
|
||||
const DUNGEON_ENTER_RESULT = 301
|
||||
|
||||
## Server -> Client: Current dungeon state snapshot
|
||||
const DUNGEON_STATE = 302
|
||||
|
||||
## Client -> Server: Leave current dungeon
|
||||
const DUNGEON_LEAVE = 303
|
||||
|
||||
## Server -> Client: Dungeon has been cleared (boss defeated)
|
||||
const DUNGEON_CLEARED = 304
|
||||
|
||||
## Server -> Client: Tile update (door opened, destructible destroyed, etc.)
|
||||
const DUNGEON_TILE_UPDATE = 305
|
||||
|
||||
# Dungeon Browser
|
||||
# ===== Dungeon Browser (310-319) =====
|
||||
|
||||
## Client -> Server: Request list of available dungeons
|
||||
const DUNGEON_LIST_REQUEST = 310
|
||||
|
||||
## Server -> Client: List of dungeons
|
||||
const DUNGEON_LIST_RESPONSE = 311
|
||||
|
||||
## Client -> Server: Create a new dungeon instance
|
||||
const DUNGEON_CREATE = 312
|
||||
|
||||
## Client -> Server: Join an existing dungeon
|
||||
const DUNGEON_JOIN = 313
|
||||
|
||||
# Asset Management
|
||||
# ===== Asset Management (320-329) =====
|
||||
|
||||
## Client -> Server: Request asset manifest (sprites, tilesets, etc.)
|
||||
const ASSET_MANIFEST_REQUEST = 320
|
||||
|
||||
## Server -> Client: Asset manifest response
|
||||
const ASSET_MANIFEST_RESPONSE = 321
|
||||
|
||||
# Input
|
||||
# ===== Crafting System (330-349) =====
|
||||
|
||||
## Client -> Server: Request crafting skills state
|
||||
const CRAFTING_SKILLS_REQUEST = 330
|
||||
|
||||
## Server -> Client: Crafting skills state
|
||||
const CRAFTING_SKILLS_STATE = 331
|
||||
|
||||
## Client -> Server: Request known crafting recipes
|
||||
const CRAFTING_RECIPES_REQUEST = 332
|
||||
|
||||
## Server -> Client: Crafting recipes state
|
||||
const CRAFTING_RECIPES_STATE = 333
|
||||
|
||||
## Client -> Server: Attempt to craft an item
|
||||
const CRAFTING_ATTEMPT = 334
|
||||
|
||||
## Server -> Client: Crafting attempt result
|
||||
const CRAFTING_RESULT = 335
|
||||
|
||||
## Server -> Client: Crafting skill leveled up
|
||||
const CRAFTING_SKILL_LEVELUP = 336
|
||||
|
||||
## Server -> Client: New recipe discovered
|
||||
const RECIPE_DISCOVERED = 337
|
||||
|
||||
## Client -> Server: Use a recipe scroll to learn recipe
|
||||
const RECIPE_SCROLL_USE = 338
|
||||
|
||||
# ===== Dungeon Input / Actions (400-449) =====
|
||||
|
||||
## Client -> Server: Movement input
|
||||
const INPUT_MOVE = 400
|
||||
|
||||
## Client -> Server: Attack action
|
||||
const INPUT_ATTACK = 401
|
||||
|
||||
## Client -> Server: Pickup item from ground
|
||||
const INPUT_PICKUP = 402
|
||||
|
||||
## Client -> Server: Interact with chest
|
||||
const INPUT_CHEST = 403
|
||||
|
||||
## Client -> Server: Use portal
|
||||
const INPUT_PORTAL = 404
|
||||
|
||||
# Escape / Portal
|
||||
# ===== Escape / Portal (450-459) =====
|
||||
|
||||
## Client -> Server: Start escape channel (return to hub)
|
||||
const ESCAPE_START = 450
|
||||
|
||||
## Client -> Server: Cancel escape channel
|
||||
const ESCAPE_CANCEL = 451
|
||||
|
||||
## Server -> Client: Escape interrupted (took damage)
|
||||
const ESCAPE_INTERRUPTED = 452
|
||||
|
||||
## Server -> Client: Escape successful, returning to hub
|
||||
const ESCAPE_SUCCESS = 453
|
||||
|
||||
## Server -> Client: Portal successfully used
|
||||
const PORTAL_USED = 454
|
||||
|
||||
# Events
|
||||
# ===== Game Events (500-599) =====
|
||||
|
||||
## Server -> Client: Damage event occurred
|
||||
const EVENT_DAMAGE = 500
|
||||
|
||||
## Server -> Client: Entity died
|
||||
const EVENT_DEATH = 501
|
||||
|
||||
## Server -> Client: Player leveled up
|
||||
const EVENT_LEVELUP = 502
|
||||
|
||||
## Server -> Client: Loot obtained
|
||||
const EVENT_LOOT = 503
|
||||
|
||||
# Server Broadcast
|
||||
# ===== Server Broadcasts (600-609) =====
|
||||
|
||||
## Server -> All Clients: Server-wide broadcast message
|
||||
const SERVER_BROADCAST = 600
|
||||
|
||||
# Stash (VIP Feature)
|
||||
# ===== Stash System - VIP Feature (610-619) =====
|
||||
|
||||
## Client -> Server: Request stash contents
|
||||
const STASH_REQUEST = 610
|
||||
|
||||
## Server -> Client: Stash contents
|
||||
const STASH_STATE = 611
|
||||
|
||||
## Client -> Server: Deposit item from inventory to stash
|
||||
const STASH_DEPOSIT = 612
|
||||
|
||||
## Client -> Server: Withdraw item from stash to inventory
|
||||
const STASH_WITHDRAW = 613
|
||||
|
||||
## Server -> Client: Stash operation result
|
||||
const STASH_RESULT = 614
|
||||
|
||||
# Legacy
|
||||
# ===== Legacy / Deprecated =====
|
||||
|
||||
## Legacy loot notification (consider consolidating with EVENT_LOOT)
|
||||
const LOOT_RECEIVED = 160
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
extends CanvasLayer
|
||||
|
||||
@onready var name_label = $Control/Panel/Name#$Control/HBoxContainer/LeftPanel/CharacterInfo/NameLabel
|
||||
@onready var level_label = $Control/Panel/Level#$Control/HBoxContainer/LeftPanel/CharacterInfo/LevelLabel
|
||||
@onready var xp_bar = $Control/Panel/ProgressBar#$Control/HBoxContainer/LeftPanel/CharacterInfo/XPBar
|
||||
@onready var gold_label = $Control/Panel/Label#$Control/HBoxContainer/LeftPanel/CharacterInfo/GoldLabel
|
||||
|
||||
@onready var str_value = $Control/Panel/HBoxContainer/StrFrame/DisplayCurrentStr/currentStrLabel#$Control/HBoxContainer/LeftPanel/StatsPanel/StrValue
|
||||
@onready var int_value = $Control/Panel/HBoxContainer/IntFrame/DisplayCurrentInt/currentIntLabel#$Control/HBoxContainer/LeftPanel/StatsPanel/IntValue
|
||||
@onready var wis_value = $Control/Panel/HBoxContainer/WisFrame/DisplayCurrentWis/currentWisLabel#$Control/HBoxContainer/LeftPanel/StatsPanel/WisValue
|
||||
@onready var end_value = $Control/Panel/HBoxContainer/EndFrame/DisplayCurrentEnd/currentStrLabel#$Control/HBoxContainer/LeftPanel/StatsPanel/EndValue
|
||||
@onready var dex_value = $Control/Panel/HBoxContainer/DexFrame/DisplayCurrentDex/currentDexLabel#$Control/HBoxContainer/LeftPanel/StatsPanel/DexValue
|
||||
@onready var points_label = $Control/Panel/HBoxContainer/RemainingPoints/DisplayCurrentRP/currentRPLabel#$Control/HBoxContainer/LeftPanel/StatsPanel/PointsRemaining
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
for stat in ["Str", "Int", "Wis", "End", "Dex"]:
|
||||
var btnMap = "plus%s" % stat
|
||||
var folderMap = "%sFrame" % stat
|
||||
var btn_path = "Control/Panel/HBoxContainer/%s/%s" % [folderMap, btnMap]
|
||||
#var btn_path = "Control/HBoxContainer/LeftPanel/StatsPanel/%sButton" % stat
|
||||
if has_node(btn_path):
|
||||
var btn = get_node(btn_path)
|
||||
btn.pressed.connect(_on_stat_allocate.bind(stat.to_lower()))
|
||||
|
||||
call_deferred("_connect_gamestate")
|
||||
|
||||
func _connect_gamestate():
|
||||
if GameState:
|
||||
GameState.character_updated.connect(_update_ui)
|
||||
|
||||
func _update_ui():
|
||||
var c = GameState.character
|
||||
name_label.text = c.name
|
||||
level_label.text = "Level %d" % c.level
|
||||
xp_bar.min_value = _xp_required_for_level(c.level)
|
||||
xp_bar.max_value = _xp_required_for_level(c.level+1)
|
||||
xp_bar.value = c.xp
|
||||
xp_bar.tooltip_text = str(c.xp," / ", _xp_required_for_level(c.level+1))
|
||||
|
||||
# Gold display
|
||||
if gold_label:
|
||||
gold_label.text = "Gold: %d" % c.get("gold", 0)
|
||||
|
||||
str_value.text = str(c.strength)
|
||||
int_value.text = str(c.intelligence)
|
||||
wis_value.text = str(c.wisdom)
|
||||
end_value.text = str(c.endurance)
|
||||
dex_value.text = str(c.dexterity)
|
||||
|
||||
points_label.text = str(c.unspent_points) #"Points: %d" %
|
||||
|
||||
# Enable/disable stat buttons based on available points
|
||||
var has_points = c.unspent_points > 0
|
||||
for stat in ["Str", "Int", "Wis", "End", "Dex"]:
|
||||
var btnMap = "plus%s" % stat
|
||||
var folderMap = "%sFrame" % stat
|
||||
var btn_path = "Control/Panel/HBoxContainer/%s/%s" % [folderMap, btnMap] #Control/HBoxContainer/LeftPanel/StatsPanel/%sButton" % stat
|
||||
if has_node(btn_path):
|
||||
var btn = get_node(btn_path)
|
||||
btn.disabled = not has_points
|
||||
|
||||
func _xp_for_level(level: int) -> int:
|
||||
return int(round(100 * pow(1.15, level)))
|
||||
|
||||
func _xp_to_next_level(level: int) -> int:
|
||||
return int(round(100 * pow(1.15, level - 1)))
|
||||
|
||||
|
||||
func _xp_required_for_level(level: int) -> int:
|
||||
if level <= 1:
|
||||
return 0
|
||||
var total := 100 * (pow(1.15, level - 1) - 1.0) / (1.15 - 1.0)
|
||||
return int(round(total))
|
||||
|
||||
func _on_stat_allocate(stat: String):
|
||||
if GameState.character.unspent_points > 0:
|
||||
GameState.send_allocate_stat(stat)
|
||||
@@ -0,0 +1 @@
|
||||
uid://ixmjbaov0w5f
|
||||
+34
-53
@@ -1,27 +1,29 @@
|
||||
extends CanvasLayer
|
||||
|
||||
@onready var name_label = $Control/HBoxContainer/LeftPanel/CharacterInfo/NameLabel
|
||||
@onready var level_label = $Control/HBoxContainer/LeftPanel/CharacterInfo/LevelLabel
|
||||
@onready var xp_bar = $Control/HBoxContainer/LeftPanel/CharacterInfo/XPBar
|
||||
@onready var gold_label = $Control/HBoxContainer/LeftPanel/CharacterInfo/GoldLabel
|
||||
|
||||
@onready var str_value = $Control/HBoxContainer/LeftPanel/StatsPanel/StrValue
|
||||
@onready var int_value = $Control/HBoxContainer/LeftPanel/StatsPanel/IntValue
|
||||
@onready var wis_value = $Control/HBoxContainer/LeftPanel/StatsPanel/WisValue
|
||||
@onready var end_value = $Control/HBoxContainer/LeftPanel/StatsPanel/EndValue
|
||||
@onready var dex_value = $Control/HBoxContainer/LeftPanel/StatsPanel/DexValue
|
||||
@onready var points_label = $Control/HBoxContainer/LeftPanel/StatsPanel/PointsRemaining
|
||||
#@onready var name_label = $Control/Panel/Name#$Control/HBoxContainer/LeftPanel/CharacterInfo/NameLabel
|
||||
#@onready var level_label = $Control/Panel/Level#$Control/HBoxContainer/LeftPanel/CharacterInfo/LevelLabel
|
||||
#@onready var xp_bar = $Control/Panel/ProgressBar#$Control/HBoxContainer/LeftPanel/CharacterInfo/XPBar
|
||||
#@onready var gold_label = $Control/Panel/Label#$Control/HBoxContainer/LeftPanel/CharacterInfo/GoldLabel
|
||||
#
|
||||
#@onready var str_value = $Control/Panel/HBoxContainer/StrFrame/DisplayCurrentStr/currentStrLabel#$Control/HBoxContainer/LeftPanel/StatsPanel/StrValue
|
||||
#@onready var int_value = $Control/Panel/HBoxContainer/IntFrame/DisplayCurrentInt/currentIntLabel#$Control/HBoxContainer/LeftPanel/StatsPanel/IntValue
|
||||
#@onready var wis_value = $Control/Panel/HBoxContainer/WisFrame/DisplayCurrentWis/currentWisLabel#$Control/HBoxContainer/LeftPanel/StatsPanel/WisValue
|
||||
#@onready var end_value = $Control/Panel/HBoxContainer/EndFrame/DisplayCurrentEnd/currentStrLabel#$Control/HBoxContainer/LeftPanel/StatsPanel/EndValue
|
||||
#@onready var dex_value = $Control/Panel/HBoxContainer/DexFrame/DisplayCurrentDex/currentDexLabel#$Control/HBoxContainer/LeftPanel/StatsPanel/DexValue
|
||||
#@onready var points_label = $Control/Panel/HBoxContainer/RemainingPoints/DisplayCurrentRP/currentRPLabel#$Control/HBoxContainer/LeftPanel/StatsPanel/PointsRemaining
|
||||
|
||||
@onready var enter_btn = $Control/HBoxContainer/RightPanel/EnterDungeonBtn
|
||||
@onready var browse_btn = $Control/HBoxContainer/RightPanel/BrowseDungeonsBtn
|
||||
@onready var dungeon_browser = $DungeonBrowser
|
||||
|
||||
@onready var inventory_btn = $Control/HBoxContainer/LeftPanel/MenuButtons/InventoryBtn
|
||||
@onready var crafting_btn = $Control/HBoxContainer/LeftPanel/MenuButtons/CraftingBtn
|
||||
@onready var stash_btn = $Control/HBoxContainer/LeftPanel/MenuButtons/StashBtn
|
||||
@onready var inventory_btn = $Control/InventoryBtn#$Control/HBoxContainer/LeftPanel/MenuButtons/InventoryBtn
|
||||
@onready var crafting_btn = $Control/CraftingBtn#$Control/HBoxContainer/LeftPanel/MenuButtons/CraftingBtn
|
||||
@onready var stash_btn = $Control/StashBtn#$Control/HBoxContainer/LeftPanel/MenuButtons/StashBtn
|
||||
@onready var inventory_ui = $InventoryUI
|
||||
@onready var crafting_ui = $CraftingUI
|
||||
@onready var stash_ui = $StashUI
|
||||
@onready var char_sheet_ui = $CharSheetUI
|
||||
@onready var char_sheet_btn = $Control/CharSheetBtn
|
||||
|
||||
var selected_tier: int = 1
|
||||
|
||||
@@ -39,6 +41,9 @@ func _ready():
|
||||
crafting_btn.pressed.connect(_on_crafting_pressed)
|
||||
if stash_btn:
|
||||
stash_btn.pressed.connect(_on_stash_pressed)
|
||||
if char_sheet_btn:
|
||||
char_sheet_btn.pressed.connect(_toggle_char_sheet)
|
||||
|
||||
|
||||
# Setup tier buttons
|
||||
for i in range(1, 6):
|
||||
@@ -47,51 +52,27 @@ func _ready():
|
||||
var btn = get_node(btn_path)
|
||||
btn.pressed.connect(_on_tier_selected.bind(i))
|
||||
|
||||
# Setup stat buttons
|
||||
for stat in ["Str", "Int", "Wis", "End", "Dex"]:
|
||||
var btn_path = "Control/HBoxContainer/LeftPanel/StatsPanel/%sButton" % stat
|
||||
if has_node(btn_path):
|
||||
var btn = get_node(btn_path)
|
||||
btn.pressed.connect(_on_stat_allocate.bind(stat.to_lower()))
|
||||
|
||||
# Defer GameState connection
|
||||
call_deferred("_connect_gamestate")
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
_handle_input()
|
||||
|
||||
func _handle_input():
|
||||
if Input.is_action_just_pressed("charSheet"):
|
||||
_toggle_char_sheet()
|
||||
#if Input.is_action_just_pressed("inventory"):
|
||||
#_toggle_inventory()
|
||||
|
||||
func _connect_gamestate():
|
||||
if GameState:
|
||||
GameState.character_updated.connect(_update_ui)
|
||||
#if GameState:
|
||||
#GameState.character_updated.connect(_update_ui)
|
||||
|
||||
# Request inventory when hub loads
|
||||
if GameState:
|
||||
GameState.request_inventory()
|
||||
|
||||
func _update_ui():
|
||||
var c = GameState.character
|
||||
name_label.text = c.name
|
||||
level_label.text = "Level %d" % c.level
|
||||
xp_bar.max_value = c.xp_next
|
||||
xp_bar.value = c.xp
|
||||
|
||||
|
||||
# Gold display
|
||||
if gold_label:
|
||||
gold_label.text = "Gold: %d" % c.get("gold", 0)
|
||||
|
||||
str_value.text = str(c.strength)
|
||||
int_value.text = str(c.intelligence)
|
||||
wis_value.text = str(c.wisdom)
|
||||
end_value.text = str(c.endurance)
|
||||
dex_value.text = str(c.dexterity)
|
||||
|
||||
points_label.text = "Points: %d" % c.unspent_points
|
||||
|
||||
# Enable/disable stat buttons based on available points
|
||||
var has_points = c.unspent_points > 0
|
||||
for stat in ["Str", "Int", "Wis", "End", "Dex"]:
|
||||
var btn_path = "Control/HBoxContainer/LeftPanel/StatsPanel/%sButton" % stat
|
||||
if has_node(btn_path):
|
||||
var btn = get_node(btn_path)
|
||||
btn.disabled = not has_points
|
||||
|
||||
func _on_tier_selected(tier: int):
|
||||
selected_tier = tier
|
||||
@@ -102,10 +83,6 @@ func _on_tier_selected(tier: int):
|
||||
var btn = get_node(btn_path)
|
||||
btn.button_pressed = (i == tier)
|
||||
|
||||
func _on_stat_allocate(stat: String):
|
||||
if GameState.character.unspent_points > 0:
|
||||
GameState.send_allocate_stat(stat)
|
||||
|
||||
func _on_enter_dungeon():
|
||||
# Quick join - uses old system for backwards compatibility
|
||||
GameState.send_enter_dungeon(selected_tier)
|
||||
@@ -128,3 +105,7 @@ func _on_crafting_pressed():
|
||||
func _on_stash_pressed():
|
||||
if stash_ui:
|
||||
stash_ui.show_stash()
|
||||
|
||||
func _toggle_char_sheet():
|
||||
if char_sheet_ui:
|
||||
char_sheet_ui.visible = not char_sheet_ui.visible
|
||||
|
||||
@@ -91,14 +91,14 @@ func _setup_inventory_slots():
|
||||
func _create_slot_button() -> Button:
|
||||
var btn = Button.new()
|
||||
btn.custom_minimum_size = Vector2(48, 48)
|
||||
btn.add_theme_stylebox_override("normal", _create_slot_style(Color(0.15, 0.15, 0.2)))
|
||||
btn.add_theme_stylebox_override("hover", _create_slot_style(Color(0.25, 0.25, 0.35)))
|
||||
btn.add_theme_stylebox_override("normal", _create_slot_style(Color(0.408, 0.408, 0.322, 1.0)))
|
||||
btn.add_theme_stylebox_override("hover", _create_slot_style(Color(0.408, 0.408, 0.322, 0.902)))
|
||||
return btn
|
||||
|
||||
func _create_slot_style(color: Color) -> StyleBoxFlat:
|
||||
var style = StyleBoxFlat.new()
|
||||
style.bg_color = color
|
||||
style.border_color = Color(0.3, 0.3, 0.4)
|
||||
style.border_color = Color(0.0, 0.0, 0.0, 1.0)
|
||||
style.set_border_width_all(2)
|
||||
style.set_corner_radius_all(4)
|
||||
return style
|
||||
|
||||
Reference in New Issue
Block a user