jumper.gd 750 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. class_name Jumper
  2. extends RigidBody2D
  3. const SPEED = 5.0
  4. const JUMP_IMPULSE = 942.5
  5. const MOVE_FORCE = 1142
  6. @export var skins: Array[Texture2D]
  7. # Set by the authority, synchronized on spawn.
  8. @export var player := 1 :
  9. set(id):
  10. player = id
  11. # Give authority over the player input to the appropriate peer.
  12. $JumperInput.set_multiplayer_authority(id)
  13. $Sprite2D.set_texture(skins[2])
  14. # Player synchronized input
  15. @onready var input: JumperInput = $JumperInput
  16. func _ready():
  17. pass
  18. func _physics_process(delta):
  19. # Handle Jump.
  20. if input.jumping:
  21. apply_impulse(Vector2.UP * JUMP_IMPULSE)
  22. # Reset jump state.
  23. input.jumping = false
  24. # Handle movement.
  25. var direction = input.direction
  26. if direction:
  27. apply_force(direction * MOVE_FORCE)