Commit 0bc233bd authored by louiz’'s avatar louiz’

Introduce the concept of cooldown

parent 87d4ecae
......@@ -141,11 +141,10 @@ std::vector<EntityId> get_selected_entities_with_ability(const GameClient* game)
if (abilities)
{
Ability* ability = abilities->find(T::ability_type);
if (ability)
if (ability && ability->cooldown.is_ready())
{
// Check cooldown, mana, etc etc
Entity* e = const_cast<Entity*>(entity);
ids.push_back(e->get_id());
ids.push_back(entity->get_id());
}
}
}
......
......@@ -53,10 +53,27 @@ void Hud::draw()
this->game->get_debug_hud().add_debug_line("One (or more) unit selected. Abilities: ", debug_color);
auto entity = *it;
auto abilities = entity->get<Abilities>();
for (const auto& ability: abilities->get())
for (const std::unique_ptr<Ability>& ability: abilities->get())
{
if (ability)
this->game->get_debug_hud().add_debug_line(ability->get_name(), debug_color);
{
std::string s = ability->get_name();
s += std::to_string(ability->cooldown.get_max_in_ticks());
if (ability->cooldown.get_max_in_ticks() > 0)
{
Cooldown& cd = ability->cooldown;
if (cd.is_ready())
s += " (ready)";
else
{
auto max = utils::sec(cd.get_max_duration());
auto remaining = utils::sec(cd.get_remaining_duration());
s += " (on cooldown: " + std::to_string(remaining.count()) + "/" + \
std::to_string(max.count()) + ")";
}
}
this->game->get_debug_hud().add_debug_line(s, debug_color);
}
}
}
......
......@@ -16,7 +16,6 @@
#define HUD_HEIGHT 323
#include <gui/camera/camera.hpp>
#include <gui/screen/screen_element.hpp>
#include <gui/hud/info_hud.hpp>
#include <gui/hud/abilities_panel.hpp>
......
......@@ -3,10 +3,10 @@
#include <chrono>
using namespace std::chrono_literals;
namespace utils
{
using namespace std::chrono_literals;
// A timepoint, gotten from a steady_clock because we don't want time to
// go backward during the game
using Time = std::chrono::time_point<std::chrono::steady_clock>;
......
#ifndef ABILITY_HPP_INCLUDED
#define ABILITY_HPP_INCLUDED
#include <world/cooldown.hpp>
#include <memory>
#include <string>
......@@ -35,6 +37,8 @@ public:
virtual const std::string& get_name() const = 0;
virtual AbilityType get_type() const = 0;
Cooldown cooldown;
private:
Ability(const Ability&) = delete;
Ability(Ability&&) = delete;
......
......@@ -25,7 +25,10 @@ public:
target_type(target_type)
{}
~ActiveAbility() = default;
void tick(Entity*, World*) override final {}
void tick(Entity*, World*) override final
{
this->cooldown.tick();
}
private:
ActiveAbility(const ActiveAbility&) = delete;
......
......@@ -6,6 +6,8 @@
#include <memory>
using namespace std::chrono_literals;
template<>
const std::string NamedAbility<Blink>::name = "Blink";
......@@ -15,6 +17,9 @@ const AbilityType NamedAbility<Blink>::ability_type = AbilityType::Blink;
Blink::Blink():
ActiveAbility(TargetType::Point)
{
log_debug("Creating a blink instance");
this->cooldown.set_max(0s);
log_debug(this->cooldown.get_max_in_ticks());
}
void Blink::cast(Entity* entity, World* world, const Position& position, const bool queue)
......@@ -26,4 +31,5 @@ void Blink::cast(Entity* entity, World* world, const Position& position, const b
entity->queue_work(std::move(work));
else
entity->set_work(std::move(work));
this->cooldown.start();
}
#ifndef COOLDOWN_HPP_INCLUDED
#define COOLDOWN_HPP_INCLUDED
#include <utils/time.hpp>
/**
* Defines a simple class that tells when an ability is ready. It’s ready
* when the current value is 0. When the ability is used, call start(). An
* optional value can be passed to set it to a specific value instead of its
* maximum. It can’t be set above the maximum, though.
* Calling refresh() makes the ability ready by setting the current value to 0.
*/
class Cooldown
{
public:
Cooldown():
max(0),
current(0)
{}
~Cooldown() = default;
void refresh()
{
this->current = 0;
}
void set_max(const utils::Duration& duration)
{
this->set_max(utils::duration_to_ticks(duration));
}
void set_max(const std::size_t ticks)
{
this->max = ticks;
}
void start(const std::size_t val=0)
{
if (val == 0)
this->current = this->max;
else
this->current = std::min(val, this->max);
}
bool is_ready() const
{
return this->current == 0;
}
void tick()
{
if (this->current)
this->current--;
}
std::size_t get_max_in_ticks() const
{
return this->max;
}
utils::Duration get_max_duration() const
{
return utils::tick_duration * this->get_max_in_ticks();
}
std::size_t get_remaining_ticks() const
{
return this->current;
}
utils::Duration get_remaining_duration() const
{
return utils::tick_duration * this->get_remaining_ticks();
}
std::size_t max;
std::size_t current;
private:
Cooldown(const Cooldown&) = delete;
Cooldown(Cooldown&&) = delete;
Cooldown& operator=(const Cooldown&) = delete;
Cooldown& operator=(Cooldown&&) = delete;
};
#endif /* COOLDOWN_HPP_INCLUDED */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment