小结
如果你跟着我的进度在制作的话,现在你写下的代码应该像这样:
Code (c):
#include <stdlib.h>
#include <time.h>
enum damage_type_e{
DAMAGE_NORMAL=0,
DAMAGE_MECHANICAL=1,
DAMAGE_ELECTRIC=2,
DAMAGE_TYPE_TOTAL=3,
};
static const double health_max_default = 100000.0;
class kaiju_t{
private:
double health_max;
double health;
static const double damage_reduction_coeff[DAMAGE_TYPE_TOTAL];
public:
double current_health() const {
return health;
}
void take_damage(double damage, damage_type_e type){
health -= damage * damage_reduction_coeff[type];
}
kaiju_t(double h) : health_max(h), health(h) {}
kaiju_t() : health_max(health_max_default), health(health_max_default) {}
void respawn(){
health_max=health_max_default;
health=health_max_default;
}
void respawn(double h){
health_max=h;
health=h;
}
};
const double kaiju_t::damage_reduction_coeff[] = {
1.0, //DAMAGE_NORMAL
0.7, //DAMAGE_MECHANICAL
1.3, //DAMAGE_ELECTRIC
};
kaiju_t& enemy(){
static kaiju_t knifehead;
return knifehead;
}
time_t& now(){
static time_t t;
return t;
}
class bling_t{
private:
double crit;
double strength;
double power;
static const double power_max;
time_t gcd_expire;
public:
double get_crit() const{
return crit;
}
double get_str() const{
return strength;
}
bool has_such_power(double p) const{
return power>=p;
}
bool is_gcd_expired() const{
return gcd_expire<=now();
}
void consume_power(double p){
power-=p;
if (power<0) power=0;
}
void resume_power(double p){
power+=p;
if (power>power_max) power=power_max;
}
void trigger_gcd(time_t duration){
gcd_expire=now()+duration;
}
class bling_spell_t{
protected:
bling_t* bling_ptr;
private:
time_t cd;
time_t gcd;
time_t cd_expire;
double power_consume;
double power_resume;
public:
bling_spell_t(bling_t* ptr, time_t _cd, time_t _gcd, double _power_consume, double _power_resume) :
bling_ptr(ptr),
cd(_cd),
gcd(_gcd),
cd_expire(0),
power_consume(_power_consume),
power_resume(_power_resume)
{};
virtual ~bling_spell_t(){};
virtual bool execute(){
if (gcd>0&&!bling_ptr->is_gcd_expired()) return false;
if (cd>0&&cd_expire>now()) return false;
if (power_consume>0&&!bling_ptr->has_such_power(power_consume)) return false;
if (gcd>0) bling_ptr->trigger_gcd(gcd);
if (cd>0) cd_expire=now()+cd;
if (power_consume>0) bling_ptr->consume_power(power_consume);
if (power_resume>0) bling_ptr->resume_power(power_resume);
return true;
};
};
class bsod_t : public bling_spell_t{
public:
bsod_t(bling_t* ptr) : bling_spell_t(ptr, 6000, 1500, 0, 50) {}
};
class smackthat_dot_exe_t : public bling_spell_t{
public:
smackthat_dot_exe_t(bling_t* ptr) : bling_spell_t(ptr, 0, 1500, 30, 0) {}
virtual bool execute(){
if (!bling_spell_t::execute()) return false;
double d = 236.0;
d += bling_ptr->get_str();
double c = 0.0;
c += bling_ptr->get_crit();
if ( static_cast<double>(rand())/RAND_MAX < c )
d *= 2.0;
enemy().take_damage(d,DAMAGE_MECHANICAL);
return true;
};
};
bling_spell_t* smackthat_dot_exe;
bling_spell_t* bsod;
bling_t(double str, double c) :
crit(c),
strength(str),
power(power_max),
gcd_expire(0){
smackthat_dot_exe = new smackthat_dot_exe_t(this);
bsod = new bsod_t(this);
}
~bling_t(){
delete smackthat_dot_exe;
delete bsod;
}
void respawn(){
delete smackthat_dot_exe;
delete bsod;
power=power_max;
gcd_expire=0;
smackthat_dot_exe = new smackthat_dot_exe_t(this);
bsod = new bsod_t(this);
}
void respawn(double str, double c){
respawn();
strength=str;
crit=c;
}
};
const double bling_t::power_max = 100.0;
bling_t& bling(){
static bling_t blington3k(500.0, 0.3);
return blington3k;
}
不到二百行,我们就差不多快要完成了。