Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Script para movimentação de player point in click baseado em um tilemap na Godot 4 :
- referencia : https://www.youtube.com/watch?v=DkAmGxRuCk4&t=906s&ab_channel=Retrobright
- extends Area2D
- @onready var tile_map = get_node("../TileMapLayer")
- @onready var camera = get_node("../Camera2D")
- var astar_grid: AStarGrid2D
- var current_id_path: Array[Vector2i] = []
- var touch_position = Vector2.ZERO
- func _ready() -> void:
- organizar()
- func _input(event: InputEvent) -> void:
- if Global.player_morto == false:
- if event is InputEventScreenTouch or event is InputEventMouseButton:
- touch_position = (event.position / camera.zoom) + camera.position
- else: # Ignora outros eventos
- return
- var id_path = astar_grid.get_id_path(
- tile_map.local_to_map(global_position),
- tile_map.local_to_map(touch_position)
- ).slice(1)
- if not id_path.is_empty():
- current_id_path = id_path
- #print(id_path)
- func _physics_process(delta: float) -> void:
- if Global.player_morto == false:
- if current_id_path.is_empty():
- return
- var target_position = tile_map.map_to_local(current_id_path.front())
- global_position = global_position.move_toward(target_position, 10)
- if global_position == target_position:
- current_id_path.pop_front()
- func organizar():
- astar_grid = AStarGrid2D.new()
- astar_grid.region = tile_map.get_used_rect()
- astar_grid.cell_size = Vector2(120, 120) #TAMANHO DAS CELULAS DA GRID
- astar_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
- astar_grid.update()
- for x in tile_map.get_used_rect().size.x:
- for y in tile_map.get_used_rect().size.y:
- var tile_position = Vector2i(
- x + tile_map.get_used_rect().position.x,
- y + tile_map.get_used_rect().position.y
- )
- var tile_data = tile_map.get_cell_tile_data(tile_position)
- if tile_data == null or tile_data.get_custom_data("wallkable") == false:
- astar_grid.set_point_solid(tile_position)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement