Beispiel #1
0
void StartScene::addHome()
{
    DataModel *m = DataModel::getModel();
    Home* home = NormalHome::home();
    WayPoint* wp = *(m->waypoints.end()-1);
    home->setPosition(wp->getPosition());
    m->home = home;
    this->addChild(home,1);
}
void HelloWorld::ResumePath(CCNode* sender) {
    Creeps *creep = (Creeps *)sender;
    
    WayPoint * cWaypoint = creep->getCurrentWaypoint();//destination
    WayPoint * lWaypoint = creep->getLastWaypoint();//startpoint
    
    float waypointDist = fabsf(cWaypoint->getPosition().x - lWaypoint->getPosition().x);
    float creepDist = fabsf(cWaypoint->getPosition().x - creep->getPosition().x);
    float distFraction = creepDist / waypointDist;
//    float moveDuration = creep->getMoveDuration() * distFraction; //Time it takes to go from one way point to another * the fraction of how far is left to
    float durScale = creep->moveDurScale();
    float moveDuration = durScale * distFraction;
//    go (meaning it will move at the correct speed)
    

    CCMoveTo* actionMove = CCMoveTo::create(moveDuration,cWaypoint->getPosition());
    CCCallFuncN* actionMoveDone = CCCallFuncN::create(this,callfuncN_selector(HelloWorld::FollowPath));
	creep->stopAllActions();
	creep->runAction(CCSequence::create(actionMove, actionMoveDone, NULL));
}
void HelloWorld::FollowPath(CCNode* sender) {
    
	Creeps *creep = (Creeps *)sender;
	
	WayPoint * waypoint =creep->getNextWaypoint();
    if(waypoint==NULL){
        return;
    }
//	int moveDuration = creep->getMoveDuration();
    int moveDuration = creep->moveDurScale();
	CCMoveTo *actionMove =CCMoveTo::create(moveDuration,waypoint->getPosition());
	CCCallFuncN* actionMoveDone =CCCallFuncN::create(this,callfuncN_selector(HelloWorld::FollowPath));
	creep->stopAllActions();
    creep->runAction(CCSequence::create(actionMove, actionMoveDone, NULL));
}
Beispiel #4
0
void StartScene::FollowPath(Node *sender)
{
    DataModel *m = DataModel::getModel();
    Creep* creep = (Creep *)sender;
    WayPoint* curWaypoint = creep->getCurrentWaypoint();
    WayPoint* waypoint = creep->getNextWaypoint();
    if (waypoint!=nullptr) {
        Point moveVector = waypoint->getPosition()-curWaypoint->getPosition();
        float angle = ccpToAngle(moveVector);
        float cocosAngle = CC_RADIANS_TO_DEGREES(angle);
//        std::ostringstream text("angle = ");
//        text<<std::to_string(cocosAngle)<<" curPoint("<<std::to_string(curWaypoint->getPosition().x)<<","<<std::to_string(curWaypoint->getPosition().y)<<") nextPoint("<<std::to_string(waypoint->getPosition().x)<<","<<std::to_string(waypoint->getPosition().y)<<")";
//        log(text.str().c_str());
        if (cocosAngle==0) {
            if (creep->inverse) {
                cocosAngle += 180;
                creep->lifeBar->setPosition(creep->sprite->getPosition().x, creep->sprite->getPosition().y-60);
            } else
                creep->lifeBar->setPosition(creep->sprite->getPosition().x, creep->sprite->getPosition().y+60);
            creep->emptyLifeBar->setPosition(creep->lifeBar->getPosition());
        } else if(cocosAngle==180) {
            if (creep->inverse) {
                cocosAngle += 180;
                creep->lifeBar->setPosition(creep->sprite->getPosition().x, creep->sprite->getPosition().y+60);
            } else
                creep->lifeBar->setPosition(creep->sprite->getPosition().x, creep->sprite->getPosition().y-60);
            creep->emptyLifeBar->setPosition(creep->lifeBar->getPosition());
        } else {
            if (!creep->inverse)
                cocosAngle += 180;
        }
        sender->setRotation(cocosAngle);
        int moveDuration = creep->moveDuration;
        auto actionMove = MoveTo::create(moveDuration, waypoint->getPosition());
        auto actionMoveDone = CallFuncN::create(this, callfuncN_selector(StartScene::FollowPath));
        creep->stopAllActions();
//        creep->lifeBar->stopAllActions();
        creep->runAction(Sequence::create(actionMove, actionMoveDone, NULL));
//        creep->lifeBar->runAction(Sequence::create(lifeBarActionMove, NULL));
    } else {
        m->home->homeHP--;
        life->setText(to_string(m->home->homeHP));
        CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("blast.wav");
        m->targets.eraseObject(creep);
        this->removeChild(creep);
        CCParticleSystem* particleSystem = CCParticleExplosion::create();
        particleSystem->setTexture(CCTextureCache::sharedTextureCache()->addImage("bullet2.png"));
        particleSystem->setPosition(creep->getPosition());
        particleSystem->setScale(0.15);
        particleSystem->setLife(1);
        this->addChild(particleSystem);
        if (m->home->homeHP<=0) {
            m->home->removeAllChildren();
            m->home->sprite = Sprite::create("destroy.png");
            m->home->sprite->setScale(0.7);
            m->home->addChild(m->home->sprite,0);
            Size visibleSize = Director::getInstance()->getVisibleSize();
            Point origin = Director::getInstance()->getVisibleOrigin();
            auto losePic = Sprite::create("over.png");
            losePic->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
            this->addChild(losePic,3);
            widget = dynamic_cast<Layout*>(cocostudio::GUIReader::getInstance()->widgetFromJsonFile("OverUi_1/LoseUi_1.json"));
            widget->setPosition(Point(visibleSize.width/2 + origin.x -widget->getContentSize().width/2, visibleSize.height/2 + origin.y - widget->getContentSize().height/2));
            this->addChild(widget,2);
            auto restartButton = dynamic_cast<Button*>(widget->getChildByName("RestartBTN"));
            auto mainMenuButton = dynamic_cast<Button*>(widget->getChildByName("MainMenuBTN"));
            restartButton->addTouchEventListener(this, toucheventselector(StartScene::restartEvent));
            mainMenuButton->addTouchEventListener(this, toucheventselector(StartScene::mainMenuEvent));
            this->cleanup();
        } else {
            std::string home = to_string(m->home->homeHP) + ".png";
            m->home->removeAllChildren();
            m->home->sprite = Sprite::create(home);
            m->home->sprite->setScale(0.7);
            m->home->addChild(m->home->sprite,0);
        }
    }
}
Beispiel #5
0
void StartScene::addTarget()
{
    DataModel *m = DataModel::getModel();
    Wave *wave = this->getCurrentWave();
    if(wave==nullptr)
        this->cleanup();
    if (wave->totalCreeps <= 0)
    {
        if (m->targets.size()==0)
            getNextWave();
        return;
    }
    wave->totalCreeps--;
    Creep *target = NULL;

    /*
     int random = rand()%3;
     if (random == 0)
     {
     target = FastGrayCreep::creep(m->waypoints.size());
     }
     else  if(random == 1)
     {
     target = StrongGreenCreep::creep(m->waypoints.size());
     }
     else
     {
     target = MonsterYellowCreep::creep(m->waypoints.size());
     }
     */


    target = Creep::createWithType(wave->creeptype,m->waypoints.size(),true);
    WayPoint *waypoint = target->getCurrentWaypoint();
    target->setPosition(waypoint->getPosition());
    waypoint = target->getNextWaypoint();
    auto lifeBar = CCProgressTimer::create(CCSprite::create("loginbg.png"));
    lifeBar->setType(kCCProgressTimerTypeBar);
    lifeBar->setPercentage(100.0f);
    lifeBar->setBarChangeRate(ccp(1, 0));//水平方向
    lifeBar->setMidpoint(ccp(0,0));//设置起始点为左下方
    lifeBar->setPosition(target->sprite->getPosition().x, target->sprite->getPosition().y+60);
    lifeBar->setScaleY(0.6);
    lifeBar->setScaleX(0.3);
    target->addChild(lifeBar,1);
    target->lifeBar = lifeBar;
    auto emptyLifeBar = Sprite::create("login_dbg.png");
    emptyLifeBar->setScaleX(0.3);
    emptyLifeBar->setScaleY(0.3);
    emptyLifeBar->setPosition(lifeBar->getPosition());
    target->addChild(emptyLifeBar,0);
    target->emptyLifeBar = emptyLifeBar;
    this->addChild(target, 1);
    int moveDuration = target->moveDuration;
    auto actionMove = CCMoveTo::create(moveDuration, waypoint->getPosition());
    auto lifeBarActionMove = MoveTo::create(moveDuration, ccp(waypoint->getPosition().x,waypoint->getPosition().y+25));
    auto actionMoveDone = CallFuncN::create(this, callfuncN_selector(StartScene::FollowPath));
    target->runAction(CCSequence::create(actionMove, actionMoveDone, NULL));
//    lifeBar->runAction(CCSequence::create(lifeBarActionMove, NULL));
    target->tag = 1;
    m->targets.pushBack(target);
}
void HelloWorld::addTarget() {
    
	DataModel *m = DataModel::getModel();
	Wave * wave = getCurrentWave();
    int tc=wave->getRedCreeps();
    int gn=wave->getGreenCreeps();
	if (tc <= 0&&gn<=0) {
		return; //[self getNextWave];
	}
	
//	wave->setTotalCreeps(--tc);
	
    Creeps *target = NULL;
//    if ((arc4random() % 2) == 0) {
//        target = FastRedCreep::creep();
//    } else {
//        target = StrongGreenCreep::creep();
//    }
	int random=CCRANDOM_0_1()*100;
    if ((random % 2) == 0) {
        if (wave->getRedCreeps()>0) {
           target =Creeps::create(ENERMY_NAME[0].c_str(),0,Info::getInfo()->creepInfo->value[0]);
            wave->setRedCreeps(--tc);
        }
        else if (wave->getGreenCreeps()>0){
           target = Creeps::create(ENERMY_NAME[1].c_str(),1,Info::getInfo()->creepInfo->value[1]);
            wave->setGreenCreeps(--gn);
            // NSLog(@"no more red");
        }
    }
    else {
        if (wave->getGreenCreeps()>0) {
          target = Creeps::create(ENERMY_NAME[1].c_str(),1,Info::getInfo()->creepInfo->value[1]);
            wave->setGreenCreeps(--gn);
        }
        else if (wave->getRedCreeps()>0){
            target = Creeps::create(ENERMY_NAME[0].c_str(),0,Info::getInfo()->creepInfo->value[0]);
            wave->setRedCreeps(--tc);
            //NSLog(@"no more green");
        }
        
    }
	WayPoint *waypoint = target->getCurrentWaypoint();
	target->setPosition( waypoint->getPosition());
	waypoint = target->getNextWaypoint();
	target->setVisible(true);
	addChild(target,1);
    
    target->setHealthBar(CCProgressTimer::create(CCSprite::create("health_bar_red.png")));
    target->getHealthBar()->setType(kCCProgressTimerTypeBar);
    target->getHealthBar()->setPercentage(100);
    target->getHealthBar()->setScale(0.1);
    target->getHealthBar()->setPosition(ccp(target->getPosition().x,(target->getPosition().y+20)));
    addChild(target->getHealthBar(),3);
    
	int moveDuration = target->getMoveDuration();
	CCMoveTo* actionMove = CCMoveTo::create(moveDuration,waypoint->getPosition());
	CCCallFuncN* actionMoveDone =CCCallFuncN::create(this,callfuncN_selector(HelloWorld::FollowPath));
	target->runAction(CCSequence::create(actionMove, actionMoveDone, NULL));
	
	// Add to targets array
	target->setTag(1);
	m->getTargets()->addObject(target);
	
}