Skip to content

Instantly share code, notes, and snippets.

@KireinaHoro
Created March 24, 2018 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KireinaHoro/3cfed3a8d3e77fe7f7b7a1d956e2a267 to your computer and use it in GitHub Desktop.
Save KireinaHoro/3cfed3a8d3e77fe7f7b7a1d956e2a267 to your computer and use it in GitHub Desktop.
class Weapon {
public:
virtual bool usable() const = 0;
virtual double attack() = 0;
};
class Sword : public Weapon {
private:
constexpr static auto startRatio = 0.2;
constexpr static auto wearRatio = 0.8;
double attackValue;
public:
explicit Sword(double ownerAttack)
: attackValue(startRatio * ownerAttack) {}
bool usable() const override { return attackValue > 0; }
double attack() override {
auto ret = attackValue;
attackValue *= wearRatio;
return ret;
}
};
class Arrow : public Weapon {
private:
int remaining = 3;
public:
static double attackValue;
Arrow() = default;
Arrow(double) {}
bool usable() const override { return remaining > 0; }
double attack() override {
if (!usable()) { return 0; }
--remaining;
return attackValue;
}
};
double Arrow::attackValue;
class Bomb : public Weapon {
public:
Bomb() = default;
Bomb(double) {}
bool usable() const override { return true; }
double attack() override { return -1; }
};
template<typename T>
Weapon *make(double param = 0) {
return new T{param};
}
using WeaponPtr = Weapon*;
using WeaponCreator = WeaponPtr(*)(double);
WeaponCreator WeaponList[] = {make<Sword>, make<Arrow>, make<Bomb>};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment