diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2025-01-08 14:57:09 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2025-01-08 14:57:09 +0100 |
commit | 714c8798dd0998ea15b1ba697962a97c586457fe (patch) | |
tree | 86d36b17a71845cd4b5d16a0e9abd11df0d55c03 /game/player/PlayerBulletScript.cpp | |
parent | fbd7c84e13381922bf327e20c1abc65337142445 (diff) | |
parent | 0de6692dcb029540f4502c5a2f1a0c6634f7b61f (diff) |
Merge remote-tracking branch 'origin/wouter/enemyAI' into niels/game
Diffstat (limited to 'game/player/PlayerBulletScript.cpp')
-rw-r--r-- | game/player/PlayerBulletScript.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/game/player/PlayerBulletScript.cpp b/game/player/PlayerBulletScript.cpp new file mode 100644 index 0000000..a76b7eb --- /dev/null +++ b/game/player/PlayerBulletScript.cpp @@ -0,0 +1,41 @@ + +#include <crepe/api/Camera.h> +#include <crepe/api/Metadata.h> +#include <crepe/api/Rigidbody.h> + +#include "PlayerBulletScript.h" + +using namespace crepe; +using namespace std; +void PlayerBulletScript::init() { + this->subscribe<CollisionEvent>([this](const CollisionEvent & e) -> bool { + return this->on_collide(e); + }); +} +void PlayerBulletScript::fixed_update(crepe::duration_t dt) { + Transform & transform = this->get_component<Transform>(); + Camera & camera = this->get_components_by_name<Camera>("camera").front(); + Transform & cam_transform = this->get_components_by_name<Transform>("camera").front(); + Rigidbody & bullet_body = this->get_component<Rigidbody>(); + transform.rotation += bullet_body.data.angular_velocity; + transform.position += bullet_body.data.linear_velocity * dt.count(); + vec2 half_screen = camera.viewport_size / 2; + float despawn_location = cam_transform.position.x + half_screen.x + 50; + if (transform.position.x > despawn_location) { + this->despawn_bullet(); + } +} + +void PlayerBulletScript::despawn_bullet() { + Transform & transform = this->get_component<Transform>(); + Rigidbody & bullet_body = this->get_component<Rigidbody>(); + bullet_body.active = false; + BehaviorScript & bullet_script = this->get_component<BehaviorScript>(); + bullet_script.active = false; + transform.position = {0, -850}; +} + +bool PlayerBulletScript::on_collide(const CollisionEvent & e) { + this->despawn_bullet(); + return false; +} |