LaserGun without collisions

This commit is contained in:
Morph01
2024-08-14 18:46:31 +02:00
parent ef0bcd0a35
commit b92c5169c4
12 changed files with 161 additions and 6 deletions

Binary file not shown.

View File

@@ -1,4 +1,6 @@
[gd_scene load_steps=3 format=3 uid="uid://d38w4ae3qj0k4"]
[gd_scene load_steps=4 format=3 uid="uid://d38w4ae3qj0k4"]
[ext_resource type="PackedScene" uid="uid://ciex3x7rhv1bx" path="res://Scenes/Weapons/steampunk_rifle.tscn" id="1_i6keg"]
[sub_resource type="CapsuleMesh" id="CapsuleMesh_ky6st"]
@@ -19,3 +21,6 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.578545, 0)
[node name="Camera" type="Camera3D" parent="Head"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.1, 0)
[node name="Steampunk Rifle" parent="Head/Camera" instance=ExtResource("1_i6keg")]
transform = Transform3D(1, 0, 0, 0, 0.998135, -0.0610485, 0, 0.0610485, 0.998135, 0.19, -0.18, -0.55)

View File

@@ -1,9 +1,9 @@
[gd_scene load_steps=18 format=3 uid="uid://coue2qehpn4fr"]
[ext_resource type="Texture2D" path="res://Assets/Textures/Sky.png" id="1_mnexj"]
[ext_resource type="Texture2D" path="res://Assets/Textures/Black.png" id="2_fkwcn"]
[ext_resource type="Texture2D" path="res://Assets/Textures/Orange.png" id="3_ux02w"]
[ext_resource type="Texture2D" path="res://Assets/Textures/Green.png" id="4_wp15n"]
[ext_resource type="Texture2D" uid="uid://dujfl12rge3p4" path="res://Assets/Textures/Sky.png" id="1_mnexj"]
[ext_resource type="Texture2D" uid="uid://b8n5rff2a8h2u" path="res://Assets/Textures/Black.png" id="2_fkwcn"]
[ext_resource type="Texture2D" uid="uid://lpbttkw7gpxj" path="res://Assets/Textures/Orange.png" id="3_ux02w"]
[ext_resource type="Texture2D" uid="uid://di3uyny341483" path="res://Assets/Textures/Green.png" id="4_wp15n"]
[ext_resource type="PackedScene" uid="uid://d38w4ae3qj0k4" path="res://Scenes/Characters/first_person_player.tscn" id="5_8ctht"]
[sub_resource type="PanoramaSkyMaterial" id="PanoramaSkyMaterial_6c4vd"]

View File

@@ -0,0 +1,19 @@
[gd_scene load_steps=3 format=3 uid="uid://do601jl7p1u22"]
[sub_resource type="BoxMesh" id="BoxMesh_ibwn0"]
size = Vector3(0.05, 0.05, 1)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2qdhl"]
albedo_color = Color(1, 0.764706, 0.294118, 1)
emission_enabled = true
emission = Color(0.568627, 1, 0.313726, 1)
emission_energy_multiplier = 5.0
[node name="Bullet" type="Bullet"]
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = SubResource("BoxMesh_ibwn0")
surface_material_override/0 = SubResource("StandardMaterial3D_2qdhl")
[node name="RayCast3D" type="RayCast3D" parent="."]
target_position = Vector3(0, 0, -0.6)

View File

@@ -0,0 +1,36 @@
[gd_scene load_steps=4 format=3 uid="uid://ciex3x7rhv1bx"]
[ext_resource type="PackedScene" uid="uid://bkskefixbe2cw" path="res://Assets/Models/Weapons/Steampunk Rifle.glb" id="1_0bnv5"]
[sub_resource type="Animation" id="Animation_20uwx"]
resource_name = "Shoot"
length = 0.1
step = 0.01
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Node:position")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.01, 0.1),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 0,
"values": [Vector3(0, 0, 0), Vector3(0, 0, 0.05), Vector3(0, 0, 0)]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_113bo"]
_data = {
"Shoot": SubResource("Animation_20uwx")
}
[node name="Steampunk Rifle" instance=ExtResource("1_0bnv5")]
[node name="AnimationPlayer" type="AnimationPlayer" parent="." index="1"]
libraries = {
"": SubResource("AnimationLibrary_113bo")
}
[node name="RayCast3D" type="RayCast3D" parent="." index="2"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1.10108)
target_position = Vector3(0, 0, -1)

19
godot/bullet.tscn Normal file
View File

@@ -0,0 +1,19 @@
[gd_scene load_steps=3 format=3 uid="uid://bt0551kcc5tw4"]
[sub_resource type="BoxMesh" id="BoxMesh_ibwn0"]
size = Vector3(0.05, 0.05, 1)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2qdhl"]
albedo_color = Color(1, 0.764706, 0.294118, 1)
emission_enabled = true
emission = Color(0.568627, 1, 0.313726, 1)
emission_energy_multiplier = 5.0
[node name="Bullet" type="Bullet"]
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = SubResource("BoxMesh_ibwn0")
surface_material_override/0 = SubResource("StandardMaterial3D_2qdhl")
[node name="RayCast3D" type="RayCast3D" parent="."]
target_position = Vector3(0, 0, -0.6)

View File

@@ -49,6 +49,11 @@ jump={
}
escape={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194305,"key_label":0,"unicode":0,"echo":false,"script":null)
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194305,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
shoot={
"deadzone": 0.5,
"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":1,"position":Vector2(351, 9),"global_position":Vector2(355, 50),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}

22
src/Bullet.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include "Bullet.h"
using namespace godot;
namespace blitz {
static constexpr float BULLET_SPEED = 40.0f;
void Bullet::_bind_methods() {}
Bullet::Bullet() {}
Bullet::~Bullet() {}
void Bullet::_ready() {}
void Bullet::_physics_process(float a_Delta) {
Vector3 movement = Vector3(0, 0, -BULLET_SPEED) * a_Delta;
set_position(get_transform().xform(movement));
}
} // namespace blitz

19
src/Bullet.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include <godot_cpp/classes/node3d.hpp>
namespace blitz {
class Bullet : public godot::Node3D {
GDCLASS(Bullet, godot::Node3D)
protected:
static void _bind_methods();
public:
Bullet();
~Bullet();
void _ready();
void _physics_process(float delta);
};
} // namespace blitz

View File

@@ -4,6 +4,7 @@
#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/input_event_mouse_motion.hpp>
#include <godot_cpp/classes/input_map.hpp>
#include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/core/math.hpp>
using namespace godot;
@@ -32,6 +33,7 @@ static constexpr float FOV_TRANSITION = 8.0f;
static constexpr float MIN_FOV_VELOCITY = 0.5;
static constexpr float MAX_FOV_VELOCITY = SPRINT_SPEED * 2.0f;
void FirstPersonPlayer::_bind_methods() {}
FirstPersonPlayer::FirstPersonPlayer() : m_BobTime(0) {}
@@ -47,6 +49,14 @@ void FirstPersonPlayer::_ready() {
DEV_ASSERT(m_Head);
m_Camera = Object::cast_to<Camera3D>(m_Head->find_child("Camera"));
DEV_ASSERT(m_Camera);
m_WeaponAnimation = Object::cast_to<AnimationPlayer>(find_child("AnimationPlayer"));
DEV_ASSERT(m_WeaponAnimation);
m_GunBarrel = Object::cast_to<RayCast3D>(find_child("RayCast3D"));
DEV_ASSERT(m_GunBarrel);
m_BulletScene = ResourceLoader::get_singleton()->load("res://Scenes/Weapons/bullet.tscn");
if (!m_BulletScene.is_valid()) {
ERR_PRINT("Failed to load bullet scene.");
}
}
void FirstPersonPlayer::_unhandled_input(const godot::Ref<godot::InputEvent>& a_Event) {
@@ -84,6 +94,16 @@ void FirstPersonPlayer::_physics_process(float a_Delta) {
UpdateFOV(a_Delta);
UpdateBobbing(a_Delta);
if (Input->is_action_pressed("shoot")) {
if (!m_WeaponAnimation->is_playing()) {
m_WeaponAnimation->play("Shoot");
m_BulletInstance = m_BulletScene->instantiate();
m_BulletInstance->set("position", m_GunBarrel->get_global_transform().origin);
m_BulletInstance->set("rotation", m_GunBarrel->get_global_transform().basis.get_euler());
get_parent()->add_child(m_BulletInstance);
}
}
move_and_slide();
}

View File

@@ -1,9 +1,13 @@
#pragma once
#include <godot_cpp/classes/animation_player.hpp>
#include <godot_cpp/classes/camera3d.hpp>
#include <godot_cpp/classes/character_body3d.hpp>
#include <godot_cpp/classes/input_event_mouse_motion.hpp>
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/classes/ray_cast3d.hpp>
#include <godot_cpp/classes/ref.hpp>
namespace blitz {
@@ -24,6 +28,10 @@ class FirstPersonPlayer : public godot::CharacterBody3D {
private:
godot::Camera3D* m_Camera;
godot::Node3D* m_Head;
godot::AnimationPlayer* m_WeaponAnimation;
godot::RayCast3D* m_GunBarrel;
godot::Node* m_BulletInstance;
godot::Ref<godot::PackedScene> m_BulletScene;
float m_BobTime;
float m_Speed;

View File

@@ -1,5 +1,6 @@
#include "register_types.h"
#include "Bullet.h"
#include "FirstPersonPlayer.h"
#include "Player.h"
#include "SpringArmPivot.h"
@@ -18,6 +19,7 @@ void initialize_example_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<blitz::Player>();
ClassDB::register_class<blitz::SpringArmPivot>();
ClassDB::register_class<blitz::FirstPersonPlayer>();
ClassDB::register_class<blitz::Bullet>();
}
void uninitialize_example_module(ModuleInitializationLevel p_level) {