Guest User

Untitled

a guest
Dec 16th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. # Estende a classe CharacterBody2D para criar um corpo de personagem que pode se mover e colidir
  2. extends CharacterBody2D
  3.  
  4. # Define o tamanho do tile (bloco) no jogo
  5. const TILE_SIZE = 16
  6. @onready var ray = $RayCast
  7. @onready var animation_tree = $AnimationTree
  8. @onready var animation_state = animation_tree["parameters/playback"] as AnimationNodeStateMachinePlayback
  9.  
  10. # Armazena a última direção que o personagem estava se movendo
  11. var last_direction = Vector2(0, 1)
  12. # Direção atual do input
  13. var input_direction = Vector2.ZERO
  14. # Posição inicial do personagem antes de começar a se mover
  15. var initial_position = Vector2.ZERO
  16. # Flag que indica se o personagem está se movendo
  17. var is_moving = false
  18. var is_running = false
  19. # Percentual da movimentação concluída para o próximo tile
  20. var percent_moved_to_next_tile = 0.0
  21. @export var walk_speed = 3.0
  22. @export var run_speed = 8.0
  23. @export var can_move : bool = true
  24.  
  25.  
  26. func _ready():
  27. # Define a posição inicial do personagem
  28. initial_position = position
  29.  
  30. func _physics_process(delta):
  31. # Se o personagem não está se movendo, processa o input do jogador
  32. if not is_moving:
  33. process_player_input()
  34.  
  35. # Se o personagem está se movendo e pode se mover, atualiza a animação e move o personagem
  36. if is_moving and can_move:
  37. last_direction = input_direction
  38. update_animation(input_direction, true)
  39. move(delta)
  40. else:
  41. # Se o personagem não está se movendo, atualiza a animação para o estado de inatividade
  42. update_animation(last_direction, false)
  43.  
  44. # Função para processar o input do jogador
  45. func process_player_input():
  46. # Calcula o input horizontal e vertical
  47. var horizontal_input = int(Input.get_action_strength("right")) - int(Input.get_action_strength("left"))
  48. var vertical_input = int(Input.get_action_strength("down")) - int(Input.get_action_strength("up"))
  49.  
  50. if Input.is_action_pressed("run"):
  51. is_running = true
  52. else:
  53. is_running = false
  54.  
  55. # Determina a direção do input priorizando o eixo com maior intensidade
  56. if abs(horizontal_input) > abs(vertical_input):
  57. input_direction = Vector2(horizontal_input, 0)
  58. elif abs(vertical_input) > abs(horizontal_input):
  59. input_direction = Vector2(0, vertical_input)
  60. else:
  61. # Se nenhum input for detectado, sai da função
  62. return
  63.  
  64.  
  65.  
  66. # Atualiza a posição inicial e verifica colisões usando o RayCast
  67. initial_position = position
  68. ray.target_position = input_direction * TILE_SIZE
  69. ray.force_raycast_update()
  70. if not ray.is_colliding():
  71. is_moving = true
  72. else:
  73. # Se houver uma colisão, reseta a direção do input
  74. input_direction = Vector2.ZERO
  75.  
  76. # Função para mover o personagem
  77. func move(delta):
  78. var speed = walk_speed
  79. if is_running:
  80. speed = run_speed
  81. else:
  82. speed = walk_speed
  83. # Incrementa o percentual de movimentação baseado na velocidade de caminhada
  84. percent_moved_to_next_tile += speed * delta
  85.  
  86. # Se o personagem se moveu 100% da distância para o próximo tile, atualiza a posição
  87. if percent_moved_to_next_tile >= 1.0:
  88. position = initial_position + (TILE_SIZE * input_direction)
  89. percent_moved_to_next_tile = 0.0
  90. is_moving = false
  91. input_direction = Vector2.ZERO # Reseta a direção do input após o movimento
  92. else:
  93. # Se ainda não se moveu completamente, atualiza a posição de forma proporcional
  94. position = initial_position + (TILE_SIZE * input_direction * percent_moved_to_next_tile)
  95.  
  96. # Função para atualizar a animação do personagem
  97. func update_animation(direction, is_moving):
  98. if is_running and is_moving:
  99. animation_state.travel("run")
  100. animation_tree.set("parameters/run/blend_position", (direction))
  101. elif is_moving:
  102. animation_state.travel("Walk")
  103. # Configura a posição de blend da animação de caminhada baseado na direção
  104. animation_tree.set("parameters/Walk/blend_position", (direction))
  105. else:
  106. animation_state.travel("idle")
  107. # Configura a posição de blend da animação de inatividade baseado na última direção
  108. animation_tree.set("parameters/idle/blend_position", direction)
  109.  
Advertisement
Add Comment
Please, Sign In to add comment