Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- # https://godot3tutorials.wordpress.com/2018/03/02/part-3-multiplayer-tutorial-godot-main-multiplayer-concepts/
- # https://docs.godotengine.org/en/3.1/tutorials/networking/high_level_multiplayer.html#rpc
- # https://www.gamefromscratch.com/page/Godot-3-Tutorial-Networking.aspx
- # Declare member variables here. Examples:
- # var a = 2
- # var b = "text"
- const UP = Vector2(0, -1)
- const ACCELERATION = 50
- #const GRAVITY = 20
- const MAX_SPEED = 200
- #const JUMP_HEIGHT = -500
- var friction = false
- var motion = Vector2()
- # Called when the node enters the scene tree for the first time.
- func _ready():
- # Set Player's Position
- # rpc_unreliable("setPosition", Vector2(position.x, position.y))
- print("Network Master: " + str(get_network_master()))
- print("Player ID: " + str(get_tree().get_network_unique_id()))
- # The idea is the different players will be spawned at different coordinates.
- if get_network_master() == 1:
- # get_tree().get_network_unique_id()
- print("Server Position - Master: " + str(get_network_master()))
- #global_position = Vector2(300,250)
- rpc_unreliable("setPosition", Vector2(300,250), get_network_master()) # The network id should be 1 for the server
- else:
- print("Client Position - Master: " + str(get_network_master()))
- rpc_unreliable("setPosition", Vector2(500,250), get_network_master())
- # sync sets for all devices including server
- sync func setPosition(pos, playerid):
- # Does This Open Up A Security Hole
- # I Probably Want To Verify Client's Position From Server
- print("Player: " + str(playerid) + " Set Position: " + str(pos))
- if get_tree().get_network_unique_id() == playerid:
- print("Own ID: " + str(get_network_master()) + " Player ID: " + str(playerid))
- print("Caller ID: " + str(get_tree().get_rpc_sender_id())) # What is the caller ID? It just returns 0 for me...
- global_position = pos
- # Called every frame. 'delta' is the elapsed time since the previous frame.
- func _physics_process(_delta):
- if is_network_master():
- if Input.is_key_pressed(KEY_W):
- #print("Up")
- motion.y = max(motion.y - ACCELERATION, -MAX_SPEED)
- elif Input.is_key_pressed(KEY_S):
- #print("Down")
- motion.y = min(motion.y + ACCELERATION, MAX_SPEED)
- else:
- friction = true;
- #$Sprite.play("Idle");
- if Input.is_key_pressed(KEY_A):
- #print("Left")
- motion.x = max(motion.x - ACCELERATION, -MAX_SPEED)
- elif Input.is_key_pressed(KEY_D):
- #print("Right")
- motion.x = min(motion.x + ACCELERATION, MAX_SPEED)
- else:
- friction = true;
- #$Sprite.play("Idle");
- if friction == true:
- motion.x = lerp(motion.x, 0, 0.2)
- motion.y = lerp(motion.y, 0, 0.2)
- friction = false
- rpc_unreliable("movePlayer", motion)
- motion = move_and_slide(motion, UP)
- # puppet (formerly slave) sets for all devices except server
- puppet func movePlayer(mot):
- motion = move_and_slide(mot, UP)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement