From 15a958a7f327bb2e81e7d445f1499ec78c7f5839 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 10 Jan 2025 12:05:16 +0100 Subject: alers scripts and quitscript with ESC key --- game/missile/AlertScript.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 game/missile/AlertScript.cpp (limited to 'game/missile/AlertScript.cpp') diff --git a/game/missile/AlertScript.cpp b/game/missile/AlertScript.cpp new file mode 100644 index 0000000..c1124ac --- /dev/null +++ b/game/missile/AlertScript.cpp @@ -0,0 +1,38 @@ +#include "AlertScript.h" +#include "../Config.h" +#include "api/CircleCollider.h" +#include "api/Sprite.h" + +#include + +using namespace crepe; + +void AlertScript::fixed_update(crepe::duration_t dt) { + const auto & cam = this->get_components_by_name("camera").front().get(); + //missile transform + const auto & this_transform = this->get_component(); + auto missile_transforms = this->get_components_by_name("missile"); + const auto & this_collider = this->get_component(); + + auto alert_sprites = this->get_components_by_name("missile_alert"); + auto alert_transforms = this->get_components_by_name("missile_alert"); + + int idx = 0; + for (int i = 0; i < missile_transforms.size(); i++) { + const auto & missile_transform = missile_transforms[i].get(); + if (this_transform.game_object_id == missile_transform.game_object_id) { + idx = i; + break; + } + } + + auto & alert_transform = alert_transforms[idx].get(); + alert_transform.position.x = cam.position.x + (VIEWPORT_X / 2 - 100); + alert_transform.position.y = this_transform.position.y; + + // check if transform is in camera view + if (this_transform.position.x > cam.position.x - (VIEWPORT_X / 2) + && this_transform.position.x < cam.position.x + (VIEWPORT_X / 2)) { + alert_sprites[idx].get().active = false; + } +} -- cgit v1.2.3 From 938b6a7bb62459e8308320280d15ccaf1b8af0ac Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 10 Jan 2025 14:43:07 +0100 Subject: adjsuted includes so that it has <> and not --- game/CMakeLists.txt | 2 +- game/QuitScript.cpp | 5 +++-- game/QuitScript.h | 3 ++- game/hud/HudScript.cpp | 3 ++- game/missile/AlertScript.cpp | 4 ++-- game/missile/AlertScript.h | 3 ++- game/missile/AlertSubScene.cpp | 11 ++++------- game/missile/AlertSubScene.h | 3 ++- game/missile/MissileScript.cpp | 10 ++++------ game/missile/SpawnEvent.cpp | 4 ++-- 10 files changed, 24 insertions(+), 24 deletions(-) (limited to 'game/missile/AlertScript.cpp') diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index b39416b..a94beca 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.28) set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 20) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) -set(CMAKE_BUILD_TYPE Debug) +set(CMAKE_BUILD_TYPE Release) project(game C CXX) add_subdirectory(../src crepe) diff --git a/game/QuitScript.cpp b/game/QuitScript.cpp index e48863f..fc33dcf 100644 --- a/game/QuitScript.cpp +++ b/game/QuitScript.cpp @@ -1,8 +1,9 @@ #include "QuitScript.h" -#include "api/Event.h" -#include "api/KeyCodes.h" + +#include +#include using namespace crepe; diff --git a/game/QuitScript.h b/game/QuitScript.h index c295183..af78ccf 100644 --- a/game/QuitScript.h +++ b/game/QuitScript.h @@ -1,7 +1,8 @@ #pragma once -#include "api/Script.h" +#include + class QuitScript : public crepe::Script { private: bool on_event(const crepe::KeyPressEvent & ev); diff --git a/game/hud/HudScript.cpp b/game/hud/HudScript.cpp index 3fb7a77..e4aeae7 100644 --- a/game/hud/HudScript.cpp +++ b/game/hud/HudScript.cpp @@ -3,6 +3,7 @@ #include "../Config.h" #include "../Events.h" +#include "api/KeyCodes.h" #include "menus/endgame/EndGameSubScript.h" #include @@ -37,7 +38,7 @@ void HudScript::init() { } bool HudScript::toggle_fps(crepe::KeyPressEvent ev) { - if (ev.key != Keycode::END) return false; + if (ev.key != Keycode::D1) return false; Text & txt_fps = this->get_components_by_name(HUD_FPS).front(); this->show_fps = !this->show_fps; if (this->show_fps) { diff --git a/game/missile/AlertScript.cpp b/game/missile/AlertScript.cpp index c1124ac..24b4af9 100644 --- a/game/missile/AlertScript.cpp +++ b/game/missile/AlertScript.cpp @@ -1,8 +1,8 @@ #include "AlertScript.h" #include "../Config.h" -#include "api/CircleCollider.h" -#include "api/Sprite.h" +#include +#include #include using namespace crepe; diff --git a/game/missile/AlertScript.h b/game/missile/AlertScript.h index ab29863..5b0b3a1 100644 --- a/game/missile/AlertScript.h +++ b/game/missile/AlertScript.h @@ -1,6 +1,7 @@ #pragma once -#include "api/Script.h" +#include + class AlertScript : public crepe::Script { private: bool has_alert = false; diff --git a/game/missile/AlertSubScene.cpp b/game/missile/AlertSubScene.cpp index c3379e8..8d59f97 100644 --- a/game/missile/AlertSubScene.cpp +++ b/game/missile/AlertSubScene.cpp @@ -1,10 +1,9 @@ #include "AlertSubScene.h" #include "../Config.h" -#include "api/Animator.h" -#include "api/BehaviorScript.h" -#include "api/Scene.h" -#include "api/Sprite.h" -#include "missile/AlertScript.h" + +#include +#include +#include using namespace crepe; @@ -13,8 +12,6 @@ MissileAlert::MissileAlert(Scene& scn){ Asset missile_alert_ss {"asset/obstacles/missile/missileAlert.png"}; - //alert.add_component().set_script(); - auto & missile_alert_sprite = alert.add_component( missile_alert_ss, Sprite::Data { diff --git a/game/missile/AlertSubScene.h b/game/missile/AlertSubScene.h index 661b3f1..8ea288f 100644 --- a/game/missile/AlertSubScene.h +++ b/game/missile/AlertSubScene.h @@ -1,6 +1,7 @@ #pragma once -#include "api/Scene.h" +#include + class MissileAlert { public: MissileAlert(crepe::Scene & scn); diff --git a/game/missile/MissileScript.cpp b/game/missile/MissileScript.cpp index 2d1052e..797e1ac 100644 --- a/game/missile/MissileScript.cpp +++ b/game/missile/MissileScript.cpp @@ -1,17 +1,15 @@ #include "MissileScript.h" #include "../Config.h" -#include "Collider.h" -#include "api/BehaviorScript.h" -#include "api/CircleCollider.h" -#include "api/Rigidbody.h" +#include +#include +#include +#include #include #include #include #include #include - -#include #include #include #include diff --git a/game/missile/SpawnEvent.cpp b/game/missile/SpawnEvent.cpp index c407832..a54d0a5 100644 --- a/game/missile/SpawnEvent.cpp +++ b/game/missile/SpawnEvent.cpp @@ -1,8 +1,8 @@ #include "SpawnEvent.h" -#include "Config.h" #include "Random.h" -#include "api/CircleCollider.h" + +#include #include #include #include -- cgit v1.2.3 From a981a42a7378ed51155590215557e52553031272 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 10 Jan 2025 16:26:05 +0100 Subject: missile solution, cannot find @max issue --- game/PreviewScene.cpp | 24 +++++++++--------------- game/missile/AlertScript.cpp | 2 +- game/missile/MissileScript.cpp | 9 ++------- game/missile/SpawnEvent.cpp | 10 +++++----- game/missile/SpawnEvent.h | 2 +- game/preview/NpcScript.cpp | 9 ++------- game/preview/NpcScript.h | 5 +---- game/preview/NpcSubScene.cpp | 6 +----- game/preview/PrevPlayerSubScene.cpp | 6 +----- 9 files changed, 23 insertions(+), 50 deletions(-) (limited to 'game/missile/AlertScript.cpp') diff --git a/game/PreviewScene.cpp b/game/PreviewScene.cpp index d9801a7..1e64e41 100644 --- a/game/PreviewScene.cpp +++ b/game/PreviewScene.cpp @@ -49,7 +49,7 @@ void PreviewScene::load_scene() { .bg_color = Color::RED, } ); - camera.add_component(Rigidbody::Data {}); + camera.add_component().set_script(); camera.add_component().set_script(); camera.add_component().set_script(); @@ -77,13 +77,16 @@ void PreviewScene::load_scene() { .collision_layer = COLL_LAY_BOT_TOP, }); ceiling.add_component(vec2(INFINITY, 200)); + GameObject world = this->new_object("world", "TAG", vec2 {0, 0}, 0, 1); - world.add_component(Rigidbody::Data { .body_type = Rigidbody::BodyType::STATIC, - .collision_layers = {0}, + .collision_layer = COLL_LAY_BOT_TOP, }); + world.add_component(vec2(100,INFINITY), vec2(VIEWPORT_X,VIEWPORT_Y)); + world.add_component(vec2(100,INFINITY), vec2(100,VIEWPORT_Y)); + PrevPlayerSubScene player(*this); NpcSubScene npc(*this); SmokeSubScene smoke(*this); @@ -111,6 +114,8 @@ void PreviewScene::load_scene() { } ); + + /* button.create( *this, ButtonSubScene::Data { @@ -158,18 +163,7 @@ void PreviewScene::load_scene() { .btn_side_color = ButtonSubScene::ButtonSideColor::ORANGE, } ); - - /* - - for (int i = 0; i < 200; ++i) { - int row = i / 10; - int col = i % 10; - float x = col * 25 + i; - float y = row * 25 - 400; - GameObject game_coin = this->new_object("coin", "coin", vec2 {x, y}, 0, 1); - Coin coin(game_coin, vec2 {0, 0}); - } - */ + */ } string PreviewScene::get_name() const { return "preview scene"; } diff --git a/game/missile/AlertScript.cpp b/game/missile/AlertScript.cpp index 24b4af9..0e6f5c5 100644 --- a/game/missile/AlertScript.cpp +++ b/game/missile/AlertScript.cpp @@ -18,7 +18,7 @@ void AlertScript::fixed_update(crepe::duration_t dt) { auto alert_transforms = this->get_components_by_name("missile_alert"); int idx = 0; - for (int i = 0; i < missile_transforms.size(); i++) { + for (int i = 0; i < missile_transforms.size(); ++i) { const auto & missile_transform = missile_transforms[i].get(); if (this_transform.game_object_id == missile_transform.game_object_id) { idx = i; diff --git a/game/missile/MissileScript.cpp b/game/missile/MissileScript.cpp index bcc4f5b..f87dd20 100644 --- a/game/missile/MissileScript.cpp +++ b/game/missile/MissileScript.cpp @@ -27,7 +27,6 @@ void MissileScript::kill_missile() { auto animations = this->get_components(); auto sprites = this->get_components(); auto collider = this->get_component(); - auto & fly_sound = this->get_components().front().get(); auto & this_script = this->get_components().front().get(); animations[0].get().active = false; @@ -36,11 +35,10 @@ void MissileScript::kill_missile() { sprites[0].get().active = false; sprites[1].get().active = false; sprites[2].get().active = true; + collider.active = false; this_script.active = false; this->seeking_disabled = false; - - fly_sound.stop(); } void MissileScript::activate() { auto anim = this->get_components(); @@ -49,14 +47,10 @@ void MissileScript::activate() { anim[0].get().active = true; anim[1].get().active = true; anim[2].get().stop(); - //anim[3].get().active = true; - sprites[0].get().active = true; sprites[1].get().active = true; sprites[2].get().active = false; - //sprites[3].get().active = true; } - bool MissileScript::on_collision(const CollisionEvent & ev) { auto & explosion_sound = this->get_components().back().get(); @@ -79,6 +73,7 @@ void MissileScript::fixed_update(crepe::duration_t dt) { const auto & cam = this->get_components_by_name("camera").front().get(); const auto & velocity = this->get_component().data.linear_velocity; + if (missile.position.x < (cam.position.x - VIEWPORT_X / 1.8)) { this->kill_missile(); return; diff --git a/game/missile/SpawnEvent.cpp b/game/missile/SpawnEvent.cpp index c7209b7..6109686 100644 --- a/game/missile/SpawnEvent.cpp +++ b/game/missile/SpawnEvent.cpp @@ -20,16 +20,17 @@ void MissileSpawnEventHandler::init() { } bool MissileSpawnEventHandler::on_event(const MissileSpawnEvent & event) { - auto missile_transforms = this->get_components_by_name("missile"); auto alert_sprites = this->get_components_by_name("missile_alert"); - auto alert_transforms = this->get_components_by_name("missile_alert"); + + auto missile_transforms = this->get_components_by_name("missile"); auto colliders = this->get_components_by_name("missile"); auto missile_behaviorscripts = this->get_components_by_name("missile"); auto missile_audiosources = this->get_components_by_name("missile"); + auto & camera_transform = this->get_components_by_name("camera").front().get(); - for (size_t i = 0; i < missile_behaviorscripts.size(); ++i) { - auto & script = missile_behaviorscripts[i * 2].get(); + for (size_t i = 0; i < missile_transforms.size(); ++i) { + BehaviorScript & script = missile_behaviorscripts[i * 2].get(); if (script.active) continue; script.active = true; colliders[i].get().active = true; @@ -39,7 +40,6 @@ bool MissileSpawnEventHandler::on_event(const MissileSpawnEvent & event) { transform.position.x = camera_transform.position.x + this->MISSILE_OFFSET; transform.position.y = Random::i(this->MAX_RANGE, this->MIN_RANGE); - auto & alert_transform = alert_transforms[i].get(); auto & alert_sprite = alert_sprites[i].get(); alert_sprite.active = true; break; diff --git a/game/missile/SpawnEvent.h b/game/missile/SpawnEvent.h index 58293d7..3b9638c 100644 --- a/game/missile/SpawnEvent.h +++ b/game/missile/SpawnEvent.h @@ -10,7 +10,7 @@ struct MissileSpawnEvent : public crepe::Event {}; class MissileSpawnEventHandler : public crepe::Script { private: static constexpr int MISSILE_OFFSET = VIEWPORT_X; - static constexpr int RANGE = GAME_HEIGHT / 4; + static constexpr int RANGE = GAME_HEIGHT / 4.5; static constexpr int MIN_RANGE = -RANGE; static constexpr int MAX_RANGE = RANGE; diff --git a/game/preview/NpcScript.cpp b/game/preview/NpcScript.cpp index c4148f2..5a93c2b 100644 --- a/game/preview/NpcScript.cpp +++ b/game/preview/NpcScript.cpp @@ -7,16 +7,15 @@ using namespace std; using namespace crepe; -void NpcScript::init() {} void NpcScript::fixed_update(duration_t dt) { auto & rb = this->get_component(); auto & npc = this->get_component(); auto & transform = this->get_component(); - if (transform.position.x < -990) { + if (transform.position.x < 200) { rb.data.linear_velocity.x *= -1; } - if (transform.position.x > 990) { + if (transform.position.x > 700) { rb.data.linear_velocity.x *= -1; } @@ -25,8 +24,4 @@ void NpcScript::fixed_update(duration_t dt) { } else { npc.data.flip = {false, false}; } - - auto & savemgr = this->get_save_manager(); - savemgr.set("npc_x", transform.position.x); - savemgr.set("npc_y", transform.position.y); } diff --git a/game/preview/NpcScript.h b/game/preview/NpcScript.h index 8d856fd..d278f83 100644 --- a/game/preview/NpcScript.h +++ b/game/preview/NpcScript.h @@ -1,11 +1,8 @@ +#pragma once #include class NpcScript : public crepe::Script { - -private: - public: - void init(); void fixed_update(crepe::duration_t dt); }; diff --git a/game/preview/NpcSubScene.cpp b/game/preview/NpcSubScene.cpp index bd6cfb2..a3781af 100644 --- a/game/preview/NpcSubScene.cpp +++ b/game/preview/NpcSubScene.cpp @@ -15,11 +15,7 @@ using namespace crepe; NpcSubScene::NpcSubScene(Scene & scn) { - auto & savemgr = scn.get_save_manager(); - ValueBroker npc_x = savemgr.get("npc_x", 500); - ValueBroker npc_y = savemgr.get("npc_y", 0); - - GameObject npc = scn.new_object("npc", "npc_tag", vec2 {npc_x.get(), npc_y.get()}, 0, 1); + GameObject npc = scn.new_object("npc", "npc_tag", vec2 {500, 0}, 0, 1); Asset npc_body {"asset/workers/worker1Body.png"}; Asset npc_head {"asset/workers/worker1Head.png"}; diff --git a/game/preview/PrevPlayerSubScene.cpp b/game/preview/PrevPlayerSubScene.cpp index b59a0af..4351deb 100644 --- a/game/preview/PrevPlayerSubScene.cpp +++ b/game/preview/PrevPlayerSubScene.cpp @@ -16,13 +16,9 @@ using namespace crepe; PrevPlayerSubScene::PrevPlayerSubScene(Scene & scn) { - auto & savemgr = scn.get_save_manager(); - - ValueBroker player_x = savemgr.get("player_x", 500); - ValueBroker player_y = savemgr.get("player_y", -100); GameObject player - = scn.new_object("player", "TAG", vec2 {player_x.get(), player_y.get()}, 0, 1); + = scn.new_object("player", "player", vec2 {800 , -100}, 0, 1); Asset player_body_asset {"asset/barry/defaultBody.png"}; Sprite & player_body_sprite = player.add_component( player_body_asset, -- cgit v1.2.3