LoginSignup
2
3

More than 5 years have passed since last update.

[覚書] 敵行動管理

Last updated at Posted at 2013-06-02

ゲーム制作しつつC++の勉強中
VisualStudio2010を使ってるけどほんともうやだ
自分のゲームのソースから抜き出してるのでところどころ変なところがあるかもしれません。

前置き

ゲームを作り始めたのが、もうかなり前で、その頃とは知識量が大分変わってきた。
その頃は STLもBOOSTも知らず、std::functionを使っていなかったため糞汚いソースを量産することになった
伝統のソースは今や濃厚な肥溜めとなっているため、ちまちまリファクタリングをしているわけで…

今回は敵行動管理をちょこちょこっと作ってみた
std::queueで一つづつ処理をしていく形にしている。

敵行動管理

std::queueにstd::functionを入れて、frontを呼ぶ
functionがfalseを返した時、ququeから一つpopされる、
という形で利用してみた。



/// フレーム管理クラス
class CTimer{
public:
    CTimer():_time(0){}
    CTimer(const CTimer& rhs){_time = rhs.get_time();}

    virtual void update(){++_time;}
    virtual void reset(){_time = 0;}
    virtual void reset(const unsigned long num){_time = num;}

// 省略…
// operator == とかを書いて 直接比較とかをできるようにしたり…

private:
    unsigned long _time;
}

/// 今回作った、行動予定を入れるクラス
class CActionPattern{
public:
    CActionPattern( std::function< const bool ( CTimer& ) > f ) :
        _f(f), _t(0){}

    const bool call(){ 
        _t.update();
        return _f( std::ref(_t) );
    }

private:
    std::function< const bool ( CTimer& ) > _f;
    CTimer _t; 
};

/// ゲームで敵になるクラス
class Hoge{
public:
    void Move(){
        // queueが空ならqueueに入れなおし
        if( _actionPattern.empty() ){
            // リセット用の std::function を持ったほうがよさそう
            _actionPattern.push( std::make_shared< CActionPattern >( boost::bind( &St1_Big_Windmill::p1, this, _1 ) ) );
            _actionPattern.push( std::make_shared< CActionPattern >( boost::bind( &St1_Big_Windmill::p2, this, _1 ) ) );
            _actionPattern.push( std::make_shared< CActionPattern >( boost::bind( &St1_Big_Windmill::p3, this, _1 ) ) );
        }
        else{
            // function呼び出し
            if( !_actionPattern.front()->call() )
                _actionPattern.pop(); //< return が false なら pop
        }
    }
private:

    std::queue< std::shared_ptr<CActionPattern> > _actionPattern;

    const bool p1( const CTimer & t ){ /* 敵の行動を書く、 return が false なら 次のfunctionへ */ return t < 600; }
    const bool p2( const CTimer & t ){ return t < 300; }
    const bool p3( const CTimer & t ){ return t < 500; }

};


気になる点

そして、いまいち良いのかわからない点が…。

  • リセット用の関数は、それぞれで用意するほうがいいのか、それともCActionPatternに std::function で持たせたほうが良いのか
  • return false で抜けるより return true で抜けたほうがよかったりするのだろうか
  • 他にも何か改善点があれば

あとプレビューだと、横が狭くて読みづらいっすね…

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3