Compare commits

7 Commits

Author SHA1 Message Date
Morph01
5c6f82c5d7 Random spawn of zombies 2024-08-22 19:40:32 +02:00
Morph01
faf6a9116d Add Dying animation 2024-08-22 17:53:25 +02:00
Morph01
1f52eb2c52 Collision of zombie body parts completed and different damage added depending on the body part hit 2024-08-22 16:15:54 +02:00
Morph01
d88cb9fbff Added zombie spawn animation, red screen and hit stagger when the player is hit. 2024-08-20 16:53:43 +02:00
Morph01
0c667944dd fix attack angle 2024-08-20 16:49:18 +02:00
Morph01
1a14c78ede Zombies with PathFinding Run and Attack ! 2024-08-16 19:17:51 +02:00
Morph01
6160561498 Squashed commit of the following:
commit 4fd60e7874
Author: Morph01 <145839520+Morph01@users.noreply.github.com>
Date:   Fri Aug 16 14:19:29 2024 +0200

    bug animation ???
2024-08-16 14:56:57 +02:00
28 changed files with 4248 additions and 19 deletions

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 KiB

View File

@@ -3771,6 +3771,6 @@ callback_mode_process = 0
tree_root = SubResource("AnimationNodeBlendTree_dptuf")
anim_player = NodePath("../AnimationPlayer")
parameters/ground_air_transition/current_state = "air"
parameters/ground_air_transition/transition_request = ""
parameters/ground_air_transition/transition_request = "air"
parameters/ground_air_transition/current_index = 1
parameters/iwr_blend/blend_amount = -1.0

File diff suppressed because one or more lines are too long

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=18 format=3 uid="uid://coue2qehpn4fr"]
[gd_scene load_steps=19 format=3 uid="uid://coue2qehpn4fr"]
[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"]
@@ -18,6 +18,12 @@ sky = SubResource("Sky_5ngqa")
tonemap_mode = 2
glow_enabled = true
[sub_resource type="NavigationMesh" id="NavigationMesh_2pdqo"]
vertices = PackedVector3Array(-5.25, 0.5, 1, -5, 0.5, -1.75, -5.5, 0.5, -2, -11.25, 0.5, 1, -5.5, 0.5, -3.75, -3.25, 0.5, -4, -3.25, 0.5, -24.5, -11.25, 0.5, 1, -5.5, 0.5, -2, -5.5, 0.5, -3.75, -24.5, 0.5, 3.25, -11.5, 0.5, 3.25, -11.25, 0.5, 1, -24.5, 0.5, 3.25, -11.25, 0.5, 1, -5.5, 0.5, -3.75, -3.25, 0.5, -24.5, -24.5, 0.5, -24.5, -3.25, 0.5, -4, 3.5, 0.5, -4, 3.5, 0.5, -24.5, -3.25, 0.5, -24.5, 5.5, 0.5, -4, 5.75, 0.5, -3, 24.5, 0.5, -3, 24.5, 0.5, -24.5, 3.5, 0.5, -24.5, 3.5, 0.5, -24.5, 3.5, 0.5, -4, 5.5, 0.5, -4, -5, 0.5, -1.75, -5.25, 0.5, 1, -5, 0.75, 2, 5.25, 0.5, -1.75, -5, 0.75, 2, -7.25, 1.5, 2, -7.25, 1.5, 6, -5, 0.75, 6.25, 24.5, 0.5, -3, 5.75, 0.5, -3, 5.25, 0.5, -1.75, -5.25, 0.5, 7.25, -6.75, 0.5, 7.25, -6.5, 0.5, 24.5, 5.25, 0.5, -1.75, -5, 0.75, 2, -5, 0.75, 6.25, 5.25, 0.5, -1.75, -5, 0.75, 6.25, -5.25, 0.5, 7.25, -6.5, 0.5, 24.5, 24.5, 0.5, 24.5, 24.5, 0.5, -3, -7.25, 1.5, 6, -7.25, 1.5, 2, -14, 3.75, 2, -14, 3.75, 6, -11.5, 0.5, 7, -11.5, 0.5, 3.25, -24.5, 0.5, 3.25, -10, 0.5, 24.5, -9.75, 0.5, 7.25, -11.5, 0.5, 7, -11.5, 0.5, 7, -24.5, 0.5, 3.25, -24.5, 0.5, 24.5, -10, 0.5, 24.5, -10, 0.5, 24.5, -6.5, 0.5, 24.5, -6.75, 0.5, 7.25, -9.75, 0.5, 7.25)
polygons = [PackedInt32Array(1, 0, 2), PackedInt32Array(2, 0, 3), PackedInt32Array(6, 5, 4), PackedInt32Array(7, 9, 8), PackedInt32Array(12, 11, 10), PackedInt32Array(14, 13, 15), PackedInt32Array(15, 13, 16), PackedInt32Array(16, 13, 17), PackedInt32Array(21, 20, 18), PackedInt32Array(18, 20, 19), PackedInt32Array(23, 22, 24), PackedInt32Array(24, 22, 25), PackedInt32Array(25, 22, 26), PackedInt32Array(29, 28, 27), PackedInt32Array(31, 30, 32), PackedInt32Array(32, 30, 33), PackedInt32Array(35, 34, 36), PackedInt32Array(36, 34, 37), PackedInt32Array(40, 39, 38), PackedInt32Array(43, 42, 41), PackedInt32Array(46, 45, 44), PackedInt32Array(48, 47, 49), PackedInt32Array(49, 47, 50), PackedInt32Array(50, 47, 52), PackedInt32Array(50, 52, 51), PackedInt32Array(56, 55, 53), PackedInt32Array(53, 55, 54), PackedInt32Array(59, 58, 57), PackedInt32Array(62, 61, 60), PackedInt32Array(66, 65, 63), PackedInt32Array(63, 65, 64), PackedInt32Array(70, 69, 67), PackedInt32Array(67, 69, 68)]
agent_radius = 0.4
agent_max_slope = 60.0
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ajchh"]
albedo_texture = ExtResource("2_fkwcn")
uv1_triplanar = true
@@ -51,40 +57,99 @@ uv1_triplanar = true
[sub_resource type="ConcavePolygonShape3D" id="ConcavePolygonShape3D_rit6o"]
data = PackedVector3Array(-12.5, 2.5, 2.5, 2.5, -2.5, 2.5, -2.5, -2.5, 2.5, -12.5, 2.5, -2.5, -2.5, -2.5, -2.5, 2.5, -2.5, -2.5, -12.5, 2.5, 2.5, -12.5, 2.5, -2.5, 2.5, -2.5, 2.5, -12.5, 2.5, -2.5, 2.5, -2.5, -2.5, 2.5, -2.5, 2.5, -12.5, 2.5, -2.5, -12.5, 2.5, 2.5, -2.5, -2.5, -2.5, -12.5, 2.5, 2.5, -2.5, -2.5, 2.5, -2.5, -2.5, -2.5, -2.5, -2.5, 2.5, 2.5, -2.5, 2.5, -2.5, -2.5, -2.5, 2.5, -2.5, 2.5, 2.5, -2.5, -2.5, -2.5, -2.5, -2.5)
[node name="World" type="Node3D"]
[node name="World" type="World"]
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
[node name="Map" type="Node3D" parent="."]
[node name="WorldEnvironment" type="WorldEnvironment" parent="Map"]
environment = SubResource("Environment_ctwiv")
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
transform = Transform3D(-0.866026, -0.433013, 0.249999, 0.5, -0.75, 0.433012, -1.3411e-07, 0.499999, 0.866026, 0, 0, 0)
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="Map"]
transform = Transform3D(-0.866026, -0.433013, 0.249999, 0.5, -0.750001, 0.433012, -1.3411e-07, 0.499999, 0.866026, 0, 0, 0)
shadow_enabled = true
[node name="Floor" type="MeshInstance3D" parent="."]
[node name="NavigationRegion3D" type="NavigationRegion3D" parent="Map"]
navigation_mesh = SubResource("NavigationMesh_2pdqo")
[node name="Floor" type="MeshInstance3D" parent="Map/NavigationRegion3D"]
mesh = SubResource("PlaneMesh_mmup0")
skeleton = NodePath("../../..")
[node name="StaticBody3D" type="StaticBody3D" parent="Floor"]
[node name="StaticBody3D" type="StaticBody3D" parent="Map/NavigationRegion3D/Floor"]
collision_layer = 3
collision_mask = 3
[node name="CollisionShape3D" type="CollisionShape3D" parent="Floor/StaticBody3D"]
[node name="CollisionShape3D" type="CollisionShape3D" parent="Map/NavigationRegion3D/Floor/StaticBody3D"]
shape = SubResource("ConcavePolygonShape3D_26ptr")
[node name="Wall" type="MeshInstance3D" parent="."]
[node name="Wall" type="MeshInstance3D" parent="Map/NavigationRegion3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, -3)
mesh = SubResource("BoxMesh_plpqy")
skeleton = NodePath("../../..")
[node name="StaticBody3D" type="StaticBody3D" parent="Wall"]
[node name="StaticBody3D" type="StaticBody3D" parent="Map/NavigationRegion3D/Wall"]
collision_layer = 3
collision_mask = 3
[node name="CollisionShape3D" type="CollisionShape3D" parent="Wall/StaticBody3D"]
[node name="CollisionShape3D" type="CollisionShape3D" parent="Map/NavigationRegion3D/Wall/StaticBody3D"]
shape = SubResource("ConcavePolygonShape3D_v7prx")
[node name="Slope" type="MeshInstance3D" parent="."]
[node name="Slope" type="MeshInstance3D" parent="Map/NavigationRegion3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2, 1, 4)
mesh = SubResource("PrismMesh_0l7qq")
skeleton = NodePath("../../..")
surface_material_override/0 = SubResource("StandardMaterial3D_pfpgv")
[node name="StaticBody3D" type="StaticBody3D" parent="Slope"]
[node name="StaticBody3D" type="StaticBody3D" parent="Map/NavigationRegion3D/Slope"]
collision_layer = 3
collision_mask = 3
[node name="CollisionShape3D" type="CollisionShape3D" parent="Slope/StaticBody3D"]
[node name="CollisionShape3D" type="CollisionShape3D" parent="Map/NavigationRegion3D/Slope/StaticBody3D"]
shape = SubResource("ConcavePolygonShape3D_rit6o")
[node name="Spawns" type="Node3D" parent="Map"]
[node name="Spawn1" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 18.6089)
[node name="Spawn2" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 18.8451, 0, 18.6089)
[node name="Spawn3" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 18.8451, 0, 0.0245247)
[node name="Spawn4" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(-0.269021, -0.963134, 0, 0.963134, -0.269021, 0, 0, 0, 1, 19.0777, 0, -20.2546)
[node name="Spawn5" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(-0.269021, -0.963134, 0, 0.963134, -0.269021, 0, 0, 0, 1, 1.64303, 0, -20.2546)
[node name="Spawn6" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(-0.269021, -0.963134, 0, 0.963134, -0.269021, 0, 0, 0, 1, -19.0255, 0, -20.2546)
[node name="Spawn7" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(-0.269021, -0.963134, 0, 0.963134, -0.269021, 0, 0, 0, 1, -19.0255, 0, 0.256641)
[node name="Spawn8" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(-0.269021, -0.963134, 0, 0.963134, -0.269021, 0, 0, 0, 1, -19.0255, 0, 16.4132)
[node name="FirstPersonPlayer" parent="." instance=ExtResource("5_8ctht")]
[node name="UI" type="Control" parent="."]
layout_mode = 3
anchors_preset = 0
offset_right = 40.0
offset_bottom = 40.0
[node name="HitRect" type="ColorRect" parent="UI"]
visible = false
layout_mode = 0
offset_left = -177.0
offset_top = -96.0
offset_right = 1365.0
offset_bottom = 780.0
color = Color(1, 0, 0, 0.34902)
[node name="ZombieSpawnTimer" type="Timer" parent="."]
wait_time = 5.0
autostart = true

View File

@@ -25,14 +25,17 @@ 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)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -56304.6)
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.6)
mesh = SubResource("BoxMesh_ibwn0")
surface_material_override/0 = SubResource("StandardMaterial3D_2qdhl")
[node name="RayCast3D" type="RayCast3D" parent="."]
target_position = Vector3(0, 0, -0.6)
collision_mask = 2
collide_with_areas = true
[node name="GPUParticles3D" type="GPUParticles3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.7)

View File

@@ -32,5 +32,5 @@ libraries = {
}
[node name="RayCast3D" type="RayCast3D" parent="." index="2"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1.10108)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.6)
target_position = Vector3(0, 0, -1)

View File

@@ -0,0 +1,55 @@
#include "BoneCollisionShape.h"
using namespace godot;
namespace blitz {
void BoneCollisionShape::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_head_damage"), &BoneCollisionShape::get_head_damage);
ClassDB::bind_method(D_METHOD("set_head_damage", "m_HeadDamage"), &BoneCollisionShape::set_head_damage);
ADD_PROPERTY(PropertyInfo(Variant::INT, "m_HeadDamage"), "set_head_damage", "get_head_damage");
ClassDB::bind_method(D_METHOD("get_body_damage"), &BoneCollisionShape::get_body_damage);
ClassDB::bind_method(D_METHOD("set_body_damage", "m_BodyDamage"), &BoneCollisionShape::set_body_damage);
ADD_PROPERTY(PropertyInfo(Variant::INT, "m_BodyDamage"), "set_head_damage", "get_body_damage");
ADD_SIGNAL(MethodInfo("a_ZombieHeadShotHit", PropertyInfo(Variant::INT, "dam")));
ADD_SIGNAL(MethodInfo("a_ZombieBodyShotHit", PropertyInfo(Variant::INT, "dam")));
ClassDB::bind_method(D_METHOD("headshot_hit"), &BoneCollisionShape::headshot_hit);
ClassDB::bind_method(D_METHOD("bodyshot_hit"), &BoneCollisionShape::bodyshot_hit);
}
BoneCollisionShape::BoneCollisionShape() {}
BoneCollisionShape::~BoneCollisionShape() {}
void BoneCollisionShape::_ready() {}
void BoneCollisionShape::set_head_damage(int a_D) {
m_HeadDamage = a_D;
}
int BoneCollisionShape::get_head_damage() const {
return m_HeadDamage;
}
void BoneCollisionShape::set_body_damage(int a_D) {
m_BodyDamage = a_D;
}
int BoneCollisionShape::get_body_damage() const {
return m_BodyDamage;
}
void BoneCollisionShape::headshot_hit() {
ERR_PRINT("zombie headshot hit");
emit_signal("a_ZombieHeadShotHit", m_HeadDamage);
}
void BoneCollisionShape::bodyshot_hit() {
ERR_PRINT("zombie body hit");
emit_signal("a_ZombieBodyShotHit", m_BodyDamage);
}
} // namespace blitz

30
src/BoneCollisionShape.h Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#include <godot_cpp/classes/area3d.hpp>
namespace blitz {
class BoneCollisionShape : public godot::Area3D {
GDCLASS(BoneCollisionShape, godot::Area3D)
protected:
static void _bind_methods();
public:
BoneCollisionShape();
~BoneCollisionShape();
void _ready();
void headshot_hit();
void bodyshot_hit();
private:
int m_HeadDamage = 2;
int m_BodyDamage = 1;
void set_head_damage(int d);
int get_head_damage() const;
void set_body_damage(int d);
int get_body_damage() const;
};
} // namespace blitz

View File

@@ -1,4 +1,5 @@
#include "Bullet.h"
#include "BoneCollisionShape.h"
using namespace godot;
@@ -21,7 +22,7 @@ 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);
m_Timer = memnew(Timer);
add_child(m_Timer);
m_Timer->connect("timeout", callable_mp(this, &Bullet::_on_timer_timeout));
}
@@ -33,6 +34,19 @@ void Bullet::_physics_process(float a_Delta) {
if (m_Ray->is_colliding()) {
m_Mesh->set_visible(false);
m_Particles->set_emitting(true);
m_Ray->set_enabled(false);
Object* collider = m_Ray->get_collider();
if (!collider)
return;
auto* area_collider = Object::cast_to<BoneCollisionShape>(collider);
if (!(area_collider && area_collider->is_in_group("enemy")))
return;
const StringName& area_name = area_collider->get_name();
area_collider->call(area_name.match("HeadArea3D") ? "headshot_hit" : "bodyshot_hit");
m_Timer->set_wait_time(1.0);
m_Timer->start();
}

View File

@@ -6,6 +6,7 @@
#include <godot_cpp/classes/input_map.hpp>
#include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/signal.hpp>
using namespace godot;
@@ -20,6 +21,8 @@ static constexpr float GRAVITY = 9.81f;
static constexpr float SENSITIVITY = 0.003f;
static constexpr float HIT_STAGGER = 8.0f;
static constexpr float BOB_FREQ = 2.0f;
static constexpr float BOB_AMP = 0.08f;
@@ -34,7 +37,9 @@ static constexpr float MIN_FOV_VELOCITY = 0.5;
static constexpr float MAX_FOV_VELOCITY = SPRINT_SPEED * 2.0f;
void FirstPersonPlayer::_bind_methods() {}
void FirstPersonPlayer::_bind_methods() {
ADD_SIGNAL(MethodInfo("a_PlayerHit"));
}
FirstPersonPlayer::FirstPersonPlayer() : m_BobTime(0) {}
@@ -156,4 +161,9 @@ void FirstPersonPlayer::UpdateFOV(float a_Delta) {
m_Camera->set_fov(Math::lerp(m_Camera->get_fov(), targetFOV, a_Delta * FOV_TRANSITION));
}
void FirstPersonPlayer::hit(Vector3 a_Dir) {
emit_signal("a_PlayerHit");
this->set_velocity(a_Dir * HIT_STAGGER);
}
} // namespace blitz

View File

@@ -24,6 +24,7 @@ class FirstPersonPlayer : public godot::CharacterBody3D {
void _unhandled_input(const godot::Ref<godot::InputEvent>&);
void _physics_process(float delta);
void _ready();
void hit(godot::Vector3 dir);
private:
godot::Camera3D* m_Camera;
@@ -39,6 +40,7 @@ class FirstPersonPlayer : public godot::CharacterBody3D {
void UpdateFOV(float delta);
void UpdateCamera(const godot::InputEventMouseMotion&);
void UpdatePosition(float delta);
};
} // namespace blitz

87
src/World.cpp Normal file
View File

@@ -0,0 +1,87 @@
#include "World.h"
#include "FirstPersonPlayer.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
using namespace godot;
namespace blitz {
void World::_bind_methods() {
ClassDB::bind_method(D_METHOD("_on_first_person_player_hit"), &World::_on_first_person_player_hit);
ClassDB::bind_method(D_METHOD("_on_timer_timeout"), &World::_on_timer_timeout);
ClassDB::bind_method(D_METHOD("_on_zombie_spawn_timer_timeout"), &World::_on_zombie_spawn_timer_timeout);
}
World::World() {}
World::~World() {}
void World::_ready() {
FirstPersonPlayer* player = Object::cast_to<FirstPersonPlayer>(find_child("FirstPersonPlayer"));
DEV_ASSERT(player);
if (player) {
player->connect("a_PlayerHit", callable_mp(this, &World::_on_first_person_player_hit));
}
m_RedRect = Object::cast_to<ColorRect>(find_child("HitRect"));
DEV_ASSERT(m_RedRect);
m_Timer = memnew(Timer);
add_child(m_Timer);
m_Timer->connect("timeout", callable_mp(this, &World::_on_timer_timeout));
m_ZombieSpawnTimer = memnew(Timer);
add_child(m_ZombieSpawnTimer);
m_ZombieSpawnTimer->connect("timeout", callable_mp(this, &World::_on_zombie_spawn_timer_timeout));
m_Spawns = Object::cast_to<Node3D>(find_child("Spawns"));
DEV_ASSERT(m_Spawns);
m_NavigationRegion = Object::cast_to<NavigationRegion3D>(find_child("NavigationRegion3D"));
DEV_ASSERT(m_NavigationRegion);
m_ZombieScene = ResourceLoader::get_singleton()->load("res://Scenes/Characters/zombie.tscn");
if (!m_ZombieScene.is_valid()) {
ERR_PRINT("Failed to load zombie scene.");
}
m_RandomId.instantiate();
m_RandomId->randomize();
m_ZombieSpawnTimer->start();
}
void World::_on_first_person_player_hit() {
#if DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
#endif
UtilityFunctions::print("Player hit detected in World.");
m_RedRect->set_visible(true);
m_Timer->set_wait_time(0.2);
m_Timer->start();
}
void World::_on_timer_timeout() {
m_RedRect->set_visible(false);
}
Node* World::get_random_child(Node* a_ParentNode) {
m_ZombieSpawnTimer->set_wait_time(4.0);
if (!a_ParentNode || a_ParentNode->get_child_count() == 0) {
return nullptr;
}
int32_t id = m_RandomId->randi() % a_ParentNode->get_child_count();
return a_ParentNode->get_child(id);
}
void World::_on_zombie_spawn_timer_timeout() {
#if DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
#endif
auto spawns = Object::cast_to<Node3D>(World::get_random_child(m_Spawns));
auto spawn_points = spawns->get_global_position();
m_ZombieInstance = Object::cast_to<Zombie>(m_ZombieScene->instantiate());
DEV_ASSERT(m_ZombieInstance);
m_ZombieInstance->set_position(spawn_points);
m_NavigationRegion->add_child(m_ZombieInstance);
}
} // namespace blitz

40
src/World.h Normal file
View File

@@ -0,0 +1,40 @@
#pragma once
#include "Zombie.h"
#include <godot_cpp/classes/color_rect.hpp>
#include <godot_cpp/classes/navigation_region3d.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/classes/random_number_generator.hpp>
#include <godot_cpp/classes/ref.hpp>
#include <godot_cpp/classes/timer.hpp>
namespace blitz {
class World : public godot::Node3D {
GDCLASS(World, godot::Node3D);
protected:
static void _bind_methods();
public:
World();
~World();
void _ready();
private:
godot::ColorRect* m_RedRect;
godot::Timer* m_Timer;
godot::Timer* m_ZombieSpawnTimer;
godot::Node3D* m_Spawns;
godot::NavigationRegion3D* m_NavigationRegion;
Zombie* m_ZombieInstance;
godot::Ref<godot::PackedScene> m_ZombieScene;
godot::Ref<godot::RandomNumberGenerator> m_RandomId;
void _on_timer_timeout();
void _on_zombie_spawn_timer_timeout();
void _on_first_person_player_hit();
Node* get_random_child(Node* parent_node);
};
} // namespace blitz

134
src/Zombie.cpp Normal file
View File

@@ -0,0 +1,134 @@
#include "Zombie.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
#include <godot_cpp/classes/window.hpp>
#include <godot_cpp/core/math.hpp>
using namespace godot;
namespace blitz {
static constexpr float SPEED = 2.0f;
static constexpr float ATTACK_RANGE = 2.0f;
void Zombie::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_m_PlayerPath", "m_PlayerPath"), &Zombie::set_m_PlayerPath);
ClassDB::bind_method(D_METHOD("get_m_PlayerPath"), &Zombie::get_m_PlayerPath);
ClassDB::bind_method(D_METHOD("hit_finished"), &Zombie::hit_finished);
ClassDB::bind_method(D_METHOD("_on_area_3d_a_zombie_head_shot_hit", "m_Health"), &Zombie::_on_area_3d_a_zombie_head_shot_hit);
ClassDB::bind_method(D_METHOD("_on_area_3d_a_zombie_body_shot_hit", "m_Health"), &Zombie::_on_area_3d_a_zombie_body_shot_hit);
ClassDB::bind_method(D_METHOD("_on_timer_timeout"), &Zombie::_on_timer_timeout);
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "m_PlayerPath"), "set_m_PlayerPath", "get_m_PlayerPath");
}
Zombie::Zombie() {}
Zombie::~Zombie() {}
void Zombie::_ready() {
#if DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
#endif
godot::Node* scene_root = Object::cast_to<godot::Node>(get_tree()->get_root()->get_child(0));
if (scene_root) {
m_Player = Object::cast_to<FirstPersonPlayer>(scene_root->find_child("FirstPersonPlayer", true));
DEV_ASSERT(m_Player);
} else {
ERR_PRINT("Scene root not found or invalid.");
}
m_NavigationAgent = Object::cast_to<NavigationAgent3D>(find_child("NavigationAgent3D"));
DEV_ASSERT(m_NavigationAgent);
m_AnimationTree = Object::cast_to<AnimationTree>(find_child("AnimationTree"));
DEV_ASSERT(m_AnimationTree);
m_StateMachine = Object::cast_to<AnimationNodeStateMachinePlayback>(m_AnimationTree->get("parameters/playback"));
DEV_ASSERT(m_StateMachine);
this->set_velocity(Vector3(0, 0, 0));
m_HeadCollision = Object::cast_to<BoneCollisionShape>(find_child("HeadArea3D"));
DEV_ASSERT(m_HeadCollision);
m_HeadCollision->connect("a_ZombieHeadShotHit", Callable(this, "_on_area_3d_a_zombie_head_shot_hit"));
connect_collision_shapes(m_BodyPartsCollision);
m_Timer = memnew(Timer);
add_child(m_Timer);
m_Timer->connect("timeout", callable_mp(this, &Zombie::_on_timer_timeout));
}
void Zombie::_process(float a_Delta) {
#if DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
#endif
if (m_StateMachine->get_current_node().match("Run")) {
m_NavigationAgent->set_target_position(m_Player->get_global_position());
m_Velocity = (m_NavigationAgent->get_next_path_position() - this->get_global_position()).normalized() * SPEED;
this->set_velocity(m_Velocity);
look_at(Vector3(this->get_global_position().x + this->get_velocity().x, this->get_global_position().y,
this->get_global_position().z + this->get_velocity().z));
} else if (m_StateMachine->get_current_node().match("Attack")) {
this->set_velocity(Vector3(0, 0, 0));
look_at(Vector3(m_Player->get_global_position().x + this->get_velocity().x, this->get_global_position().y,
m_Player->get_global_position().z + this->get_velocity().z));
}
m_AnimationTree->set("parameters/conditions/run", !_target_in_range());
m_AnimationTree->set("parameters/conditions/attack", _target_in_range());
move_and_slide();
}
bool Zombie::_target_in_range() {
return this->get_global_position().distance_to(m_Player->get_global_position()) < ATTACK_RANGE;
}
void Zombie::set_m_PlayerPath(const NodePath& path) {
m_PlayerPath = path;
}
NodePath Zombie::get_m_PlayerPath() const {
return m_PlayerPath;
}
void Zombie::hit_finished() {
if (this->get_global_position().distance_to(m_Player->get_global_position()) < (ATTACK_RANGE + 1.0)) {
Vector3 dir = this->get_global_position().direction_to(m_Player->get_global_position());
m_Player->hit(dir);
}
}
void Zombie::apply_damage(int dam) {
m_Health -= dam;
if (m_Health <= 0) {
m_AnimationTree->set("parameters/conditions/die", true);
this->set_velocity(Vector3(0, 0, 0));
m_Timer->set_wait_time(4.0);
m_Timer->start();
}
}
void Zombie::_on_area_3d_a_zombie_head_shot_hit(int dam) {
apply_damage(dam);
}
void Zombie::_on_area_3d_a_zombie_body_shot_hit(int dam) {
apply_damage(dam);
}
void Zombie::connect_collision_shapes(const std::vector<StringName>& body_parts) {
for (const StringName& part : body_parts) {
StringName area_name = String(part) + "Area3D";
BoneCollisionShape* collision_shape = Object::cast_to<BoneCollisionShape>(find_child(area_name));
DEV_ASSERT(collision_shape);
collision_shape->connect("a_ZombieBodyShotHit", Callable(this, "_on_area_3d_a_zombie_body_shot_hit"));
}
}
void Zombie::_on_timer_timeout() {
queue_free();
}
} // namespace blitz

51
src/Zombie.h Normal file
View File

@@ -0,0 +1,51 @@
#pragma once
#include "BoneCollisionShape.h"
#include "FirstPersonPlayer.h"
#include <godot_cpp/classes/animation_node_state_machine_playback.hpp>
#include <godot_cpp/classes/animation_tree.hpp>
#include <godot_cpp/classes/character_body3d.hpp>
#include <godot_cpp/classes/navigation_agent3d.hpp>
#include <godot_cpp/variant/node_path.hpp>
#include <godot_cpp/variant/string_name.hpp>
#include <godot_cpp/classes/timer.hpp>
namespace blitz {
class Zombie : public godot::CharacterBody3D {
GDCLASS(Zombie, godot::CharacterBody3D)
protected:
static void _bind_methods();
public:
Zombie();
~Zombie();
void _ready();
void _process(float delta);
bool _target_in_range();
void _on_area_3d_a_zombie_head_shot_hit(int dam);
void _on_area_3d_a_zombie_body_shot_hit(int dam);
private:
godot::NavigationAgent3D* m_NavigationAgent;
FirstPersonPlayer* m_Player;
godot::NodePath m_PlayerPath;
godot::Vector3 m_Velocity;
godot::AnimationTree* m_AnimationTree;
godot::AnimationNodeStateMachinePlayback* m_StateMachine;
BoneCollisionShape* m_HeadCollision;
std::vector<godot::StringName> m_BodyPartsCollision = {"Neck", "Spine", "Hips", "LeftArm", "LeftForearm", "LeftHand", "RightArm",
"RightForearm", "RightHand", "LeftUpLeg", "RightUpLeg", "LeftLeg", "RightLeg", "LeftFoot", "RightFoot"};
int m_Health = 6;
godot::Timer* m_Timer;
void set_m_PlayerPath(const godot::NodePath& path);
godot::NodePath get_m_PlayerPath() const;
void hit_finished();
void apply_damage(int dam);
void connect_collision_shapes(const std::vector<godot::StringName>& body_parts);
void _on_timer_timeout();
};
} // namespace blitz

View File

@@ -1,9 +1,12 @@
#include "register_types.h"
#include "BoneCollisionShape.h"
#include "Bullet.h"
#include "FirstPersonPlayer.h"
#include "Player.h"
#include "SpringArmPivot.h"
#include "World.h"
#include "Zombie.h"
#include <gdextension_interface.h>
#include <godot_cpp/core/defs.hpp>
@@ -20,6 +23,9 @@ void initialize_example_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<blitz::SpringArmPivot>();
ClassDB::register_class<blitz::FirstPersonPlayer>();
ClassDB::register_class<blitz::Bullet>();
ClassDB::register_class<blitz::Zombie>();
ClassDB::register_class<blitz::BoneCollisionShape>();
ClassDB::register_class<blitz::World>();
}
void uninitialize_example_module(ModuleInitializationLevel p_level) {