From b445a1716a46dc875e0b2180c1a1b6022ec7a6d3 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 8 Jan 2025 14:10:27 +0100 Subject: missile/preview/schedular/PreviewScene --- game/missile/MissileScript.cpp | 105 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 game/missile/MissileScript.cpp (limited to 'game/missile/MissileScript.cpp') diff --git a/game/missile/MissileScript.cpp b/game/missile/MissileScript.cpp new file mode 100644 index 0000000..6d0e40e --- /dev/null +++ b/game/missile/MissileScript.cpp @@ -0,0 +1,105 @@ +#include "MissileScript.h" +#include "../Config.h" +#include "api/BehaviorScript.h" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +using namespace crepe; + +void MissileScript::init() { + subscribe([this](const CollisionEvent & ev) -> bool { + return this->on_collision(ev); + }); + this->seeking_disabled = false; +} +void MissileScript::kill_missile() { + auto animations = this->get_components(); + auto sprites = this->get_components(); + auto & fly_sound = this->get_components().front().get(); + auto & this_script = this->get_components().back().get(); + + animations[0].get().active = false; + animations[1].get().active = false; + animations[2].get().active = true; + sprites[0].get().active = false; + sprites[1].get().active = false; + sprites[2].get().active = true; + + this_script.active = false; + this->seeking_disabled = false; + + fly_sound.stop(); +} +void MissileScript::activate() { + auto anim = this->get_components(); + auto sprites = this->get_components(); + + anim[0].get().active = true; + anim[1].get().active = true; + anim[2].get().stop(); + sprites[0].get().active = true; + sprites[1].get().active = true; + sprites[2].get().active = false; +} + +bool MissileScript::on_collision(const CollisionEvent & ev) { + auto & explosion_sound = this->get_components().back().get(); + + this->kill_missile(); + explosion_sound.play(); + + return false; +} + +bool MissileScript::is_in_x_range(const Transform & missile, const Transform & player) { + return fabs(missile.position.x - player.position.x) <= this->X_RANGE; +} + +void MissileScript::fixed_update(crepe::duration_t dt) { + auto & explosion_anim = this->get_components().back().get(); + auto & missile = this->get_component(); + auto & m_ai = this->get_component(); + + const auto & player = this->get_components_by_name("player").front().get(); + const auto & cam = this->get_components_by_name("camera").front().get(); + const auto & velocity = this->get_component().data.linear_velocity; + + if (missile.position.x < (cam.position.x - VIEWPORT_X / 1.8)) { + this->kill_missile(); + return; + } + + // check if animation is at the end + if (explosion_anim.data.row == 7) { + this->activate(); + this->seeking_disabled = false; + } + + if (this->seeking_disabled) { + m_ai.seek_off(); + } else { + m_ai.seek_target = player.position; + m_ai.seek_on(); + + if (is_in_x_range(missile, player)) { + this->seeking_disabled = true; + m_ai.seek_off(); + } + } + + vec2 angle_pos = velocity; + float angle = atan2(angle_pos.y, angle_pos.x) * (180 / M_PI); + + missile.rotation = angle; + missile.position += velocity * dt.count(); +} -- cgit v1.2.3