Improved network handling
This commit is contained in:
33
network/Network.gd
Normal file
33
network/Network.gd
Normal file
@ -0,0 +1,33 @@
|
||||
extends Node
|
||||
|
||||
var udp := PacketPeerUDP.new()
|
||||
var handlers: Array[Node] = []
|
||||
|
||||
enum Packet {
|
||||
PING = 1,
|
||||
LOGIN = 2,
|
||||
LOGOUT = 3,
|
||||
MOVE = 10,
|
||||
}
|
||||
|
||||
func _init():
|
||||
handlers.resize(256)
|
||||
udp.connect_to_host("127.0.0.1", 4242)
|
||||
|
||||
func _process(_delta):
|
||||
if udp.get_available_packet_count() <= 0:
|
||||
return
|
||||
|
||||
var packet := udp.get_packet()
|
||||
var type := packet.decode_u8(0) as Packet
|
||||
var handler := handlers[type]
|
||||
|
||||
if handler == null:
|
||||
push_warning("Unknown packet type %d" % type)
|
||||
return
|
||||
|
||||
handler.handle_packet(packet)
|
||||
|
||||
func add_handler(packet: Packet, node: Node):
|
||||
assert(node.has_method("handle_packet"))
|
||||
handlers[packet] = node
|
Reference in New Issue
Block a user