Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
ai_controller.h
Go to the documentation of this file.
1#pragma once
2
8
17enum AIControllerMode : uint8_t {
18 PATROL = 0,
19 CHASE = 1,
20};
21
48class AIController : public Renderable {
49 public:
50 AIController(GameObject& pathfinding_game_object);
51
52 void update(float dt) override;
53
57 void chase(float dt);
58
62 void patrol(float dt);
63
71 void try_traverse_graph(Transform& source, Transform& target, float dt);
72
75
76 std::optional<std::reference_wrapper<GameObject>> get_chase_target() const;
79
80 std::optional<std::reference_wrapper<GameObject>> get_patrol_target() const;
83
84 float get_arrival_threshold() const;
86
87 float get_speed() const;
88 AIController& set_speed(float speed);
89
90 float get_width() const;
91 AIController& set_width(float width);
92
93 float get_height() const;
94 AIController& set_height(float height);
95
96 std::vector<std::reference_wrapper<GameObject>>& get_path();
97
98 bool enable_graph_traversal() noexcept;
99 bool disable_graph_traversal() noexcept;
100
102 std::function<void(AIController&)> action);
104
105 std::string type_name() const override;
106
107 private:
108 std::vector<std::reference_wrapper<GameObject>> path_;
109 std::optional<std::reference_wrapper<GameObject>> pathfinding_game_object_;
110 std::optional<std::reference_wrapper<GameObject>> chase_target_;
111 std::optional<std::reference_wrapper<GameObject>> patrol_target_;
112
113 std::optional<std::reference_wrapper<Rigidbody2D>> rigidbody_{std::nullopt};
114
116 bool use_graph_traversal_{true};
117
118 Transform initial_transform;
119 bool returning_to_start_{false};
120
121 Vector3 last_position_{0, 0, 0};
122 float stuck_timer_{0.0f};
123 float stuck_threshold_{1.0f};
124
125 float node_distance_threshold_{2.0f};
126 float arrival_threshold_{24.0f};
127
132 float speed_{10.0f};
133
136 float width_{10.0f};
137 float height_{10.0f};
138
139 std::reference_wrapper<Pathfinding> get_pathfinding_component();
140
141 std::vector<std::function<void(AIController&)>> on_patrol_complete_actions_;
142};
AIControllerMode
Enumeration for AI Controller modes.
Definition ai_controller.h:17
@ PATROL
Definition ai_controller.h:18
@ CHASE
Definition ai_controller.h:19
Component responsible for controlling AI behavior.
Definition ai_controller.h:48
float get_width() const
AIController & clear_chase_target()
AIController & set_mode(AIControllerMode mode)
void chase(float dt)
Handles chasing behavior towards the chase target.
size_t add_on_patrol_complete_action(std::function< void(AIController &)> action)
AIController & clear_patrol_target()
void remove_on_patrol_complete_action(size_t index)
AIControllerMode get_mode() const
void try_traverse_graph(Transform &source, Transform &target, float dt)
Attempts to traverse the navigation graph from source to target.
AIController & set_arrival_threshold(float threshold)
float get_height() const
std::optional< std::reference_wrapper< GameObject > > get_chase_target() const
bool disable_graph_traversal() noexcept
std::optional< std::reference_wrapper< GameObject > > get_patrol_target() const
std::vector< std::reference_wrapper< GameObject > > & get_path()
AIController & set_height(float height)
float get_speed() const
AIController & set_width(float width)
std::string type_name() const override
Provides a consistent type name for the component.
void patrol(float dt)
Handles patrolling behavior between patrol points.
float get_arrival_threshold() const
AIController(GameObject &pathfinding_game_object)
AIController & set_patrol_target(GameObject &target)
AIController & set_chase_target(GameObject &target)
void update(float dt) override
bool enable_graph_traversal() noexcept
AIController & set_speed(float speed)
Concept to constrain types to be derived from Component.
Definition gameObject.h:33
Base class for renderable components in the engine.
Definition renderable.h:16
A 2D rigidbody component that adds physics properties to a GameObject.
Definition rigidbody_2d.h:23
Represents the position, rotation, and scale of a GameObject in 3D space. The Transform class encapsu...
Definition transform.h:22
Represents a 3D vector with x, y, and z components. This class provides basic vector operations such ...
Definition vector3.h:8