Skip to content

Instantly share code, notes, and snippets.

@wmw

wmw/cyousEx.cpp Secret

Created May 26, 2015
Embed
What would you like to do?
cyousEx
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <chrono>
#include <iostream>
#include <Windows.h>
#include <string>
#include <vector>
#include <thread>
#include <memory>
using ms = std::chrono::milliseconds;
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
namespace
{
bool s_running = true;
};
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
BOOL __stdcall consoleCtrlHandler(DWORD type)
{
switch (type)
{
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
s_running = false;
return TRUE;
// break;
default:
return FALSE;
// break;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
class Spell
{
public:
Spell(ms cooldown) : m_cooldown(cooldown) { };
void Cast()
{
if (m_sinceLastCast < m_cooldown)
return;
m_sinceLastCast = ms(0);
__Cast();
};
void Update(ms delta)
{
m_sinceLastCast += delta;
};
protected:
const ms m_cooldown;
virtual void __Cast() = 0;
private:
ms m_sinceLastCast;
};
class OozeSpell : public Spell
{
public:
OozeSpell() : Spell(ms(10000)) { };
virtual void __Cast() override
{
std::cout << "Casting OozeSpell!" << std::endl;
};
};
class SlimerSpell : public Spell
{
public:
SlimerSpell() : Spell(ms(2000)) { };
virtual void __Cast() override
{
std::cout << "Casting SlimerSpell!" << std::endl;
};
};
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
class Boss
{
public:
Boss(const std::string& name, int hp) : m_name(name), m_hp(hp)
{
std::cout << "Boss " << name << " enters the battle!" << std::endl;
};
void Update(ms delta)
{
__UpdateSpells(delta);
__Update(delta);
}
std::string m_name;
int m_hp;
std::vector<std::shared_ptr<Spell>> m_spells;
protected:
virtual void __Update(ms delta) = 0;
private:
void __UpdateSpells(ms delta)
{
for (auto& spell : m_spells)
{
spell->Update(delta);
}
};
};
class SlimeBoss : public Boss
{
public:
SlimeBoss() : Boss("Slime Boss", 10)
{
m_spells.emplace_back(std::make_shared<SlimerSpell>());
m_spells.push_back(std::make_shared<OozeSpell>());
};
protected:
virtual void __Update(ms delta) override
{
for (auto& spell : m_spells)
{
spell->Cast();
}
};
};
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Init" << std::endl;
using sc = std::chrono::system_clock;
SetConsoleCtrlHandler(static_cast<PHANDLER_ROUTINE>(consoleCtrlHandler), TRUE);
sc::time_point last = sc::now();
std::cout << "Setup" << std::endl;
SlimeBoss slimeBoss;
std::cout << "Main Loop" << std::endl;
while (s_running)
{
sc::time_point now = sc::now();
auto delta = now - last;
slimeBoss.Update(std::chrono::duration_cast<ms>(delta));
last = now;
std::this_thread::sleep_for(ms(200));
}
std::cout << "Exit" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment