Skip to content

Commit

Permalink
Elemental type system prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
justindriggers committed Oct 19, 2023
1 parent 0b3bf2a commit 0f27bbb
Show file tree
Hide file tree
Showing 20 changed files with 380 additions and 226 deletions.
5 changes: 2 additions & 3 deletions linguine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ add_library(linguine
src/Engine.cpp
src/data/Grid.cpp
src/data/spells/actions/Apply.cpp
src/data/spells/actions/Heal.cpp
src/data/spells/effects/DamageOverTime.cpp
src/data/spells/actions/ModifyHealth.cpp
src/data/spells/effects/Effect.cpp
src/data/spells/effects/HealOverTime.cpp
src/data/spells/effects/ModifyHealthOverTime.cpp
src/entity/archetype/Archetype.cpp
src/entity/archetype/ArchetypeEntityManager.cpp
src/entity/archetype/ArchetypeResult.cpp
Expand Down
11 changes: 11 additions & 0 deletions linguine/src/components/UnitType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "data/spells/Type.h"

namespace linguine {

struct UnitType {
Type type;
};

} // namespace linguine
10 changes: 6 additions & 4 deletions linguine/src/data/spells/Spell.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
#include <glm/vec3.hpp>
#include <utility>

#include "Category.h"
#include "Type.h"
#include "actions/Action.h"

namespace linguine {

struct Spell {
Spell(std::string name, float castTime, float cooldown, Category category, glm::vec3 color, std::string tooltip, std::unique_ptr<Action> action)
: name(std::move(name)), castTime(castTime), cooldown(cooldown), category(category), color(color), tooltip(std::move(tooltip)), action(std::move(action)) {}
Spell(std::string name, float castTime, float cooldown, Type type, glm::vec3 color, std::string tooltip, std::unique_ptr<Action> action)
: name(std::move(name)), castTime(castTime), cooldown(cooldown), type(type), color(color), tooltip(std::move(tooltip)), action(std::move(action)) {

}

std::string name;
float castTime;
float cooldown;
Category category;
Type type;
glm::vec3 color;
std::string tooltip;
std::unique_ptr<Action> action;
Expand Down
13 changes: 6 additions & 7 deletions linguine/src/data/spells/SpellDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

#include "Spell.h"
#include "actions/Apply.h"
#include "actions/Heal.h"
#include "effects/DamageOverTime.h"
#include "effects/HealOverTime.h"
#include "actions/ModifyHealth.h"
#include "effects/ModifyHealthOverTime.h"

namespace linguine {

Expand All @@ -15,10 +14,10 @@ class SpellDatabase {
SpellDatabase(ServiceLocator& serviceLocator,
EntityManager& entityManager)
: _spells {
{ 0, std::make_shared<Spell>("Infuse", 0.0f, 6.0f, Category::Defense, glm::vec3(1.0f, 0.7991f, 0.12214f), "Cast time Instant\nCooldown 6s\nPower 175", std::make_unique<Heal>(entityManager, 50)) },
{ 1, std::make_shared<Spell>("Restore", 2.5f, 0.0f, Category::Defense, glm::vec3(0.12477f, 0.56471f, 0.07421f), "Cast time 2.5s\nCooldown None\nPower 250",std::make_unique<Heal>(entityManager, 75)) },
{ 2, std::make_shared<Spell>("Siphon", 1.5f, 0.0f, Category::Defense, glm::vec3(0.02519f, 0.80695f, 0.90466f), "Cast time 1.5s\nCooldown None\nPower 50/s for 12s",std::make_unique<Apply>(serviceLocator, entityManager, std::make_unique<HealOverTime>(entityManager, glm::vec3(0.02519f, 0.80695f, 0.90466f), 12.0f, 6, 25))) },
{ 3, std::make_shared<Spell>("Scorch", 1.5f, 0.0f, Category::Offense, glm::vec3(0.78354f, 0.04374f, 0.05781f), "Cast time 1.5s\nCooldown None\nPower 35/s for 18s",std::make_unique<Apply>(serviceLocator, entityManager, std::make_unique<DamageOverTime>(entityManager, glm::vec3(0.78354f, 0.04374f, 0.05781f), 18.0f, 6, 50))) }
{ 0, std::make_shared<Spell>("Spark", 0.0f, 6.0f, Type::Storm, glm::vec3(1.0f, 0.7991f, 0.12214f), "Cooldown 6s\nPower 175", std::make_unique<ModifyHealth>(entityManager, Type::Storm, 50)) },
{ 1, std::make_shared<Spell>("Growth", 0.0f, 3.5f, Type::Growth, glm::vec3(0.12477f, 0.56471f, 0.07421f), "Cooldown 2.5s\nPower 250",std::make_unique<ModifyHealth>(entityManager, Type::Growth, 75)) },
{ 2, std::make_shared<Spell>("Splash", 0.0f, 4.0f, Type::Water, glm::vec3(0.02519f, 0.80695f, 0.90466f), "Cooldown 1.5s\nPower 50/s for 12s",std::make_unique<Apply>(serviceLocator, entityManager, std::make_unique<ModifyHealthOverTime>(entityManager, Type::Water, glm::vec3(0.02519f, 0.80695f, 0.90466f), 12.0f, 6, 25))) },
{ 3, std::make_shared<Spell>("Scorch", 0.0f, 4.0f, Type::Fire, glm::vec3(0.78354f, 0.04374f, 0.05781f), "Cooldown 1.5s\nPower 35/s for 18s",std::make_unique<Apply>(serviceLocator, entityManager, std::make_unique<ModifyHealthOverTime>(entityManager, Type::Fire, glm::vec3(0.78354f, 0.04374f, 0.05781f), 18.0f, 6, 35))) }
} {}

Spell& getSpellById(uint64_t id) {
Expand Down
14 changes: 14 additions & 0 deletions linguine/src/data/spells/Type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

namespace linguine {

enum Type {
Fire,
Water,
Storm,
Earth,
Decay,
Growth
};

} // namespace linguine
91 changes: 91 additions & 0 deletions linguine/src/data/spells/TypeEvaluator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#pragma once

#include "Type.h"

namespace linguine {

class TypeEvaluator {
public:
static float calculateModifier(Type spellType, Type targetType) {
switch (targetType) {
case Fire:
switch (spellType) {
case Fire:
case Storm:
return Heal;
case Water:
case Earth:
return Weak;
default:
return Base;
}
case Water:
switch (spellType) {
case Fire:
return Resist;
case Water:
return Heal;
case Storm:
return Weak;
default:
return Base;
}
case Storm:
switch (spellType) {
case Storm:
return Heal;
default:
return Base;
}
case Earth:
switch (spellType) {
case Fire:
case Storm:
return Resist;
case Water:
case Growth:
return Weak;
case Earth:
return Heal;
default:
return Base;
}
break;
case Decay:
switch (spellType) {
case Fire:
case Growth:
return Weak;
case Decay:
return Heal;
default:
return Base;
}
break;
case Growth:
switch (spellType) {
case Fire:
case Decay:
return Weak;
case Water:
case Growth:
return Heal;
case Earth:
return Resist;
default:
return Base;
}
break;
default:
throw std::runtime_error("Unsupported type application");
}
}

private:
constexpr static float Base = -1.0f;
constexpr static float Resist = -0.5f;
constexpr static float Weak = -2.0f;
constexpr static float Heal = 1.0f;
};

} // namespace linguine
15 changes: 0 additions & 15 deletions linguine/src/data/spells/actions/Heal.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions linguine/src/data/spells/actions/Heal.h

This file was deleted.

23 changes: 23 additions & 0 deletions linguine/src/data/spells/actions/ModifyHealth.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "ModifyHealth.h"

#include <glm/common.hpp>

#include "components/Health.h"
#include "components/UnitType.h"
#include "data/spells/TypeEvaluator.h"
#include "entity/Result.h"

namespace linguine {

void ModifyHealth::execute(Entity& target) {
auto health = target.get<Health>();
auto targetType = target.get<UnitType>();

auto modifier = static_cast<int32_t>(
glm::round(static_cast<float>(_power) * TypeEvaluator::calculateModifier(_type, targetType->type))
);

health->current = glm::min(health->current + modifier, health->max);
}

} // namespace linguine
25 changes: 25 additions & 0 deletions linguine/src/data/spells/actions/ModifyHealth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "Action.h"

#include "data/spells/Type.h"
#include "entity/EntityManager.h"

namespace linguine {

class ModifyHealth : public Action {
public:
ModifyHealth(EntityManager& entityManager, Type type, int32_t power)
: _entityManager(entityManager), _type(type), _power(power) {}

~ModifyHealth() override = default;

void execute(Entity& target) override;

private:
EntityManager& _entityManager;
Type _type;
int32_t _power;
};

} // namespace linguine
17 changes: 0 additions & 17 deletions linguine/src/data/spells/effects/DamageOverTime.cpp

This file was deleted.

12 changes: 6 additions & 6 deletions linguine/src/data/spells/effects/Effect.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <glm/vec3.hpp>

#include "data/spells/Category.h"
#include "data/spells/Type.h"
#include "entity/Component.h"

namespace linguine {
Expand All @@ -13,8 +13,8 @@ class Effect {
public:
virtual ~Effect() = default;

[[nodiscard]] Category getCategory() const {
return _category;
[[nodiscard]] Type getType() const {
return _type;
}

[[nodiscard]] glm::vec3 getColor() const {
Expand All @@ -38,11 +38,11 @@ class Effect {
virtual void onRemove(Component<EffectTracker>& tracker) = 0;

protected:
Effect(Category category, glm::vec3 color, float duration, uint32_t ticks)
: _category(category), _color(color), _duration(duration), _ticks(ticks) {}
Effect(Type type, glm::vec3 color, float duration, uint32_t ticks)
: _type(type), _color(color), _duration(duration), _ticks(ticks) {}

private:
Category _category;
Type _type;
glm::vec3 _color;
float _duration;
uint32_t _ticks;
Expand Down
17 changes: 0 additions & 17 deletions linguine/src/data/spells/effects/HealOverTime.cpp

This file was deleted.

27 changes: 0 additions & 27 deletions linguine/src/data/spells/effects/HealOverTime.h

This file was deleted.

25 changes: 25 additions & 0 deletions linguine/src/data/spells/effects/ModifyHealthOverTime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "ModifyHealthOverTime.h"

#include <glm/common.hpp>

#include "components/EffectTracker.h"
#include "components/Health.h"
#include "components/UnitType.h"
#include "data/spells/TypeEvaluator.h"
#include "entity/Entity.h"

namespace linguine {

void ModifyHealthOverTime::onTick(Component<EffectTracker>& tracker) {
auto target = _entityManager.getById(tracker->targetId);
auto health = target->get<Health>();
auto targetType = target->get<UnitType>();

auto modifier = static_cast<int32_t>(
glm::round(static_cast<float>(_powerPerTick) * TypeEvaluator::calculateModifier(getType(), targetType->type))
);

health->current = glm::min(health->current + modifier, health->max);
}

} // namespace linguine

0 comments on commit 0f27bbb

Please sign in to comment.