85 lines
1.8 KiB
C++
85 lines
1.8 KiB
C++
#include "actor_state_machine.h"
|
|
#include "core/config/engine.h"
|
|
#include "tabtargeting/actor_body.h"
|
|
|
|
void ActorState::_bind_methods() {
|
|
}
|
|
|
|
void ActorStateMachine::_bind_methods() {
|
|
}
|
|
|
|
void ActorStateMachine::_notification(int what) {
|
|
if(Engine::get_singleton()->is_editor_hint()) {
|
|
return;
|
|
}
|
|
switch(what) {
|
|
case NOTIFICATION_READY:
|
|
this->ready();
|
|
break;
|
|
case NOTIFICATION_PROCESS:
|
|
this->process(this->get_process_delta_time());
|
|
break;
|
|
default: break;
|
|
}
|
|
}
|
|
|
|
void ActorStateMachine::ready() {
|
|
this->set_process(true);
|
|
this->body = Object::cast_to<ActorBody>(this->get_parent());
|
|
this->add_state<IdleState>();
|
|
this->add_state<ActingState>();
|
|
this->current_state->state_entered();
|
|
}
|
|
|
|
void ActorStateMachine::process(double delta) {
|
|
this->current_state->process(delta);
|
|
this->try_next_state();
|
|
}
|
|
|
|
void ActorStateMachine::try_next_state() {
|
|
StringName name{this->current_state->next_state()};
|
|
if(name != this->current_state->get_class()) {
|
|
this->switch_state(this->states[name]);
|
|
}
|
|
}
|
|
|
|
void ActorStateMachine::switch_state(ActorState *state) {
|
|
this->current_state->state_exited();
|
|
this->current_state = state;
|
|
this->current_state->state_entered();
|
|
}
|
|
|
|
void IdleState::_bind_methods() {}
|
|
|
|
void IdleState::state_exited() {
|
|
print_line("state idle exited");
|
|
if(this->body->is_attack_requested()) {
|
|
this->body->_attack();
|
|
}
|
|
}
|
|
|
|
StringName IdleState::next_state() const {
|
|
if(this->body->is_attack_requested()) {
|
|
return ActingState::get_class_static();
|
|
}
|
|
return this->get_class_name();
|
|
}
|
|
|
|
void ActingState::_bind_methods() {}
|
|
|
|
void ActingState::state_entered() {
|
|
print_line("state acting exited");
|
|
this->timer = this->seconds_to_wait;
|
|
}
|
|
|
|
void ActingState::process(double delta) {
|
|
this->timer -= delta;
|
|
}
|
|
|
|
StringName ActingState::next_state() const {
|
|
if(this->timer <= 0) {
|
|
return IdleState::get_class_static();
|
|
}
|
|
return this->get_class();
|
|
}
|