Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ---- This code is in player_ship.gd ----
- # Called when a new player joins the game
- func onPlayerJoin(args):
- print("entered onPlayerJoin")
- var player_state = args[0]
- print("new player joined: ", player_state.id)
- print("args: " + str(args))
- var player := player_scene.instance() as PlayerShip
- get_parent().add_child(player)
- player.player_state = player_state
- player.joined = true
- var index := randi() % player_positions.size()
- player.position = player_positions[index]
- player_positions.remove(index)
- players.append(player)
- # Listen to onQuit event
- player_state.onQuit(bridgeToJS("onPlayerQuit"))
- func _physics_process(delta):
- var movement_vector = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
- #print("movement vector = " + str(movement_vector))
- if joined:
- #print("joined = true")
- if player_state:
- #print("player state = " + str(player_state))
- player_state.setState(
- str(MultiplayerMsg.EPlayerMsg.MOVEMENT_VECTOR),
- movement_vector
- )
- # --------
- # ---- This code is in playroom_manager.gd ----
- func _move_player(player: PlayerShip, input_vector_js) -> void:
- var input_v := Vector2(input_vector_js.x, input_vector_js.y)
- player.move_and_slide(input_v * player.speed)
- player.player_state.setState(
- str(MultiplayerMsg.EPlayerMsg.POSITION),
- player.position
- )
- func _set_player_position(player: PlayerShip, position_js) -> void:
- var pos := Vector2(position_js.x, position_js.y)
- player.position = pos
- # Called every frame. 'delta' is the elapsed time since the previous frame.
- func _physics_process(delta):
- #var input_vector = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
- if Playroom.isHost():
- #print("is host = true")
- for player in players:
- if player:
- #print("player = " + str(player))
- if player.player_state:
- #print("player.player_state = " + str(player.player_state))
- var input_vector = player.player_state.getState(
- str(MultiplayerMsg.EPlayerMsg.MOVEMENT_VECTOR)
- )
- #print("input_vector = " + str(input_vector))
- if input_vector != null:
- print("input_vector != null")
- _move_player(player, input_vector)
- else:
- #print("is host = false")
- for player in players:
- if player:
- #print("player = " + str(player))
- if player.player_state:
- #print("player.player_state = " + str(player.player_state))
- var pos = player.player_state.getState(
- str(MultiplayerMsg.EPlayerMsg.POSITION)
- )
- #print("pos = " + str(pos))
- if pos != null:
- #print("pos != null")
- _set_player_position(player, pos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement