Advertisement
equus09

Untitled

Jan 9th, 2025 (edited)
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 2.60 KB | Source Code | 0 0
  1. # ---- This code is in player_ship.gd ----
  2. # Called when a new player joins the game
  3. func onPlayerJoin(args):
  4.     print("entered onPlayerJoin")
  5.     var player_state = args[0]
  6.     print("new player joined: ", player_state.id)
  7.     print("args: " + str(args))
  8.    
  9.     var player := player_scene.instance() as PlayerShip
  10.     get_parent().add_child(player)
  11.     player.player_state = player_state
  12.     player.joined = true
  13.     var index := randi() % player_positions.size()
  14.     player.position = player_positions[index]
  15.     player_positions.remove(index)
  16.     players.append(player)
  17.     # Listen to onQuit event
  18.     player_state.onQuit(bridgeToJS("onPlayerQuit"))
  19.  
  20. func _physics_process(delta):
  21.     var movement_vector = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
  22.     #print("movement vector = " + str(movement_vector))
  23.     if joined:
  24.         #print("joined = true")
  25.         if player_state:
  26.             #print("player state = " + str(player_state))
  27.             player_state.setState(
  28.                     str(MultiplayerMsg.EPlayerMsg.MOVEMENT_VECTOR),
  29.                     movement_vector
  30.                     )
  31.  
  32. # --------
  33.  
  34. # ---- This code is in playroom_manager.gd ----
  35. func _move_player(player: PlayerShip, input_vector_js) -> void:
  36.     var input_v := Vector2(input_vector_js.x, input_vector_js.y)
  37.     player.move_and_slide(input_v * player.speed)
  38.     player.player_state.setState(
  39.             str(MultiplayerMsg.EPlayerMsg.POSITION),
  40.             player.position
  41.             )
  42.  
  43.  
  44. func _set_player_position(player: PlayerShip, position_js) -> void:
  45.     var pos := Vector2(position_js.x, position_js.y)
  46.     player.position = pos
  47.  
  48. # Called every frame. 'delta' is the elapsed time since the previous frame.
  49. func _physics_process(delta):
  50.     #var input_vector = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
  51.    
  52.     if Playroom.isHost():
  53.         #print("is host = true")
  54.         for player in players:
  55.             if player:
  56.                 #print("player = " + str(player))
  57.                 if player.player_state:
  58.                     #print("player.player_state = " + str(player.player_state))
  59.                     var input_vector = player.player_state.getState(
  60.                             str(MultiplayerMsg.EPlayerMsg.MOVEMENT_VECTOR)
  61.                             )
  62.                     #print("input_vector = " + str(input_vector))
  63.                     if input_vector != null:
  64.                         print("input_vector != null")
  65.                         _move_player(player, input_vector)
  66.     else:
  67.         #print("is host = false")
  68.         for player in players:
  69.             if player:
  70.                 #print("player = " + str(player))
  71.                 if player.player_state:
  72.                     #print("player.player_state = " + str(player.player_state))
  73.                     var pos = player.player_state.getState(
  74.                             str(MultiplayerMsg.EPlayerMsg.POSITION)
  75.                             )
  76.                     #print("pos = " + str(pos))
  77.                     if pos != null:
  78.                         #print("pos != null")
  79.                         _set_player_position(player, pos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement