当前位置:首页>>攻略文章>>正文
魔兽科普:模拟器组成原理(二)
2013-12-30 18:24:47 作者:fhsvengetta 来源:NGA 浏览次数:0
摘要:如果你觉得看不懂……那真的很抱歉,我已经写得非常通俗了,甚至已经给每一句贴进来的SimC源代码配上了注释,我觉得拿来当作C++入门教材都快够用了!

小结 

 

如果你跟着我的进度在制作的话,现在你写下的代码应该像这样:

 

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; 

}

 

 

不到二百行,我们就差不多快要完成了。 



相关报道:

[关闭] [返回顶部]


  返回首页 | 最新资讯 | 资源下载 | 魔兽图片 | 单机文档 | 技术攻略 | 玩家视频
备案号:蜀ICP备2024062380号-1
免责声明:本网站为热爱怀旧WOW的玩家们建立的魔兽世界资料网站,仅供交流和学习使用,非盈利和商用.如有侵权之处,请联系我们,我们会在24小时内确认删除侵权内容,谢谢合作。
Copyright © 2024 - 2024 WOWAII.COM Corporation, All Rights Reserved

机器人国度