現在只有hero一個在走來走去沒什麼意思,現在新增幾個npc,給hero點壓力,好的那就開始吧!

新增enemy.h檔案和enemy.h檔案

#ifndef _ENEMY_H_//66
#define _ENEMY_H_
#include"cocos2d.h"
#include"DBgame.h"
#define MaxBuf 20
using namespace cocos2d;
class Enemy:public CCNode
{
public:
    Enemy(void);
    ~Enemy(void);
    //靜態方法用於建立hero例項
    static Enemy* monster(char *monsterName);
    void setAnimation(Direction dir,Behaviour be,char* monsterName);
                                                                                   
    Direction getDirection(); 
    Behaviour getBehaviour();
    void getCurrentAnimationIndex();
    void boxAttacked(Direction dir,Behaviour be,int i);
    void boxAttackedDone(CCNode* pSender,void *i);
    void portal(Direction dir,Behaviour be,int i);
    void portalDone(CCNode* pSender,void *i);
    void cube(Direction dir,Behaviour be,int i);
    void cubeDone(CCNode* pSender,void *i);
    void executeAnimation(Direction dir,Behaviour be,int nextAni,char* monsterName);
    void executeDone(CCNode* pSender,void* i);
    void deadAnimation(int id,char* monsterName);
    void enemyDeadAnimation(int id,char* monsterName);
    void enemyDeadAnimationDone();
    void enemypDingDeadAnimation(int id,char* monsterName);
    void enemypDingDeadAnimationDone();
    void enemyWingDeadAnimation(int id,char* monsterName);
    void enemyWingDeadAnimationDone();
    void dingDead(int id,char* monsterName);
    void dingDeadDone();
    int getDeadID();
    int id;
    void pDingDead(int id,char* monsterName);
    void pDingDeadDone();
    int getpDingDeadID();
    int pId;
    int wID;
    int getWingDeadID();
    //void wingDead(int id,char* monsterName);
    //void wingDeadDone();
    CCRect getRect();
    CCRect getCollisionRect(int width,int height);
                                                                                   
    Behaviour behaviour;
    Direction direction;
    void jump();
    CCSprite* getMonsterSprite();
    CCAnimate *getAnimate(char* monsterName);
protected:
                                                                                   
    CCAnimation* getAnimation();
    CCSprite *sprite;
    float xVel;
    float yVel;
    CCPoint touchPoint;
    bool init(char* monsterName);
};
#define MAXLIFE 100
class EnemyData
{
public:
    //yDing     42
    EnemyData(void);
    float getMonsterHeight(int i);
    //void initMonsterHeightArray(int i);
    void setMonsterHeight(float height,int i);
    int getEnemyLife(int i);
    void setEnemyLife(int life,int i);
    float getActionTime(int i);
    void setActionTime(float val,int i);
    CCPoint getPositionMonster(int i);
    void setPositionMonster(CCPoint pos,int i);
private:
    //CCMutableArray<float*> *monsterHeight;
    CCPoint monPosition[MaxBuf];
    float actionTime[MaxBuf];
    int enemyLife[MaxBuf];
    float monsterHeightArray[MaxBuf];
};
#endif//

enemy.h檔案

#include"enemy.h"//497
#include"cocos2d.h"
#include"coreoy.h"
#include"DBgame.h"
#include"coreAnimation.h"
using namespace cocos2d;
Enemy::Enemy()
{
    xVel=0;
    yVel=0;
}
Enemy::~Enemy()
{
}
Enemy* Enemy::monster(char* monsterName)
{
                                                                       
    Enemy *monster = new Enemy();
    if (monster && monster->init(monsterName))
    {
                                                                           
        monster->autorelease();
        return monster;
    }
    CC_SAFE_DELETE(monster);
    return NULL;
}
bool Enemy::init(char* monsterName)
{
    bool bRet = false;
    do{
        //printf("hello-------------
");
        this->setAnchorPoint(CCPointZero);
        //建立動畫
        //sprite=CCSprite::spriteWithSpriteFrameName("yDing_0.png");
        char charBuffer[128]=" ";
                                                                           
        sprintf(charBuffer,"%s_0.png",monsterName);
        //CCLOG("hello-------------%s",charBuffer);
        sprite=CCSprite::spriteWithSpriteFrameName(charBuffer);//yDing,pDing
        sprite->setAnchorPoint(CCPointZero);
        this->addChild(sprite);
        //設定狀態
        const char* str=charBuffer;
        if(strcmp("box_0.png",str)==0)
        {
            //CCLOG("BOX------------------");
            behaviour=MOVE;
            direction=BOVERLOOK;
        }
        else if(strcmp("pDing_0.png",str)==0)
        {
            //CCLOG("BOX------------------");
            behaviour=MOVE;
            direction=MOVERLOOK;
        }
        else if(strcmp("wing_0.png",str)==0)
        {
            behaviour=MOVE;
            direction=WLEFT;
        }
        else if(strcmp("yDing_0.png",str)==0)
        {
            behaviour=MOVE;
            //behaviour=DEAD;
            direction=MOVERLOOK;
            //direction=REMOVE;
        }
        else if(strcmp("portal_0.png",str)==0)
        {
            behaviour=MOVE;
            //behaviour=DEAD;
            direction=PORTALONE;
            sprite->setIsVisible(false);
            //direction=REMOVE;
        }
        else if(strcmp("cube_0.png",str)==0)
        {
            behaviour=MOVE;
            direction=CUBEONE;
        }
        //behaviour=MOVE;
        //direction=MOVERLOOK;
        //direction=MATTACK;
        CCAction* action=CCRepeatForever::actionWithAction(getAnimate(monsterName));//monsterName ---"yDing pDing"
        //sprite->runAction(CCRepeatForever::actionWithAction(getAnimate()));
        sprite->runAction(action);
        //
        bRet=true;
    }while(0);
    return bRet;
}
void Enemy::getCurrentAnimationIndex()
{
    /*
    假設有一個CCAnimation* anim,由5張圖組成。
    將這個anim包裝成CCAnimate* animate。使用的時侯sprite->runAction(animate),播放動畫。
    */
    //這裡我們來獲取第幾幀:
    int currentAnimIndex = 0; //精靈當前播放的是第幾幀
    CCAnimation* anim=getAnimation();
    for(int i = 1; i <4; i++)
    {
        //CCLOG("animIndex");
        //5張圖5幀
        if(sprite->displayedFrame() == anim->getFrames()->getObjectAtIndex(i))
        {
            //這個i返回的只是一個索引,如果幀數是從1開始計算就要+1
            currentAnimIndex = i+1;
            CCLOG("animaIndex:%d",currentAnimIndex);
        }
    }
}
CCSprite* Enemy::getMonsterSprite()
{
    return this->sprite;
}
void Enemy::boxAttacked(Direction dir,Behaviour be,int i)
{
    behaviour=be;
    direction=dir;
                                                                       
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate("box"),
        CCCallFuncND::actionWithTarget(this, callfuncND_selector(Enemy::boxAttackedDone),(void*)i),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
//--------------------2013.3.3----------------------------------
//下一關的通道有關函式
void Enemy::portal(Direction dir,Behaviour be,int i)
{
    behaviour=be;
    direction=dir;
                                                                       
    sprite->setIsVisible(true);
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate("portal"),
        CCCallFuncND::actionWithTarget(this, callfuncND_selector(Enemy::portalDone),(void*)i),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::portalDone(CCNode* pSender,void* i)
{
    int j=(int)i;
    if(j==1)
    {
        behaviour=MOVE;
        direction=PORTALTWO;
    }
    sprite->stopAllActions();
    sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("portal")));
}
//---------------------2013.3.3--------------------------
//道具獲得箱子的有關函式
void Enemy::cube(Direction dir,Behaviour be,int i)
{
    behaviour=be;
    direction=dir;
                                                                       
    sprite->setIsVisible(true);
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate("cube"),
        CCCallFuncND::actionWithTarget(this, callfuncND_selector(Enemy::cubeDone),(void*)i),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::cubeDone(CCNode *pSender,void *i)
{
    int j=(int)i;
    if(j==1)
    {
        behaviour=MOVE;
        direction=CUBEONE;
    }
    else if(j==2)
    {
        behaviour=MOVE;
        direction=CUBETWO;
    }
    sprite->stopAllActions();
    sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("cube")));
    sprite->setIsVisible(false);
}
void Enemy::enemyDeadAnimation(int id,char* monsterName)
{
    behaviour=DEAD;
    direction=REMOVE;
    this->id=id;
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate(monsterName),
        CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::enemyDeadAnimationDone)),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::enemyDeadAnimationDone()
{
    sGlobal->enemyDeadAnimationDone(1,true);
}
void Enemy::enemypDingDeadAnimation(int id,char* monsterName)
{
    behaviour=DEAD;
    direction=REMOVE;
    this->pId=id;
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate(monsterName),
        CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::enemypDingDeadAnimationDone)),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::enemypDingDeadAnimationDone()
{
    sGlobal->enemyDeadAnimationDone(2,true);
}
void Enemy::enemyWingDeadAnimation(int id,char* monsterName)
{
    behaviour=DEAD;
    direction=WREMOVE;
    this->wID=id;
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate(monsterName),
        CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::enemyWingDeadAnimationDone)),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::enemyWingDeadAnimationDone()
{
    sGlobal->enemyDeadAnimationDone(3,true);
}
int Enemy::getWingDeadID()
{
    return this->wID;
}
void Enemy::deadAnimation(int id,char* monsterName)
{
    behaviour=DEAD;
    direction=REMOVE;
    this->id=id;
    sprite->stopAllActions();
    sprite->runAction(CCRepeatForever::actionWithAction(getAnimate(monsterName)));
}
void Enemy::dingDead(int id,char* monsterName)
{
    behaviour=DEAD;
    direction=REMOVE;
    this->id=id;
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate(monsterName),
        CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::dingDeadDone)),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::pDingDead(int id,char* monsterName)
{
    behaviour=DEAD;
    direction=REMOVE;
    this->pId=id;
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate(monsterName),
        CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::pDingDeadDone)),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
int Enemy::getpDingDeadID()
{
    return this->pId;
}
int Enemy::getDeadID()
{
    return this->id;
}
void Enemy::dingDeadDone()
{
    //behaviour=DEAD;
    //direction=REMOVE;
    //sprite->stopAllActions();
    //運動的演算法
    CCPoint pos=this->getMonsterSprite()->getPosition();
    //sprite->stopAllActions();
    //sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("yDing")));
    sprite->runAction(CCMoveTo::actionWithDuration(1, CCPointMake(pos.x+40,pos.y)));
    //sprite->setIsVisible(false);
}
void Enemy::pDingDeadDone()
{
    //
}
void Enemy::boxAttackedDone(CCNode* pSender,void* i)
{
    int j=(int)i;
    if(j==1)
    {
        behaviour=MOVE;
        direction=BONE;
    }
    else if(j==2)
    {
        behaviour=MOVE;
        direction=BTWO;
    }
    else if(j==3)
    {
        behaviour=MOVE;
        direction=BTHREE;
    }
                                                                       
    sprite->stopAllActions();
    sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("box")));
                                                                       
}
void Enemy::setAnimation(Direction dir,Behaviour be,char* monsterName)
{
    direction=dir;
    behaviour=be;
    sprite->stopAllActions();
    sprite->runAction(CCRepeatForever::actionWithAction(getAnimate(monsterName)));
}
//1 2 3 yDing          11 12 13 pDing
void Enemy::executeAnimation(Direction dir,Behaviour be,int nextAni,char* monsterName)
{
    behaviour=be;
    direction=dir;
                                                                       
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate(monsterName),
        CCCallFuncND::actionWithTarget(this, callfuncND_selector(Enemy::executeDone),(void*)nextAni),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::executeDone(CCNode* pSender,void* i)
{
    int next=(int)i;
    if(next==1)
    {
        behaviour=MOVE;
        direction=MOVERLOOK;
    }
    else if(next==2)
    {
        behaviour=MOVE;
        direction=MATTACK;
    }
    else if(next==3)
    {
        behaviour=DEAD;
        direction=REMOVE;
    }
    else if(next==11)
    {
        behaviour=MOVE;
        direction=MOVERLOOK;
    }
    else if(next==12)
    {
        behaviour=MOVE;
        direction=MATTACK;
    }
    else if(next==13)
    {
        behaviour=DEAD;
        direction=REMOVE;
    }
    else if(next==21)
    {
        behaviour=MOVE;
        direction=WRIGHT;
    }
    else if(next=22)
    {
        behaviour=MOVE;
        direction=WLEFT;
    }
    else if(next==23)
    {
        behaviour=MOVE;
        direction=WATTACK;
    }
    else if(next=24)
    {
        behaviour=DEAD;
        direction=WREMOVE;
    }
    sprite->stopAllActions();
    if(next<20&&next>10)
    {
        sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("pDing")));
    }
    else if(next<10)
    {
        sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("yDing")));
    }
    else if(next>20)
    {
        sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("wing")));
    }
                                                                       
}
//注意,不同精靈,這個函式寫法不同,主要區別都是OVERLOOK上,有些精靈是沒有四方向的
CCAnimate * Enemy::getAnimate(char* monsterName)
{
    if (behaviour==DEAD)
    {
        //direction=MOVERLOOK;
    }
                                                                       
    return AnimationManager::getInstance()->getAnimate(monsterName,behaviour,direction);
                                                                       
}
CCAnimation* Enemy::getAnimation()
{
    if (behaviour==DEAD)
    {
        //direction=MOVERLOOK;
    }
    return AnimationManager::getInstance()->getAnimation("yDing",behaviour,direction);
}
Direction Enemy::getDirection()
{
    return this->direction;
}
Behaviour Enemy::getBehaviour()
{
    return this->behaviour;
}
CCRect Enemy::getRect()
{
    float x=sprite->getPosition().x;
    float y=sprite->getPosition().y;
    //printf("x:%f  y:%f
",x,y);
    return CCRectMake(x,y,70,62);
                                                                       
}
CCRect Enemy::getCollisionRect(int width,int height)
{
    float x=sprite->getPosition().x;
    float y=sprite->getPosition().y;
    //CCLOG("Enemy--x:%f  y:%f   width:%d    height:%d
",x,y,width,height);
    return CCRectMake(x,y,width,height);
}
void Enemy::jump()
{
    this->sprite->runAction(CCJumpTo::actionWithDuration(0.05f,ccp(this->sprite->getPosition().x , this->sprite->getPosition().y),20,100));
}
EnemyData::EnemyData(void)
{
    for(int i=0;i<MaxBuf;i++)
    {
        enemyLife[i]=MAXLIFE;
        monsterHeightArray[i]=2.5;
        actionTime[i]=0.0;
        monPosition[i]=CCPointZero;
    }
    //monsterHeight=new CCMutableArray<float*>();
}
int EnemyData::getEnemyLife(int i)
{
    return this->enemyLife[i];
                                                                       
}
void EnemyData::setEnemyLife(int life,int i)
{
                                                                       
    this->enemyLife[i]=life;
}
float EnemyData::getMonsterHeight(int i)
{
    return this->monsterHeightArray[i];
}
void EnemyData::setMonsterHeight(float height,int i)
{
    this->monsterHeightArray[i]=height;
}
void EnemyData::setActionTime(float val,int i)
{
    this->actionTime[i]=val;
}
float EnemyData::getActionTime(int i)
{
    return this->actionTime[i];
}
void EnemyData::setPositionMonster(CCPoint pos,int i)
{
    this->monPosition[i]=pos;
}
CCPoint EnemyData::getPositionMonster(int i)
{
    return this->monPosition[i];
}

現在可以在scene中新增npc了,說到就開始。

Global.h中新增如下定義:

CCMutableArray<Enemy*> *enemyArray;
void addMonster();

Global.cpp中實現:

CCMutableArray<CCStringToStringDictionary *> * all = data->getObjects();
    do
    {
        for (int i = 0 ; i <all->count(); i++)
        {
            CCStringToStringDictionary * temp = all->getObjectAtIndex(i);
            if(temp->objectForKey("name")->toStdString() == std::string("y"))
            {
                float x = temp->objectForKey("x")->toFloat();    
                float y = temp->objectForKey("y")->toFloat();
                CCVector2d* vector=new CCVector2d();
                vector->x=x;
                vector->y=5;
                monsterPositionArray->addObject(vector);
                Enemy* monster = Enemy::monster("yDing");
                monster->getMonsterSprite()->setPosition(ccp(x,5));
                map->addChild(monster,map->getChildren()->count());
                enemyArray->addObject(monster);//yDing
            }
}

HelloworldScene.cpp中的init()中新增如下

sGlobal->addMonster();

下一篇將會講解enemy的群集運動以及配合動畫,以及hero的碰撞檢測準備。

待續。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

long原創