aboutsummaryrefslogtreecommitdiff
path: root/game/player/PlayerScript.cpp
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2025-01-08 18:40:11 +0100
committerJAROWMR <jarorutjes07@gmail.com>2025-01-08 18:40:11 +0100
commitef3f5b0845ba127c0bfcda8c2fce37013fef3878 (patch)
treee5690ff875a662ee7603844e858d82428d973a78 /game/player/PlayerScript.cpp
parent424da5eb1500d90389d939cd0b3e6e75d729578d (diff)
parent2ad15f3efab481659543a1c03cd70a36fd297538 (diff)
Merge branch 'master' of github.com:lonkaars/crepe into jaro/game
Diffstat (limited to 'game/player/PlayerScript.cpp')
-rw-r--r--game/player/PlayerScript.cpp40
1 files changed, 37 insertions, 3 deletions
diff --git a/game/player/PlayerScript.cpp b/game/player/PlayerScript.cpp
index d45a519..57819c0 100644
--- a/game/player/PlayerScript.cpp
+++ b/game/player/PlayerScript.cpp
@@ -1,9 +1,10 @@
#include "PlayerScript.h"
#include "../Config.h"
-
+#include "../enemy/BattleScript.h"
#include <crepe/api/Animator.h>
#include <crepe/api/AudioSource.h>
+#include <crepe/api/BoxCollider.h>
#include <crepe/api/ParticleEmitter.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Transform.h>
@@ -16,8 +17,8 @@ void PlayerScript::init() {
subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool {
return this->on_collision(ev);
});
+ this->last_fired = std::chrono::steady_clock::now();
}
-
bool PlayerScript::on_collision(const CollisionEvent & ev) {
BehaviorScript & play_scr = this->get_components_by_name<BehaviorScript>("player").front();
BehaviorScript & end_scr = this->get_components_by_name<BehaviorScript>("player").back();
@@ -59,7 +60,8 @@ bool PlayerScript::on_collision(const CollisionEvent & ev) {
audio.play();
return false;
- } else if (ev.info.other.metadata.tag == "missile") {
+ } else if (ev.info.other.metadata.tag == "missile"
+ || ev.info.other.metadata.tag == "enemy_bullet") {
for (Animator & anim : animators) {
anim.active = true;
anim.set_anim(5);
@@ -92,6 +94,15 @@ void PlayerScript::fixed_update(crepe::duration_t dt) {
}
Rigidbody & rb = this->get_components_by_name<Rigidbody>("player").front();
+ if (this->get_key_state(Keycode::ENTER)) {
+
+ auto now = std::chrono::steady_clock::now();
+ std::chrono::duration<float> elapsed = now - last_fired;
+ if (elapsed > shot_delay) {
+ this->shoot(transform.position, 0);
+ last_fired = now;
+ }
+ }
if (this->get_key_state(Keycode::SPACE)) {
rb.add_force_linear(vec2(0, -PLAYER_GRAVITY_SCALE / 2.5) * dt.count() / 0.02);
if (prev_anim != 1) {
@@ -139,3 +150,26 @@ void PlayerScript::fixed_update(crepe::duration_t dt) {
}
}
}
+
+void PlayerScript::shoot(const vec2 & location, float angle) {
+ RefVector<Transform> bullet_transforms
+ = this->get_components_by_tag<Transform>("player_bullet");
+
+ for (Transform & bullet_pos : bullet_transforms) {
+ if (bullet_pos.position.x == 0 && bullet_pos.position.y == -850) {
+
+ bullet_pos.position = location;
+ bullet_pos.position.x += 20;
+ Rigidbody & bullet_body
+ = this->get_components_by_id<Rigidbody>(bullet_pos.game_object_id).front();
+ BoxCollider bullet_collider
+ = this->get_components_by_id<BoxCollider>(bullet_pos.game_object_id).front();
+ bullet_body.active = true;
+ BehaviorScript & bullet_script
+ = this->get_components_by_id<BehaviorScript>(bullet_pos.game_object_id)
+ .front();
+ bullet_script.active = true;
+ return;
+ }
+ }
+}