feat: more work on checkpoints

This commit is contained in:
Sara 2025-06-11 23:57:43 +02:00
parent 69b1f267ea
commit 15b890ad55
5 changed files with 21 additions and 22 deletions

View file

@ -5,8 +5,7 @@
void Checkpoint::_bind_methods() { void Checkpoint::_bind_methods() {
BIND_PROPERTY(Variant::VECTOR3, location); BIND_PROPERTY(Variant::VECTOR3, location);
BIND_PROPERTY(Variant::BOOL, can_jump); BIND_PROPERTY(Variant::VECTOR3, camera_location);
BIND_PROPERTY(Variant::BOOL, can_bash);
} }
void Checkpoint::set_location(Transform3D location) { void Checkpoint::set_location(Transform3D location) {
@ -17,32 +16,22 @@ Transform3D Checkpoint::get_location() const {
return this->location; return this->location;
} }
void Checkpoint::set_can_jump(bool can_jump) { void Checkpoint::set_camera_location(Transform3D location) {
this->can_jump = can_jump; this->camera_location = location;
} }
bool Checkpoint::get_can_jump() const { Transform3D Checkpoint::get_camera_location() const {
return this->can_jump; return this->camera_location;
}
void Checkpoint::set_can_bash(bool can_bash) {
this->can_bash = can_bash;
}
bool Checkpoint::get_can_bash() const {
return this->can_bash;
} }
void Checkpoint::load(PlayerBody *body) const { void Checkpoint::load(PlayerBody *body) const {
body->set_global_transform(this->location); body->set_global_transform(this->location);
body->set_can_jump(this->can_jump); body->get_camera()->set_global_transform(this->camera_location);
// body->set_can_bash(this->can_bash);
} }
void Checkpoint::save(PlayerBody *body) { void Checkpoint::save(PlayerBody *body) {
this->set_location(body->get_global_transform_interpolated()); this->set_location(body->get_global_transform_interpolated());
this->set_can_jump(body->get_can_jump()); this->set_camera_location(body->get_camera()->get_global_transform_interpolated());
// self->set_can_bash(body->get_can_bash());
} }
Ref<Checkpoint> Checkpoint::save_new(PlayerBody *body) { Ref<Checkpoint> Checkpoint::save_new(PlayerBody *body) {

View file

@ -13,16 +13,15 @@ class Checkpoint : public Resource {
public: public:
void set_location(Transform3D location); void set_location(Transform3D location);
Transform3D get_location() const; Transform3D get_location() const;
void set_can_jump(bool can_jump); void set_camera_location(Transform3D camera_location);
bool get_can_jump() const; Transform3D get_camera_location() const;
void set_can_bash(bool can_bash);
bool get_can_bash() const;
void load(PlayerBody *body) const; void load(PlayerBody *body) const;
void save(PlayerBody *body); void save(PlayerBody *body);
static Ref<Checkpoint> save_new(PlayerBody *body); static Ref<Checkpoint> save_new(PlayerBody *body);
private: private:
Transform3D location{}; Transform3D location{};
Transform3D camera_location{};
bool can_jump{false}, can_bash{false}; bool can_jump{false}, can_bash{false};
}; };

View file

@ -46,6 +46,9 @@ void PlayerBody::_notification(int what) {
case NOTIFICATION_ENTER_TREE: case NOTIFICATION_ENTER_TREE:
this->enter_tree(); this->enter_tree();
return; return;
case NOTIFICATION_READY:
this->ready();
return;
case NOTIFICATION_PROCESS: case NOTIFICATION_PROCESS:
this->process(this->get_process_delta_time()); this->process(this->get_process_delta_time());
return; return;
@ -61,6 +64,10 @@ void PlayerBody::enter_tree() {
this->model = Object::cast_to<Node3D>(this->get_node(NodePath("character"))); this->model = Object::cast_to<Node3D>(this->get_node(NodePath("character")));
this->anim = Object::cast_to<AnimationPlayer>(this->get_node(NodePath("character/AnimationPlayer"))); this->anim = Object::cast_to<AnimationPlayer>(this->get_node(NodePath("character/AnimationPlayer")));
this->camera = Object::cast_to<Camera3D>(this->get_node(NodePath("Camera3D"))); this->camera = Object::cast_to<Camera3D>(this->get_node(NodePath("Camera3D")));
this->state = Object::cast_to<PlayerStateMachine>(this->get_node(NodePath("%PlayerStateMachine")));
}
void PlayerBody::ready() {
this->camera->set_fov(this->min_fov); this->camera->set_fov(this->min_fov);
this->last_checkpoint = Checkpoint::save_new(this); this->last_checkpoint = Checkpoint::save_new(this);
} }
@ -85,6 +92,8 @@ void PlayerBody::save_checkpoint() {
void PlayerBody::load_checkpoint() { void PlayerBody::load_checkpoint() {
this->last_checkpoint->load(this); this->last_checkpoint->load(this);
this->force_update_transform();
this->set_velocity(Vector3{});
this->state->force_state<FallingState>(); this->state->force_state<FallingState>();
} }

View file

@ -12,6 +12,7 @@ class PlayerBody : public CharacterBody3D {
static void _bind_methods(); static void _bind_methods();
void _notification(int what); void _notification(int what);
void enter_tree(); void enter_tree();
void ready();
void process(double delta); void process(double delta);
void physics_process(double delta); void physics_process(double delta);

View file

@ -36,6 +36,7 @@ target_speed = 25.0
jump_impulse = Vector2(0.9, 5) jump_impulse = Vector2(0.9, 5)
[node name="PlayerStateMachine" type="PlayerStateMachine" parent="."] [node name="PlayerStateMachine" type="PlayerStateMachine" parent="."]
unique_name_in_owner = true
[node name="CollisionShape3D" type="CollisionShape3D" parent="."] [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.05124, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.05124, 0)