diff options
author | Max-001 <maxsmits21@kpnmail.nl> | 2025-01-07 13:21:09 +0100 |
---|---|---|
committer | Max-001 <maxsmits21@kpnmail.nl> | 2025-01-07 13:21:09 +0100 |
commit | 80fc064c77bb61d9b329b58532c9cacd99038e0a (patch) | |
tree | 35f3a52350294801f8bcee2b66befba5df2ec695 | |
parent | a2d9f875a219d6d1c53784a307b4915cc4e1ee14 (diff) |
First setup for NPCs
-rw-r--r-- | game/CMakeLists.txt | 2 | ||||
-rw-r--r-- | game/Config.h | 1 | ||||
-rw-r--r-- | game/GameScene.cpp | 3 | ||||
-rw-r--r-- | game/workers/WorkerScript.cpp | 106 | ||||
-rw-r--r-- | game/workers/WorkerScript.h | 8 | ||||
-rw-r--r-- | game/workers/WorkersSubScene.cpp | 405 | ||||
-rw-r--r-- | game/workers/WorkersSubScene.h | 20 |
7 files changed, 545 insertions, 0 deletions
diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 0b85b96..8a63bd0 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -22,6 +22,8 @@ add_executable(main player/PlayerEndScript.cpp player/PlayerAudioScript.cpp background/StartSubScene.cpp + workers/WorkersSubScene.cpp + workers/WorkerScript.cpp main.cpp ) diff --git a/game/Config.h b/game/Config.h index d6f8349..ea95a90 100644 --- a/game/Config.h +++ b/game/Config.h @@ -6,6 +6,7 @@ static constexpr int SORT_IN_LAY_FORE_BACKGROUND = 5; // For all scenes static constexpr int SORT_IN_LAY_PARTICLES_BACKGROUND = 6; // For all scenes static constexpr int SORT_IN_LAY_OBSTACLES = 8; // Only for GameScene static constexpr int SORT_IN_LAY_PLAYER = 10; // Only for GameScene +static constexpr int SORT_IN_LAY_WORKERS = 12; // Only for GameScene static constexpr int SORT_IN_LAY_PARTICLES_FOREGROUND = 15; // Only for GameScene static constexpr int COLL_LAY_BOT_TOP = 1; // Only for GameScene diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 2b6b051..02af8db 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -5,6 +5,7 @@ #include "background/BackgroundSubScene.h" #include "player/PlayerSubScene.h" +#include "workers/WorkersSubScene.h" #include <cmath> #include <crepe/api/Animator.h> @@ -41,6 +42,8 @@ void GameScene::load_scene() { PlayerSubScene player(*this); + WorkersSubScene workers(*this); + GameObject floor = new_object("floor", "game_world", vec2(0, 325)); floor.add_component<Rigidbody>(Rigidbody::Data { .body_type = Rigidbody::BodyType::STATIC, diff --git a/game/workers/WorkerScript.cpp b/game/workers/WorkerScript.cpp new file mode 100644 index 0000000..d641c6a --- /dev/null +++ b/game/workers/WorkerScript.cpp @@ -0,0 +1,106 @@ +#include "WorkerScript.h" + +#include "../Config.h" + +#include <crepe/api/Animator.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Sprite.h> +#include <crepe/api/Transform.h> +#include <crepe/types.h> +#include <cstdlib> + +using namespace crepe; +using namespace std; + +void WorkerScript::fixed_update(duration_t dt) { + RefVector<Rigidbody> rb_workers = this->get_components_by_tag<Rigidbody>("worker"); + RefVector<Transform> trans_workers = this->get_components_by_tag<Transform>("worker"); + + Rigidbody & rb_cam = this->get_components_by_name<Rigidbody>("camera").back(); + Transform & trans_cam = this->get_components_by_name<Transform>("camera").back(); + + int counter = 0; + for (Rigidbody & rb_worker : rb_workers) { + Transform & trans_worker = trans_workers.at(counter); + + float result_x = rb_cam.data.linear_velocity.x - rb_worker.data.linear_velocity.x; + + if (result_x > 0) { + float left_cam_pos_x = trans_cam.position.x - VIEWPORT_X / 2; + if (trans_worker.position.x < left_cam_pos_x - 1000) { + trans_worker.position.x = left_cam_pos_x + VIEWPORT_X + 1000; + + do { + float min_value = -2500 * dt.count(); + float max_value = 2500 * dt.count(); + rb_worker.data.linear_velocity.x + = min_value + + static_cast<float>(rand()) + / (static_cast<float>(RAND_MAX / (max_value - min_value))); + } while (rb_worker.data.linear_velocity.x < 500 * dt.count() + && rb_worker.data.linear_velocity.x > -500 * dt.count()); + + RefVector<Sprite> sprite_worker + = 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); + + if (rb_worker.data.linear_velocity.x < 0) { + sprite_worker.front().get().data.flip.flip_x = true; + sprite_worker.back().get().data.flip.flip_x = true; + + animator_worker.front().get().data.fps + = -rb_worker.data.linear_velocity.x / 5; + animator_worker.back().get().data.fps + = -rb_worker.data.linear_velocity.x / 5; + } else { + sprite_worker.front().get().data.flip.flip_x = false; + sprite_worker.back().get().data.flip.flip_x = false; + + animator_worker.front().get().data.fps + = rb_worker.data.linear_velocity.x / 5; + animator_worker.back().get().data.fps + = rb_worker.data.linear_velocity.x / 5; + } + } + } else { + float right_cam_pos_x = trans_cam.position.x + VIEWPORT_X / 2; + if (trans_worker.position.x > right_cam_pos_x + 1000) { + do { + float min_value = -2500 * dt.count(); + float max_value = 2500 * dt.count(); + rb_worker.data.linear_velocity.x + = min_value + + static_cast<float>(rand()) + / (static_cast<float>(RAND_MAX / (max_value - min_value))); + } while (rb_worker.data.linear_velocity.x < 500 * dt.count() + && rb_worker.data.linear_velocity.x > -500 * dt.count()); + + RefVector<Sprite> sprite_worker + = 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); + + if (rb_worker.data.linear_velocity.x < 0) { + sprite_worker.front().get().data.flip.flip_x = true; + sprite_worker.back().get().data.flip.flip_x = true; + + animator_worker.front().get().data.fps + = -rb_worker.data.linear_velocity.x / 5; + animator_worker.back().get().data.fps + = -rb_worker.data.linear_velocity.x / 5; + } else { + sprite_worker.front().get().data.flip.flip_x = false; + sprite_worker.back().get().data.flip.flip_x = false; + + animator_worker.front().get().data.fps + = rb_worker.data.linear_velocity.x / 5; + animator_worker.back().get().data.fps + = rb_worker.data.linear_velocity.x / 5; + } + } + } + + counter++; + } +} diff --git a/game/workers/WorkerScript.h b/game/workers/WorkerScript.h new file mode 100644 index 0000000..ae4a6c7 --- /dev/null +++ b/game/workers/WorkerScript.h @@ -0,0 +1,8 @@ +#pragma once + +#include <crepe/api/Script.h> + +class WorkerScript : public crepe::Script { +public: + void fixed_update(crepe::duration_t dt); +}; diff --git a/game/workers/WorkersSubScene.cpp b/game/workers/WorkersSubScene.cpp new file mode 100644 index 0000000..94d9848 --- /dev/null +++ b/game/workers/WorkersSubScene.cpp @@ -0,0 +1,405 @@ +#include "WorkersSubScene.h" +#include "WorkerScript.h" + +#include "../Config.h" +#include "api/GameObject.h" + +#include <crepe/api/Animator.h> +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/BoxCollider.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Sprite.h> + +using namespace crepe; +using namespace std; + +WorkersSubScene::WorkersSubScene(Scene & scn) { + this->worker1(scn, 1200, -50); + this->worker2(scn, 1300, 20); + this->worker3(scn, 1400, -40); + this->worker4(scn, 7250, 50); + this->worker5(scn, 3400, -20); + this->worker6(scn, 2000, 30); + this->worker7(scn, 3725, 35); + this->worker8(scn, 2200, -15); + + GameObject script = scn.new_object("workers_script"); + script.add_component<BehaviorScript>().set_script<WorkerScript>(); +} + +void WorkersSubScene::worker1(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_1 = scn.new_object("worker_1", "worker", vec2(start_x, 200)); + Sprite & worker_1_body_sprite = worker_1.add_component<Sprite>( + Asset {"asset/workers/worker1Body.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 0, + .size = vec2(0, 50), + } + ); + worker_1.add_component<Animator>( + worker_1_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_1_head_sprite = worker_1.add_component<Sprite>( + Asset {"asset/workers/worker1Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 1, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_1.add_component<Animator>( + worker_1_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + worker_1.add_component<BoxCollider>(vec2(50, 50)); + worker_1.add_component<Rigidbody>(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_TOP}, + }); + + if (init_speed < 0) { + worker_1_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_1_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker2(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_2 = scn.new_object("worker_2", "worker", vec2(start_x, 200)); + Sprite & worker_2_body_sprite = worker_2.add_component<Sprite>( + Asset {"asset/workers/worker2Body.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 2, + .size = vec2(0, 50), + } + ); + worker_2.add_component<Animator>( + worker_2_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_2_head_sprite = worker_2.add_component<Sprite>( + Asset {"asset/workers/worker1Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 3, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_2.add_component<Animator>( + worker_2_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + worker_2.add_component<BoxCollider>(vec2(50, 50)); + worker_2.add_component<Rigidbody>(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_TOP}, + }); + + if (init_speed < 0) { + worker_2_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_2_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker3(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_3 = scn.new_object("worker_3", "worker", vec2(start_x, 200)); + Sprite & worker_3_body_sprite = worker_3.add_component<Sprite>( + Asset {"asset/workers/worker1Body.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 4, + .size = vec2(0, 50), + } + ); + worker_3.add_component<Animator>( + worker_3_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_3_head_sprite = worker_3.add_component<Sprite>( + Asset {"asset/workers/worker2Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 5, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_3.add_component<Animator>( + worker_3_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + worker_3.add_component<BoxCollider>(vec2(50, 50)); + worker_3.add_component<Rigidbody>(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_TOP}, + }); + + if (init_speed < 0) { + worker_3_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_3_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker4(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_4 = scn.new_object("worker_4", "worker", vec2(start_x, 200)); + Sprite & worker_4_body_sprite = worker_4.add_component<Sprite>( + Asset {"asset/workers/worker2Body.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 6, + .size = vec2(0, 50), + } + ); + worker_4.add_component<Animator>( + worker_4_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_4_head_sprite = worker_4.add_component<Sprite>( + Asset {"asset/workers/worker2Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 7, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_4.add_component<Animator>( + worker_4_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + worker_4.add_component<BoxCollider>(vec2(50, 50)); + worker_4.add_component<Rigidbody>(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_HIGH}, + }); + + if (init_speed < 0) { + worker_4_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_4_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker5(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_5 = scn.new_object("worker_5", "worker", vec2(start_x, 200)); + Sprite & worker_5_body_sprite = worker_5.add_component<Sprite>( + Asset {"asset/workers/workerFatBody.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 8, + .size = vec2(0, 50), + } + ); + worker_5.add_component<Animator>( + worker_5_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_5_head_sprite = worker_5.add_component<Sprite>( + Asset {"asset/workers/worker1Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 9, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_5.add_component<Animator>( + worker_5_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + worker_5.add_component<BoxCollider>(vec2(50, 50)); + worker_5.add_component<Rigidbody>(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_HIGH}, + }); + + if (init_speed < 0) { + worker_5_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_5_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker6(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_6 = scn.new_object("worker_6", "worker", vec2(start_x, 200)); + Sprite & worker_6_body_sprite = worker_6.add_component<Sprite>( + Asset {"asset/workers/workerFatBody.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 10, + .size = vec2(0, 50), + } + ); + worker_6.add_component<Animator>( + worker_6_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_6_head_sprite = worker_6.add_component<Sprite>( + Asset {"asset/workers/worker2Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 11, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_6.add_component<Animator>( + worker_6_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + worker_6.add_component<BoxCollider>(vec2(50, 50)); + worker_6.add_component<Rigidbody>(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_LOW}, + }); + + if (init_speed < 0) { + worker_6_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_6_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker7(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_7 = scn.new_object("worker_7", "worker", vec2(start_x, 200)); + Sprite & worker_7_body_sprite = worker_7.add_component<Sprite>( + Asset {"asset/workers/workerTallBody.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 12, + .size = vec2(0, 50), + } + ); + worker_7.add_component<Animator>( + worker_7_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_7_head_sprite = worker_7.add_component<Sprite>( + Asset {"asset/workers/worker1Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 13, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_7.add_component<Animator>( + worker_7_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + worker_7.add_component<BoxCollider>(vec2(50, 50)); + worker_7.add_component<Rigidbody>(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_LOW}, + }); + + if (init_speed < 0) { + worker_7_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_7_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker8(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_8 = scn.new_object("worker_8", "worker", vec2(start_x, 200)); + Sprite & worker_8_body_sprite = worker_8.add_component<Sprite>( + Asset {"asset/workers/workerTallBody.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 14, + .size = vec2(0, 50), + } + ); + worker_8.add_component<Animator>( + worker_8_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_8_head_sprite = worker_8.add_component<Sprite>( + Asset {"asset/workers/worker2Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 15, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_8.add_component<Animator>( + worker_8_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast<unsigned int>(abs(init_speed) / 5), + .looping = true, + } + ); + worker_8.add_component<BoxCollider>(vec2(50, 50)); + worker_8.add_component<Rigidbody>(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_LOW}, + }); + + if (init_speed < 0) { + worker_8_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_8_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} diff --git a/game/workers/WorkersSubScene.h b/game/workers/WorkersSubScene.h new file mode 100644 index 0000000..6692d87 --- /dev/null +++ b/game/workers/WorkersSubScene.h @@ -0,0 +1,20 @@ +#pragma once + +namespace crepe { +class Scene; +} + +class WorkersSubScene { +public: + WorkersSubScene(crepe::Scene & scn); + +private: + void worker1(crepe::Scene & scn, float start_x, float init_speed); + void worker2(crepe::Scene & scn, float start_x, float init_speed); + void worker3(crepe::Scene & scn, float start_x, float init_speed); + void worker4(crepe::Scene & scn, float start_x, float init_speed); + void worker5(crepe::Scene & scn, float start_x, float init_speed); + void worker6(crepe::Scene & scn, float start_x, float init_speed); + void worker7(crepe::Scene & scn, float start_x, float init_speed); + void worker8(crepe::Scene & scn, float start_x, float init_speed); +}; |