Advertisement
Zunesha

Script para movimentação de player point in click baseado em um tilemap na Godot 4 :

Oct 8th, 2024 (edited)
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Script para movimentação de player point in click baseado em um tilemap na Godot 4 :
  2.  
  3. referencia : https://www.youtube.com/watch?v=DkAmGxRuCk4&t=906s&ab_channel=Retrobright
  4.  
  5. extends Area2D
  6.  
  7. @onready var tile_map = get_node("../TileMapLayer")
  8. @onready var camera = get_node("../Camera2D")
  9.  
  10.  
  11. var astar_grid: AStarGrid2D
  12. var current_id_path: Array[Vector2i] = []
  13.  
  14. var touch_position = Vector2.ZERO
  15.  
  16.  
  17. func _ready() -> void:
  18.     organizar()
  19.  
  20. func _input(event: InputEvent) -> void:
  21.  
  22.     if Global.player_morto == false:
  23.  
  24.         if event is InputEventScreenTouch or event is InputEventMouseButton:
  25.  
  26.             touch_position = (event.position / camera.zoom) + camera.position
  27.  
  28.         else:  # Ignora outros eventos
  29.             return
  30.    
  31.  
  32.    
  33.     var id_path = astar_grid.get_id_path(
  34.         tile_map.local_to_map(global_position),
  35.         tile_map.local_to_map(touch_position)
  36.     ).slice(1)
  37.    
  38.     if not id_path.is_empty():
  39.         current_id_path = id_path
  40.         #print(id_path)
  41.    
  42.    
  43. func _physics_process(delta: float) -> void:
  44.     if Global.player_morto == false:
  45.         if current_id_path.is_empty():
  46.             return
  47.        
  48.         var target_position = tile_map.map_to_local(current_id_path.front())
  49.         global_position = global_position.move_toward(target_position, 10)
  50.        
  51.         if global_position == target_position:
  52.             current_id_path.pop_front()
  53.            
  54.  
  55.  
  56. func organizar():
  57.     astar_grid = AStarGrid2D.new()
  58.     astar_grid.region = tile_map.get_used_rect()
  59.     astar_grid.cell_size = Vector2(120, 120)                          #TAMANHO DAS CELULAS DA GRID
  60.     astar_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
  61.     astar_grid.update()
  62.    
  63.     for x in tile_map.get_used_rect().size.x:
  64.         for y in tile_map.get_used_rect().size.y:
  65.             var tile_position = Vector2i(
  66.                 x + tile_map.get_used_rect().position.x,
  67.                 y + tile_map.get_used_rect().position.y
  68.             )
  69.            
  70.             var tile_data = tile_map.get_cell_tile_data(tile_position)
  71.            
  72.             if  tile_data == null or  tile_data.get_custom_data("wallkable") == false:
  73.                 astar_grid.set_point_solid(tile_position)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement