From d2c8a2bf07275f5458c576990b8742a710b15013 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 19 Dec 2024 20:16:30 +0100 Subject: is_nan fix --- src/crepe/api/Vector2.hpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/crepe/api/Vector2.hpp') diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp index ff53cb0..a470595 100644 --- a/src/crepe/api/Vector2.hpp +++ b/src/crepe/api/Vector2.hpp @@ -163,4 +163,9 @@ Vector2 Vector2::perpendicular() const { return {-y, x}; } +template +bool Vector2::is_nan() const { + return std::isnan(x) && std::isnan(y); +} + } // namespace crepe -- cgit v1.2.3 From 3894948275e10b6a0e3614ba0da90b9ea8d6cd4e Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 19 Dec 2024 20:27:39 +0100 Subject: make format --- src/crepe/api/Vector2.hpp | 2 +- src/crepe/system/CollisionSystem.cpp | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'src/crepe/api/Vector2.hpp') diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp index a470595..e195760 100644 --- a/src/crepe/api/Vector2.hpp +++ b/src/crepe/api/Vector2.hpp @@ -165,7 +165,7 @@ Vector2 Vector2::perpendicular() const { template bool Vector2::is_nan() const { - return std::isnan(x) && std::isnan(y); + return std::isnan(x) && std::isnan(y); } } // namespace crepe diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 0bceac7..e4396b9 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -168,7 +168,7 @@ bool CollisionSystem::detect_collision(CollisionInternal & self, CollisionIntern // Get resolution vector from box-box collision detection resolution = this->get_box_box_detection(BOX1, BOX2); // If no collision (NaN values), return false - if(resolution.is_nan()) return false; + if (resolution.is_nan()) return false; break; } case CollisionInternalType::BOX_CIRCLE: { @@ -184,7 +184,7 @@ bool CollisionSystem::detect_collision(CollisionInternal & self, CollisionIntern // Get resolution vector from box-circle collision detection resolution = this->get_box_circle_detection(BOX1, CIRCLE2); // If no collision (NaN values), return false - if(resolution.is_nan()) return false; + if (resolution.is_nan()) return false; // Invert the resolution vector for proper collision response resolution = -resolution; break; @@ -202,7 +202,7 @@ bool CollisionSystem::detect_collision(CollisionInternal & self, CollisionIntern // Get resolution vector from circle-circle collision detection resolution = this->get_circle_circle_detection(CIRCLE1, CIRCLE2); // If no collision (NaN values), return false - if(resolution.is_nan()) return false; + if (resolution.is_nan()) return false; break; } case CollisionInternalType::CIRCLE_BOX: { @@ -218,7 +218,7 @@ bool CollisionSystem::detect_collision(CollisionInternal & self, CollisionIntern // Get resolution vector from box-circle collision detection (order swapped) resolution = this->get_box_circle_detection(BOX2, CIRCLE1); // If no collision (NaN values), return false - if(resolution.is_nan()) return false; + if (resolution.is_nan()) return false; break; } case CollisionInternalType::NONE: @@ -239,7 +239,8 @@ bool CollisionSystem::detect_collision(CollisionInternal & self, CollisionIntern return true; } -vec2 CollisionSystem::get_box_box_detection(const BoxColliderInternal & box1, const BoxColliderInternal & box2) const { +vec2 CollisionSystem::get_box_box_detection(const BoxColliderInternal & box1, + const BoxColliderInternal & box2) const { vec2 resolution{NAN, NAN}; // Get current positions of colliders vec2 pos1 = AbsolutePosition::get_position(box1.transform, box1.collider.offset); @@ -370,7 +371,8 @@ vec2 CollisionSystem::get_circle_circle_detection( return resolution; } // No collision - return vec2{NAN, NAN};; + return vec2{NAN, NAN}; + ; } CollisionSystem::Direction -- 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 'src/crepe/api/Vector2.hpp') 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 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 'src/crepe/api/Vector2.hpp') 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 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 'src/crepe/api/Vector2.hpp') 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