Compare commits

..

3 commits

Author SHA1 Message Date
Sara 7c4c75d193 feat: defined state and state machine concepts 2025-07-19 20:06:45 +02:00
Sara 7c0b338f68 feat: added health status class to ClassDB 2025-07-19 20:05:03 +02:00
Sara 144e18b2d0 fix: look_at normal and up aligning 2025-07-19 20:04:52 +02:00
6 changed files with 128 additions and 1 deletions

View file

@ -39,7 +39,8 @@ void HitscanMuzzle::shoot() {
get_tree()->get_current_scene()->add_child(effect);
Vector3 const point{ get_collision_point() };
Vector3 const normal{ get_collision_normal() };
effect->look_at_from_position(point, point + normal);
Vector3 const position{ get_global_position() };
effect->look_at_from_position(point, point + normal, (point - position).normalized());
} else if (effect_as_node) {
effect_as_node->queue_free();
}

View file

@ -1,10 +1,13 @@
#include "register_types.h"
#include "core/object/class_db.h"
#include "wave_survival/health_status.h"
#include "wave_survival/hitscan_muzzle.h"
#include "wave_survival/player_body.h"
#include "wave_survival/player_camera.h"
#include "wave_survival/player_input.h"
#include "wave_survival/state.h"
#include "wave_survival/state_machine.h"
#include "wave_survival/weapon_base.h"
#include "wave_survival/weapon_inventory.h"
#include "wave_survival/weapons/rifle.h"
@ -20,6 +23,9 @@ void initialize_wave_survival_module(ModuleInitializationLevel p_level) {
GDREGISTER_CLASS(Rifle);
GDREGISTER_CLASS(WeaponInventory);
GDREGISTER_CLASS(HitscanMuzzle);
GDREGISTER_CLASS(HealthStatus);
GDREGISTER_CLASS(StateMachine);
GDREGISTER_CLASS(State);
}
void uninitialize_wave_survival_module(ModuleInitializationLevel p_level) {

View file

@ -0,0 +1,16 @@
#include "state.h"
void State::_bind_methods() {
}
String State::get_next_state() const {
return this->get_class();
}
void State::set_state_machine(StateMachine *machine) {
this->state_machine = machine;
}
StateMachine *State::get_state_machine() const {
return this->state_machine;
}

View file

@ -0,0 +1,26 @@
#ifndef STATE_H
#define STATE_H
#include "core/object/class_db.h"
#include "core/object/object.h"
class StateMachine;
class Node;
class State : public Object {
GDCLASS(State, Object);
static void _bind_methods();
public:
virtual void set_target(Node *target) {}
virtual void enter_state() {}
virtual void exit_state() {}
virtual void process(double delta) {}
virtual String get_next_state() const;
void set_state_machine(StateMachine *machine);
StateMachine *get_state_machine() const;
private:
StateMachine *state_machine{ nullptr };
};
#endif // !STATE_H

View file

@ -0,0 +1,55 @@
#include "state_machine.h"
#include "state.h"
void StateMachine::_bind_methods() {}
void StateMachine::add_state(State *state) {
state->set_state_machine(this);
state->set_target(this->get_parent());
this->states.insert(state->get_class(), state);
if (this->current_state == nullptr) {
this->switch_to_state(state);
}
}
void StateMachine::switch_to_state(State *state) {
if (this->current_state) {
this->current_state->exit_state();
}
this->current_state = state;
if (this->current_state) {
this->current_state->enter_state();
} else {
print_error("StateMachine::switch_to_state: New state is nullptr, StateMachine will now stop working");
}
}
void StateMachine::ready() {
}
void StateMachine::process(double delta) {
if (this->current_state) {
this->current_state->process(delta);
String new_state{ this->current_state->get_next_state() };
if (new_state != this->current_state->get_class()) {
this->switch_to_state(this->states[new_state]);
}
}
}
void StateMachine::_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,23 @@
#ifndef STATE_MACHINE_H
#define STATE_MACHINE_H
#include "scene/main/node.h"
class State;
class StateMachine : public Node {
GDCLASS(StateMachine, Node);
static void _bind_methods();
void add_state(State *state);
void switch_to_state(State *state);
void ready();
void process(double delta);
protected:
void _notification(int what);
private:
State *current_state{ nullptr };
HashMap<StringName, State *> states{};
};
#endif // !STATE_MACHINE_H