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

Component responsible for controlling AI behavior. More...

#include <ai_controller.h>

Inheritance diagram for AIController:
[legend]

Public Member Functions

 AIController (GameObject &pathfinding_game_object)
 
void update (float dt) override
 
void chase (float dt)
 Handles chasing behavior towards the chase target.
 
void patrol (float dt)
 Handles patrolling behavior between patrol points.
 
void try_traverse_graph (Transform &source, Transform &target, float dt)
 Attempts to traverse the navigation graph from source to target.
 
AIControllerMode get_mode () const
 
AIControllerset_mode (AIControllerMode mode)
 
std::optional< std::reference_wrapper< GameObject > > get_chase_target () const
 
AIControllerset_chase_target (GameObject &target)
 
AIControllerclear_chase_target ()
 
std::optional< std::reference_wrapper< GameObject > > get_patrol_target () const
 
AIControllerset_patrol_target (GameObject &target)
 
AIControllerclear_patrol_target ()
 
float get_arrival_threshold () const
 
AIControllerset_arrival_threshold (float threshold)
 
float get_speed () const
 
AIControllerset_speed (float speed)
 
float get_width () const
 
AIControllerset_width (float width)
 
float get_height () const
 
AIControllerset_height (float height)
 
std::vector< std::reference_wrapper< GameObject > > & get_path ()
 
bool enable_graph_traversal () noexcept
 
bool disable_graph_traversal () noexcept
 
size_t add_on_patrol_complete_action (std::function< void(AIController &)> action)
 
void remove_on_patrol_complete_action (size_t index)
 
std::string type_name () const override
 Provides a consistent type name for the component.
 
- Public Member Functions inherited from Renderable
 Renderable ()
 
 Renderable (int layer)
 
 ~Renderable () override=default
 
Renderableorder_in_layer (int layer)
 
int order_in_layer () const
 
void set_render_strategy (Component &component)
 
virtual IRenderingStrategyrender_strategy () const
 
std::string type_name () const override
 Provides a consistent type name for the component.
 
Renderabledisable_draw () noexcept
 
Renderableenable_draw () noexcept
 
bool should_draw () const noexcept
 
- 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)
 

Additional Inherited Members

- Protected Attributes inherited from Renderable
std::unique_ptr< IRenderingStrategyrender_strategy_
 
int ordering_layer_ = 0
 
bool draw {true}
 

Detailed Description

Component responsible for controlling AI behavior.

This component utilizes the Pathfinding component to navigate the AI-controlled GameObject towards a specified target GameObject.

The AIController can operate in different modes, such as PATROL and CHASE, allowing for versatile AI behaviors.

There still is a possibility that the AI gets stuck on corners or obstacles depending on the navigation graph and collider setup. It has some basic stuck detection and will try to recalculate the path if it detects that it's not moving.

NOTE: for this to work you need to set up a proper navigation graph using the Pathfinding component and ensure that the AI GameObject has appropriate colliders (and Rigidbody2D if using physics-based movement).

Usage:

  • Create an AIController component and attach it to a GameObject.
  • Set the desired mode (PATROL or CHASE) and configure targets.
  • Adjust properties such as speed, arrival threshold, and dimensions.
  • The AIController will automatically handle movement and pathfinding during the update cycle.
    Note
    Ensure the GameObject has a Pathfinding component and appropriate

Constructor & Destructor Documentation

◆ AIController()

AIController::AIController ( GameObject pathfinding_game_object)

Member Function Documentation

◆ add_on_patrol_complete_action()

size_t AIController::add_on_patrol_complete_action ( std::function< void(AIController &)>  action)

◆ chase()

void AIController::chase ( float  dt)

Handles chasing behavior towards the chase target.

◆ clear_chase_target()

AIController & AIController::clear_chase_target ( )

◆ clear_patrol_target()

AIController & AIController::clear_patrol_target ( )

◆ disable_graph_traversal()

bool AIController::disable_graph_traversal ( )
noexcept

◆ enable_graph_traversal()

bool AIController::enable_graph_traversal ( )
noexcept

◆ get_arrival_threshold()

float AIController::get_arrival_threshold ( ) const

◆ get_chase_target()

std::optional< std::reference_wrapper< GameObject > > AIController::get_chase_target ( ) const

◆ get_height()

float AIController::get_height ( ) const

◆ get_mode()

AIControllerMode AIController::get_mode ( ) const

◆ get_path()

std::vector< std::reference_wrapper< GameObject > > & AIController::get_path ( )

◆ get_patrol_target()

std::optional< std::reference_wrapper< GameObject > > AIController::get_patrol_target ( ) const

◆ get_speed()

float AIController::get_speed ( ) const

◆ get_width()

float AIController::get_width ( ) const

◆ patrol()

void AIController::patrol ( float  dt)

Handles patrolling behavior between patrol points.

◆ remove_on_patrol_complete_action()

void AIController::remove_on_patrol_complete_action ( size_t  index)

◆ set_arrival_threshold()

AIController & AIController::set_arrival_threshold ( float  threshold)

◆ set_chase_target()

AIController & AIController::set_chase_target ( GameObject target)

◆ set_height()

AIController & AIController::set_height ( float  height)

◆ set_mode()

AIController & AIController::set_mode ( AIControllerMode  mode)

◆ set_patrol_target()

AIController & AIController::set_patrol_target ( GameObject target)

◆ set_speed()

AIController & AIController::set_speed ( float  speed)

◆ set_width()

AIController & AIController::set_width ( float  width)

◆ try_traverse_graph()

void AIController::try_traverse_graph ( Transform source,
Transform target,
float  dt 
)

Attempts to traverse the navigation graph from source to target.

Parameters
sourceThe Transform of the AI-controlled GameObject.
targetThe Transform of the target GameObject.
dtThe delta time since the last update.

◆ type_name()

std::string AIController::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 AIController::update ( float  dt)
overridevirtual

Implements Component.


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