Bullets Colliding and Disappearing

This commit is contained in:
Morph01
2024-08-15 13:28:16 +02:00
parent b92c5169c4
commit 91d492c2d8
3 changed files with 69 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
#include "Bullet.h"
#include <godot_cpp/classes/timer.hpp>
using namespace godot;
@@ -6,17 +7,40 @@ namespace blitz {
static constexpr float BULLET_SPEED = 40.0f;
void Bullet::_bind_methods() {}
void Bullet::_bind_methods() {
ClassDB::bind_method(D_METHOD("_on_timer_timeout"), &Bullet::_on_timer_timeout);
}
Bullet::Bullet() {}
Bullet::~Bullet() {}
void Bullet::_ready() {}
void Bullet::_ready() {
m_Particles = Object::cast_to<GPUParticles3D>(find_child("GPUParticles3D"));
DEV_ASSERT(m_Particles);
m_Ray = Object::cast_to<RayCast3D>(find_child("RayCast3D"));
DEV_ASSERT(m_Ray);
m_Mesh = Object::cast_to<MeshInstance3D>(find_child("MeshInstance3D"));
DEV_ASSERT(m_Mesh);
}
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();
}
}
void Bullet::_on_timer_timeout() {
queue_free();
}
} // namespace blitz

View File

@@ -1,6 +1,9 @@
#pragma once
#include <godot_cpp/classes/gpu_particles3d.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp>
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/ray_cast3d.hpp>
namespace blitz {
class Bullet : public godot::Node3D {
@@ -14,6 +17,13 @@ class Bullet : public godot::Node3D {
void _ready();
void _physics_process(float delta);
private:
godot::GPUParticles3D* m_Particles;
godot::RayCast3D* m_Ray;
godot::MeshInstance3D* m_Mesh;
void _on_timer_timeout();
};
} // namespace blitz