feat: created CarPlayer class as a client to CarPhysics

This commit is contained in:
Sara 2024-05-16 08:44:37 +02:00
parent ea2e6fc13c
commit 069c1f988a
2 changed files with 65 additions and 0 deletions

35
src/car_player.cpp Normal file
View file

@ -0,0 +1,35 @@
#include "car_player.hpp"
#include "utils/player_input.hpp"
#include <godot_cpp/variant/transform3d.hpp>
#include <godot_cpp/classes/physics_direct_body_state3d.hpp>
namespace godot {
void CarPlayer::_bind_methods() {
#define CLASSNAME CarPlayer
}
void CarPlayer::setup_player_input(PlayerInput *input) {
input->listen_to(PlayerInput::Listener("steer_right", "steer_left", callable_mp(this, &CarPlayer::on_steer)));
input->listen_to(PlayerInput::Listener("accelerate", callable_mp(this, &CarPlayer::on_accelerate)));
input->listen_to(PlayerInput::Listener("brake", callable_mp(this, &CarPlayer::on_brake)));
}
void CarPlayer::spawn_at_position(Transform3D const &transform) {
this->set_transform(transform);
this->set_scale({1,1,1});
}
void CarPlayer::on_steer(Ref<InputEvent> event, float value) {
this->set_current_steering(value * this->steering_factor);
}
void CarPlayer::on_brake(Ref<InputEvent> input, float value) {
this->set_brake(value != 0);
}
void CarPlayer::on_accelerate(Ref<InputEvent> input, float value) {
this->set_target_speed(max_speed * value);
}
Node *CarPlayer::to_node() { return this; }
}

30
src/car_player.hpp Normal file
View file

@ -0,0 +1,30 @@
#ifndef CAR_PLAYER_HPP
#define CAR_PLAYER_HPP
#include "car_physics.hpp"
#include "utils/player.hpp"
#include <godot_cpp/classes/input_event.hpp>
#include <godot_cpp/classes/rigid_body3d.hpp>
#include <godot_cpp/classes/collision_shape3d.hpp>
namespace godot {
class CarPlayer : public CarPhysics,
public IPlayer {
GDCLASS(CarPlayer, CarPhysics);
static void _bind_methods();
public:
virtual void setup_player_input(PlayerInput *input) override;
virtual void spawn_at_position(Transform3D const &transform) override;
virtual Node *to_node() override;
void on_steer(Ref<InputEvent> event, float value);
void on_brake(Ref<InputEvent> event, float value);
void on_accelerate(Ref<InputEvent> event, float value);
private:
const float max_speed{34.f};
const float max_speed_reverse{10.f};
const float steering_factor{1.f};
};
};
#endif // !CAR_PLAYER_HPP