#ifndef ENEMY_CAR_HPP
#define ENEMY_CAR_HPP

#include "car_physics.hpp"
#include "car_player.hpp"
#include "godot_cpp/classes/navigation_agent3d.hpp"

namespace godot {
//! The enemy car, inherits CarPhysics subclass sandbox and uses a NavigationAgent3D to direct it.
class EnemyCar : public CarPhysics {
    GDCLASS(EnemyCar, CarPhysics);
    //! Register damage function
    static void _bind_methods();
public:
    //! Call parent _enter_tree and fetch object references for later use.
    virtual void _enter_tree() override;
    //! Update current navigation and acceleration. Recalculate path every recalc_frame_interval frames. Update CarPhysics sandbox functions to drive towards next path position.
    virtual void _process(double delta_time) override;
    //! Destroy this object
    void damage();
    //! Called from RallyRushGameMode shield pickup observer. Start fleeing from the player.
    void start_fleeing();
    //! Called from RallyRushGameMode shield pickup end observer. Stop fleeing from the player.
    void stop_fleeing();
protected:
    //! Calculate a new path to the player.
    void recalculate_navigation();
private:
    //! Frame timer to next recalculate_navigation call.
    int next_recalc{0};
    //! Interval in frames between calls to recalculate_navigation.
    int recalc_frame_interval{60};
    //! Desired speed when approaching a corner.
    float turn_target_speed{3.f};
    //! Max speed when driving straight ahead.
    float max_speed{45.f};
    //! Distance from corners at which to start braking.
    float brake_distance{7.f};
    //! Speed at which to steer.
    float steering_speed{1.5f};
    //! If true, invert goal direction and drive away from the player.
    bool flee{false};
    //! Navigation agent child node.
    NavigationAgent3D *agent{nullptr};
    //! Reference to the player car.
    CarPlayer *player{nullptr};
};
}

#endif // !ENEMY_CAR_HPP
