diff options
author | Max-001 <maxsmits21@kpnmail.nl> | 2025-01-08 09:51:06 +0100 |
---|---|---|
committer | Max-001 <maxsmits21@kpnmail.nl> | 2025-01-08 09:51:06 +0100 |
commit | e9082de7d114ce024a884cac38c545c99de4026a (patch) | |
tree | ad10395021fed08e8143ea40b42bc9784ed4dc4e | |
parent | b8ff1dcd4e531c3c858ee09f705cf553e86ed7ff (diff) |
Finnished collision
-rw-r--r-- | game/CMakeLists.txt | 1 | ||||
-rw-r--r-- | game/GameScene.cpp | 6 | ||||
-rw-r--r-- | game/workers/CollisionScript.cpp | 48 | ||||
-rw-r--r-- | game/workers/PanicFromPlayerScript.cpp | 14 | ||||
-rw-r--r-- | game/workers/WorkerScript.cpp | 29 | ||||
-rw-r--r-- | game/workers/WorkersSubScene.cpp | 9 |
6 files changed, 90 insertions, 17 deletions
diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 0fb2424..ab770eb 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -25,6 +25,7 @@ add_executable(main workers/WorkersSubScene.cpp workers/WorkerScript.cpp workers/PanicFromPlayerScript.cpp + workers/CollisionScript.cpp main.cpp ) diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 02af8db..97f62e2 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -80,7 +80,7 @@ void GameScene::load_scene() { boom_audio.add_component<AudioSource>(boom_audio_asset); // zapper, laser and missile (below) for testing purpose only!!! - GameObject zapper = new_object("zapper", "zapper", vec2(1000, 0)); + GameObject zapper = new_object("zapper", "zapper", vec2(1000, 200)); Asset zapper_asset {"asset/obstacles/zapper/regular_zappers/zapEffect.png"}; Sprite & zapper_sprite = zapper.add_component<Sprite>( zapper_asset, @@ -96,7 +96,7 @@ void GameScene::load_scene() { .collision_layer = COLL_LAY_ZAPPER, }); zapper.add_component<BoxCollider>(vec2(100, 100)); - GameObject laser = new_object("laser", "laser", vec2(2000, 0)); + GameObject laser = new_object("laser", "laser", vec2(2000, 200)); Asset laser_asset {"asset/obstacles/laser/laserPower.png"}; Sprite & laser_sprite = laser.add_component<Sprite>( laser_asset, @@ -112,7 +112,7 @@ void GameScene::load_scene() { .collision_layer = COLL_LAY_LASER, }); laser.add_component<BoxCollider>(vec2(100, 100)); - GameObject missile = new_object("missile", "missile", vec2(4000, 0)); + GameObject missile = new_object("missile", "missile", vec2(4000, 200)); Asset missile_asset {"asset/obstacles/missile/missile.png"}; Sprite & missile_sprite = missile.add_component<Sprite>( missile_asset, diff --git a/game/workers/CollisionScript.cpp b/game/workers/CollisionScript.cpp index baa058b..deaf0ee 100644 --- a/game/workers/CollisionScript.cpp +++ b/game/workers/CollisionScript.cpp @@ -1,6 +1,11 @@ #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; @@ -13,31 +18,50 @@ void CollisionScript::init() { 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 = true; - anim.set_anim(4); - anim.data.looping = true; + 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 true; + return false; } else if (ev.info.other.metadata.tag == "laser") { for (Animator & anim : animators) { - anim.active = true; - anim.set_anim(4); - anim.data.looping = true; + 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 true; + return false; } else if (ev.info.other.metadata.tag == "missile") { for (Animator & anim : animators) { - anim.active = true; - anim.set_anim(5); - anim.data.looping = true; + 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 true; + return false; } return false; 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 215e3f2..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" @@ -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}; @@ -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}; @@ -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}; @@ -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}; @@ -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}; @@ -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}; @@ -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}; @@ -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}; |