From 9df52e00165eeaca0818d22e0fa597b5708e1068 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sun, 25 Aug 2024 12:03:52 +0200 Subject: [PATCH] stick pens on walls --- godot/Scenes/Characters/remy.tscn | 6 ++-- godot/Scenes/Levels/world.tscn | 2 ++ godot/Scenes/Weapons/pen.tscn | 7 ++-- godot/project.godot | 7 ++++ include/client/Bullet.h | 24 +++++++++++++ src/client/Bullet.cpp | 56 +++++++++++++++++++++++++++++++ src/client/register_types.cpp | 2 ++ 7 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 include/client/Bullet.h create mode 100644 src/client/Bullet.cpp diff --git a/godot/Scenes/Characters/remy.tscn b/godot/Scenes/Characters/remy.tscn index 639c0dc..c382416 100644 --- a/godot/Scenes/Characters/remy.tscn +++ b/godot/Scenes/Characters/remy.tscn @@ -222,8 +222,8 @@ albedo_color = Color(0, 0.4, 1, 1) [sub_resource type="CylinderMesh" id="CylinderMesh_spot0"] material = SubResource("StandardMaterial3D_8rx3k") -top_radius = 0.05 -bottom_radius = 0.05 +top_radius = 0.03 +bottom_radius = 0.03 height = 0.2 [sub_resource type="AnimationNodeAnimation" id="AnimationNodeAnimation_jv0gp"] @@ -315,6 +315,8 @@ transitions = ["Start", "Movement", SubResource("AnimationNodeStateMachineTransi graph_offset = Vector2(18, 52) [node name="Player" type="CharacterBody3D"] +collision_layer = 2 +collision_mask = 3 [node name="Head" type="Node3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.55647, 0) diff --git a/godot/Scenes/Levels/world.tscn b/godot/Scenes/Levels/world.tscn index 98553bc..ec3b959 100644 --- a/godot/Scenes/Levels/world.tscn +++ b/godot/Scenes/Levels/world.tscn @@ -87,3 +87,5 @@ surface_material_override/0 = SubResource("StandardMaterial3D_pfpgv") shape = SubResource("ConcavePolygonShape3D_rit6o") [node name="Players" type="Node" parent="."] + +[node name="Entities" type="Node" parent="."] diff --git a/godot/Scenes/Weapons/pen.tscn b/godot/Scenes/Weapons/pen.tscn index 96b547c..c35bfbb 100644 --- a/godot/Scenes/Weapons/pen.tscn +++ b/godot/Scenes/Weapons/pen.tscn @@ -3,13 +3,14 @@ [ext_resource type="ArrayMesh" path="res://Assets/Models/Weapons/pen_base.res" id="1_clcko"] [ext_resource type="ArrayMesh" path="res://Assets/Models/Weapons/pen_lid.res" id="2_0u0nh"] -[node name="Stylo" type="Node3D"] +[node name="Stylo" type="Bullet"] +transform = Transform3D(3, 0, 0, 0, 3, 0, 0, 0, 3, 0, -7.45058e-09, 0) [node name="Armature" type="Node3D" parent="."] -transform = Transform3D(5, 0, 0, 0, 0.0178025, -4.99997, 0, 4.99997, 0.0178025, 0, 0, -0.0892955) +transform = Transform3D(5, 0, 0, 0, 0.0178025, -4.99997, 0, 4.99997, 0.0178025, 0, 0, -0.0828303) [node name="pen_base" type="MeshInstance3D" parent="Armature"] -transform = Transform3D(0.140708, 0, 0, 0, -1.67737e-08, 0.140708, 0, -0.140708, -1.67737e-08, -1.12406e-11, 0.00945318, -3.65384e-09) +transform = Transform3D(0.140708, 0, 0, 0, -1.6822e-08, 0.140708, 0, -0.140708, -1.6822e-08, -1.12406e-11, 0.00809256, -4.84818e-06) mesh = ExtResource("1_clcko") skeleton = NodePath("") diff --git a/godot/project.godot b/godot/project.godot index 28d79d0..36c1822 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -62,3 +62,10 @@ shoot={ "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null) ] } + +[layer_names] + +3d_physics/layer_1="World" +3d_physics/layer_2="No Team" +3d_physics/layer_3="Team 1" +3d_physics/layer_4="Team 2" diff --git a/include/client/Bullet.h b/include/client/Bullet.h new file mode 100644 index 0000000..8a8ab52 --- /dev/null +++ b/include/client/Bullet.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +namespace blitz { + +class Bullet : public godot::Node3D { + GDCLASS(Bullet, godot::Node3D) + protected: + static void _bind_methods(); + + public: + Bullet(); + ~Bullet(); + void _process(float delta); + void _ready() override; + + private: + bool m_Stuck; + float m_LifeTime; +}; + +} // namespace blitz \ No newline at end of file diff --git a/src/client/Bullet.cpp b/src/client/Bullet.cpp new file mode 100644 index 0000000..874a427 --- /dev/null +++ b/src/client/Bullet.cpp @@ -0,0 +1,56 @@ +#include + +#include +#include +#include +#include +#include + +namespace blitz { + +using namespace godot; + +static constexpr float SPEED = 20.0f; +static constexpr float MaxAlive = 10.0f; + +void Bullet::_bind_methods() {} + +Bullet::Bullet() : m_Stuck(false) {} + +void Bullet::_ready() {} + +Bullet::~Bullet() {} + +void Bullet::_process(float a_Delta) { + if (Engine::get_singleton()->is_editor_hint()) { + return; + } + + m_LifeTime += a_Delta; + if (m_LifeTime > MaxAlive) { + queue_free(); + return; + } + + if (m_Stuck) + return; + + Vector3 start = get_position(); + Vector3 finish = start + get_transform().basis.xform(Vector3{0, 0, -SPEED * a_Delta}); + + auto query = PhysicsRayQueryParameters3D::create(start, finish, 1); + auto result = get_world_3d()->get_direct_space_state()->intersect_ray(query); + + if (result.is_empty()) { + set_position(finish); + return; + } + + m_Stuck = true; + Vector3 intersectPoint = result["position"]; + + auto* head = get_node("Armature"); + head->set_global_position(intersectPoint); +} + +} // namespace blitz \ No newline at end of file diff --git a/src/client/register_types.cpp b/src/client/register_types.cpp index 99b775e..d920a65 100644 --- a/src/client/register_types.cpp +++ b/src/client/register_types.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -23,6 +24,7 @@ static void RegisterClasses() { GDREGISTER_ABSTRACT_CLASS(blitz::World); GDREGISTER_CLASS(blitz::ClientWorld); GDREGISTER_CLASS(blitz::ServerWorld); + GDREGISTER_CLASS(blitz::Bullet); } static void initialize_blitz_module(ModuleInitializationLevel p_level) {