Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
NavigationGraph Class Reference

Component representing a navigation graph for AI pathfinding. More...

#include <navigation_graph.h>

Inheritance diagram for NavigationGraph:
[legend]

Public Member Functions

 NavigationGraph (int grid_size=16, int stride=10)
 
void update (float dt) override
 Update is left empty as the navigation graph does not require per-frame updates.
 
NavigationGraphgenerate_graph ()
 Generates the navigation graph by populating it with nodes.
 
NavigationGraphclear ()
 Clears all nodes from the navigation graph.
 
std::unordered_map< GraphPosition, std::reference_wrapper< NavigationNode >, GraphPositionHash > & get_nodes ()
 
std::optional< std::reference_wrapper< NavigationNode > > get_node (const GraphPosition &position) const
 Retrieves a navigation node at the specified position.
 
std::optional< GraphPositionget_position_of_node (const NavigationNode &node) const
 
GraphPosition world_to_graph_position (const Vector3 &world_position) const
 
NavigationGraphadd_node (const GraphPosition &position, NavigationNode &node)
 Adds a navigation node at the specified position.
 
NavigationGraphremove_node (const GraphPosition &position)
 Removes a navigation node at the specified position.
 
std::optional< std::reference_wrapper< NavigationNode > > get_closest_node (const GraphPosition &position) const
 Finds the closest navigation node to the specified position.
 
std::optional< std::reference_wrapper< NavigationNode > > get_closest_node (const Vector3 &world_position) const
 
int get_grid_size () const noexcept
 
int get_grid_max_x () const noexcept
 
int get_grid_max_y () const noexcept
 
int get_stride () const noexcept
 
NavigationGraphstride (int stride) noexcept
 
int max_drop_distance () const noexcept
 
NavigationGraphmax_drop_distance (int distance) noexcept
 
int max_jump_distance () const noexcept
 
NavigationGraphmax_jump_distance (int distance) noexcept
 
std::string type_name () const override
 Provides a consistent type name for the component.
 
- Public Member Functions inherited from Component
 Component ()
 
virtual ~Component ()=default
 
bool active () const noexcept
 Checks if the component is active.
 
Componentactive (bool value) noexcept
 
bool marked_for_deletion () const noexcept
 Activates the component.
 
Componentmark_for_deletion () noexcept
 
virtual void on_attach ()
 Called when the component is attached to a GameObject.
 
virtual void on_detach ()
 Called when the component is detached from a GameObject.
 
virtual void on_serialize (std::vector< uint8_t > &) const
 Serializes the component's state into a byte array.
 
virtual void on_deserialize (const std::vector< uint8_t > &, size_t &)
 Deserializes the component's state from a byte array.
 
const std::optional< std::reference_wrapper< GameObject > > & parent () const noexcept
 Gets the parent GameObject of this component.
 
std::optional< std::reference_wrapper< GameObject > > & parent () noexcept
 Gets the parent GameObject of this component.
 
Componentparent (GameObject &parent)
 
Componentparent (std::nullopt_t nullopt)
 
size_t add_on_attach (const std::function< void(Component &)> &action)
 Retrieves the type information of the component.
 
void remove_on_attach (size_t index)
 
size_t add_on_detach (const std::function< void(Component &)> &action)
 Adds an action to be performed when the component is attached.
 
void remove_on_detach (size_t index)
 

Detailed Description

Component representing a navigation graph for AI pathfinding.

This component manages a collection of navigation nodes, allowing for the generation, clearing, and querying of the navigation graph.

NOTE: This component should be placed on a GameObject that serves as the parent for all the tiles. NOTHING ELSE should be a child of this GameObject except the tiles, which automatically get assigned navigation nodes as children when generating the graph.

Usage:

  • Create a NavigationGraph component and attach it to a GameObject.
  • Use generate_graph() to populate the graph with nodes based on child GameObjects.
  • Use clear() to remove all nodes from the graph.
  • Query nodes and positions using provided methods.
    Note
    Ensure that child GameObjects are properly set up to represent

Constructor & Destructor Documentation

◆ NavigationGraph()

NavigationGraph::NavigationGraph ( int  grid_size = 16,
int  stride = 10 
)

Member Function Documentation

◆ add_node()

NavigationGraph & NavigationGraph::add_node ( const GraphPosition position,
NavigationNode node 
)

Adds a navigation node at the specified position.

Parameters
positionThe position to add the navigation node.
nodeThe NavigationNode to add.
Returns
Reference to the NavigationGraph instance for method chaining.

◆ clear()

NavigationGraph & NavigationGraph::clear ( )

Clears all nodes from the navigation graph.

Returns
Reference to the NavigationGraph instance for method chaining.

◆ generate_graph()

NavigationGraph & NavigationGraph::generate_graph ( )

Generates the navigation graph by populating it with nodes.

Generates nodes based on the children of the parent GameObject and tries to connect them based on the specified node distance.

Returns
Reference to the NavigationGraph instance for method chaining.

◆ get_closest_node() [1/2]

std::optional< std::reference_wrapper< NavigationNode > > NavigationGraph::get_closest_node ( const GraphPosition position) const

Finds the closest navigation node to the specified position.

Parameters
positionThe position to find the closest navigation node to.
Returns
A reference to the closest NavigationNode.

◆ get_closest_node() [2/2]

std::optional< std::reference_wrapper< NavigationNode > > NavigationGraph::get_closest_node ( const Vector3 world_position) const

◆ get_grid_max_x()

int NavigationGraph::get_grid_max_x ( ) const
noexcept

◆ get_grid_max_y()

int NavigationGraph::get_grid_max_y ( ) const
noexcept

◆ get_grid_size()

int NavigationGraph::get_grid_size ( ) const
noexcept

◆ get_node()

std::optional< std::reference_wrapper< NavigationNode > > NavigationGraph::get_node ( const GraphPosition position) const

Retrieves a navigation node at the specified position.

Parameters
positionThe position to query for a navigation node.
Returns
An optional reference to the NavigationNode if found, std::nullopt otherwise.

◆ get_nodes()

std::unordered_map< GraphPosition, std::reference_wrapper< NavigationNode >, GraphPositionHash > & NavigationGraph::get_nodes ( )

◆ get_position_of_node()

std::optional< GraphPosition > NavigationGraph::get_position_of_node ( const NavigationNode node) const

◆ get_stride()

int NavigationGraph::get_stride ( ) const
noexcept

◆ max_drop_distance() [1/2]

int NavigationGraph::max_drop_distance ( ) const
noexcept

◆ max_drop_distance() [2/2]

NavigationGraph & NavigationGraph::max_drop_distance ( int  distance)
noexcept

◆ max_jump_distance() [1/2]

int NavigationGraph::max_jump_distance ( ) const
noexcept

◆ max_jump_distance() [2/2]

NavigationGraph & NavigationGraph::max_jump_distance ( int  distance)
noexcept

◆ remove_node()

NavigationGraph & NavigationGraph::remove_node ( const GraphPosition position)

Removes a navigation node at the specified position.

Parameters
positionThe position of the navigation node to remove.
Returns
Reference to the NavigationGraph instance for method chaining.

◆ stride()

NavigationGraph & NavigationGraph::stride ( int  stride)
noexcept

◆ type_name()

std::string NavigationGraph::type_name ( ) const
overridevirtual

Provides a consistent type name for the component.

Returns
A string representing the type name of the component. @CAUTION: All derived components must implement this method to ensure consistent serialization across different compilers and platforms.

Implements Component.

◆ update()

void NavigationGraph::update ( float  dt)
inlineoverridevirtual

Update is left empty as the navigation graph does not require per-frame updates.

Implements Component.

◆ world_to_graph_position()

GraphPosition NavigationGraph::world_to_graph_position ( const Vector3 world_position) const

The documentation for this class was generated from the following file: