Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. # https://godot3tutorials.wordpress.com/2018/03/02/part-3-multiplayer-tutorial-godot-main-multiplayer-concepts/
  4. # https://docs.godotengine.org/en/3.1/tutorials/networking/high_level_multiplayer.html#rpc
  5. # https://www.gamefromscratch.com/page/Godot-3-Tutorial-Networking.aspx
  6.  
  7. # Declare member variables here. Examples:
  8. # var a = 2
  9. # var b = "text"
  10. const UP = Vector2(0, -1)
  11. const ACCELERATION = 50
  12. #const GRAVITY = 20
  13. const MAX_SPEED = 200
  14. #const JUMP_HEIGHT = -500
  15. var friction = false
  16.  
  17. var motion = Vector2()
  18.  
  19. # Called when the node enters the scene tree for the first time.
  20. func _ready():
  21. # Set Player's Position
  22. # rpc_unreliable("setPosition", Vector2(position.x, position.y))
  23.  
  24. print("Network Master: " + str(get_network_master()))
  25. print("Player ID: " + str(get_tree().get_network_unique_id()))
  26.  
  27. # The idea is the different players will be spawned at different coordinates.
  28. if get_network_master() == 1:
  29. # get_tree().get_network_unique_id()
  30. print("Server Position - Master: " + str(get_network_master()))
  31.  
  32. #global_position = Vector2(300,250)
  33. rpc_unreliable("setPosition", Vector2(300,250), get_network_master()) # The network id should be 1 for the server
  34. else:
  35. print("Client Position - Master: " + str(get_network_master()))
  36. rpc_unreliable("setPosition", Vector2(500,250), get_network_master())
  37.  
  38. # sync sets for all devices including server
  39. sync func setPosition(pos, playerid):
  40. # Does This Open Up A Security Hole
  41. # I Probably Want To Verify Client's Position From Server
  42.  
  43. print("Player: " + str(playerid) + " Set Position: " + str(pos))
  44.  
  45. if get_tree().get_network_unique_id() == playerid:
  46. print("Own ID: " + str(get_network_master()) + " Player ID: " + str(playerid))
  47. print("Caller ID: " + str(get_tree().get_rpc_sender_id())) # What is the caller ID? It just returns 0 for me...
  48. global_position = pos
  49.  
  50. # Called every frame. 'delta' is the elapsed time since the previous frame.
  51. func _physics_process(_delta):
  52. if is_network_master():
  53. if Input.is_key_pressed(KEY_W):
  54. #print("Up")
  55. motion.y = max(motion.y - ACCELERATION, -MAX_SPEED)
  56. elif Input.is_key_pressed(KEY_S):
  57. #print("Down")
  58. motion.y = min(motion.y + ACCELERATION, MAX_SPEED)
  59. else:
  60. friction = true;
  61. #$Sprite.play("Idle");
  62.  
  63. if Input.is_key_pressed(KEY_A):
  64. #print("Left")
  65. motion.x = max(motion.x - ACCELERATION, -MAX_SPEED)
  66. elif Input.is_key_pressed(KEY_D):
  67. #print("Right")
  68. motion.x = min(motion.x + ACCELERATION, MAX_SPEED)
  69. else:
  70. friction = true;
  71. #$Sprite.play("Idle");
  72.  
  73. if friction == true:
  74. motion.x = lerp(motion.x, 0, 0.2)
  75. motion.y = lerp(motion.y, 0, 0.2)
  76. friction = false
  77.  
  78. rpc_unreliable("movePlayer", motion)
  79. motion = move_and_slide(motion, UP)
  80.  
  81. # puppet (formerly slave) sets for all devices except server
  82. puppet func movePlayer(mot):
  83. motion = move_and_slide(mot, UP)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement