going/modules/going/checkpoint.cpp
2025-06-11 23:58:04 +02:00

55 lines
1.4 KiB
C++

#include "checkpoint.h"
#include "core/config/engine.h"
#include "going/player_body.h"
#include "macros.h"
void Checkpoint::_bind_methods() {
BIND_PROPERTY(Variant::VECTOR3, location);
BIND_PROPERTY(Variant::VECTOR3, camera_location);
}
void Checkpoint::set_location(Transform3D location) {
this->location = location;
}
Transform3D Checkpoint::get_location() const {
return this->location;
}
void Checkpoint::set_camera_location(Transform3D location) {
this->camera_location = location;
}
Transform3D Checkpoint::get_camera_location() const {
return this->camera_location;
}
void Checkpoint::load(PlayerBody *body) const {
body->set_global_transform(this->location);
body->get_camera()->set_global_transform(this->camera_location);
}
void Checkpoint::save(PlayerBody *body) {
this->set_location(body->get_global_transform_interpolated());
this->set_camera_location(body->get_camera()->get_global_transform_interpolated());
}
Ref<Checkpoint> Checkpoint::save_new(PlayerBody *body) {
Ref<Checkpoint> self{memnew(Checkpoint)};
self->save(body);
return self;
}
void CheckpointArea::_notification(int what) {
if(!Engine::get_singleton()->is_editor_hint() && what == NOTIFICATION_READY) {
this->connect(this->body_entered, callable_mp(this, &self_type::on_body_entered));
}
}
void CheckpointArea::on_body_entered(Node3D *body) {
if(PlayerBody *player{Object::cast_to<PlayerBody>(body)}) {
player->save_checkpoint();
}
}