From 7c78473d77bc387c6416eba5f218c3b91d72d196 Mon Sep 17 00:00:00 2001 From: IHIHHIHI Date: Sun, 1 Jan 2023 21:14:26 +0900 Subject: [PATCH 1/7] Add Collatz Damage Component and add function OncollideCollatz --- .../CubbyTower/Components/CollatzDamage.hpp | 23 ++++++++++ .../CubbyTower/Helpers/ShootingHelpers.cpp | 45 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 Includes/CubbyTower/Components/CollatzDamage.hpp diff --git a/Includes/CubbyTower/Components/CollatzDamage.hpp b/Includes/CubbyTower/Components/CollatzDamage.hpp new file mode 100644 index 0000000..c6c2b24 --- /dev/null +++ b/Includes/CubbyTower/Components/CollatzDamage.hpp @@ -0,0 +1,23 @@ +// Copyright (c) 2021 CubbyTower Team +// Chris Ohk, Minkyu Lee, Minjune Yi +// We are making my contributions/submissions to this project solely in our +// personal capacity and are not conveying any rights to any intellectual +// property of any third parties. + +#ifndef CUBBYTOWER_COLLATZDAMAGE_HPP +#define CUBBYTOWER_COLLATZDAMAGE_HPP + +namespace CubbyTower +{ +//! +//! \brief Damage struct for Collatz tower. +//! +//! This struct stores the max damage of the tower. +//! +struct CollatzDamage +{ + int maxDamage; +}; +} // namespace CubbyTower + +#endif // CUBBYTOWER_DAMAGE_HPP \ No newline at end of file diff --git a/Sources/CubbyTower/Helpers/ShootingHelpers.cpp b/Sources/CubbyTower/Helpers/ShootingHelpers.cpp index 4c8b021..899a25c 100644 --- a/Sources/CubbyTower/Helpers/ShootingHelpers.cpp +++ b/Sources/CubbyTower/Helpers/ShootingHelpers.cpp @@ -5,6 +5,7 @@ // property of any third parties. #include +#include #include #include #include @@ -53,6 +54,48 @@ static void OnCollideArrow(entt::registry& registry, entt::entity entity) registry.emplace(fxEntity, color); } +static void OnCollideCollatz(entt::registry& registry, entt::entity entity) +{ + auto impactPoint = registry.get(entity); + auto color = registry.get(entity); + auto collatzDamage = registry.get(entity); + + registry.destroy(entity); + + registry.view().each( + [®istry, impactPoint, collatzDamage]( + entt::entity entity, [[maybe_unused]] const Health& health, + const Position& position) { + auto dx = position.x - impactPoint.x; + auto dy = position.y - impactPoint.y; + auto dist = dx * dx + dy * dy; + + if (dist < 0.5f) + { + auto health = registry.get(entity); + int damage = collatzDamage.maxDamage; + if (health.curAmount % 2 == 0) + { + if (damage > health.curAmount / 2) + damage = health.curAmount / 2; + } + else + { + damage = -(health.curAmount * 2 + 1); + } + GiveDamage(registry, entity, damage); + } + }); + + auto fxEntity = registry.create(); + registry.emplace(fxEntity, impactPoint); + registry.emplace(fxEntity, 0.4f); + registry.emplace(fxEntity, 0.5f); + registry.emplace(fxEntity, Shape::DrawCircle); + registry.emplace(fxEntity, 0.0f, 25.0f, 0.15f, 0.5f); + registry.emplace(fxEntity, color); +} + void CreateArrow(entt::registry& registry, const Position& from, const Position& to, int damage) { @@ -79,6 +122,8 @@ void GiveDamage(entt::registry& registry, entt::entity& target, int damage) auto& health = registry.get(target); health.curAmount -= damage; + if (health.curAmount > health.maxAmount) + health.curAmount = health.maxAmount; if (health.curAmount <= 0) { From 4b3683077a30eca64b308a898aa92163fa2949ab Mon Sep 17 00:00:00 2001 From: IHIHHIHI Date: Sun, 1 Jan 2023 21:27:31 +0900 Subject: [PATCH 2/7] Add CreateCollatz Function --- Includes/CubbyTower/Helpers/ShootingHelpers.hpp | 9 +++++++++ Sources/CubbyTower/Helpers/ShootingHelpers.cpp | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Includes/CubbyTower/Helpers/ShootingHelpers.hpp b/Includes/CubbyTower/Helpers/ShootingHelpers.hpp index 2afb32b..1c0c990 100644 --- a/Includes/CubbyTower/Helpers/ShootingHelpers.hpp +++ b/Includes/CubbyTower/Helpers/ShootingHelpers.hpp @@ -24,6 +24,15 @@ namespace Shooting void CreateArrow(entt::registry& registry, const Position& from, const Position& to, int damage); +//! Creates an Collatz entity to shoot a target. +//! \param registry A registry that handles entities. +//! \param from The start position of the collatz for the collatz tower. +//! \param to The end position of the collatz for target. +//! \param collatzDamage Handle information about the collatz entity should give +//! damage +void CreateCollatz(entt::registry& registry, const Position& from, + const Position& to, CollatzDamage collatzDamage); + //! Gives a damage to a target. //! \param registry A registry that handles entities. //! \param target A target that takes a damage. diff --git a/Sources/CubbyTower/Helpers/ShootingHelpers.cpp b/Sources/CubbyTower/Helpers/ShootingHelpers.cpp index 899a25c..9dfb605 100644 --- a/Sources/CubbyTower/Helpers/ShootingHelpers.cpp +++ b/Sources/CubbyTower/Helpers/ShootingHelpers.cpp @@ -113,6 +113,23 @@ void CreateArrow(entt::registry& registry, const Position& from, registry.emplace(entity, damage); } +void CreateCollatz(entt::registry& registry, const Position& from, + const Position& to, CollatzDamage collatzDamage) +{ + const float dx = to.x - from.x; + const float dy = to.y - from.y; + const float len = std::sqrt(dx * dx + dy * dy); + + auto entity = registry.create(); + registry.emplace(entity, from); + registry.emplace(entity, from, to, 0.0f, len / 10.0f, + OnCollideCollatz); + registry.emplace(entity, Shape::DrawBox); + registry.emplace(entity, 0.15f, 0.15f); + registry.emplace(entity, Color{ 0.0f, 1.0f, 1.0f, 1.0f }); + registry.emplace(entity, collatzDamage); +} + void GiveDamage(entt::registry& registry, entt::entity& target, int damage) { if (!registry.all_of(target)) From 43112d0fd0d4ffdee6d5d68d9402371d278b5407 Mon Sep 17 00:00:00 2001 From: IHIHHIHI Date: Sun, 1 Jan 2023 21:31:16 +0900 Subject: [PATCH 3/7] Add ShootCollatz Function --- Includes/CubbyTower/Helpers/TowerHelpers.hpp | 7 +++++++ Sources/CubbyTower/Helpers/TowerHelpers.cpp | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/Includes/CubbyTower/Helpers/TowerHelpers.hpp b/Includes/CubbyTower/Helpers/TowerHelpers.hpp index bce9a14..bfcc698 100644 --- a/Includes/CubbyTower/Helpers/TowerHelpers.hpp +++ b/Includes/CubbyTower/Helpers/TowerHelpers.hpp @@ -48,6 +48,13 @@ void ShootArrowLv1(entt::registry& registry, entt::entity target, void ShootArrowLv2(entt::registry& registry, entt::entity target, entt::entity from); +// Shoots a collatz instance for collatz tower. +//! \param registry A registry that handles entities. +//! \param target A target entity to shoot. +//! \param from A tower entity that shoots collatz instance. +void ShootCollatz(entt::registry& registry, entt::entity target, + entt::entity from); + //! Finds a first target within a range. //! \param registry A registry that handles entities. //! \return a first target if it is within a range, entt::null otherwise. diff --git a/Sources/CubbyTower/Helpers/TowerHelpers.cpp b/Sources/CubbyTower/Helpers/TowerHelpers.cpp index c07e6bd..959d8c5 100644 --- a/Sources/CubbyTower/Helpers/TowerHelpers.cpp +++ b/Sources/CubbyTower/Helpers/TowerHelpers.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -91,6 +92,15 @@ void ShootArrowLv2(entt::registry& registry, entt::entity target, // Do nothing } +void ShootCollatz(entt::registry& registry, entt::entity target, + entt::entity from) +{ + Shooting::CreateCollatz(registry, registry.get(from), + registry.get(target), + CollatzDamage{ 100 }); + Audio::PlaySound("arrow"); +} + entt::entity FindFirstTarget(entt::registry& registry) { return entt::null; From 7b5c8061af3466312f4568d77bfac3757591f153 Mon Sep 17 00:00:00 2001 From: IHIHHIHI Date: Sun, 1 Jan 2023 21:36:52 +0900 Subject: [PATCH 4/7] Add BuyCollatzTower function --- Includes/CubbyTower/Commons/TowerData.hpp | 3 +++ Includes/CubbyTower/Helpers/TowerHelpers.hpp | 5 ++++ Sources/CubbyTower/Helpers/TowerHelpers.cpp | 24 ++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/Includes/CubbyTower/Commons/TowerData.hpp b/Includes/CubbyTower/Commons/TowerData.hpp index c531475..d34f9b8 100644 --- a/Includes/CubbyTower/Commons/TowerData.hpp +++ b/Includes/CubbyTower/Commons/TowerData.hpp @@ -14,6 +14,9 @@ constexpr static int ARROW_TOWER_LV1_PRICE = 100; //! The price of arrow tower at level 2. constexpr static int ARROW_TOWER_LV2_PRICE = 200; + +//! The price of collatz tower. +constexpr static int COLLATZ_TOWER_PRICE = 500; } // namespace CubbyTower #endif // CUBBYTOWER_TOWER_DATA_HPP \ No newline at end of file diff --git a/Includes/CubbyTower/Helpers/TowerHelpers.hpp b/Includes/CubbyTower/Helpers/TowerHelpers.hpp index bfcc698..ab34583 100644 --- a/Includes/CubbyTower/Helpers/TowerHelpers.hpp +++ b/Includes/CubbyTower/Helpers/TowerHelpers.hpp @@ -48,6 +48,11 @@ void ShootArrowLv1(entt::registry& registry, entt::entity target, void ShootArrowLv2(entt::registry& registry, entt::entity target, entt::entity from); +//! Buys a collatz tower. +//! \param registry A registry that handles entities. +//! \param position The position to place a collatz tower. +void BuyCollatzTower(entt::registry& registry, const Position& position); + // Shoots a collatz instance for collatz tower. //! \param registry A registry that handles entities. //! \param target A target entity to shoot. diff --git a/Sources/CubbyTower/Helpers/TowerHelpers.cpp b/Sources/CubbyTower/Helpers/TowerHelpers.cpp index 959d8c5..f1c4b5e 100644 --- a/Sources/CubbyTower/Helpers/TowerHelpers.cpp +++ b/Sources/CubbyTower/Helpers/TowerHelpers.cpp @@ -92,6 +92,30 @@ void ShootArrowLv2(entt::registry& registry, entt::entity target, // Do nothing } +void BuyCollatzTower(entt::registry& registry, const Position& position) +{ + // Check the player can buy arrow tower + if (!Bank::Withdraw(registry, registry.view()[0], + COLLATZ_TOWER_PRICE)) + { + return; + } + + auto entity = registry.create(); + registry.emplace(entity); + registry.emplace(entity, position); + registry.emplace(entity, TOWER_SIZE, TOWER_SIZE); + registry.emplace(entity, TOWER_LEVEL1_COLOR); + registry.emplace(entity, "C", Color{ 0.0f, 0.0f, 0.0f, 0.0f }, + 0.5f); + registry.emplace(entity, Shape::DrawBox); + registry.emplace(entity, "Collatz Tower(WIP)"); + registry.emplace(entity, TargetMask{ GROUND | AIR }, + 210.0f / 60.0f, 1.0f, ShootCollatz); + registry.emplace(entity); + registry.emplace(entity, FindFirstTarget); +} + void ShootCollatz(entt::registry& registry, entt::entity target, entt::entity from) { From fbbfb555419caf1f3170487457aed26ccea97a85 Mon Sep 17 00:00:00 2001 From: IHIHHIHI Date: Sun, 1 Jan 2023 21:45:41 +0900 Subject: [PATCH 5/7] Add Button for Collatz Tower --- Sources/CubbyTower/Game.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/CubbyTower/Game.cpp b/Sources/CubbyTower/Game.cpp index a5bda5f..8224dce 100644 --- a/Sources/CubbyTower/Game.cpp +++ b/Sources/CubbyTower/Game.cpp @@ -137,6 +137,12 @@ void Initialize(entt::registry& registry) [](entt::registry& registry, entt::entity button) { Tower::CreatePlacer(registry, Tower::BuyArrowTower); }); + + UI::CreateTowerButton( + registry, "Collatz Tower", { -1, 15.5f }, COLLATZ_TOWER_PRICE, + [](entt::registry& registry, entt::entity button) { + Tower::CreatePlacer(registry, Tower::BuyCollatzTower); + }); } } From d568a2f1471076fce9828c49b80d43482ee8905b Mon Sep 17 00:00:00 2001 From: MinJune Yi <52493099+IHIHHIHI@users.noreply.github.com> Date: Sun, 1 Jan 2023 22:18:53 +0900 Subject: [PATCH 6/7] Remove Health component --- Sources/CubbyTower/Helpers/ShootingHelpers.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/CubbyTower/Helpers/ShootingHelpers.cpp b/Sources/CubbyTower/Helpers/ShootingHelpers.cpp index 9dfb605..bc8d232 100644 --- a/Sources/CubbyTower/Helpers/ShootingHelpers.cpp +++ b/Sources/CubbyTower/Helpers/ShootingHelpers.cpp @@ -72,7 +72,6 @@ static void OnCollideCollatz(entt::registry& registry, entt::entity entity) if (dist < 0.5f) { - auto health = registry.get(entity); int damage = collatzDamage.maxDamage; if (health.curAmount % 2 == 0) { @@ -154,4 +153,4 @@ void Kill(entt::registry& registry, entt::entity& target) registry.destroy(target); } -} // namespace CubbyTower::Shooting \ No newline at end of file +} // namespace CubbyTower::Shooting From 2fa6f30c305535a98597f2800a92a53a58dd0294 Mon Sep 17 00:00:00 2001 From: MinJune Yi <52493099+IHIHHIHI@users.noreply.github.com> Date: Sat, 28 Jan 2023 22:20:51 +0900 Subject: [PATCH 7/7] Apply suggestions from code review Co-authored-by: Chris Ohk --- Includes/CubbyTower/Components/CollatzDamage.hpp | 6 +++--- Includes/CubbyTower/Helpers/ShootingHelpers.hpp | 3 +-- Sources/CubbyTower/Helpers/ShootingHelpers.cpp | 8 ++++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Includes/CubbyTower/Components/CollatzDamage.hpp b/Includes/CubbyTower/Components/CollatzDamage.hpp index c6c2b24..28e7eff 100644 --- a/Includes/CubbyTower/Components/CollatzDamage.hpp +++ b/Includes/CubbyTower/Components/CollatzDamage.hpp @@ -4,8 +4,8 @@ // personal capacity and are not conveying any rights to any intellectual // property of any third parties. -#ifndef CUBBYTOWER_COLLATZDAMAGE_HPP -#define CUBBYTOWER_COLLATZDAMAGE_HPP +#ifndef CUBBYTOWER_COLLATZ_DAMAGE_HPP +#define CUBBYTOWER_COLLATZ_DAMAGE_HPP namespace CubbyTower { @@ -20,4 +20,4 @@ struct CollatzDamage }; } // namespace CubbyTower -#endif // CUBBYTOWER_DAMAGE_HPP \ No newline at end of file +#endif // CUBBYTOWER_COLLATZ_DAMAGE_HPP \ No newline at end of file diff --git a/Includes/CubbyTower/Helpers/ShootingHelpers.hpp b/Includes/CubbyTower/Helpers/ShootingHelpers.hpp index 1c0c990..f1e559e 100644 --- a/Includes/CubbyTower/Helpers/ShootingHelpers.hpp +++ b/Includes/CubbyTower/Helpers/ShootingHelpers.hpp @@ -28,8 +28,7 @@ void CreateArrow(entt::registry& registry, const Position& from, //! \param registry A registry that handles entities. //! \param from The start position of the collatz for the collatz tower. //! \param to The end position of the collatz for target. -//! \param collatzDamage Handle information about the collatz entity should give -//! damage +//! \param collatzDamage The amount of collatz damage to give. void CreateCollatz(entt::registry& registry, const Position& from, const Position& to, CollatzDamage collatzDamage); diff --git a/Sources/CubbyTower/Helpers/ShootingHelpers.cpp b/Sources/CubbyTower/Helpers/ShootingHelpers.cpp index bc8d232..7597f1f 100644 --- a/Sources/CubbyTower/Helpers/ShootingHelpers.cpp +++ b/Sources/CubbyTower/Helpers/ShootingHelpers.cpp @@ -73,15 +73,16 @@ static void OnCollideCollatz(entt::registry& registry, entt::entity entity) if (dist < 0.5f) { int damage = collatzDamage.maxDamage; + if (health.curAmount % 2 == 0) { - if (damage > health.curAmount / 2) - damage = health.curAmount / 2; + damage = std::min(damage, health.curAmount / 2); } else { damage = -(health.curAmount * 2 + 1); } + GiveDamage(registry, entity, damage); } }); @@ -138,8 +139,7 @@ void GiveDamage(entt::registry& registry, entt::entity& target, int damage) auto& health = registry.get(target); health.curAmount -= damage; - if (health.curAmount > health.maxAmount) - health.curAmount = health.maxAmount; + health.curAmount = std::min(health.curAmount, health.maxAmount); if (health.curAmount <= 0) {