aboutsummaryrefslogtreecommitdiff
path: root/game/workers
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2025-01-08 12:03:28 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2025-01-08 12:03:28 +0100
commitc9c9702edc58ff8f40b13dc6b86b216421f79e9b (patch)
tree3f3e710735105cbbd63ebd4f320c88fbc0ecfa0c /game/workers
parent8055d401fc7c553a7036336b4b2fb2fca99a5986 (diff)
parentb1e2df1e75e7fc8ea4c30b7deaa54a7acf02d951 (diff)
Merge branch 'max/game2' of github.com:lonkaars/crepe
Diffstat (limited to 'game/workers')
-rw-r--r--game/workers/CollisionScript.cpp68
-rw-r--r--game/workers/CollisionScript.h12
-rw-r--r--game/workers/PanicFromPlayerScript.cpp14
-rw-r--r--game/workers/WorkerScript.cpp29
-rw-r--r--game/workers/WorkersSubScene.cpp41
5 files changed, 146 insertions, 18 deletions
diff --git a/game/workers/CollisionScript.cpp b/game/workers/CollisionScript.cpp
new file mode 100644
index 0000000..deaf0ee
--- /dev/null
+++ b/game/workers/CollisionScript.cpp
@@ -0,0 +1,68 @@
+#include "CollisionScript.h"
+
+#include <crepe/api/Animator.h>
+#include <crepe/api/BehaviorScript.h>
+#include <crepe/api/Rigidbody.h>
+#include <crepe/api/Sprite.h>
+#include <crepe/api/Transform.h>
+#include <crepe/types.h>
+
+using namespace crepe;
+using namespace std;
+
+void CollisionScript::init() {
+ subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool {
+ return this->on_collision(ev);
+ });
+}
+
+bool CollisionScript::on_collision(const CollisionEvent & ev) {
+ RefVector<Animator> animators = this->get_components<Animator>();
+ RefVector<Sprite> sprites = this->get_components<Sprite>();
+ Rigidbody & rb = this->get_component<Rigidbody>();
+ Transform & tr = this->get_component<Transform>();
+ BehaviorScript & bs_panic = this->get_components<BehaviorScript>().front();
+
+ if (ev.info.other.metadata.tag == "zapper") {
+ for (Animator & anim : animators) {
+ anim.active = false;
+ anim.set_anim(3);
+ }
+ for (Sprite & sprite : sprites) {
+ sprite.data.position_offset.x = 15;
+ }
+ rb.data.linear_velocity_coefficient = {0.5, 0.5};
+ tr.rotation = 90;
+ bs_panic.active = false;
+
+ return false;
+ } else if (ev.info.other.metadata.tag == "laser") {
+ for (Animator & anim : animators) {
+ anim.active = false;
+ anim.set_anim(3);
+ }
+ for (Sprite & sprite : sprites) {
+ sprite.data.position_offset.x = 15;
+ }
+ rb.data.linear_velocity_coefficient = {0.5, 0.5};
+ tr.rotation = 90;
+ bs_panic.active = false;
+
+ return false;
+ } else if (ev.info.other.metadata.tag == "missile") {
+ for (Animator & anim : animators) {
+ anim.active = false;
+ anim.set_anim(3);
+ }
+ for (Sprite & sprite : sprites) {
+ sprite.data.position_offset.x = 15;
+ }
+ rb.data.linear_velocity_coefficient = {0.5, 0.5};
+ tr.rotation = 90;
+ bs_panic.active = false;
+
+ return false;
+ }
+
+ return false;
+}
diff --git a/game/workers/CollisionScript.h b/game/workers/CollisionScript.h
new file mode 100644
index 0000000..70c5fe1
--- /dev/null
+++ b/game/workers/CollisionScript.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <crepe/api/Event.h>
+#include <crepe/api/Script.h>
+
+class CollisionScript : public crepe::Script {
+public:
+ void init();
+
+private:
+ bool on_collision(const crepe::CollisionEvent & ev);
+};
diff --git a/game/workers/PanicFromPlayerScript.cpp b/game/workers/PanicFromPlayerScript.cpp
index 1e49aaa..baa48df 100644
--- a/game/workers/PanicFromPlayerScript.cpp
+++ b/game/workers/PanicFromPlayerScript.cpp
@@ -32,11 +32,21 @@ void PanicFromPlayerScript::fixed_update(duration_t dt) {
}
if (result_x < 0) {
- rb_worker.data.linear_velocity.x = 10000 * dt.count();
+ float min_value = 8000;
+ float max_value = 10000;
+ float value = min_value
+ + static_cast<float>(rand())
+ / (static_cast<float>(RAND_MAX / (max_value - min_value)));
+ rb_worker.data.linear_velocity.x = value * dt.count();
sprite_worker.front().get().data.flip.flip_x = false;
sprite_worker.back().get().data.flip.flip_x = false;
} else {
- rb_worker.data.linear_velocity.x = -5000 * dt.count();
+ float min_value = -4000;
+ float max_value = -5000;
+ float value = min_value
+ + static_cast<float>(rand())
+ / (static_cast<float>(RAND_MAX / (max_value - min_value)));
+ rb_worker.data.linear_velocity.x = value * dt.count();
sprite_worker.front().get().data.flip.flip_x = true;
sprite_worker.back().get().data.flip.flip_x = true;
}
diff --git a/game/workers/WorkerScript.cpp b/game/workers/WorkerScript.cpp
index 1bcf8d5..b0bfc4e 100644
--- a/game/workers/WorkerScript.cpp
+++ b/game/workers/WorkerScript.cpp
@@ -1,6 +1,7 @@
#include "WorkerScript.h"
#include "../Config.h"
+#include "api/BehaviorScript.h"
#include <crepe/api/Animator.h>
#include <crepe/api/Rigidbody.h>
@@ -44,6 +45,9 @@ void WorkerScript::fixed_update(duration_t dt) {
= this->get_components_by_id<Sprite>(trans_worker.game_object_id);
RefVector<Animator> animator_worker
= this->get_components_by_id<Animator>(trans_worker.game_object_id);
+ BehaviorScript & bs_panic
+ = this->get_components_by_id<BehaviorScript>(trans_worker.game_object_id)
+ .front();
if (rb_worker.data.linear_velocity.x < 0) {
sprite_worker.front().get().data.flip.flip_x = true;
@@ -55,6 +59,8 @@ void WorkerScript::fixed_update(duration_t dt) {
= -rb_worker.data.linear_velocity.x / 5;
animator_worker.front().get().set_anim(0);
animator_worker.back().get().set_anim(0);
+ animator_worker.front().get().active = true;
+ animator_worker.back().get().active = true;
} else {
sprite_worker.front().get().data.flip.flip_x = false;
sprite_worker.back().get().data.flip.flip_x = false;
@@ -65,6 +71,15 @@ void WorkerScript::fixed_update(duration_t dt) {
= rb_worker.data.linear_velocity.x / 5;
animator_worker.front().get().set_anim(0);
animator_worker.back().get().set_anim(0);
+ animator_worker.front().get().active = true;
+ animator_worker.back().get().active = true;
+ }
+
+ trans_worker.rotation = 0;
+ bs_panic.active = true;
+ rb_worker.data.linear_velocity_coefficient = {1, 1};
+ for (Sprite & sprite : sprite_worker) {
+ sprite.data.position_offset.x = 0;
}
}
} else {
@@ -84,6 +99,9 @@ void WorkerScript::fixed_update(duration_t dt) {
= this->get_components_by_id<Sprite>(trans_worker.game_object_id);
RefVector<Animator> animator_worker
= this->get_components_by_id<Animator>(trans_worker.game_object_id);
+ BehaviorScript & bs_panic
+ = this->get_components_by_id<BehaviorScript>(trans_worker.game_object_id)
+ .front();
if (rb_worker.data.linear_velocity.x < 0) {
sprite_worker.front().get().data.flip.flip_x = true;
@@ -96,6 +114,8 @@ void WorkerScript::fixed_update(duration_t dt) {
animator_worker.front().get().set_anim(0);
animator_worker.back().get().set_anim(0);
+ animator_worker.front().get().active = true;
+ animator_worker.back().get().active = true;
} else {
sprite_worker.front().get().data.flip.flip_x = false;
sprite_worker.back().get().data.flip.flip_x = false;
@@ -107,6 +127,15 @@ void WorkerScript::fixed_update(duration_t dt) {
animator_worker.front().get().set_anim(0);
animator_worker.back().get().set_anim(0);
+ animator_worker.front().get().active = true;
+ animator_worker.back().get().active = true;
+ }
+
+ trans_worker.rotation = 0;
+ bs_panic.active = true;
+ rb_worker.data.linear_velocity_coefficient = {1, 1};
+ for (Sprite & sprite : sprite_worker) {
+ sprite.data.position_offset.x = 0;
}
}
}
diff --git a/game/workers/WorkersSubScene.cpp b/game/workers/WorkersSubScene.cpp
index e5b4678..54996d1 100644
--- a/game/workers/WorkersSubScene.cpp
+++ b/game/workers/WorkersSubScene.cpp
@@ -1,4 +1,5 @@
#include "WorkersSubScene.h"
+#include "CollisionScript.h"
#include "PanicFromPlayerScript.h"
#include "WorkerScript.h"
@@ -34,7 +35,7 @@ void WorkersSubScene::worker1(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_1_body_sprite = worker_1.add_component<Sprite>(
Asset {"asset/workers/worker1Body.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_BACK,
.order_in_layer = 0,
.size = vec2(0, 50),
}
@@ -49,7 +50,7 @@ void WorkersSubScene::worker1(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_1_head_sprite = worker_1.add_component<Sprite>(
Asset {"asset/workers/worker1Head.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_BACK,
.order_in_layer = 1,
.size = vec2(0, 50),
.position_offset = vec2(0, -20),
@@ -70,6 +71,7 @@ void WorkersSubScene::worker1(crepe::Scene & scn, float start_x, float init_spee
.collision_layers = {COLL_LAY_BOT_TOP},
});
worker_1.add_component<BehaviorScript>().set_script<PanicFromPlayerScript>();
+ worker_1.add_component<BehaviorScript>().set_script<CollisionScript>();
if (init_speed < 0) {
worker_1_body_sprite.data.flip = Sprite::FlipSettings {true, false};
@@ -82,7 +84,7 @@ void WorkersSubScene::worker2(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_2_body_sprite = worker_2.add_component<Sprite>(
Asset {"asset/workers/worker2Body.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_FRONT,
.order_in_layer = 2,
.size = vec2(0, 50),
}
@@ -97,7 +99,7 @@ void WorkersSubScene::worker2(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_2_head_sprite = worker_2.add_component<Sprite>(
Asset {"asset/workers/worker1Head.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_FRONT,
.order_in_layer = 3,
.size = vec2(0, 50),
.position_offset = vec2(0, -20),
@@ -118,6 +120,7 @@ void WorkersSubScene::worker2(crepe::Scene & scn, float start_x, float init_spee
.collision_layers = {COLL_LAY_BOT_TOP},
});
worker_2.add_component<BehaviorScript>().set_script<PanicFromPlayerScript>();
+ worker_2.add_component<BehaviorScript>().set_script<CollisionScript>();
if (init_speed < 0) {
worker_2_body_sprite.data.flip = Sprite::FlipSettings {true, false};
@@ -130,7 +133,7 @@ void WorkersSubScene::worker3(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_3_body_sprite = worker_3.add_component<Sprite>(
Asset {"asset/workers/worker1Body.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_BACK,
.order_in_layer = 4,
.size = vec2(0, 50),
}
@@ -145,7 +148,7 @@ void WorkersSubScene::worker3(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_3_head_sprite = worker_3.add_component<Sprite>(
Asset {"asset/workers/worker2Head.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_BACK,
.order_in_layer = 5,
.size = vec2(0, 50),
.position_offset = vec2(0, -20),
@@ -166,6 +169,7 @@ void WorkersSubScene::worker3(crepe::Scene & scn, float start_x, float init_spee
.collision_layers = {COLL_LAY_BOT_TOP},
});
worker_3.add_component<BehaviorScript>().set_script<PanicFromPlayerScript>();
+ worker_3.add_component<BehaviorScript>().set_script<CollisionScript>();
if (init_speed < 0) {
worker_3_body_sprite.data.flip = Sprite::FlipSettings {true, false};
@@ -178,7 +182,7 @@ void WorkersSubScene::worker4(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_4_body_sprite = worker_4.add_component<Sprite>(
Asset {"asset/workers/worker2Body.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_FRONT,
.order_in_layer = 6,
.size = vec2(0, 50),
}
@@ -193,7 +197,7 @@ void WorkersSubScene::worker4(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_4_head_sprite = worker_4.add_component<Sprite>(
Asset {"asset/workers/worker2Head.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_FRONT,
.order_in_layer = 7,
.size = vec2(0, 50),
.position_offset = vec2(0, -20),
@@ -214,6 +218,7 @@ void WorkersSubScene::worker4(crepe::Scene & scn, float start_x, float init_spee
.collision_layers = {COLL_LAY_BOT_HIGH},
});
worker_4.add_component<BehaviorScript>().set_script<PanicFromPlayerScript>();
+ worker_4.add_component<BehaviorScript>().set_script<CollisionScript>();
if (init_speed < 0) {
worker_4_body_sprite.data.flip = Sprite::FlipSettings {true, false};
@@ -226,7 +231,7 @@ void WorkersSubScene::worker5(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_5_body_sprite = worker_5.add_component<Sprite>(
Asset {"asset/workers/workerFatBody.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_BACK,
.order_in_layer = 8,
.size = vec2(0, 50),
}
@@ -241,7 +246,7 @@ void WorkersSubScene::worker5(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_5_head_sprite = worker_5.add_component<Sprite>(
Asset {"asset/workers/worker1Head.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_BACK,
.order_in_layer = 9,
.size = vec2(0, 50),
.position_offset = vec2(0, -20),
@@ -262,6 +267,7 @@ void WorkersSubScene::worker5(crepe::Scene & scn, float start_x, float init_spee
.collision_layers = {COLL_LAY_BOT_HIGH},
});
worker_5.add_component<BehaviorScript>().set_script<PanicFromPlayerScript>();
+ worker_5.add_component<BehaviorScript>().set_script<CollisionScript>();
if (init_speed < 0) {
worker_5_body_sprite.data.flip = Sprite::FlipSettings {true, false};
@@ -274,7 +280,7 @@ void WorkersSubScene::worker6(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_6_body_sprite = worker_6.add_component<Sprite>(
Asset {"asset/workers/workerFatBody.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_FRONT,
.order_in_layer = 10,
.size = vec2(0, 50),
}
@@ -289,7 +295,7 @@ void WorkersSubScene::worker6(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_6_head_sprite = worker_6.add_component<Sprite>(
Asset {"asset/workers/worker2Head.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_FRONT,
.order_in_layer = 11,
.size = vec2(0, 50),
.position_offset = vec2(0, -20),
@@ -310,6 +316,7 @@ void WorkersSubScene::worker6(crepe::Scene & scn, float start_x, float init_spee
.collision_layers = {COLL_LAY_BOT_LOW},
});
worker_6.add_component<BehaviorScript>().set_script<PanicFromPlayerScript>();
+ worker_6.add_component<BehaviorScript>().set_script<CollisionScript>();
if (init_speed < 0) {
worker_6_body_sprite.data.flip = Sprite::FlipSettings {true, false};
@@ -322,7 +329,7 @@ void WorkersSubScene::worker7(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_7_body_sprite = worker_7.add_component<Sprite>(
Asset {"asset/workers/workerTallBody.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_BACK,
.order_in_layer = 12,
.size = vec2(0, 50),
}
@@ -337,7 +344,7 @@ void WorkersSubScene::worker7(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_7_head_sprite = worker_7.add_component<Sprite>(
Asset {"asset/workers/worker1Head.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_BACK,
.order_in_layer = 13,
.size = vec2(0, 50),
.position_offset = vec2(0, -20),
@@ -358,6 +365,7 @@ void WorkersSubScene::worker7(crepe::Scene & scn, float start_x, float init_spee
.collision_layers = {COLL_LAY_BOT_LOW},
});
worker_7.add_component<BehaviorScript>().set_script<PanicFromPlayerScript>();
+ worker_7.add_component<BehaviorScript>().set_script<CollisionScript>();
if (init_speed < 0) {
worker_7_body_sprite.data.flip = Sprite::FlipSettings {true, false};
@@ -370,7 +378,7 @@ void WorkersSubScene::worker8(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_8_body_sprite = worker_8.add_component<Sprite>(
Asset {"asset/workers/workerTallBody.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_FRONT,
.order_in_layer = 14,
.size = vec2(0, 50),
}
@@ -385,7 +393,7 @@ void WorkersSubScene::worker8(crepe::Scene & scn, float start_x, float init_spee
Sprite & worker_8_head_sprite = worker_8.add_component<Sprite>(
Asset {"asset/workers/worker2Head.png"},
Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_WORKERS,
+ .sorting_in_layer = SORT_IN_LAY_WORKERS_FRONT,
.order_in_layer = 15,
.size = vec2(0, 50),
.position_offset = vec2(0, -20),
@@ -406,6 +414,7 @@ void WorkersSubScene::worker8(crepe::Scene & scn, float start_x, float init_spee
.collision_layers = {COLL_LAY_BOT_LOW},
});
worker_8.add_component<BehaviorScript>().set_script<PanicFromPlayerScript>();
+ worker_8.add_component<BehaviorScript>().set_script<CollisionScript>();
if (init_speed < 0) {
worker_8_body_sprite.data.flip = Sprite::FlipSettings {true, false};