From a600cc55468a6513743ce916aa3da129270c23f0 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 22 Dec 2024 20:04:33 +0100 Subject: more WIP zapper --- game/prefab/ZapperObject.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 game/prefab/ZapperObject.cpp (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp new file mode 100644 index 0000000..2791162 --- /dev/null +++ b/game/prefab/ZapperObject.cpp @@ -0,0 +1,84 @@ +#include "Config.h" +#include "ZapperObject.h" +#include "ZapperScript.h" + +using namespace crepe; + +ZapperObject::ZapperObject(crepe::GameObject && base) + : GameObject(std::move(base)), + sprite { + .orb_start = add_component( + Asset {"asset/obstacles/zapper/orbAnim.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = 1, + .size = vec2{0, 1} * SCALE, + .position_offset = {0, 100}, + } + ), + .orb_end = add_component( + sprite.orb_start.source, + Sprite::Data { + .flip = {true, true}, + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = 1, + .size = vec2{0, 1} * SCALE, + .position_offset = {0, -100}, + } + ), + .glow_start = add_component( + Asset {"asset/obstacles/zapper/regular_zappers/glow.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = -1, + .size = vec2{2, 2} * SCALE, + .position_offset = {0, 100}, + } + ), + .glow_end = add_component( + sprite.glow_start.source, + Sprite::Data { + .flip = {true, true}, + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = -1, + .size = vec2{2, 2} * SCALE, + .position_offset = {0, -100}, + } + ), + .beam = add_component( + Asset {"asset/obstacles/zapper/regular_zappers/zapEffect.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = 0, + .size = vec2{200, 50}, + .angle_offset = 90, + } + ), + }, + animator { + .orb_start = add_component( + sprite.orb_start, ivec2(62, 42), uvec2(4, 1), + Animator::Data { + .fps = 10, + .looping = true, + } + ), + .orb_end = add_component( + sprite.orb_end, ivec2(62, 42), uvec2(4, 1), + animator.orb_start.data + ), + .glow_start = add_component( + sprite.glow_start, ivec2(128, 128), uvec2(4, 4), + Animator::Data { + .fps = 30, + .looping = true, + } + ), + .glow_end = add_component( + sprite.glow_end, ivec2(128, 128), uvec2(4, 4), + animator.glow_start.data + ), + }, + controller(add_component().set_script(*this)) +{ } + -- cgit v1.2.3 From ec69f40540317215d0f30e1f8e43d0e18450f8cc Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 22 Dec 2024 20:05:02 +0100 Subject: `make format` --- game/GameScene.cpp | 2 +- game/prefab/PlayerObject.cpp | 3 +- game/prefab/PlayerScript.cpp | 8 ++-- game/prefab/PlayerScript.h | 1 - game/prefab/ZapperObject.cpp | 92 ++++++++++++++++++------------------- game/prefab/ZapperObject.h | 1 - game/prefab/ZapperScript.cpp | 9 ++-- game/prefab/ZapperScript.h | 1 - src/crepe/api/Animator.cpp | 1 - src/crepe/api/Animator.h | 2 +- src/crepe/api/Engine.cpp | 3 +- src/crepe/facade/SignalCatch.cpp | 9 +--- src/crepe/facade/SignalCatch.h | 3 +- src/crepe/manager/SystemManager.cpp | 10 +++- src/crepe/manager/SystemManager.hpp | 2 +- src/crepe/system/AnimatorSystem.cpp | 2 +- src/crepe/system/ScriptSystem.cpp | 12 ++++- 17 files changed, 77 insertions(+), 84 deletions(-) (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 90a8b58..b287763 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -14,8 +14,8 @@ #include #include -#include "GameScene.h" #include "Config.h" +#include "GameScene.h" #include "MoveCameraManualyScript.h" #include "StartGameScript.h" diff --git a/game/prefab/PlayerObject.cpp b/game/prefab/PlayerObject.cpp index ef7b6cc..2d48bf8 100644 --- a/game/prefab/PlayerObject.cpp +++ b/game/prefab/PlayerObject.cpp @@ -1,5 +1,5 @@ -#include "Config.h" #include "PlayerObject.h" +#include "Config.h" #include "PlayerScript.h" using namespace crepe; @@ -69,4 +69,3 @@ PlayerObject::PlayerObject(crepe::GameObject && base) sprite.jetpack.active = false; controller.active = false; } - diff --git a/game/prefab/PlayerScript.cpp b/game/prefab/PlayerScript.cpp index 67aadb6..3d06476 100644 --- a/game/prefab/PlayerScript.cpp +++ b/game/prefab/PlayerScript.cpp @@ -1,15 +1,13 @@ -#include #include +#include #include "PlayerScript.h" using namespace crepe; using namespace std; -PlayerScript::PlayerScript(const PlayerObject & player) : player(player) { } +PlayerScript::PlayerScript(const PlayerObject & player) : player(player) {} void PlayerScript::fixed_update(crepe::duration_t dt) { - if (this->get_key_state(Keycode::SPACE)) - player.body.add_force_linear({ 0, -10 }); + if (this->get_key_state(Keycode::SPACE)) player.body.add_force_linear({0, -10}); } - diff --git a/game/prefab/PlayerScript.h b/game/prefab/PlayerScript.h index 131c73f..cc0f0aa 100644 --- a/game/prefab/PlayerScript.h +++ b/game/prefab/PlayerScript.h @@ -14,4 +14,3 @@ protected: protected: PlayerObject player; }; - diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index 2791162..b848f18 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -1,5 +1,5 @@ -#include "Config.h" #include "ZapperObject.h" +#include "Config.h" #include "ZapperScript.h" using namespace crepe; @@ -11,74 +11,70 @@ ZapperObject::ZapperObject(crepe::GameObject && base) Asset {"asset/obstacles/zapper/orbAnim.png"}, Sprite::Data { .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = 1, - .size = vec2{0, 1} * SCALE, - .position_offset = {0, 100}, + .order_in_layer = 1, + .size = vec2 {0, 1} * SCALE, + .position_offset = {0, 100}, } ), .orb_end = add_component( - sprite.orb_start.source, - Sprite::Data { - .flip = {true, true}, - .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = 1, - .size = vec2{0, 1} * SCALE, - .position_offset = {0, -100}, - } + sprite.orb_start.source, + Sprite::Data { + .flip = {true, true}, + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = 1, + .size = vec2 {0, 1} * SCALE, + .position_offset = {0, -100}, + } ), .glow_start = add_component( Asset {"asset/obstacles/zapper/regular_zappers/glow.png"}, Sprite::Data { .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = -1, - .size = vec2{2, 2} * SCALE, - .position_offset = {0, 100}, + .order_in_layer = -1, + .size = vec2 {2, 2} * SCALE, + .position_offset = {0, 100}, } ), .glow_end = add_component( - sprite.glow_start.source, - Sprite::Data { - .flip = {true, true}, - .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = -1, - .size = vec2{2, 2} * SCALE, - .position_offset = {0, -100}, - } + sprite.glow_start.source, + Sprite::Data { + .flip = {true, true}, + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = -1, + .size = vec2 {2, 2} * SCALE, + .position_offset = {0, -100}, + } ), - .beam = add_component( + .beam = add_component( Asset {"asset/obstacles/zapper/regular_zappers/zapEffect.png"}, Sprite::Data { .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = 0, - .size = vec2{200, 50}, - .angle_offset = 90, + .order_in_layer = 0, + .size = vec2 {200, 50}, + .angle_offset = 90, } - ), + ), }, animator { - .orb_start = add_component( + .orb_start = add_component( sprite.orb_start, ivec2(62, 42), uvec2(4, 1), Animator::Data { .fps = 10, .looping = true, } - ), - .orb_end = add_component( - sprite.orb_end, ivec2(62, 42), uvec2(4, 1), - animator.orb_start.data - ), - .glow_start = add_component( - sprite.glow_start, ivec2(128, 128), uvec2(4, 4), - Animator::Data { - .fps = 30, - .looping = true, - } - ), - .glow_end = add_component( - sprite.glow_end, ivec2(128, 128), uvec2(4, 4), - animator.glow_start.data - ), + ), + .orb_end = add_component( + sprite.orb_end, ivec2(62, 42), uvec2(4, 1), animator.orb_start.data + ), + .glow_start = add_component( + sprite.glow_start, ivec2(128, 128), uvec2(4, 4), + Animator::Data { + .fps = 30, + .looping = true, + } + ), + .glow_end = add_component( + sprite.glow_end, ivec2(128, 128), uvec2(4, 4), animator.glow_start.data + ), }, - controller(add_component().set_script(*this)) -{ } - + controller(add_component().set_script(*this)) {} diff --git a/game/prefab/ZapperObject.h b/game/prefab/ZapperObject.h index 6b68146..151e368 100644 --- a/game/prefab/ZapperObject.h +++ b/game/prefab/ZapperObject.h @@ -34,4 +34,3 @@ public: private: static constexpr int SCALE = 60; }; - diff --git a/game/prefab/ZapperScript.cpp b/game/prefab/ZapperScript.cpp index b9b24be..01eb7aa 100644 --- a/game/prefab/ZapperScript.cpp +++ b/game/prefab/ZapperScript.cpp @@ -1,12 +1,12 @@ -#include #include +#include #include "ZapperScript.h" using namespace crepe; using namespace std; -ZapperScript::ZapperScript(const ZapperObject & zapper) : zapper(zapper) { } +ZapperScript::ZapperScript(const ZapperObject & zapper) : zapper(zapper) {} void ZapperScript::init() { zapper.sprite.beam.mask = { @@ -17,7 +17,4 @@ void ZapperScript::init() { }; } -void ZapperScript::frame_update(duration_t delta_time) { - zapper.sprite.beam.mask.x += 4; -} - +void ZapperScript::frame_update(duration_t delta_time) { zapper.sprite.beam.mask.x += 4; } diff --git a/game/prefab/ZapperScript.h b/game/prefab/ZapperScript.h index 5a960df..b090c8d 100644 --- a/game/prefab/ZapperScript.h +++ b/game/prefab/ZapperScript.h @@ -15,4 +15,3 @@ protected: protected: ZapperObject zapper; }; - diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 5ac0617..f6f2654 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -44,4 +44,3 @@ void Animator::set_cycle_range(int start, int end) { this->data.cycle_start = start; this->data.cycle_end = end; } - diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 8be693e..bcf6379 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,7 +1,7 @@ #pragma once -#include "../types.h" #include "../manager/LoopTimerManager.h" +#include "../types.h" #include "Component.h" #include "Sprite.h" diff --git a/src/crepe/api/Engine.cpp b/src/crepe/api/Engine.cpp index bf3f50c..8766aea 100644 --- a/src/crepe/api/Engine.cpp +++ b/src/crepe/api/Engine.cpp @@ -1,7 +1,7 @@ #include -#include "../util/Log.h" #include "../facade/SignalCatch.h" +#include "../util/Log.h" #include "Engine.h" @@ -77,4 +77,3 @@ void Engine::frame_update() { this->system_manager.frame_update(); this->loop_timer.enforce_frame_rate(); } - diff --git a/src/crepe/facade/SignalCatch.cpp b/src/crepe/facade/SignalCatch.cpp index ad92d28..4988047 100644 --- a/src/crepe/facade/SignalCatch.cpp +++ b/src/crepe/facade/SignalCatch.cpp @@ -15,11 +15,6 @@ SignalCatch::~SignalCatch() { segvcatch::init_fpe(); } -void SignalCatch::segv() { - throw runtime_error("segmentation fault"); -} - -void SignalCatch::fpe() { - throw domain_error("floating point exception"); -} +void SignalCatch::segv() { throw runtime_error("segmentation fault"); } +void SignalCatch::fpe() { throw domain_error("floating point exception"); } diff --git a/src/crepe/facade/SignalCatch.h b/src/crepe/facade/SignalCatch.h index 4562215..cf86b56 100644 --- a/src/crepe/facade/SignalCatch.h +++ b/src/crepe/facade/SignalCatch.h @@ -20,5 +20,4 @@ public: SignalCatch & operator=(SignalCatch &&) = delete; }; -} - +} // namespace crepe diff --git a/src/crepe/manager/SystemManager.cpp b/src/crepe/manager/SystemManager.cpp index c8f4f3d..fea59aa 100644 --- a/src/crepe/manager/SystemManager.cpp +++ b/src/crepe/manager/SystemManager.cpp @@ -37,7 +37,10 @@ void SystemManager::fixed_update() noexcept { try { entry.system.fixed_update(); } catch (const exception & e) { - Log::logf(Log::Level::WARNING, "Uncaught exception in {} fixed update: {}", entry.name, e.what()); + Log::logf( + Log::Level::WARNING, "Uncaught exception in {} fixed update: {}", entry.name, + e.what() + ); } } } @@ -48,7 +51,10 @@ void SystemManager::frame_update() noexcept { try { entry.system.frame_update(); } catch (const exception & e) { - Log::logf(Log::Level::WARNING, "Uncaught exception in {} frame update: {}", entry.name, e.what()); + Log::logf( + Log::Level::WARNING, "Uncaught exception in {} frame update: {}", entry.name, + e.what() + ); } } } diff --git a/src/crepe/manager/SystemManager.hpp b/src/crepe/manager/SystemManager.hpp index a4c11e3..a19a253 100644 --- a/src/crepe/manager/SystemManager.hpp +++ b/src/crepe/manager/SystemManager.hpp @@ -38,7 +38,7 @@ void SystemManager::load_system() { throw runtime_error(format("SystemManager: {} is already initialized", type.name())); System * system = new T(this->mediator); this->systems[type] = unique_ptr(system); - this->system_order.push_back(SystemEntry{ + this->system_order.push_back(SystemEntry { .system = *this->systems[type], .name = type.name(), }); diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 467d2a2..6c93372 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -30,7 +30,7 @@ void AnimatorSystem::frame_update() { if (animator.elapsed > frame_duration) { animator.elapsed = 0ms; animator.data.frame++; - + if (animator.data.looping && animator.data.frame >= animator.data.cycle_end) animator.data.frame = animator.data.cycle_start; } diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index 411a9a3..f1e31f9 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -36,7 +36,11 @@ void ScriptSystem::update( script->init(); script->initialized = true; } catch (const exception & e) { - Log::logf(Log::Level::WARNING, "Disabled script \"{}\" due to exception in init function: {}", behavior_script.name, e.what()); + Log::logf( + Log::Level::WARNING, + "Disabled script \"{}\" due to exception in init function: {}", + behavior_script.name, e.what() + ); behavior_script.active = false; } } @@ -45,7 +49,11 @@ void ScriptSystem::update( (*script.*update_function)(delta_time); } catch (const exception & e) { // TODO: discern between fixed/frame update - Log::logf(Log::Level::WARNING, "Disabled script \"{}\" due to exception in update function: {}", behavior_script.name, e.what()); + Log::logf( + Log::Level::WARNING, + "Disabled script \"{}\" due to exception in update function: {}", + behavior_script.name, e.what() + ); behavior_script.active = false; } } -- cgit v1.2.3 From 7126db6e1d97b7ede4cc929abfdeadd360e4cb74 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 6 Jan 2025 11:58:27 +0100 Subject: WIP --- game/prefab/CMakeLists.txt | 1 - game/prefab/ZapperObject.cpp | 8 +++----- game/prefab/ZapperObject.h | 2 -- game/prefab/ZapperScript.cpp | 20 -------------------- game/prefab/ZapperScript.h | 17 ----------------- game/util/scrollgen | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 39 insertions(+), 45 deletions(-) delete mode 100644 game/prefab/ZapperScript.cpp delete mode 100644 game/prefab/ZapperScript.h create mode 100755 game/util/scrollgen (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/prefab/CMakeLists.txt b/game/prefab/CMakeLists.txt index 03084e4..5585a32 100644 --- a/game/prefab/CMakeLists.txt +++ b/game/prefab/CMakeLists.txt @@ -2,6 +2,5 @@ target_sources(main PUBLIC PlayerObject.cpp PlayerScript.cpp ZapperObject.cpp - ZapperScript.cpp ) diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index b848f18..e63a5f2 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -1,6 +1,5 @@ #include "ZapperObject.h" #include "Config.h" -#include "ZapperScript.h" using namespace crepe; @@ -67,14 +66,13 @@ ZapperObject::ZapperObject(crepe::GameObject && base) sprite.orb_end, ivec2(62, 42), uvec2(4, 1), animator.orb_start.data ), .glow_start = add_component( - sprite.glow_start, ivec2(128, 128), uvec2(4, 4), + sprite.glow_start, ivec2(128, 128), uvec2(16, 1), Animator::Data { .fps = 30, .looping = true, } ), .glow_end = add_component( - sprite.glow_end, ivec2(128, 128), uvec2(4, 4), animator.glow_start.data + sprite.glow_end, ivec2(128, 128), uvec2(16, 1), animator.glow_start.data ), - }, - controller(add_component().set_script(*this)) {} + } {} diff --git a/game/prefab/ZapperObject.h b/game/prefab/ZapperObject.h index 151e368..cc2d37c 100644 --- a/game/prefab/ZapperObject.h +++ b/game/prefab/ZapperObject.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include #include @@ -29,7 +28,6 @@ public: // crepe::Rigidbody & body; // crepe::BoxCollider & collider; - crepe::BehaviorScript & controller; private: static constexpr int SCALE = 60; diff --git a/game/prefab/ZapperScript.cpp b/game/prefab/ZapperScript.cpp deleted file mode 100644 index 01eb7aa..0000000 --- a/game/prefab/ZapperScript.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -#include "ZapperScript.h" - -using namespace crepe; -using namespace std; - -ZapperScript::ZapperScript(const ZapperObject & zapper) : zapper(zapper) {} - -void ZapperScript::init() { - zapper.sprite.beam.mask = { - .w = 350, - .h = 117, - .x = 0, - .y = 0, - }; -} - -void ZapperScript::frame_update(duration_t delta_time) { zapper.sprite.beam.mask.x += 4; } diff --git a/game/prefab/ZapperScript.h b/game/prefab/ZapperScript.h deleted file mode 100644 index b090c8d..0000000 --- a/game/prefab/ZapperScript.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include - -#include "ZapperObject.h" - -class ZapperScript : public crepe::Script { -public: - ZapperScript(const ZapperObject & zapper); - -protected: - void init(); - void frame_update(crepe::duration_t delta_time); - -protected: - ZapperObject zapper; -}; diff --git a/game/util/scrollgen b/game/util/scrollgen new file mode 100755 index 0000000..0389107 --- /dev/null +++ b/game/util/scrollgen @@ -0,0 +1,36 @@ +#!/bin/sh +INPUT="$1" +FRAMES="$2" + +die() { + echo "$@" + exit 1 +} +check_command() { + cmd="$1" + command -v "$cmd" > /dev/null || die "command '$cmd' not found" +} + +check_command magick +check_command identify +[ "$#" -eq 2 ] || die "usage: $0 " +[ -e "$INPUT" ] || die "file not found: $INPUT" +[ "$FRAMES" -gt 0 ] || die "invalid frame count: $FRAMES" + +tile_width=$(identify -format "%w" "$INPUT") +tile_height=$(identify -format "%h" "$INPUT") + +OUTPUT="$INPUT.scroll.png" +magick -size "${tile_width}x$(( $tile_height * $FRAMES ))" 'xc:#ff00ff00' "$OUTPUT" + +for i in $(seq 0 $(( $FRAMES - 1 ))); do + offset_x=$(( $tile_width * $i / $FRAMES )) + offset_y=$(( i * $tile_height )) + + magick "$OUTPUT" "$INPUT" -geometry "+${offset_x}+${offset_y}" -composite "$OUTPUT" + magick "$OUTPUT" "$INPUT" -geometry "+$(( $offset_x - $tile_width ))+${offset_y}" -composite "$OUTPUT" + echo "+${offset_x}+${offset_y}" +done + +# magick -size ${total_width}x${sprite_height} xc:none canvas.png + -- cgit v1.2.3 From 8620f57f7101b2090add89330e74854b77161d3f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 6 Jan 2025 13:18:40 +0100 Subject: place zapper method --- game/prefab/ZapperObject.cpp | 35 +++++++++++++++++++++++++---------- game/prefab/ZapperObject.h | 5 ++++- src/crepe/api/Vector2.h | 39 +++++++++++++++++++++------------------ src/crepe/api/Vector2.hpp | 10 ++++++++++ src/test/Vector2Test.cpp | 10 ++++++++++ 5 files changed, 70 insertions(+), 29 deletions(-) (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index e63a5f2..fcc6467 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -1,3 +1,5 @@ +#include + #include "ZapperObject.h" #include "Config.h" @@ -11,8 +13,7 @@ ZapperObject::ZapperObject(crepe::GameObject && base) Sprite::Data { .sorting_in_layer = SORT_IN_LAY_OBSTACLES, .order_in_layer = 1, - .size = vec2 {0, 1} * SCALE, - .position_offset = {0, 100}, + .size = vec2(0, 42) * SCALE, } ), .orb_end = add_component( @@ -21,8 +22,7 @@ ZapperObject::ZapperObject(crepe::GameObject && base) .flip = {true, true}, .sorting_in_layer = SORT_IN_LAY_OBSTACLES, .order_in_layer = 1, - .size = vec2 {0, 1} * SCALE, - .position_offset = {0, -100}, + .size = vec2(0, 42) * SCALE, } ), .glow_start = add_component( @@ -30,8 +30,7 @@ ZapperObject::ZapperObject(crepe::GameObject && base) Sprite::Data { .sorting_in_layer = SORT_IN_LAY_OBSTACLES, .order_in_layer = -1, - .size = vec2 {2, 2} * SCALE, - .position_offset = {0, 100}, + .size = vec2(128, 128) * SCALE, } ), .glow_end = add_component( @@ -40,8 +39,7 @@ ZapperObject::ZapperObject(crepe::GameObject && base) .flip = {true, true}, .sorting_in_layer = SORT_IN_LAY_OBSTACLES, .order_in_layer = -1, - .size = vec2 {2, 2} * SCALE, - .position_offset = {0, -100}, + .size = vec2(128, 128) * SCALE, } ), .beam = add_component( @@ -49,7 +47,7 @@ ZapperObject::ZapperObject(crepe::GameObject && base) Sprite::Data { .sorting_in_layer = SORT_IN_LAY_OBSTACLES, .order_in_layer = 0, - .size = vec2 {200, 50}, + .size = vec2(0, 40 * SCALE), .angle_offset = 90, } ), @@ -75,4 +73,21 @@ ZapperObject::ZapperObject(crepe::GameObject && base) .glow_end = add_component( sprite.glow_end, ivec2(128, 128), uvec2(16, 1), animator.glow_start.data ), - } {} + } { + this->place(this->transform.position, 0, 300); + } + +void ZapperObject::place(const crepe::vec2 & position, float rotation, float length) { + this->transform.position = position; + this->transform.rotation = rotation; + + vec2 offset = vec2(0, 1) * length / 2; + + this->sprite.orb_start.data.position_offset = offset; + this->sprite.glow_start.data.position_offset = offset; + this->sprite.orb_end.data.position_offset = -offset; + this->sprite.glow_end.data.position_offset = -offset; + + this->sprite.beam.data.size.x = length; +} + diff --git a/game/prefab/ZapperObject.h b/game/prefab/ZapperObject.h index cc2d37c..b4bb942 100644 --- a/game/prefab/ZapperObject.h +++ b/game/prefab/ZapperObject.h @@ -30,5 +30,8 @@ public: // crepe::BoxCollider & collider; private: - static constexpr int SCALE = 60; + static constexpr float SCALE = 0.8; + +public: + void place(const crepe::vec2 & position, float rotation, float length); }; diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 52e1bb6..0f46964 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -11,55 +11,55 @@ struct Vector2 { T y = 0; //! Subtracts another vector from this vector and returns the result. - Vector2 operator-(const Vector2 & other) const; + Vector2 operator-(const Vector2 & other) const; //! Subtracts a scalar value from both components of this vector and returns the result. - Vector2 operator-(T scalar) const; + Vector2 operator-(T scalar) const; //! Adds another vector to this vector and returns the result. - Vector2 operator+(const Vector2 & other) const; + Vector2 operator+(const Vector2 & other) const; //! Adds a scalar value to both components of this vector and returns the result. - Vector2 operator+(T scalar) const; + Vector2 operator+(T scalar) const; //! Multiplies this vector by another vector element-wise and returns the result. - Vector2 operator*(const Vector2 & other) const; + Vector2 operator*(const Vector2 & other) const; //! Multiplies this vector by a scalar and returns the result. - Vector2 operator*(T scalar) const; + Vector2 operator*(T scalar) const; //! Divides this vector by another vector element-wise and returns the result. - Vector2 operator/(const Vector2 & other) const; + Vector2 operator/(const Vector2 & other) const; //! Divides this vector by a scalar and returns the result. - Vector2 operator/(T scalar) const; + Vector2 operator/(T scalar) const; //! Adds another vector to this vector and updates this vector. - Vector2 & operator+=(const Vector2 & other); + Vector2 & operator+=(const Vector2 & other); //! Adds a scalar value to both components of this vector and updates this vector. - Vector2 & operator+=(T other); + Vector2 & operator+=(T other); //! Subtracts another vector from this vector and updates this vector. - Vector2 & operator-=(const Vector2 & other); + Vector2 & operator-=(const Vector2 & other); //! Subtracts a scalar value from both components of this vector and updates this vector. - Vector2 & operator-=(T other); + Vector2 & operator-=(T other); //! Multiplies this vector by another vector element-wise and updates this vector. - Vector2 & operator*=(const Vector2 & other); + Vector2 & operator*=(const Vector2 & other); //! Multiplies this vector by a scalar and updates this vector. - Vector2 & operator*=(T other); + Vector2 & operator*=(T other); //! Divides this vector by another vector element-wise and updates this vector. - Vector2 & operator/=(const Vector2 & other); + Vector2 & operator/=(const Vector2 & other); //! Divides this vector by a scalar and updates this vector. - Vector2 & operator/=(T other); + Vector2 & operator/=(T other); //! Returns the negation of this vector. - Vector2 operator-() const; + Vector2 operator-() const; //! Checks if this vector is equal to another vector. bool operator==(const Vector2 & other) const; @@ -89,10 +89,13 @@ struct Vector2 { T distance_squared(const Vector2 & other) const; //! Returns the perpendicular vector to this vector. - Vector2 perpendicular() const; + Vector2 perpendicular() const; //! Checks if both components of the vector are NaN. bool is_nan() const; + + //! Rotate this vector clockwise by \c deg degrees + Vector2 rotate(float deg) const; }; } // namespace crepe diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp index e195760..a4ce641 100644 --- a/src/crepe/api/Vector2.hpp +++ b/src/crepe/api/Vector2.hpp @@ -168,4 +168,14 @@ bool Vector2::is_nan() const { return std::isnan(x) && std::isnan(y); } +template +Vector2 Vector2::rotate(float deg) const { + float rad = -deg / 180 * M_PI; + return { + x * std::cos(rad) - y * std::sin(rad), + x * std::sin(rad) + y * std::cos(rad), + }; +} + + } // namespace crepe diff --git a/src/test/Vector2Test.cpp b/src/test/Vector2Test.cpp index 1e21af9..b17f95a 100644 --- a/src/test/Vector2Test.cpp +++ b/src/test/Vector2Test.cpp @@ -1,6 +1,7 @@ #include #include +#include using namespace crepe; @@ -530,3 +531,12 @@ TEST_F(Vector2Test, Perpendicular) { EXPECT_FLOAT_EQ(result4.x, -4.0f); EXPECT_FLOAT_EQ(result4.y, 3.0f); } + +TEST_F(Vector2Test, Rotate) { + vec2 foo {0, 1}; + + foo = foo.rotate(90); + const float GOOD_ENOUGH = 0.001; + EXPECT_NEAR(foo.x, 1, GOOD_ENOUGH); + EXPECT_NEAR(foo.y, 0, GOOD_ENOUGH); +} -- cgit v1.2.3 From f8496d3d48beeb6e0c5ef8dc82e16d281c94148e Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 7 Jan 2025 10:05:58 +0100 Subject: add collider to zapper --- game/prefab/ZapperObject.cpp | 11 +++++++++++ game/prefab/ZapperObject.h | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index fcc6467..f70415c 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -73,6 +73,15 @@ ZapperObject::ZapperObject(crepe::GameObject && base) .glow_end = add_component( sprite.glow_end, ivec2(128, 128), uvec2(16, 1), animator.glow_start.data ), + }, + body { + add_component(Rigidbody::Data { + .body_type = Rigidbody::BodyType::KINEMATIC, + .kinematic_collision = false, + }) + }, + collider { + add_component(vec2(0, 0)) } { this->place(this->transform.position, 0, 300); } @@ -89,5 +98,7 @@ void ZapperObject::place(const crepe::vec2 & position, float rotation, float len this->sprite.glow_end.data.position_offset = -offset; this->sprite.beam.data.size.x = length; + + this->collider.dimensions = offset.rotate(rotation) * 2 + vec2(30, 30) * SCALE; } diff --git a/game/prefab/ZapperObject.h b/game/prefab/ZapperObject.h index b4bb942..c43af67 100644 --- a/game/prefab/ZapperObject.h +++ b/game/prefab/ZapperObject.h @@ -26,8 +26,8 @@ public: crepe::Animator & glow_end; } animator; - // crepe::Rigidbody & body; - // crepe::BoxCollider & collider; + crepe::Rigidbody & body; + crepe::BoxCollider & collider; private: static constexpr float SCALE = 0.8; -- cgit v1.2.3 From 725bb89cd3690d396502ab90baa35fa4eec6fe98 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 7 Jan 2025 10:08:47 +0100 Subject: `make format` --- game/GameScene.cpp | 2 +- game/prefab/ZapperObject.cpp | 23 +++++++++-------------- src/crepe/api/Vector2.hpp | 1 - 3 files changed, 10 insertions(+), 16 deletions(-) (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 71612e3..dc0038b 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -19,8 +19,8 @@ #include "MoveCameraManualyScript.h" #include "StartGameScript.h" -#include "background/BackgroundSubScene.h" #include "PlayerSubScene.h" +#include "background/BackgroundSubScene.h" #include "prefab/ZapperObject.h" using namespace crepe; diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index f70415c..8df2075 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -1,7 +1,7 @@ #include -#include "ZapperObject.h" #include "Config.h" +#include "ZapperObject.h" using namespace crepe; @@ -73,18 +73,14 @@ ZapperObject::ZapperObject(crepe::GameObject && base) .glow_end = add_component( sprite.glow_end, ivec2(128, 128), uvec2(16, 1), animator.glow_start.data ), - }, - body { - add_component(Rigidbody::Data { - .body_type = Rigidbody::BodyType::KINEMATIC, - .kinematic_collision = false, - }) - }, - collider { - add_component(vec2(0, 0)) - } { - this->place(this->transform.position, 0, 300); - } + }, + body {add_component(Rigidbody::Data { + .body_type = Rigidbody::BodyType::KINEMATIC, + .kinematic_collision = false, + })}, + collider {add_component(vec2(0, 0))} { + this->place(this->transform.position, 0, 300); +} void ZapperObject::place(const crepe::vec2 & position, float rotation, float length) { this->transform.position = position; @@ -101,4 +97,3 @@ void ZapperObject::place(const crepe::vec2 & position, float rotation, float len this->collider.dimensions = offset.rotate(rotation) * 2 + vec2(30, 30) * SCALE; } - diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp index a4ce641..5709f46 100644 --- a/src/crepe/api/Vector2.hpp +++ b/src/crepe/api/Vector2.hpp @@ -177,5 +177,4 @@ Vector2 Vector2::rotate(float deg) const { }; } - } // namespace crepe -- cgit v1.2.3 From bcfa2d9543c2fc7d83003b243e526ab64efab6e1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 7 Jan 2025 10:40:16 +0100 Subject: cleanup --- game/GameScene.cpp | 17 ----------------- game/prefab/ZapperObject.cpp | 1 + 2 files changed, 1 insertion(+), 17 deletions(-) (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 4193581..c393ef7 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -74,23 +74,6 @@ void GameScene::load_scene() { GameObject start_game_script = new_object("start_game_script", "script", vec2(0, 0)); start_game_script.add_component().set_script(); - // zapper, laser and missile (below) for testing purpose only!!! - GameObject zapper = new_object("zapper", "zapper", vec2(1000, 0)); - Asset zapper_asset {"asset/obstacles/zapper/regular_zappers/zapEffect.png"}; - Sprite & zapper_sprite = zapper.add_component( - zapper_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = 0, - .size = vec2(100, 100), - } - ); - zapper.add_component(Rigidbody::Data { - .body_type = Rigidbody::BodyType::KINEMATIC, - .kinematic_collision = false, - .collision_layer = COLL_LAY_ZAPPER, - }); - zapper.add_component(vec2(100, 100)); GameObject laser = new_object("laser", "laser", vec2(2000, 0)); Asset laser_asset {"asset/obstacles/laser/laserPower.png"}; Sprite & laser_sprite = laser.add_component( diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index 8df2075..944a53f 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -77,6 +77,7 @@ ZapperObject::ZapperObject(crepe::GameObject && base) body {add_component(Rigidbody::Data { .body_type = Rigidbody::BodyType::KINEMATIC, .kinematic_collision = false, + .collision_layer = COLL_LAY_ZAPPER, })}, collider {add_component(vec2(0, 0))} { this->place(this->transform.position, 0, 300); -- cgit v1.2.3 From aa2d2e05bc90b90ab503eff1f58bfc8c2b9c1741 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 7 Jan 2025 10:40:28 +0100 Subject: `make format` --- game/GameScene.cpp | 2 +- game/prefab/ZapperObject.cpp | 2 +- src/crepe/api/Engine.cpp | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index c393ef7..1eefce1 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -21,8 +21,8 @@ #include "GameScene.h" #include "MoveCameraManualyScript.h" -#include "player/PlayerSubScene.h" #include "background/BackgroundSubScene.h" +#include "player/PlayerSubScene.h" #include "prefab/ZapperObject.h" using namespace crepe; diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index 944a53f..4055ac4 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -77,7 +77,7 @@ ZapperObject::ZapperObject(crepe::GameObject && base) body {add_component(Rigidbody::Data { .body_type = Rigidbody::BodyType::KINEMATIC, .kinematic_collision = false, - .collision_layer = COLL_LAY_ZAPPER, + .collision_layer = COLL_LAY_ZAPPER, })}, collider {add_component(vec2(0, 0))} { this->place(this->transform.position, 0, 300); diff --git a/src/crepe/api/Engine.cpp b/src/crepe/api/Engine.cpp index bfc1c4a..35743e3 100644 --- a/src/crepe/api/Engine.cpp +++ b/src/crepe/api/Engine.cpp @@ -70,4 +70,3 @@ void Engine::loop() { } } } - -- cgit v1.2.3 From 6d69c8ef6b663bd6716b441cc7d01164c7e33dfc Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 7 Jan 2025 13:04:49 +0100 Subject: more WIP zapperpool --- game/GameScene.cpp | 4 ++-- game/prefab/CMakeLists.txt | 1 + game/prefab/ZapperObject.cpp | 18 ++++++++++++++++++ game/prefab/ZapperObject.h | 1 + game/prefab/ZapperPoolScript.h | 8 ++++++++ game/prefab/ZapperPoolSubScene.cpp | 13 +++++++++++++ game/prefab/ZapperPoolSubScene.h | 21 +++++++++++++++++++++ 7 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 game/prefab/ZapperPoolScript.h create mode 100644 game/prefab/ZapperPoolSubScene.cpp create mode 100644 game/prefab/ZapperPoolSubScene.h (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 1eefce1..698677b 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -23,7 +23,7 @@ #include "background/BackgroundSubScene.h" #include "player/PlayerSubScene.h" -#include "prefab/ZapperObject.h" +#include "prefab/ZapperPoolSubScene.h" using namespace crepe; using namespace std; @@ -69,7 +69,7 @@ void GameScene::load_scene() { }); ceiling.add_component(vec2(INFINITY, 200)); - ZapperObject {new_object("zapper", "zapper", vec2(800, 0))}; + ZapperPoolSubScene {*this}; GameObject start_game_script = new_object("start_game_script", "script", vec2(0, 0)); start_game_script.add_component().set_script(); diff --git a/game/prefab/CMakeLists.txt b/game/prefab/CMakeLists.txt index 5f8ea6c..d77b9b1 100644 --- a/game/prefab/CMakeLists.txt +++ b/game/prefab/CMakeLists.txt @@ -1,4 +1,5 @@ target_sources(main PUBLIC ZapperObject.cpp + ZapperPoolSubScene.cpp ) diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index 4055ac4..3c70d04 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -81,6 +81,7 @@ ZapperObject::ZapperObject(crepe::GameObject && base) })}, collider {add_component(vec2(0, 0))} { this->place(this->transform.position, 0, 300); + this->set_active(false); } void ZapperObject::place(const crepe::vec2 & position, float rotation, float length) { @@ -98,3 +99,20 @@ void ZapperObject::place(const crepe::vec2 & position, float rotation, float len this->collider.dimensions = offset.rotate(rotation) * 2 + vec2(30, 30) * SCALE; } + +void ZapperObject::set_active(bool active) { + this->sprite.orb_start.active = active; + this->sprite.orb_end.active = active; + this->sprite.glow_start.active = active; + this->sprite.glow_end.active = active; + this->sprite.beam.active = active; + + this->animator.orb_start.active = active; + this->animator.orb_end.active = active; + this->animator.glow_start.active = active; + this->animator.glow_end.active = active; + + this->body.active = active; + this->collider.active = active; +} + diff --git a/game/prefab/ZapperObject.h b/game/prefab/ZapperObject.h index c43af67..9f4e4a1 100644 --- a/game/prefab/ZapperObject.h +++ b/game/prefab/ZapperObject.h @@ -34,4 +34,5 @@ private: public: void place(const crepe::vec2 & position, float rotation, float length); + void set_active(bool active); }; diff --git a/game/prefab/ZapperPoolScript.h b/game/prefab/ZapperPoolScript.h new file mode 100644 index 0000000..79f9a89 --- /dev/null +++ b/game/prefab/ZapperPoolScript.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +class ZapperPoolScript : public crepe::Script { + +}; + diff --git a/game/prefab/ZapperPoolSubScene.cpp b/game/prefab/ZapperPoolSubScene.cpp new file mode 100644 index 0000000..578688a --- /dev/null +++ b/game/prefab/ZapperPoolSubScene.cpp @@ -0,0 +1,13 @@ +#include "ZapperPoolSubScene.h" + +using namespace crepe; +using namespace std; + +ZapperPoolSubScene::ZapperPoolSubScene(Scene & scene) + : controller { scene.new_object("controller") } { + for (size_t i = 0; i < this->POOL_SIZE; i++) + zappers.emplace_back(scene.new_object("zapper")); + + +} + diff --git a/game/prefab/ZapperPoolSubScene.h b/game/prefab/ZapperPoolSubScene.h new file mode 100644 index 0000000..79598ce --- /dev/null +++ b/game/prefab/ZapperPoolSubScene.h @@ -0,0 +1,21 @@ +#pragma once + +#include + +#include +#include + +#include "ZapperObject.h" + +class ZapperPoolSubScene { +public: + ZapperPoolSubScene(crepe::Scene & scene); + +public: + crepe::GameObject controller; + std::vector zappers; + +private: + static constexpr size_t POOL_SIZE = 4; + +}; -- cgit v1.2.3 From 06d41eb2d58de21a8340ca59952b96cabc8caee6 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 8 Jan 2025 11:13:40 +0100 Subject: pooling working --- game/prefab/ZapperObject.cpp | 10 +++++++--- game/prefab/ZapperPoolScript.cpp | 37 ++++++++++++++++++++++++------------- game/prefab/ZapperPoolScript.h | 11 ++++++++--- game/prefab/ZapperPoolSubScene.cpp | 14 ++++---------- game/prefab/ZapperPoolSubScene.h | 8 -------- 5 files changed, 43 insertions(+), 37 deletions(-) (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index 61681ae..712eca9 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -13,7 +13,7 @@ ZapperObject::ZapperObject(crepe::GameObject && base) Sprite::Data { .sorting_in_layer = SORT_IN_LAY_OBSTACLES, .order_in_layer = 1, - .size = vec2(0, 42) * SCALE, + .size = vec2(0, 50) * SCALE, } ), .orb_end = add_component( @@ -22,7 +22,7 @@ ZapperObject::ZapperObject(crepe::GameObject && base) .flip = {true, true}, .sorting_in_layer = SORT_IN_LAY_OBSTACLES, .order_in_layer = 1, - .size = vec2(0, 42) * SCALE, + .size = vec2(0, 50) * SCALE, } ), .glow_start = add_component( @@ -81,10 +81,12 @@ ZapperObject::ZapperObject(crepe::GameObject && base) })}, collider {add_component(vec2(0, 0))} { this->set_active(false); - Log::logf(Log::DEBUG, "creating zapper"); + Log::logf(Log::DEBUG, "Creating zapper"); } void ZapperObject::place(const crepe::vec2 & position, float rotation, float length) { + Log::logf(Log::DEBUG, "Placing zapper [position = {}, rotation = {}, length = {}]", position, rotation, length); + this->transform.position = position; this->transform.rotation = rotation; @@ -98,6 +100,8 @@ void ZapperObject::place(const crepe::vec2 & position, float rotation, float len this->sprite.beam.data.size.x = length; this->collider.dimensions = offset.rotate(rotation) * 2 + vec2(30, 30) * SCALE; + + this->set_active(true); } void ZapperObject::set_active(bool active) { diff --git a/game/prefab/ZapperPoolScript.cpp b/game/prefab/ZapperPoolScript.cpp index c6ce1e7..812024b 100644 --- a/game/prefab/ZapperPoolScript.cpp +++ b/game/prefab/ZapperPoolScript.cpp @@ -10,37 +10,48 @@ using namespace crepe; using namespace std; -ZapperPoolScript::ZapperPoolScript(ZapperPoolSubScene & pool) : pool(pool) {} +ZapperPoolScript::ZapperPoolScript(std::vector && pool) : pool(pool) {} void ZapperPoolScript::init() { subscribe([this](const CreateZapperEvent &) { this->spawn_random(); return true; }); + + camera_transform = get_components_by_name(CAMERA_NAME).back(); + camera_camera = get_components_by_name(CAMERA_NAME).back(); } void ZapperPoolScript::fixed_update(crepe::duration_t) { - if (i++ < 80) return; - i = 0; + for (ZapperObject & zapper : this->pool) { + if (!zapper.active) continue; + + if (zapper.transform.position.x < camera_transform->position.x) + zapper.set_active(false); + } + + if (i-- > 0) return; + i = 200; queue_event(); } void ZapperPoolScript::spawn_random() { - OptionalRef zapper = pool.get_next_zapper(); + OptionalRef zapper = this->get_next_zapper(); if (!zapper) return; // pool exhausted - vec2 pos = this->get_camera_pos(); - pos.y = Random::f(0.5, -0.5) * HALLWAY_HEIGHT; + vec2 pos = { + .x = camera_transform->position.x + camera_camera->viewport_size.x / 2, + .y = Random::f(0.5, -0.5) * HALLWAY_HEIGHT, + }; zapper->place(pos, 0, Random::f(400, 200)); - logf(Log::DEBUG, "Spawning random zapper at {}", pos); } -vec2 ZapperPoolScript::get_camera_pos() { - Transform & transform = get_components_by_name(CAMERA_NAME).back(); - Camera & camera = get_components_by_name(CAMERA_NAME).back(); - - // right middle edge position - return transform.position + vec2(camera.viewport_size.x / 2, 0); +OptionalRef ZapperPoolScript::get_next_zapper() { + for (ZapperObject & zapper : this->pool) { + if (zapper.active) continue; + return zapper; + } + return {}; } diff --git a/game/prefab/ZapperPoolScript.h b/game/prefab/ZapperPoolScript.h index 7c4701c..5daba79 100644 --- a/game/prefab/ZapperPoolScript.h +++ b/game/prefab/ZapperPoolScript.h @@ -2,11 +2,14 @@ #include +#include "ZapperObject.h" +#include "util/OptionalRef.h" + class ZapperPoolSubScene; class ZapperPoolScript : public crepe::Script { public: - ZapperPoolScript(ZapperPoolSubScene & pool); + ZapperPoolScript(std::vector && pool); void init(); void fixed_update(crepe::duration_t); @@ -14,10 +17,12 @@ public: unsigned i = 0; private: - ZapperPoolSubScene & pool; + std::vector pool; private: - crepe::vec2 get_camera_pos(); + crepe::OptionalRef camera_transform; + crepe::OptionalRef camera_camera; + crepe::OptionalRef get_next_zapper(); private: void spawn_random(); diff --git a/game/prefab/ZapperPoolSubScene.cpp b/game/prefab/ZapperPoolSubScene.cpp index d7d30ea..923ed57 100644 --- a/game/prefab/ZapperPoolSubScene.cpp +++ b/game/prefab/ZapperPoolSubScene.cpp @@ -8,18 +8,12 @@ using namespace std; ZapperPoolSubScene::ZapperPoolSubScene(Scene & scene) : controller { scene.new_object("controller") } { - this->controller.add_component().set_script(*this); Log::logf(Log::DEBUG, "Building zapper pool..."); + vector pool; for (size_t i = 0; i < this->POOL_SIZE; i++) - zappers.emplace_back(scene.new_object("zapper")); -} - -OptionalRef ZapperPoolSubScene::get_next_zapper() { - for (ZapperObject & zapper : this->zappers) { - if (!zapper.active) continue; - return zapper; - } - return {}; + pool.emplace_back(scene.new_object("zapper")); + BehaviorScript & behavior = this->controller.add_component(); + behavior.set_script(std::move(pool)); } diff --git a/game/prefab/ZapperPoolSubScene.h b/game/prefab/ZapperPoolSubScene.h index 25328ee..f930e22 100644 --- a/game/prefab/ZapperPoolSubScene.h +++ b/game/prefab/ZapperPoolSubScene.h @@ -1,14 +1,10 @@ #pragma once -#include - #include #include #include #include -#include "ZapperObject.h" - class CreateZapperEvent : public crepe::Event {}; class ZapperPoolSubScene { @@ -17,11 +13,7 @@ public: private: crepe::GameObject controller; - std::vector zappers; private: static constexpr size_t POOL_SIZE = 4; - -public: - crepe::OptionalRef get_next_zapper(); }; -- cgit v1.2.3 From 46c2cbdcbe5e7dbc4d86a5d43b3bd7275b7a1c33 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 8 Jan 2025 11:59:51 +0100 Subject: WIP --- game/GameScene.cpp | 33 --------------------------------- game/Random.cpp | 4 ++++ game/Random.h | 1 + game/prefab/ZapperObject.cpp | 2 -- game/prefab/ZapperPoolScript.cpp | 12 +++++++++--- game/prefab/ZapperPoolScript.h | 3 +++ game/prefab/ZapperPoolSubScene.cpp | 2 +- 7 files changed, 18 insertions(+), 39 deletions(-) (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 8bd6c74..6c21843 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -103,39 +103,6 @@ void GameScene::load_scene() { Asset boom_audio_asset {"asset/sfx/window_smash.ogg"}; boom_audio.add_component(boom_audio_asset); - GameObject laser = new_object("laser", "laser", vec2(2000, 0)); - Asset laser_asset {"asset/obstacles/laser/laserPower.png"}; - Sprite & laser_sprite = laser.add_component( - laser_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = 0, - .size = vec2(100, 100), - } - ); - laser.add_component(Rigidbody::Data { - .body_type = Rigidbody::BodyType::KINEMATIC, - .kinematic_collision = false, - .collision_layer = COLL_LAY_LASER, - }); - laser.add_component(vec2(100, 100)); - GameObject missile = new_object("missile", "missile", vec2(4000, 0)); - Asset missile_asset {"asset/obstacles/missile/missile.png"}; - Sprite & missile_sprite = missile.add_component( - missile_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = 0, - .size = vec2(100, 100), - } - ); - missile.add_component(Rigidbody::Data { - .body_type = Rigidbody::BodyType::KINEMATIC, - .kinematic_collision = false, - .collision_layer = COLL_LAY_MISSILE, - }); - missile.add_component(vec2(100, 100)); - EndGameSubScene endgamewindow; endgamewindow.create(*this); } diff --git a/game/Random.cpp b/game/Random.cpp index 59be3c5..821ddc8 100644 --- a/game/Random.cpp +++ b/game/Random.cpp @@ -26,3 +26,7 @@ unsigned Random::u(unsigned upper, unsigned lower) { return x + lower; } +bool Random::b() { + return rand() % 2; +} + diff --git a/game/Random.h b/game/Random.h index cf05e87..8db616c 100644 --- a/game/Random.h +++ b/game/Random.h @@ -6,6 +6,7 @@ public: static double d(double upper = 1.0, double lower = 0.0); static int i(int upper, int lower = 0); static unsigned u(unsigned upper, unsigned lower = 0); + static bool b(); }; diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index 712eca9..0a290e0 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -100,8 +100,6 @@ void ZapperObject::place(const crepe::vec2 & position, float rotation, float len this->sprite.beam.data.size.x = length; this->collider.dimensions = offset.rotate(rotation) * 2 + vec2(30, 30) * SCALE; - - this->set_active(true); } void ZapperObject::set_active(bool active) { diff --git a/game/prefab/ZapperPoolScript.cpp b/game/prefab/ZapperPoolScript.cpp index 812024b..e201149 100644 --- a/game/prefab/ZapperPoolScript.cpp +++ b/game/prefab/ZapperPoolScript.cpp @@ -23,10 +23,11 @@ void ZapperPoolScript::init() { } void ZapperPoolScript::fixed_update(crepe::duration_t) { + float threshold = camera_transform->position.x - camera_camera->viewport_size.x / 2 - OFFSCREEN_MARGIN; for (ZapperObject & zapper : this->pool) { if (!zapper.active) continue; - if (zapper.transform.position.x < camera_transform->position.x) + if (zapper.transform.position.x < threshold) zapper.set_active(false); } @@ -40,11 +41,16 @@ void ZapperPoolScript::spawn_random() { if (!zapper) return; // pool exhausted vec2 pos = { - .x = camera_transform->position.x + camera_camera->viewport_size.x / 2, + .x = camera_transform->position.x + camera_camera->viewport_size.x / 2 + OFFSCREEN_MARGIN, .y = Random::f(0.5, -0.5) * HALLWAY_HEIGHT, }; - zapper->place(pos, 0, Random::f(400, 200)); + bool horizontal = Random::b(); + float rotation = 90.0 * horizontal; + float length = horizontal ? Random::f(400, 200) : Random::f(200, 50); + + zapper->place(pos, rotation, length); + zapper->set_active(true); } OptionalRef ZapperPoolScript::get_next_zapper() { diff --git a/game/prefab/ZapperPoolScript.h b/game/prefab/ZapperPoolScript.h index 5daba79..6aee8b2 100644 --- a/game/prefab/ZapperPoolScript.h +++ b/game/prefab/ZapperPoolScript.h @@ -26,5 +26,8 @@ private: private: void spawn_random(); + +private: + static constexpr float OFFSCREEN_MARGIN = 40; }; diff --git a/game/prefab/ZapperPoolSubScene.cpp b/game/prefab/ZapperPoolSubScene.cpp index 923ed57..e341090 100644 --- a/game/prefab/ZapperPoolSubScene.cpp +++ b/game/prefab/ZapperPoolSubScene.cpp @@ -12,7 +12,7 @@ ZapperPoolSubScene::ZapperPoolSubScene(Scene & scene) Log::logf(Log::DEBUG, "Building zapper pool..."); vector pool; for (size_t i = 0; i < this->POOL_SIZE; i++) - pool.emplace_back(scene.new_object("zapper")); + pool.emplace_back(scene.new_object("zapper", "zapper")); BehaviorScript & behavior = this->controller.add_component(); behavior.set_script(std::move(pool)); } -- cgit v1.2.3 From f22a282b7863d35928fa3290f964ce3be5c0dbc9 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 8 Jan 2025 15:24:09 +0100 Subject: tweak zapper spawning logic --- game/prefab/ZapperObject.cpp | 3 --- game/prefab/ZapperPoolScript.cpp | 23 ++++++++++------------- game/prefab/ZapperPoolScript.h | 3 ++- 3 files changed, 12 insertions(+), 17 deletions(-) (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index 0a290e0..6139d53 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -81,12 +81,9 @@ ZapperObject::ZapperObject(crepe::GameObject && base) })}, collider {add_component(vec2(0, 0))} { this->set_active(false); - Log::logf(Log::DEBUG, "Creating zapper"); } void ZapperObject::place(const crepe::vec2 & position, float rotation, float length) { - Log::logf(Log::DEBUG, "Placing zapper [position = {}, rotation = {}, length = {}]", position, rotation, length); - this->transform.position = position; this->transform.rotation = rotation; diff --git a/game/prefab/ZapperPoolScript.cpp b/game/prefab/ZapperPoolScript.cpp index ac6ce96..168637c 100644 --- a/game/prefab/ZapperPoolScript.cpp +++ b/game/prefab/ZapperPoolScript.cpp @@ -30,33 +30,30 @@ void ZapperPoolScript::fixed_update(crepe::duration_t) { if (zapper.transform.position.x < threshold) zapper.set_active(false); } - - if (i-- > 0) return; - i = 200; - queue_event(); } void ZapperPoolScript::spawn_random() { OptionalRef zapper = this->get_next_zapper(); if (!zapper) return; // pool exhausted - vec2 pos = { - .x = camera_transform->position.x + camera_camera->viewport_size.x / 2 + OFFSCREEN_MARGIN, - .y = Random::f(0.5, -0.5) * HALLWAY_HEIGHT, - }; - bool horizontal = Random::b(); + vec2 pos; float rotation, length; + pos.x = camera_transform->position.x + camera_camera->viewport_size.x / 2 + OFFSCREEN_MARGIN; if (horizontal) { rotation = 90; length = Random::f(400, 200); + pos.y = Random::f(0.5, -0.5) * HALLWAY_HEIGHT; + // align zapper to right edge of camera + pos.x -= MAX_LENGTH - (length / 2); } else { rotation = 0; - length = Random::f(200, 50); - if (abs(pos.y) + length / 2 > HALLWAY_HEIGHT / 2) { - // TODO: fix offset - } + length = Random::f(200, 75); + // ensure zapper doesn't crash into ceiling or floor + pos.y = Random::f(0.5, -0.5) * (HALLWAY_HEIGHT - length); + // align zapper to right edge of camera + pos.x -= MAX_LENGTH; } zapper->place(pos, rotation, length); diff --git a/game/prefab/ZapperPoolScript.h b/game/prefab/ZapperPoolScript.h index 6aee8b2..dd60071 100644 --- a/game/prefab/ZapperPoolScript.h +++ b/game/prefab/ZapperPoolScript.h @@ -28,6 +28,7 @@ private: void spawn_random(); private: - static constexpr float OFFSCREEN_MARGIN = 40; + static constexpr float MAX_LENGTH = 400; + static constexpr float OFFSCREEN_MARGIN = 50 + MAX_LENGTH; }; -- cgit v1.2.3 From 12cf43dd062f41af226e0ac038b2f2838391015f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 8 Jan 2025 15:24:38 +0100 Subject: `make format` --- game/Config.h | 3 +-- game/Random.cpp | 5 +---- game/Random.h | 1 - game/menus/mainmenu/ITransitionScript.cpp | 2 +- game/menus/shop/ShopMenuScene.cpp | 3 +-- game/prefab/ZapperObject.cpp | 1 - game/prefab/ZapperPoolScript.cpp | 12 ++++++------ game/prefab/ZapperPoolScript.h | 1 - game/prefab/ZapperPoolSubScene.cpp | 5 ++--- game/prefab/ZapperPoolSubScene.h | 4 ++-- src/crepe/api/Vector2.hpp | 4 ++-- 11 files changed, 16 insertions(+), 25 deletions(-) (limited to 'game/prefab/ZapperObject.cpp') diff --git a/game/Config.h b/game/Config.h index 64f2828..580e9ec 100644 --- a/game/Config.h +++ b/game/Config.h @@ -58,5 +58,4 @@ static constexpr const char * PLAYER_NAME = "player"; static constexpr int PLAYER_SPEED = 7500; // In game units static constexpr int PLAYER_GRAVITY_SCALE = 60; // In game units -static constexpr const char* CAMERA_NAME = "camera"; - +static constexpr const char * CAMERA_NAME = "camera"; diff --git a/game/Random.cpp b/game/Random.cpp index 821ddc8..64cf1f3 100644 --- a/game/Random.cpp +++ b/game/Random.cpp @@ -26,7 +26,4 @@ unsigned Random::u(unsigned upper, unsigned lower) { return x + lower; } -bool Random::b() { - return rand() % 2; -} - +bool Random::b() { return rand() % 2; } diff --git a/game/Random.h b/game/Random.h index 4a1108a..94f98d2 100644 --- a/game/Random.h +++ b/game/Random.h @@ -8,4 +8,3 @@ public: static unsigned u(unsigned upper, unsigned lower = 0); static bool b(); }; - diff --git a/game/menus/mainmenu/ITransitionScript.cpp b/game/menus/mainmenu/ITransitionScript.cpp index 3e51a90..54b0875 100644 --- a/game/menus/mainmenu/ITransitionScript.cpp +++ b/game/menus/mainmenu/ITransitionScript.cpp @@ -1,8 +1,8 @@ #include "ITransitionScript.h" #include "MainMenuConfig.h" -#include "../MenusConfig.h" #include "../../Config.h" +#include "../MenusConfig.h" #include #include diff --git a/game/menus/shop/ShopMenuScene.cpp b/game/menus/shop/ShopMenuScene.cpp index f4d5e76..d4542ba 100644 --- a/game/menus/shop/ShopMenuScene.cpp +++ b/game/menus/shop/ShopMenuScene.cpp @@ -1,11 +1,10 @@ #include "ShopMenuScene.h" +#include "../../Config.h" #include "../BannerSubScene.h" #include "../ButtonSubScene.h" #include "../MenusConfig.h" -#include "../BannerSubScene.h" -#include "../../Config.h" #include #include diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index 6139d53..24bbbd2 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -116,4 +116,3 @@ void ZapperObject::set_active(bool active) { this->active = active; } - diff --git a/game/prefab/ZapperPoolScript.cpp b/game/prefab/ZapperPoolScript.cpp index 168637c..b832ddd 100644 --- a/game/prefab/ZapperPoolScript.cpp +++ b/game/prefab/ZapperPoolScript.cpp @@ -23,23 +23,24 @@ void ZapperPoolScript::init() { } void ZapperPoolScript::fixed_update(crepe::duration_t) { - float threshold = camera_transform->position.x - camera_camera->viewport_size.x / 2 - OFFSCREEN_MARGIN; + float threshold + = camera_transform->position.x - camera_camera->viewport_size.x / 2 - OFFSCREEN_MARGIN; for (ZapperObject & zapper : this->pool) { if (!zapper.active) continue; - if (zapper.transform.position.x < threshold) - zapper.set_active(false); + if (zapper.transform.position.x < threshold) zapper.set_active(false); } } void ZapperPoolScript::spawn_random() { OptionalRef zapper = this->get_next_zapper(); if (!zapper) return; // pool exhausted - + bool horizontal = Random::b(); vec2 pos; float rotation, length; - pos.x = camera_transform->position.x + camera_camera->viewport_size.x / 2 + OFFSCREEN_MARGIN; + pos.x + = camera_transform->position.x + camera_camera->viewport_size.x / 2 + OFFSCREEN_MARGIN; if (horizontal) { rotation = 90; @@ -67,4 +68,3 @@ OptionalRef ZapperPoolScript::get_next_zapper() { } return {}; } - diff --git a/game/prefab/ZapperPoolScript.h b/game/prefab/ZapperPoolScript.h index dd60071..2208c80 100644 --- a/game/prefab/ZapperPoolScript.h +++ b/game/prefab/ZapperPoolScript.h @@ -31,4 +31,3 @@ private: static constexpr float MAX_LENGTH = 400; static constexpr float OFFSCREEN_MARGIN = 50 + MAX_LENGTH; }; - diff --git a/game/prefab/ZapperPoolSubScene.cpp b/game/prefab/ZapperPoolSubScene.cpp index e341090..8422b8c 100644 --- a/game/prefab/ZapperPoolSubScene.cpp +++ b/game/prefab/ZapperPoolSubScene.cpp @@ -1,13 +1,13 @@ #include -#include "ZapperPoolSubScene.h" #include "ZapperPoolScript.h" +#include "ZapperPoolSubScene.h" using namespace crepe; using namespace std; ZapperPoolSubScene::ZapperPoolSubScene(Scene & scene) - : controller { scene.new_object("controller") } { + : controller {scene.new_object("controller")} { Log::logf(Log::DEBUG, "Building zapper pool..."); vector pool; @@ -16,4 +16,3 @@ ZapperPoolSubScene::ZapperPoolSubScene(Scene & scene) BehaviorScript & behavior = this->controller.add_component(); behavior.set_script(std::move(pool)); } - diff --git a/game/prefab/ZapperPoolSubScene.h b/game/prefab/ZapperPoolSubScene.h index f930e22..6f6e297 100644 --- a/game/prefab/ZapperPoolSubScene.h +++ b/game/prefab/ZapperPoolSubScene.h @@ -1,8 +1,8 @@ #pragma once -#include -#include #include +#include +#include #include class CreateZapperEvent : public crepe::Event {}; diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp index e2f96ed..30441d2 100644 --- a/src/crepe/api/Vector2.hpp +++ b/src/crepe/api/Vector2.hpp @@ -180,7 +180,7 @@ Vector2 Vector2::rotate(float deg) const { } // namespace crepe template -std::format_context::iterator std::formatter>::format(crepe::Vector2 vec, format_context & ctx) const { +std::format_context::iterator +std::formatter>::format(crepe::Vector2 vec, format_context & ctx) const { return formatter::format(std::format("{{{}, {}}}", vec.x, vec.y), ctx); } - -- cgit v1.2.3