From 91d492c2d8290b303ace46a4716e0dfdef347de0 Mon Sep 17 00:00:00 2001 From: Morph01 <145839520+Morph01@users.noreply.github.com> Date: Thu, 15 Aug 2024 13:28:16 +0200 Subject: [PATCH] Bullets Colliding and Disappearing --- godot/Scenes/Weapons/bullet.tscn | 34 +++++++++++++++++++++++++++++++- src/Bullet.cpp | 28 ++++++++++++++++++++++++-- src/Bullet.h | 10 ++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/godot/Scenes/Weapons/bullet.tscn b/godot/Scenes/Weapons/bullet.tscn index e37839b..25f1faf 100644 --- a/godot/Scenes/Weapons/bullet.tscn +++ b/godot/Scenes/Weapons/bullet.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=3 format=3 uid="uid://do601jl7p1u22"] +[gd_scene load_steps=6 format=3 uid="uid://do601jl7p1u22"] [sub_resource type="BoxMesh" id="BoxMesh_ibwn0"] size = Vector3(0.05, 0.05, 1) @@ -9,7 +9,23 @@ emission_enabled = true emission = Color(0.568627, 1, 0.313726, 1) emission_energy_multiplier = 5.0 +[sub_resource type="ParticleProcessMaterial" id="ParticleProcessMaterial_618v8"] +direction = Vector3(0, 0, 1) +initial_velocity_min = 3.0 +initial_velocity_max = 5.0 + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_gn2fy"] +albedo_color = Color(1, 1, 0.443137, 1) +emission_enabled = true +emission = Color(0.568627, 1, 0.313726, 1) +emission_energy_multiplier = 8.0 + +[sub_resource type="BoxMesh" id="BoxMesh_5un5e"] +material = SubResource("StandardMaterial3D_gn2fy") +size = Vector3(0.04, 0.04, 0.04) + [node name="Bullet" type="Bullet"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -52730.2) [node name="MeshInstance3D" type="MeshInstance3D" parent="."] mesh = SubResource("BoxMesh_ibwn0") @@ -17,3 +33,19 @@ surface_material_override/0 = SubResource("StandardMaterial3D_2qdhl") [node name="RayCast3D" type="RayCast3D" parent="."] target_position = Vector3(0, 0, -0.6) + +[node name="GPUParticles3D" type="GPUParticles3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.7) +emitting = false +one_shot = true +explosiveness = 1.0 +visibility_aabb = AABB(-1.00001, -6.06334, -1.00001, 2.00002, 7.06267, 41.4102) +process_material = SubResource("ParticleProcessMaterial_618v8") +draw_pass_1 = SubResource("BoxMesh_5un5e") + +[node name="Timer" type="Timer" parent="."] +wait_time = 10.0 +one_shot = true +autostart = true + +[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"] diff --git a/src/Bullet.cpp b/src/Bullet.cpp index 51a3cf8..f43823b 100644 --- a/src/Bullet.cpp +++ b/src/Bullet.cpp @@ -1,4 +1,5 @@ #include "Bullet.h" +#include 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(find_child("GPUParticles3D")); + DEV_ASSERT(m_Particles); + m_Ray = Object::cast_to(find_child("RayCast3D")); + DEV_ASSERT(m_Ray); + m_Mesh = Object::cast_to(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 \ No newline at end of file diff --git a/src/Bullet.h b/src/Bullet.h index 75fcecd..063a308 100644 --- a/src/Bullet.h +++ b/src/Bullet.h @@ -1,6 +1,9 @@ #pragma once +#include +#include #include +#include 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 \ No newline at end of file