敵ベースクラスの作成

7410 ワード

class EnemyBase : public Sprite  
{  
public:      
    virtual bool init() override;  
    CREATE_FUNC(EnemyBase);  
    Animation* createAnimation(std::string prefixName, int framesNum, float delay);  
    void changeDirection(float dt);  
    Node* currPoint();  
    Node* nextPoint();  
    void runFllowPoint();  
    void setPointsVector(Vector<Node*> points);      
private:  
    Vector<Node*> pointsVector;  
protected:  
    int pointCounter;  
    Animation *animationRight;  
    Animation *animationLeft;  
    CC_SYNTHESIZE(float, runSpeed, RunSpeed);      
};
// 
Node* EnemyBase::currPoint()  
{  
    return this->pointsVector.at(pointCounter);  
}  
Node* EnemyBase::nextPoint()  
{  
    int maxCount = this->pointsVector.size();  
pointCounter++;  
if (pointCounter < maxCount  ){  
auto node =this->pointsVector.at(pointCounter);  
        return node;  
    }  
    else{  
        pointCounter = maxCount -1 ;  
    }  
    return NULL;  
}
// 
class Thief : public EnemyBase  
{  
public:  
    virtual bool init() override;      
    static Thief* createThief(Vector<Node*> points);  
};
// , Thief , , , 。   
bool Thief::init()  
{  
if (!Sprite::init())  
{  
return false;  
}  
// 1  
    setRunSpeed(6);  
    animationRight = createAnimation("enemyRight1", 4, 0.1f);  
AnimationCache::getInstance()->addAnimation(animationRight, "runright");  
    animationLeft = createAnimation("enemyLeft1", 4, 0.1f);  
AnimationCache::getInstance()->addAnimation(animationLeft, "runleft");  
// 2  
    schedule(schedule_selector(EnemyBase::changeDirection), 0.4f);  
return true;  
}
// 
Thief* Thief::createThief(Vector<Node*> points)  
{  
    Thief *pRet = new Thief();  
    if (pRet && pRet->init())  
    {  
     //    
        pRet->setPointsVector(points);  
        //    
        pRet->runFllowPoint();  
        pRet->autorelease();  
        return pRet;  
    }  
    else  
    {  
        delete pRet;  
        pRet = NULL;  
        return NULL;  
    }  
}