Added zombie spawn animation, red screen and hit stagger when the player is hit.

This commit is contained in:
Morph01
2024-08-20 16:53:43 +02:00
parent 0c667944dd
commit d88cb9fbff
10 changed files with 260 additions and 113 deletions

View File

@@ -21,7 +21,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));
}

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

42
src/World.cpp Normal file
View File

@@ -0,0 +1,42 @@
#include "World.h"
#include "FirstPersonPlayer.h"
#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);
}
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));
}
void World::_on_first_person_player_hit() {
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);
}
} // namespace blitz

27
src/World.h Normal file
View File

@@ -0,0 +1,27 @@
#pragma once
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/color_rect.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;
void _on_timer_timeout();
void _on_first_person_player_hit();
};
} // namespace blitz

View File

@@ -1,6 +1,7 @@
#include "Zombie.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/core/math.hpp>
using namespace godot;
@@ -12,6 +13,7 @@ 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);
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "m_PlayerPath"), "set_m_PlayerPath", "get_m_PlayerPath");
}
@@ -73,4 +75,11 @@ void Zombie::set_m_PlayerPath(const NodePath& 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);
}
}
} // namespace blitz

View File

@@ -32,6 +32,7 @@ class Zombie : public godot::CharacterBody3D {
void set_m_PlayerPath(const godot::NodePath& path);
godot::NodePath get_m_PlayerPath() const;
void hit_finished();
};
} // namespace blitz

View File

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