Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Includes/CubbyTower/Commons/TowerData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions Includes/CubbyTower/Components/CollatzDamage.hpp
Original file line number Diff line number Diff line change
@@ -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_COLLATZ_DAMAGE_HPP
#define CUBBYTOWER_COLLATZ_DAMAGE_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_COLLATZ_DAMAGE_HPP
8 changes: 8 additions & 0 deletions Includes/CubbyTower/Helpers/ShootingHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ 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 The amount of collatz damage to give.
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.
Expand Down
12 changes: 12 additions & 0 deletions Includes/CubbyTower/Helpers/TowerHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ 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.
//! \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.
Expand Down
6 changes: 6 additions & 0 deletions Sources/CubbyTower/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}

Expand Down
63 changes: 62 additions & 1 deletion Sources/CubbyTower/Helpers/ShootingHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// property of any third parties.

#include <CubbyTower/Commons/Tags.hpp>
#include <CubbyTower/Components/CollatzDamage.hpp>
#include <CubbyTower/Components/Color.hpp>
#include <CubbyTower/Components/Damage.hpp>
#include <CubbyTower/Components/DeathTimer.hpp>
Expand Down Expand Up @@ -53,6 +54,48 @@ static void OnCollideArrow(entt::registry& registry, entt::entity entity)
registry.emplace<Color>(fxEntity, color);
}

static void OnCollideCollatz(entt::registry& registry, entt::entity entity)
{
auto impactPoint = registry.get<Position>(entity);
auto color = registry.get<Color>(entity);
auto collatzDamage = registry.get<CollatzDamage>(entity);

registry.destroy(entity);

registry.view<Health, Position>().each(
[&registry, 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)
{
int damage = collatzDamage.maxDamage;

if (health.curAmount % 2 == 0)
{
damage = std::min(damage, health.curAmount / 2);
}
else
{
damage = -(health.curAmount * 2 + 1);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음수 데미지가 바람직한 방식인지는 모르겠습니다.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

구현 방향을 정하기 나름일 거 같은데, 패널티를 어떻게 주고 싶음?
데미지를 0으로 만들기 또는 체력을 오히려 회복하기

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

회복하는게 의도입니다.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

흠... 데미지를 -로 주는 건 체력을 회복하는 의도와는 맞지 않으니, 별도로 체력을 회복하는 로직을 넣는게 좋겠다고 생각해. GiveDamage()가 아닌 TakeHeal()라는 이름의 함수를 하나 만들어서 처리합시다.

}

GiveDamage(registry, entity, damage);
}
});

auto fxEntity = registry.create();
registry.emplace<Position>(fxEntity, impactPoint);
registry.emplace<DeathTimer>(fxEntity, 0.4f);
registry.emplace<Size>(fxEntity, 0.5f);
registry.emplace<ShapeRenderer>(fxEntity, Shape::DrawCircle);
registry.emplace<SizePulseAnim>(fxEntity, 0.0f, 25.0f, 0.15f, 0.5f);
registry.emplace<Color>(fxEntity, color);
}

void CreateArrow(entt::registry& registry, const Position& from,
const Position& to, int damage)
{
Expand All @@ -70,6 +113,23 @@ void CreateArrow(entt::registry& registry, const Position& from,
registry.emplace<Damage>(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<Position>(entity, from);
registry.emplace<PositionAnim>(entity, from, to, 0.0f, len / 10.0f,
OnCollideCollatz);
registry.emplace<ShapeRenderer>(entity, Shape::DrawBox);
registry.emplace<Size>(entity, 0.15f, 0.15f);
registry.emplace<Color>(entity, Color{ 0.0f, 1.0f, 1.0f, 1.0f });
registry.emplace<CollatzDamage>(entity, collatzDamage);
}

void GiveDamage(entt::registry& registry, entt::entity& target, int damage)
{
if (!registry.all_of<Health>(target))
Expand All @@ -79,6 +139,7 @@ void GiveDamage(entt::registry& registry, entt::entity& target, int damage)

auto& health = registry.get<Health>(target);
health.curAmount -= damage;
health.curAmount = std::min(health.curAmount, health.maxAmount);

if (health.curAmount <= 0)
{
Expand All @@ -92,4 +153,4 @@ void Kill(entt::registry& registry, entt::entity& target)

registry.destroy(target);
}
} // namespace CubbyTower::Shooting
} // namespace CubbyTower::Shooting
34 changes: 34 additions & 0 deletions Sources/CubbyTower/Helpers/TowerHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <CubbyTower/Commons/Constants.hpp>
#include <CubbyTower/Commons/Tags.hpp>
#include <CubbyTower/Commons/TowerData.hpp>
#include <CubbyTower/Components/CollatzDamage.hpp>
#include <CubbyTower/Components/Color.hpp>
#include <CubbyTower/Components/FindTarget.hpp>
#include <CubbyTower/Components/Hoverable.hpp>
Expand Down Expand Up @@ -91,6 +92,39 @@ 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<Tag::Player>()[0],
COLLATZ_TOWER_PRICE))
{
return;
}

auto entity = registry.create();
registry.emplace<Tag::Tower>(entity);
registry.emplace<Position>(entity, position);
registry.emplace<Size>(entity, TOWER_SIZE, TOWER_SIZE);
registry.emplace<Color>(entity, TOWER_LEVEL1_COLOR);
registry.emplace<TextRenderer>(entity, "C", Color{ 0.0f, 0.0f, 0.0f, 0.0f },
0.5f);
registry.emplace<ShapeRenderer>(entity, Shape::DrawBox);
registry.emplace<Name>(entity, "Collatz Tower(WIP)");
registry.emplace<Targeter>(entity, TargetMask{ GROUND | AIR },
210.0f / 60.0f, 1.0f, ShootCollatz);
registry.emplace<Hoverable>(entity);
registry.emplace<FindTarget>(entity, FindFirstTarget);
}

void ShootCollatz(entt::registry& registry, entt::entity target,
entt::entity from)
{
Shooting::CreateCollatz(registry, registry.get<Position>(from),
registry.get<Position>(target),
CollatzDamage{ 100 });
Audio::PlaySound("arrow");
}

entt::entity FindFirstTarget(entt::registry& registry)
{
return entt::null;
Expand Down