player.gd 833 B

12345678910111213141516171819202122232425262728293031
  1. extends Node2D
  2. @onready var parent = get_parent()
  3. const speed : float = Globals.tile_size * 9
  4. const jump_height : float = Globals.tile_size * 2.25
  5. const jump_velocity : float = -sqrt(jump_height * 2 * Globals.gravity)
  6. func _ready() -> void:
  7. EventBus.player_moving_to_box.connect(move_to_box)
  8. func _physics_process(delta: float) -> void:
  9. if Input.is_action_just_pressed("player_jump") and parent.is_on_floor():
  10. parent.velocity.y = jump_velocity
  11. var direction := Input.get_axis("player_left", "player_right")
  12. if direction:
  13. parent.velocity.x = direction * speed
  14. parent.getting_force = true
  15. else:
  16. parent.getting_force = false
  17. func delete_from_parent() -> void:
  18. get_parent().remove_child(self)
  19. parent.velocity.x = 0
  20. func move_to_box(box : Box) -> void:
  21. delete_from_parent()
  22. box.add_child(self)
  23. self.parent = box