Player.gd 881 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. extends CharacterBody2D
  2. const SPEED = 300.0
  3. const SLIDE = 50.0
  4. const FORCE = 800.0
  5. func _physics_process(delta):
  6. prepare_to_move()
  7. var collided = move_and_slide()
  8. if collided:
  9. try_to_shoot()
  10. func prepare_to_move():
  11. if Input.is_action_pressed("ui_right"):
  12. velocity.x = SPEED
  13. elif Input.is_action_pressed("ui_left"):
  14. velocity.x = -SPEED
  15. else:
  16. velocity.x = move_toward(velocity.x, 0, SLIDE)
  17. if Input.is_action_pressed("ui_down"):
  18. velocity.y = SPEED
  19. elif Input.is_action_pressed("ui_up"):
  20. velocity.y = -SPEED
  21. else:
  22. velocity.y = move_toward(velocity.y, 0, SLIDE)
  23. func try_to_shoot():
  24. var collision = get_last_slide_collision()
  25. var target = collision.get_collider()
  26. var normal = collision.get_normal()
  27. if target is PhysicsBody2D:
  28. shoot(target, -normal)
  29. func shoot(target: PhysicsBody2D, dir: Vector2) -> void:
  30. target.apply_force(dir * FORCE)