Better Implementation of Timer

This commit is contained in:
Morph01
2024-08-16 10:36:32 +02:00
parent 91d492c2d8
commit 1662691b6e
2 changed files with 7 additions and 6 deletions

View File

@@ -1,5 +1,4 @@
#include "Bullet.h"
#include <godot_cpp/classes/timer.hpp>
using namespace godot;
@@ -22,20 +21,20 @@ void Bullet::_ready() {
DEV_ASSERT(m_Ray);
m_Mesh = Object::cast_to<MeshInstance3D>(find_child("MeshInstance3D"));
DEV_ASSERT(m_Mesh);
m_Timer = memnew(godot::Timer);
add_child(m_Timer);
m_Timer->connect("timeout", callable_mp(this, &Bullet::_on_timer_timeout));
}
void Bullet::_physics_process(float a_Delta) {
Vector3 movement = Vector3(0, 0, -BULLET_SPEED) * a_Delta;
set_position(get_transform().xform(movement));
godot::Timer* timer = memnew(godot::Timer);
add_child(timer);
timer->connect("timeout", callable_mp(this, &Bullet::_on_timer_timeout));
if (m_Ray->is_colliding()) {
m_Mesh->set_visible(false);
m_Particles->set_emitting(true);
timer->set_wait_time(1.0);
timer->start();
m_Timer->set_wait_time(1.0);
m_Timer->start();
}
}

View File

@@ -4,6 +4,7 @@
#include <godot_cpp/classes/mesh_instance3d.hpp>
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/ray_cast3d.hpp>
#include <godot_cpp/classes/timer.hpp>
namespace blitz {
class Bullet : public godot::Node3D {
@@ -22,6 +23,7 @@ class Bullet : public godot::Node3D {
godot::GPUParticles3D* m_Particles;
godot::RayCast3D* m_Ray;
godot::MeshInstance3D* m_Mesh;
godot::Timer* m_Timer;
void _on_timer_timeout();
};