feat: reworked enemy wretched logic
This commit is contained in:
		
							parent
							
								
									2cd1cba04f
								
							
						
					
					
						commit
						74aead946c
					
				| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#include "enemy_wretched.h"
 | 
					#include "enemy_wretched.h"
 | 
				
			||||||
 | 
					#include "scene/animation/animation_player.h"
 | 
				
			||||||
#include "wave_survival/npc_unit.h"
 | 
					#include "wave_survival/npc_unit.h"
 | 
				
			||||||
#include "wave_survival/patrol_path.h"
 | 
					#include "wave_survival/patrol_path.h"
 | 
				
			||||||
#include "wave_survival/player_body.h"
 | 
					#include "wave_survival/player_body.h"
 | 
				
			||||||
| 
						 | 
					@ -7,12 +8,19 @@
 | 
				
			||||||
void EnemyWretched::_bind_methods() {
 | 
					void EnemyWretched::_bind_methods() {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void EnemyWretched::ready() {
 | 
					void EnemyWretched::on_child_entered(Node *node) {
 | 
				
			||||||
	if (StateMachine * fsm{ cast_to<StateMachine>(get_node(NodePath("%StateMachine"))) }) {
 | 
						if (StateMachine * fsm{ cast_to<StateMachine>(node) }) {
 | 
				
			||||||
		fsm->add_state(memnew(WretchedPatrolState));
 | 
							this->fsm = fsm;
 | 
				
			||||||
		fsm->add_state(memnew(WretchedChaseState));
 | 
					 | 
				
			||||||
		fsm->add_state(memnew(WretchedAttackState));
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if (node->has_node(NodePath("AnimationPlayer"))) {
 | 
				
			||||||
 | 
							this->anim = cast_to<AnimationPlayer>(node->get_node(NodePath("AnimationPlayer")));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					void EnemyWretched::ready() {
 | 
				
			||||||
 | 
						print_line("ready");
 | 
				
			||||||
 | 
						fsm->add_state(memnew(WretchedPatrolState));
 | 
				
			||||||
 | 
						fsm->add_state(memnew(WretchedChaseState));
 | 
				
			||||||
 | 
						fsm->add_state(memnew(WretchedAttackState));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void EnemyWretched::_notification(int what) {
 | 
					void EnemyWretched::_notification(int what) {
 | 
				
			||||||
| 
						 | 
					@ -22,16 +30,21 @@ void EnemyWretched::_notification(int what) {
 | 
				
			||||||
	switch (what) {
 | 
						switch (what) {
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
			return;
 | 
								return;
 | 
				
			||||||
 | 
							case NOTIFICATION_ENTER_TREE:
 | 
				
			||||||
 | 
								connect("child_entered_tree", callable_mp(this, &self_type::on_child_entered));
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
		case NOTIFICATION_READY:
 | 
							case NOTIFICATION_READY:
 | 
				
			||||||
			ready();
 | 
								ready();
 | 
				
			||||||
			return;
 | 
								return;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					AnimationPlayer *EnemyWretched::get_anim() const {
 | 
				
			||||||
 | 
						return this->anim;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void WretchedState::set_target(Node *node) {
 | 
					void WretchedState::set_target(Node *node) {
 | 
				
			||||||
	this->target = cast_to<EnemyWretched>(node);
 | 
						this->target = cast_to<EnemyWretched>(node);
 | 
				
			||||||
	this->nav = this->target->get_nav();
 | 
					 | 
				
			||||||
	this->unit = this->target->get_unit();
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
EnemyWretched *WretchedState::get_target() const {
 | 
					EnemyWretched *WretchedState::get_target() const {
 | 
				
			||||||
| 
						 | 
					@ -39,11 +52,15 @@ EnemyWretched *WretchedState::get_target() const {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
NpcUnit *WretchedState::get_unit() const {
 | 
					NpcUnit *WretchedState::get_unit() const {
 | 
				
			||||||
	return this->unit;
 | 
						return this->target->get_unit();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
NavigationAgent3D *WretchedState::get_nav() const {
 | 
					NavigationAgent3D *WretchedState::get_nav() const {
 | 
				
			||||||
	return this->nav;
 | 
						return this->target->get_nav();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					AnimationPlayer *WretchedState::get_anim() const {
 | 
				
			||||||
 | 
						return this->target->get_anim();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void WretchedPatrolState::set_patrol_target(Vector3 path_point) {
 | 
					void WretchedPatrolState::set_patrol_target(Vector3 path_point) {
 | 
				
			||||||
| 
						 | 
					@ -85,6 +102,7 @@ void WretchedChaseState::enter_state() {
 | 
				
			||||||
	get_target()->set_movement_speed(get_unit()->get_patrol_speed() * 2.f);
 | 
						get_target()->set_movement_speed(get_unit()->get_patrol_speed() * 2.f);
 | 
				
			||||||
	get_nav()->set_max_speed(get_unit()->get_patrol_speed() * 2.f);
 | 
						get_nav()->set_max_speed(get_unit()->get_patrol_speed() * 2.f);
 | 
				
			||||||
	get_nav()->set_target_position(PlayerBody::get_singleton()->get_global_position());
 | 
						get_nav()->set_target_position(PlayerBody::get_singleton()->get_global_position());
 | 
				
			||||||
 | 
						get_anim()->play("ready"); // TODO: replace this with "run"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void WretchedChaseState::process(double delta) {
 | 
					void WretchedChaseState::process(double delta) {
 | 
				
			||||||
| 
						 | 
					@ -101,6 +119,13 @@ String WretchedChaseState::get_next_state() const {
 | 
				
			||||||
	return get_class();
 | 
						return get_class();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
String WretchedAttackState::get_next_state() const {
 | 
					void WretchedAttackState::enter_state() {
 | 
				
			||||||
	return WretchedChaseState::get_class_static();
 | 
						get_anim()->play("attack");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					String WretchedAttackState::get_next_state() const {
 | 
				
			||||||
 | 
						if (get_anim()->get_current_animation().is_empty()) {
 | 
				
			||||||
 | 
							return WretchedChaseState::get_class_static();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return get_class();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,14 +5,24 @@
 | 
				
			||||||
#include "wave_survival/state.h"
 | 
					#include "wave_survival/state.h"
 | 
				
			||||||
class PatrolPath;
 | 
					class PatrolPath;
 | 
				
			||||||
class NpcUnit;
 | 
					class NpcUnit;
 | 
				
			||||||
 | 
					class AnimationPlayer;
 | 
				
			||||||
 | 
					class StateMachine;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class EnemyWretched : public EnemyBody {
 | 
					class EnemyWretched : public EnemyBody {
 | 
				
			||||||
	GDCLASS(EnemyWretched, EnemyBody);
 | 
						GDCLASS(EnemyWretched, EnemyBody);
 | 
				
			||||||
	static void _bind_methods();
 | 
						static void _bind_methods();
 | 
				
			||||||
 | 
						void on_child_entered(Node *node);
 | 
				
			||||||
	void ready();
 | 
						void ready();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
protected:
 | 
					protected:
 | 
				
			||||||
	void _notification(int what);
 | 
						void _notification(int what);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
						AnimationPlayer *get_anim() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
						StateMachine *fsm{ nullptr };
 | 
				
			||||||
 | 
						AnimationPlayer *anim{ nullptr };
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* ============================== STATES ============================== */
 | 
					/* ============================== STATES ============================== */
 | 
				
			||||||
| 
						 | 
					@ -26,11 +36,10 @@ public:
 | 
				
			||||||
	EnemyWretched *get_target() const;
 | 
						EnemyWretched *get_target() const;
 | 
				
			||||||
	NpcUnit *get_unit() const;
 | 
						NpcUnit *get_unit() const;
 | 
				
			||||||
	NavigationAgent3D *get_nav() const;
 | 
						NavigationAgent3D *get_nav() const;
 | 
				
			||||||
 | 
						AnimationPlayer *get_anim() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
	NpcUnit *unit{ nullptr };
 | 
					 | 
				
			||||||
	EnemyWretched *target{ nullptr };
 | 
						EnemyWretched *target{ nullptr };
 | 
				
			||||||
	NavigationAgent3D *nav{ nullptr };
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class WretchedPatrolState : public WretchedState {
 | 
					class WretchedPatrolState : public WretchedState {
 | 
				
			||||||
| 
						 | 
					@ -63,6 +72,7 @@ class WretchedAttackState : public WretchedState {
 | 
				
			||||||
	static void _bind_methods() {}
 | 
						static void _bind_methods() {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
 | 
						virtual void enter_state() override;
 | 
				
			||||||
	virtual String get_next_state() const override;
 | 
						virtual String get_next_state() const override;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue