feat: first basic enemy patrolling behaviour

This commit is contained in:
Sara 2025-07-20 21:41:22 +02:00
parent 7c4c75d193
commit 526f756736
15 changed files with 801 additions and 386 deletions

View file

@ -3,21 +3,12 @@
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) {
if (this->current_state != nullptr) {
this->current_state->exit_state();
}
this->current_state = state;
if (this->current_state) {
if (this->current_state != nullptr) {
this->current_state->enter_state();
} else {
print_error("StateMachine::switch_to_state: New state is nullptr, StateMachine will now stop working");
@ -53,3 +44,18 @@ void StateMachine::_notification(int what) {
return;
}
}
StateMachine::~StateMachine() {
for (KeyValue<StringName, State *> kvp : this->states) {
memdelete(kvp.value);
}
}
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);
}
}