feat: defined state and state machine concepts

This commit is contained in:
Sara 2025-07-19 20:06:42 +02:00
parent 7c0b338f68
commit 7c4c75d193
5 changed files with 124 additions and 0 deletions

View file

@ -6,6 +6,8 @@
#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"
@ -22,6 +24,8 @@ void initialize_wave_survival_module(ModuleInitializationLevel p_level) {
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