Client.gd 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. extends Node
  2. var net = NetworkedMultiplayerENet.new()
  3. var api = MultiplayerAPI.new()
  4. var port = 8081
  5. var ip = '127.0.0.1'
  6. # Called when the node enters the scene tree for the first time.
  7. func _ready():
  8. print('connecting to port: ' + str(port))
  9. net.create_client(ip,port)
  10. self.set_custom_multiplayer(api)
  11. custom_multiplayer.set_root_node(self)
  12. custom_multiplayer.set_network_peer(net)
  13. net.connect("connection_failed", self, "_On_Connection_Failed")
  14. net.connect("connection_succeeded", self, "_On_Connection_Succeeded")
  15. pass # Replace with function body.
  16. func _process(_delta):
  17. custom_multiplayer.poll()
  18. remote func ping():
  19. print('messaged received from server')
  20. func _On_Connection_Failed():
  21. print("Failed to connect to game server")
  22. var playerphsyics = preload('res://playersprite.tscn')
  23. func _On_Connection_Succeeded():
  24. $Input.enabled = true
  25. print("Succesfully connected to game server")
  26. var instance = playerphsyics.instance()
  27. instance.name = str(api.get_network_unique_id())
  28. $players.add_child(instance)
  29. # extra stuff
  30. var ping = 0
  31. func latency_test():
  32. rpc_id(1,'latency',OS.get_system_time_msecs())
  33. remote func returnping(arg):
  34. ping = (OS.get_system_time_msecs() - arg)
  35. func send_input(arg):
  36. rpc_id(1,'input_handler',arg)
  37. remote func player_position(arg):
  38. for id in arg.keys():
  39. if $players.has_node(str(id)):
  40. var node = $players.get_node(str(id))
  41. if node.global_position.x != arg[id].x:
  42. node.scale.x = -1 if node.global_position.x > arg[id].x else 1
  43. node.global_position = arg[id]
  44. remote func player_animation(arg):
  45. for id in arg.keys():
  46. if $players.has_node(str(id)):
  47. $players.get_node(str(id)).animation = arg[id]
  48. remote func enemies_position(arg):
  49. if has_node('enemies'):
  50. for i in $enemies.get_children():
  51. i.hide()
  52. for id in arg.keys().size():
  53. var node = $enemies.get_child(id)
  54. node.global_position = arg[arg.keys()[id]][0]
  55. node.animation = arg[arg.keys()[id]][1]
  56. node.show()
  57. remote func attack_anim(arg):
  58. $players.get_node( str(api.get_network_unique_id()) ).get_node('weapon/AnimationPlayer').play(arg)