Adding pathing

This commit is contained in:
Dan 2024-06-01 19:29:26 +01:00
parent a91635a68c
commit 9f9f1a7502
5 changed files with 187 additions and 7 deletions

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"godotTools.editorPath.godot4": "c:\\Dev\\Godot\\Godot_v4.2.1-stable_win64.exe"
}

View file

@ -1,11 +1,33 @@
extends Node2D extends Node2D
class_name Glowling class_name Glowling
# Called when the node enters the scene tree for the first time. @onready var tilemap = $"../TileMap" # This needs rectifying
func _ready(): var current_path : Array[Vector2i] = []
pass # Replace with function body.
var speed : int = 10
func _ready():
$GlowlingAnimations.play("GlowingPulse")
pass
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta): func _process(_delta):
if current_path.is_empty():
return
var target_position = tilemap.map_to_local(current_path.front())
global_position = global_position.move_toward(target_position, speed)
if global_position == target_position:
current_path.pop_front()
pass pass
## This is the temporary way for having something move
func _unhandled_input(event):
var click_position = get_global_mouse_position()
if event.is_action_pressed("move_to"):
if tilemap.is_point_walkable(click_position):
current_path = tilemap.astar.get_id_path(
tilemap.local_to_map(global_position),
tilemap.local_to_map(click_position)
).slice(1)

45
levels/scripts/map.gd Normal file
View file

@ -0,0 +1,45 @@
extends TileMap
var astar : AStarGrid2D = AStarGrid2D.new()
@onready var tilemap_size : Vector2i = get_used_rect().end - get_used_rect().position
@onready var map_rect : Rect2i = Rect2i(Vector2i.ZERO, tilemap_size)
func _ready() -> void:
setup_pathing()
pass
func setup_pathing() -> void:
# Setup the AStarGrid2D for pathfinding
var tile_size : Vector2i = get_tileset().tile_size
Log.pr("Tilemap size: " + str(tilemap_size))
astar.region = map_rect
astar.cell_size = tile_size
astar.default_compute_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
astar.default_estimate_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
astar.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
astar.update()
update_passable()
pass
func update_passable() -> void:
# Update the AStarGrid2D with the new passable tiles
for i in tilemap_size.x:
for j in tilemap_size.y:
var coords : Vector2i = Vector2i(i, j)
var tile_data = get_cell_tile_data(0, coords)
if tile_data and tile_data.get_custom_data('navtype') == 'wall':
astar.set_point_solid(coords)
pass
func is_point_walkable(check_position) -> bool:
var map_position = local_to_map(check_position)
if map_rect.has_point(map_position) and not astar.is_point_solid(map_position):
return true
return false

File diff suppressed because one or more lines are too long

View file

@ -24,3 +24,11 @@ window/stretch/scale_mode="integer"
[editor_plugins] [editor_plugins]
enabled=PackedStringArray("res://addons/log/plugin.cfg") enabled=PackedStringArray("res://addons/log/plugin.cfg")
[input]
move_to={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(253, 34),"global_position":Vector2(259, 96),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}