fix: state machines now handle being disabled
This commit is contained in:
		
							parent
							
								
									707c38894a
								
							
						
					
					
						commit
						5d0ae90fbc
					
				| 
						 | 
					@ -5,10 +5,12 @@ void StateMachine::_bind_methods() {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void StateMachine::switch_to_state(State *state) {
 | 
					void StateMachine::switch_to_state(State *state) {
 | 
				
			||||||
	if (this->current_state != nullptr) {
 | 
						if (this->current_state != nullptr) {
 | 
				
			||||||
 | 
							this->current_state_is_active = false;
 | 
				
			||||||
		this->current_state->exit_state();
 | 
							this->current_state->exit_state();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	this->current_state = state;
 | 
						this->current_state = state;
 | 
				
			||||||
	if (this->current_state != nullptr) {
 | 
						if (this->current_state != nullptr) {
 | 
				
			||||||
 | 
							this->current_state_is_active = true;
 | 
				
			||||||
		this->current_state->enter_state();
 | 
							this->current_state->enter_state();
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		print_error("StateMachine::switch_to_state: New state is nullptr, StateMachine will now stop working");
 | 
							print_error("StateMachine::switch_to_state: New state is nullptr, StateMachine will now stop working");
 | 
				
			||||||
| 
						 | 
					@ -38,6 +40,20 @@ void StateMachine::_notification(int what) {
 | 
				
			||||||
		case NOTIFICATION_PROCESS:
 | 
							case NOTIFICATION_PROCESS:
 | 
				
			||||||
			process(get_process_delta_time());
 | 
								process(get_process_delta_time());
 | 
				
			||||||
			return;
 | 
								return;
 | 
				
			||||||
 | 
							case NOTIFICATION_DISABLED:
 | 
				
			||||||
 | 
							case NOTIFICATION_EXIT_TREE:
 | 
				
			||||||
 | 
								if (this->current_state_is_active && this->current_state != nullptr) {
 | 
				
			||||||
 | 
									this->current_state_is_active = false;
 | 
				
			||||||
 | 
									this->current_state->exit_state();
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
 | 
							case NOTIFICATION_ENABLED:
 | 
				
			||||||
 | 
							case NOTIFICATION_ENTER_TREE:
 | 
				
			||||||
 | 
								if (!this->current_state_is_active && this->current_state != nullptr) {
 | 
				
			||||||
 | 
									this->current_state_is_active = true;
 | 
				
			||||||
 | 
									this->current_state->enter_state();
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,6 +19,7 @@ public:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
	State *current_state{ nullptr };
 | 
						State *current_state{ nullptr };
 | 
				
			||||||
 | 
						bool current_state_is_active{ false };
 | 
				
			||||||
	HashMap<StringName, State *> states{};
 | 
						HashMap<StringName, State *> states{};
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,7 +6,8 @@
 | 
				
			||||||
script/source = "extends EnemyWretched
 | 
					script/source = "extends EnemyWretched
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func _on_health_status_death() -> void:
 | 
					func _on_health_status_death() -> void:
 | 
				
			||||||
	%StateMachine.set_process(false)
 | 
						%StateMachine.process_mode = Node.PROCESS_MODE_DISABLED
 | 
				
			||||||
 | 
						%NavigationAgent3D.process_mode = Node.PROCESS_MODE_DISABLED
 | 
				
			||||||
	$wretched/AnimationPlayer.play(\"death\")
 | 
						$wretched/AnimationPlayer.play(\"death\")
 | 
				
			||||||
	$CollisionShape3D.disabled = true
 | 
						$CollisionShape3D.disabled = true
 | 
				
			||||||
	set_movement_direction(Vector2())
 | 
						set_movement_direction(Vector2())
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue