Skip to content

Implement simple pathfinding for nuntius #14

Description

@mlgarchery

Messengers should deliver their order message to the target unit, and avoiding obstacles doing so. Messengers are of type Unit (they take dmg and are blocked by obstacles).

Here's is a little help from our chatGPT friend:

Show me how to do simple path finding in godot 3.5 in 2D

Certainly! I can provide you with a simple example of how to implement pathfinding in Godot 3.5 in a 2D game. In this example, we'll be using the A* (A-star) algorithm for pathfinding.

Step 1: Set up the scene

  1. Create a new scene in Godot and add a TileMap node to it. This will be the map where the pathfinding will occur.
  2. Add a TileSet to the TileMap node and create two tiles: one for the walkable area and another for obstacles or unwalkable areas.
  3. Design your map by painting the tiles on the TileMap. Ensure you have both walkable and unwalkable areas to create a pathfinding scenario.

Step 2: Implement the pathfinding logic

  1. Create a new script for your main character or any other node that requires pathfinding.
  2. In the script, add the following code:
extends Node2D

var path = []  # The array to store the calculated path

func _ready():
    var tilemap = $TileMap  # Replace "TileMap" with the actual name of your TileMap node
    var navigation = Navigation2D.new()
    add_child(navigation)
    navigation.navpoly_add_tilemap(tilemap)
    
func _input(event):
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
        var target = get_global_mouse_position()
        var start = position
        var navigation = $Navigation2D  # Replace "Navigation2D" with the actual name of your Navigation2D node
        path = navigation.get_simple_path(start, target)
        if path.size() > 0:
            path.pop_front()
            follow_path()

func follow_path():
    if path.size() > 0:
        var next_point = path.front()
        var direction = next_point - position
        direction = direction.normalized()
        position += direction * speed * delta
        if position.distance_to(next_point) < 1:
            path.pop_front()
            follow_path()

Let's go through the code step by step:

  • In the _ready() function, we create a new Navigation2D node and add it as a child of our main character node. We also call navpoly_add_tilemap() to initialize the navigation data based on the TileMap's tileset.
  • In the _input() function, we handle the left mouse button click event. When the button is pressed, we calculate a new path from the current position to the mouse's position using get_simple_path(). If a valid path is found, we remove the starting point from the path and call follow_path() to start moving along the path.
  • The follow_path() function moves the character along the path by continuously updating the position based on the direction to the next point in the path. If the character reaches a point close enough to the next point, we remove it from the path and recursively call follow_path() to continue moving.

Step 3: Test the pathfinding

  1. Attach the script to your main character node or the node that will perform the pathfinding.
  2. Run the scene and click somewhere on the map with the left mouse button.
  3. The character should start moving towards the clicked position, following the calculated path.

That's it! You now have a basic implementation of pathfinding using the A* algorithm in Godot 3.5 for a 2D game. Feel free to modify and expand upon this example to suit your specific game requirements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions