feat: initialized template and wrote first gameplay code

This commit is contained in:
Sara 2025-07-14 23:03:04 +02:00
parent 82f2cae0f6
commit 46a958f801
47 changed files with 1093 additions and 33 deletions

View file

@ -0,0 +1,4 @@
Import('env')
env.add_source_files(env.modules_sources, "*.cpp")
env.add_source_files(env.modules_sources, "weapons/*.cpp")

View file

@ -0,0 +1,5 @@
def can_build(env, platform):
return True;
def configure(env):
pass;

View file

@ -0,0 +1,27 @@
#ifndef GODOT_EXTRA_MACROS_H
#define GODOT_EXTRA_MACROS_H
#define BIND_GET_SET(m_property) \
ClassDB::bind_method(D_METHOD("set_" #m_property, #m_property), \
&self_type::set_##m_property); \
ClassDB::bind_method(D_METHOD("get_" #m_property), \
&self_type::get_##m_property)
#define BIND_HPROPERTY(m_type, m_property, ...) \
BIND_GET_SET(m_property); \
ADD_PROPERTY(PropertyInfo(m_type, #m_property, __VA_ARGS__), \
"set_" #m_property, "get_" #m_property)
#define BIND_PROPERTY(m_type, m_property) \
BIND_GET_SET(m_property); \
ADD_PROPERTY(PropertyInfo(m_type, #m_property), "set_" #m_property, \
"get_" #m_property)
#define GETSET(m_value, m_block) \
{ \
auto m_value{ get_##m_value() }; \
m_block \
set_##m_value(m_value); \
}
#endif // !GODOT_EXTRA_MACROS_H

View file

@ -0,0 +1,89 @@
#include "player_body.h"
#include "macros.h"
#include "player_input.h"
PlayerBody *PlayerBody::singleton_instance{ nullptr };
void PlayerBody::_bind_methods() {
}
void PlayerBody::ready() {
PlayerInput *input{ cast_to<PlayerInput>(get_node(NodePath("%PlayerInput"))) };
input->connect(PlayerInput::sig_movement_input, callable_mp(this, &self_type::set_movement_input));
input->connect(PlayerInput::sig_look_input, callable_mp(this, &self_type::on_look_input));
input->connect(PlayerInput::sig_jump, callable_mp(this, &self_type::on_jump_input));
input->connect(PlayerInput::sig_run, callable_mp(this, &self_type::on_run_input));
}
void PlayerBody::process(double delta) {
}
void PlayerBody::physics_process(double delta) {
GETSET(velocity, {
Vector2 input{ this->movement_input.normalized() };
if (get_is_running()) {
input.y *= this->run_speed;
input.x *= this->walk_speed;
} else {
input *= this->walk_speed;
}
velocity = velocity.move_toward(Vector3{ input.y * get_global_basis().get_column(2) + input.x * get_global_basis().get_column(0) } + Vector3{ 0.f, velocity.y, 0.f }, delta * this->acceleration);
velocity += get_gravity() * delta;
});
move_and_slide();
}
void PlayerBody::set_movement_input(Vector2 state) {
this->movement_input = state;
}
void PlayerBody::on_look_input(Vector2 look) {
rotate_y(look.x);
}
void PlayerBody::on_jump_input() {
if (this->is_on_floor()) {
GETSET(velocity, {
velocity.y = this->jump_strength;
});
}
}
void PlayerBody::on_run_input(bool run) {
this->try_running = run;
}
void PlayerBody::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return; // don't run in editor
}
switch (what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
singleton_instance = this;
return;
case NOTIFICATION_EXIT_TREE:
singleton_instance = nullptr;
return;
case NOTIFICATION_READY:
set_process(true);
set_physics_process(true);
ready();
return;
case NOTIFICATION_PROCESS:
process(get_process_delta_time());
return;
case NOTIFICATION_PHYSICS_PROCESS:
physics_process(get_physics_process_delta_time());
return;
}
}
PlayerBody *PlayerBody::get_singleton() {
return singleton_instance;
}
bool PlayerBody::get_is_running() const {
return this->try_running && this->movement_input.y > 0.f;
}

View file

@ -0,0 +1,37 @@
#ifndef PLAYER_BODY_H
#define PLAYER_BODY_H
#include "scene/3d/physics/character_body_3d.h"
class PlayerBody : public CharacterBody3D {
GDCLASS(PlayerBody, CharacterBody3D);
static void _bind_methods();
static PlayerBody *singleton_instance;
private:
void ready();
void process(double delta);
void physics_process(double delta);
void set_movement_input(Vector2 state);
void on_look_input(Vector2 look);
void on_jump_input();
void on_run_input(bool run);
protected:
void _notification(int what);
public:
static PlayerBody *get_singleton();
bool get_is_running() const;
private:
bool try_running{ false };
float walk_speed{ 7.f };
float run_speed{ 10.f };
float acceleration{ 40.f };
float jump_strength{ 3.5f };
Vector2 movement_input{};
};
#endif // !PLAYER_BODY_H

View file

@ -0,0 +1,38 @@
#include "player_camera.h"
#include "macros.h"
#include "player_input.h"
void PlayerCamera::_bind_methods() {
}
void PlayerCamera::on_look_input(Vector2 input) {
GETSET(rotation, {
rotation.x = CLAMP(rotation.x + input.y, -Math::PI / 2.0, Math::PI / 2.0);
});
}
void PlayerCamera::update_fov() {
if (!Engine::get_singleton()->is_editor_hint() && is_ready()) {
set_fov(this->base_fov * this->fov_factor);
}
}
void PlayerCamera::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
if (what == NOTIFICATION_READY) {
PlayerInput *input{ cast_to<PlayerInput>(get_node(NodePath("%PlayerInput"))) };
input->connect(PlayerInput::sig_look_input, callable_mp(this, &self_type::on_look_input));
this->base_fov = get_fov();
}
}
void PlayerCamera::set_fov_factor(float value) {
this->fov_factor = value;
update_fov();
}
float PlayerCamera::get_fov_factor() const {
return this->fov_factor;
}

View file

@ -0,0 +1,24 @@
#ifndef PLAYER_CAMERA_H
#define PLAYER_CAMERA_H
#include "scene/3d/camera_3d.h"
class PlayerCamera : public Camera3D {
GDCLASS(PlayerCamera, Camera3D);
static void _bind_methods();
void on_look_input(Vector2 input);
void update_fov();
protected:
void _notification(int what);
public:
void set_fov_factor(float value);
float get_fov_factor() const;
private:
float base_fov{ 60.f };
float fov_factor{ 1.0f };
};
#endif // !PLAYER_CAMERA_H

View file

@ -0,0 +1,60 @@
#include "player_input.h"
String PlayerInput::sig_movement_input{ "movement_input" };
String PlayerInput::sig_look_input{ "look_input" };
String PlayerInput::sig_primary_fire{ "primary_fire" };
String PlayerInput::sig_alt_mode{ "alt_mode" };
String PlayerInput::sig_run{ "run" };
String PlayerInput::sig_jump{ "jump" };
String PlayerInput::sig_crouch{ "crouch" };
void PlayerInput::_bind_methods() {
ADD_SIGNAL(MethodInfo(sig_movement_input, PropertyInfo(Variant::VECTOR2, "axes")));
ADD_SIGNAL(MethodInfo(sig_look_input, PropertyInfo(Variant::VECTOR2, "axes")));
ADD_SIGNAL(MethodInfo(sig_primary_fire, PropertyInfo(Variant::BOOL, "is_pressed")));
ADD_SIGNAL(MethodInfo(sig_alt_mode, PropertyInfo(Variant::BOOL, "is_active")));
ADD_SIGNAL(MethodInfo(sig_run, PropertyInfo(Variant::BOOL, "is_running")));
ADD_SIGNAL(MethodInfo(sig_jump));
ADD_SIGNAL(MethodInfo(sig_crouch, PropertyInfo(Variant::BOOL, "is_crouching")));
}
void PlayerInput::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
if (what == NOTIFICATION_READY) {
set_process_unhandled_input(true);
Input::get_singleton()->set_mouse_mode(Input::MouseMode::MOUSE_MODE_CAPTURED);
}
}
void PlayerInput::unhandled_input(Ref<InputEvent> const &event) {
Input *input{ Input::get_singleton() };
if (event->is_action("move_left") || event->is_action("move_right") || event->is_action("move_forward") || event->is_action("move_back")) {
Vector2 state{
input->get_axis("move_right", "move_left"),
input->get_axis("move_back", "move_forward")
};
emit_signal(sig_movement_input, state);
}
Ref<InputEventMouseMotion> mouse_motion{ event };
if (mouse_motion.is_valid()) {
Vector2 state{ -mouse_motion->get_relative() * 0.001f };
emit_signal(sig_look_input, state);
}
if (event->is_action("primary_fire")) {
bool state{ event->is_pressed() };
emit_signal(sig_primary_fire, state);
}
if (event->is_action("alt_mode")) {
bool state{ event->is_pressed() };
emit_signal(sig_alt_mode, state);
}
if (event->is_action("run")) {
bool state{ event->is_pressed() };
emit_signal(sig_run, state);
}
if (event->is_pressed() && event->is_action("jump") && input->is_action_just_pressed("jump")) {
emit_signal(sig_jump);
}
}

View file

@ -0,0 +1,22 @@
#ifndef PLAYER_INPUT_H
#define PLAYER_INPUT_H
#include "scene/main/node.h"
class PlayerInput : public Node {
GDCLASS(PlayerInput, Node);
static void _bind_methods();
void _notification(int what);
virtual void unhandled_input(Ref<InputEvent> const &event) override;
public:
static String sig_movement_input;
static String sig_look_input;
static String sig_primary_fire;
static String sig_alt_mode;
static String sig_run;
static String sig_jump;
static String sig_crouch;
};
#endif // !PLAYER_INPUT_H

View file

@ -0,0 +1,25 @@
#include "register_types.h"
#include "core/object/class_db.h"
#include "wave_survival/player_body.h"
#include "wave_survival/player_camera.h"
#include "wave_survival/player_input.h"
#include "wave_survival/weapon_base.h"
#include "wave_survival/weapons/rifle.h"
void initialize_wave_survival_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
GDREGISTER_CLASS(PlayerBody);
GDREGISTER_CLASS(PlayerInput);
GDREGISTER_CLASS(PlayerCamera);
GDREGISTER_RUNTIME_CLASS(WeaponBase);
GDREGISTER_CLASS(Rifle);
}
void uninitialize_wave_survival_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
}

View file

@ -0,0 +1,9 @@
#ifndef wave_survival_REGISTER_TYPES_H
#define wave_survival_REGISTER_TYPES_H
#include "modules/register_module_types.h"
void initialize_wave_survival_module(ModuleInitializationLevel p_level);
void uninitialize_wave_survival_module(ModuleInitializationLevel p_level);
#endif // !wave_survival_REGISTER_TYPES_H

View file

@ -0,0 +1,39 @@
#include "weapon_base.h"
#include "macros.h"
#include "player_camera.h"
#include "scene/animation/animation_player.h"
void WeaponBase::_bind_methods() {
BIND_HPROPERTY(Variant::OBJECT, anim, PROPERTY_HINT_NODE_TYPE, AnimationPlayer::get_class_static());
}
void WeaponBase::ready() {
this->camera = cast_to<PlayerCamera>(get_node(NodePath("%PlayerCamera")));
if (this->anim) {
this->anim->play("RESET");
}
}
void WeaponBase::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
ready();
}
}
void WeaponBase::set_anim(AnimationPlayer *anim) {
this->anim = anim;
}
AnimationPlayer *WeaponBase::get_anim() const {
return this->anim;
}
PlayerCamera *WeaponBase::get_camera() const {
return this->camera;
}

View file

@ -0,0 +1,27 @@
#ifndef WEAPON_BASE_H
#define WEAPON_BASE_H
#include "scene/3d/node_3d.h"
class AnimationPlayer;
class PlayerCamera;
class WeaponBase : public Node3D {
GDCLASS(WeaponBase, Node3D);
static void _bind_methods();
void ready();
protected:
void _notification(int what);
public:
void set_anim(AnimationPlayer *player);
AnimationPlayer *get_anim() const;
PlayerCamera *get_camera() const;
private:
AnimationPlayer *anim{ nullptr };
PlayerCamera *camera{ nullptr };
};
#endif // !WEAPON_BASE_H

View file

@ -0,0 +1,76 @@
#include "rifle.h"
#include "scene/animation/animation_player.h"
#include "wave_survival/player_body.h"
#include "wave_survival/player_input.h"
void Rifle::_bind_methods() {}
void Rifle::on_primary_fire(bool pressed) {
if (pressed && get_anim()->get_queue().size() == 0) {
if (this->request_alt_mode) {
get_anim()->queue("fire_aim");
} else if (get_anim()->get_current_animation() == "") {
get_anim()->queue("fire_hip");
}
}
}
void Rifle::on_alt_mode(bool alt_mode) {
this->request_alt_mode = alt_mode;
}
void Rifle::on_animation_changed(String new_animation) {
if (new_animation == "aim") {
this->in_alt_mode = true;
get_camera()->set_fov_factor(this->ads_factor);
} else if (new_animation == "RESET") {
this->in_alt_mode = false;
get_camera()->set_fov_factor(1.f);
}
}
void Rifle::ready() {
get_anim()->connect("current_animation_changed", callable_mp(this, &self_type::on_animation_changed));
PlayerInput *input{ cast_to<PlayerInput>(get_node(NodePath("%PlayerInput"))) };
input->connect(PlayerInput::sig_primary_fire, callable_mp(this, &self_type::on_primary_fire));
input->connect(PlayerInput::sig_alt_mode, callable_mp(this, &self_type::on_alt_mode));
get_anim()->animation_set_next("hip_to_aim", "aim");
get_anim()->animation_set_next("fire_aim", "aim");
get_anim()->animation_set_next("aim_to_hip", "RESET");
get_anim()->animation_set_next("fire_hip", "RESET");
}
void Rifle::process(double delta) {
String const current{ get_anim()->get_current_animation() };
float const progress{ float(CLAMP(get_anim()->get_current_animation_position() / get_anim()->get_current_animation_length(), 0.0, 1.0)) };
if (current == "hip_to_aim") {
get_camera()->set_fov_factor(Math::lerp(1.f, this->ads_factor, progress));
} else if (current == "aim_to_hip") {
get_camera()->set_fov_factor(Math::lerp(this->ads_factor, 1.f, progress));
} else if (this->request_alt_mode != this->in_alt_mode && current.is_empty()) {
get_anim()->clear_queue();
if (this->request_alt_mode) {
get_anim()->queue("hip_to_aim");
} else {
get_anim()->queue("aim_to_hip");
}
}
}
void Rifle::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
set_process(true);
ready();
return;
case NOTIFICATION_PROCESS:
process(get_process_delta_time());
return;
}
}

View file

@ -0,0 +1,26 @@
#ifndef WEAPONS_RIFLE_H
#define WEAPONS_RIFLE_H
#include "wave_survival/player_camera.h"
#include "wave_survival/weapon_base.h"
class PlayerBody;
class Rifle : public WeaponBase {
GDCLASS(Rifle, WeaponBase);
static void _bind_methods();
void on_primary_fire(bool down);
void on_alt_mode(bool alt_mode);
void on_animation_changed(String new_anim);
void ready();
void process(double delta);
public:
void _notification(int what);
private:
float ads_factor{ 0.5f };
bool request_alt_mode{ false };
bool in_alt_mode{ false };
};
#endif // !WEAPONS_RIFLE_H