extends Node
const SERVER_PORT = 8080const SERVER_IP = "XXX.XXX.XX.XX"
var multiplayer_scene = preload("res://scenes/multiplayer_player.tscn")
var _player_spawn_nodevar host_mode_enabled = falsevar mutiplayer_mode_enabled = falsevar respawn_point = Vector2(30, 20)
var chat_content = []
func become_host(): print("Become") _player_spawn_node = get_tree().get_current_scene().get_node("Players") host_mode_enabled = true mutiplayer_mode_enabled = true var server_peer = WebSocketMultiplayerPeer.new() server_peer.create_server(SERVER_PORT) multiplayer.multiplayer_peer = server_peer multiplayer.peer_connected.connect(_add_player_to_game) multiplayer.peer_disconnected.connect(_del_player) _remove_single_player()
if not OS.has_feature("dedicated_server"): _add_player_to_game(1) func join_game(): print("Join") mutiplayer_mode_enabled = true var client_peer = WebSocketMultiplayerPeer.new() client_peer.create_client("ws://" + SERVER_IP + ":" + str(SERVER_PORT)) multiplayer.multiplayer_peer = client_peer _remove_single_player()
func _add_player_to_game(id: int): print("Player %s joined the game!" % id) var player_to_add = multiplayer_scene.instantiate() player_to_add.player_id = id player_to_add.name = str(id) _player_spawn_node.add_child(player_to_add, true) func _del_player(id: int): print("Player %s left the game!" % id) if not _player_spawn_node.has_node(str(id)): return _player_spawn_node.get_node(str(id)).queue_free()
func _remove_single_player(): print("Remove single player") var player_to_remove = get_tree().get_current_scene().get_node("Player") player_to_remove.queue_free()
extends CharacterBody2D
const SPEED = 130.0const JUMP_VELOCITY = -300.0
@onready var animated_sprite = $AnimatedSprite2D
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var direction = 1var do_jump = falsevar _is_on_floor = truevar alive = true
@export var player_id = 1: set(id): player_id = id %InputSynchronizer.set_multiplayer_authority(id) func _ready(): if multiplayer.get_unique_id() == player_id: $Camera2D.make_current() else: $AnimatedSprite2D.set_modulate(Color(1,1,1,0.3)) $Camera2D.enabled = false
func _apply_animations(delta): # Flip the Sprite if direction > 0: animated_sprite.flip_h = false elif direction < 0: animated_sprite.flip_h = true # Play animations if _is_on_floor: if direction == 0: animated_sprite.play("idle") else: animated_sprite.play("run") else: animated_sprite.play("jump")
func _apply_movement_from_input(delta): # Add the gravity. if not is_on_floor(): velocity.y += gravity * delta
# Handle jump. if do_jump and is_on_floor(): velocity.y = JUMP_VELOCITY do_jump = false
# Get the input direction: -1, 0, 1 direction = %InputSynchronizer.input_direction # Apply movement if direction: velocity.x = direction * SPEED else: velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
func _physics_process(delta): if multiplayer.is_server(): if not alive && is_on_floor(): _set_alive() _is_on_floor = is_on_floor() _apply_movement_from_input(delta)
if not multiplayer.is_server() || MultiplayerManager.host_mode_enabled: _apply_animations(delta)
func mark_dead(): alive = false $CollisionShape2D.set_deferred("disabled", true) $RespawnTimer.start() func _respawn(): position = MultiplayerManager.respawn_point $CollisionShape2D.set_deferred("disabled", false) func _set_alive(): alive = true Engine.time_scale = 1.0
extends MultiplayerSynchronizer
@onready var player = $".."
var input_direction
func _ready(): if get_multiplayer_authority() != multiplayer.get_unique_id(): set_process(false) set_physics_process(false) input_direction = Input.get_axis("move_left", "move_right")
func _process(delta): if Input.is_action_just_pressed("jump"): jump.rpc()
func _physics_process(delta): input_direction = Input.get_axis("move_left", "move_right")
@rpc("call_local")func jump(): if multiplayer.is_server() && player._is_on_floor: player.do_jump = true