Example #1
0
bool ArrowTower::init()
{
	if (!TowerBase::init())
	{
		return false;
	}

	setScope(120);
	setLethality(1);
	setTowerValue(120);
	setRate(3);
	setLevel(0);
	setTowerType(0);



	auto ll = getLevel();
	if( ll == 0 )
	{
		tower= Sprite::createWithSpriteFrameName("tower1.png");
	}
	else if ( ll == 1)
	{
		tower= Sprite::createWithSpriteFrameName("tower2.png");
	}
	else 
	{
		tower= Sprite::createWithSpriteFrameName("tower3.png");
	}
	this->addChild(tower);
	schedule(schedule_selector(ArrowTower::rotateAndShoot), 0.8f);
	return true;
}
bool PowerPlantTower::init()
{
	if (!Sprite::init()) //如果忘记了这句话则会在runApplication报错
	{
		return false;
	}

	
	setTowerType(5);
	setIsSelected(false);
	setGrade(0);//开始没有等级
	setPower(0);//塔消耗电力
	setMove(false);//开始不处于移动状态
	setIsPowerEnough(true);//开始状态电力足够
	setIsPowerConsumption(false);//该塔不耗电
	setTowerSize(Size(3, 3));//占地面积3*3

	towerSprite = Sprite::create("towerItem/Item6.png");
	towerSprite->setScale(0.4);
	addChild(towerSprite, 5);

	//血量条背景图片
	towerHpSprite = Sprite::create("manaBarBg.png");
	towerHpSprite->setPosition(towerSprite->getPosition()+Point(0,50));
	towerHpSprite->setScale(0.3);
	addChild(towerHpSprite, 10);
	//炮塔血量
	hp = 4;
	//炮塔血量进度条
	towerHp = ProgressTimer::create(Sprite::create("soldierProduceTimeBar.png")); //参数是一个图片sprite
	towerHp->setScaleX(2);
	towerHp->setScaleY(5.2);
	towerHp->setType(ProgressTimer::Type::BAR);
	towerHp->setMidpoint(Point(0, 0.5f));
	towerHp->setBarChangeRate(Point(1, 0));
	towerHp->setPercentage(100);
	towerHp->setPosition(Point(towerHpSprite->getContentSize().width / 2, towerHpSprite->getContentSize().height / 3 * 2 - 10));
	towerHpSprite->addChild(towerHp, 5);
	//初始化不可见
	towerHp->setVisible(false);
	towerHpSprite->setVisible(false);

	addTouch();//添加触摸事件

	return true;
}
bool SimpleMageTowerlvl2::init()
{
	if (!Sprite::init())
	{
		return false;
	}
	setTowerType(MAGE_2);
	setLevel(2);
	addTerrain();
	initTower(2);
	setListener();
	setScope(180.0f);
	setUpdateMoney(240);
	setBuildMoney(260);
	isUpdateMenuShown = false;
	schedule(schedule_selector(SimpleMageTowerlvl2::shoot), 2.0f);
	SoundManager::playMageTower2Ready();
	return true;
}
Example #4
0
bool AttackTower2::init()
{
    if (!TowerBase::init())
	{
		return false;
	}
    
    setScope(120);
	setTowerValue(150);
    setLethality(6);
    setRate(2);
	setLevel(0);
	setTowerType(1);
	//add by  
    //tower= Sprite::createWithSpriteFrameName("attackTower.png");
	tower= Sprite::createWithSpriteFrameName("tower2_2.png");
    this->addChild(tower);
    
    schedule(schedule_selector(AttackTower2::shoot), 0.8f);
	return true;
}
Example #5
0
bool ArchMageTower::init()
{
	if (!Sprite::init())
	{
		return false;
	}
	setTowerType(ARCHMAGE);
	DelayBullet[0] = NULL;
	DelayBullet[1] = NULL;
	DelayBullet[2] = NULL;
	setLevel(4);
	addTerrain();
	initTower(4);
	setListener();
	setScope(220.0f);
	setBuildMoney(730);
	isUpdateMenuShown = false;
	SoundManager::playArchmageReady();
	schedule(schedule_selector(ArchMageTower::Attack), 2.0f);
	schedule(schedule_selector(ArchMageTower::AtTwister), 7.0f);
	return true;
}
bool ForetellingTower::init()
{
	if (!Sprite::init()) //如果忘记了这句话则会在runApplication报错
	{
		return false;
	}

	setTowerType(6);
	setIsSelected(false);
	setGrade(0);//开始没有等级
	setPower(60);//塔消耗电力
	setMove(false);//开始不处于移动状态
	setIsPowerEnough(true);//开始状态电力足够
	setIsPowerConsumption(false);//该塔不耗电
	setTowerSize(Size(3, 3));//占地面积3*3

	towerSprite = Sprite::create("towerItem/Item7.png");
	towerSprite->setScale(0.4);
	addChild(towerSprite, 5);

	addTouch();//添加触摸事件

	return true;
}
bool ArtilleryTower::init()
{
	if (!Sprite::init()) //如果忘记了这句话则会在runApplication报错
	{
		return false;
	}
	
	setScope(300);
	setAttack(2);
	setRate(2);
	setIsSelected(false);
	setTowerType(1);
	setGrade(0);//开始没有等级
	setPower(0);//塔消耗电力
	setMove(false);//开始不处于移动状态
	setIsPowerEnough(true);//开始状态电力足够
	setIsPowerConsumption(false);//该塔耗电

	basePlate = Sprite::createWithSpriteFrameName("fort1.png"); //塔的基座
	addChild(basePlate,2);
	//log("111111111111111");
	rotateArtillery = Sprite::createWithSpriteFrameName("fort2.png");  //塔的弓箭
	rotateArtillery->setPosition(0, 0);
	rotateArtillery->setRotation(270);
	addChild(rotateArtillery,3);

	//这个不能少,虽然不显示,这是因为这个炮塔比较特殊,是由2个sprite拼接而成的
	towerSprite = Sprite::create("towerItem/Item2.png");
	addChild(towerSprite);
	towerSprite->setVisible(false);

	gradeSprite = Sprite::create("level1.png");
	gradeSprite->setScale(2);
	gradeSprite->setAnchorPoint(Point(0, 0));
	gradeSprite->setPosition(this->getPosition().x + 60, -towerSprite->getBoundingBox().size.height / 2-10);
	gradeSprite->setOpacity(0);//开始让其不可见
	addChild(gradeSprite, 6);

	//沉默图片
	noAttackSprite = Sprite::create("noSpeak.png");
	noAttackSprite->setScale(0.9);
	noAttackSprite->setAnchorPoint(Point(0.5, 0));
	noAttackSprite->setPosition(towerSprite->getBoundingBox().size.width / 2 - 70, towerSprite->getBoundingBox().size.height - 60);
	addChild(noAttackSprite, 8);
	noAttackSprite->setVisible(false);

	//血量条背景图片
	towerHpSprite = Sprite::create("manaBarBg.png");
	towerHpSprite->setPosition(towerSprite->getPosition() + Point(0, 100));
	towerHpSprite->setScale(0.8);
	addChild(towerHpSprite, 10);
	//炮塔血量
	hp = 2;
	maxHp = 2;
	//炮塔血量进度条
	towerHp = ProgressTimer::create(Sprite::create("soldierProduceTimeBar.png")); //参数是一个图片sprite
	towerHp->setScaleX(2);
	towerHp->setScaleY(5.2);
	towerHp->setType(ProgressTimer::Type::BAR);
	towerHp->setMidpoint(Point(0, 0.5f));
	towerHp->setBarChangeRate(Point(1, 0));
	towerHp->setPercentage(100);
	towerHp->setPosition(Point(towerHpSprite->getContentSize().width / 2, towerHpSprite->getContentSize().height / 3 * 2 - 10));
	towerHpSprite->addChild(towerHp, 5);
	//初始化不可见
	towerHp->setVisible(false);
	towerHpSprite->setVisible(false);

    schedule(schedule_selector(ArtilleryTower::rotateAndShoot), 0.5f); //别忘了调用函数,时间间隔越小,炮塔旋转越平滑
	schedule(schedule_selector(ArtilleryTower::shoot), 2.5f);
	
	addTouch();

	return true;
}
bool AntiVisibleTower::init()
{
	if (!Sprite::init()) //如果忘记了这句话则会在runApplication报错
	{
		return false;
	}
	
	instance = GameManager::getInstance();   

	setScope(300);
	setAttack(0);
	setRate(5);//4秒钟开火一次
	setTowerType(3);
	setIsSelected(false);
	setGrade(0);//开始没有等级
	setPower(40);//塔消耗电力
	setMove(false);//开始不处于移动状态
	setIsPowerEnough(true);//开始状态电力足够
	setIsPowerConsumption(true);//该塔耗电
	nearestEnemy = nullptr;

	towerSprite = Sprite::create("towerItem/Item99.png");
	addChild(towerSprite, 5);

	gradeSprite = Sprite::create("level1.png");
	gradeSprite->setAnchorPoint(Point(0, 0));
	gradeSprite->setPosition(this->getPosition().x + 10, -towerSprite->getBoundingBox().size.height / 2);
	gradeSprite->setOpacity(0);//开始让其不可见
	addChild(gradeSprite, 6);

	noPowerSprite = Sprite::create("noPower.png");
	noPowerSprite->setAnchorPoint(Point(0.5, 0));
	noPowerSprite->setScale(1.4);
	noPowerSprite->setPosition(towerSprite->getBoundingBox().size.width / 2 - 70, towerSprite->getBoundingBox().size.height - 40);
	noPowerSprite->setVisible(false);
	addChild(noPowerSprite, 7);

	//血量条背景图片
	towerHpSprite = Sprite::create("manaBarBg.png");
	towerHpSprite->setPosition(noPowerSprite->getPosition()-Point(0,25));
	towerHpSprite->setScale(0.8);
	addChild(towerHpSprite, 10);
	//炮塔血量
	hp = 4;
	//炮塔血量进度条
	towerHp = ProgressTimer::create(Sprite::create("soldierProduceTimeBar.png")); //参数是一个图片sprite
	towerHp->setScaleX(2);
	towerHp->setScaleY(5.2);
	towerHp->setType(ProgressTimer::Type::BAR);
	towerHp->setMidpoint(Point(0, 0.5f));
	towerHp->setBarChangeRate(Point(1, 0));
	towerHp->setPercentage(100);
	towerHp->setPosition(Point(towerHpSprite->getContentSize().width / 2, towerHpSprite->getContentSize().height / 3 * 2 - 10));
	towerHpSprite->addChild(towerHp, 5);
	//初始化不可见
	towerHp->setVisible(false);
	towerHpSprite->setVisible(false);

	schedule(schedule_selector(AntiVisibleTower::shoot), 0.1f);
	schedule(schedule_selector(AntiVisibleTower::checkNearestEnemy), 0.2);
	addTouch();//添加触摸事件 

	return true;
}
bool TrainingTower::init()
{
	if (!Sprite::init()) //如果忘记了这句话则会在runApplication报错
	{
		return false;
	}

	instance = GameManager::getInstance();

	soldierToProduceCount = 0;
	isTrainingSpriteSelected = false;
	instance->totalSoldierNum = 0;

	setTowerType(7);
	setIsSelected(false);
	setGrade(0);//开始没有等级
	setPower(40);//塔消耗电力
	setMove(false);//开始不处于移动状态
	setIsPowerEnough(true);//开始状态电力足够
	setIsPowerConsumption(false);//该塔不耗电

	towerSprite = Sprite::create("towerItem/Item8.png");
	towerSprite->setScale(0.4);
	addChild(towerSprite, 5);

	trainingSprite = Sprite::create("traingButton.png"); //训练button,开始不显示,点击该建筑才显示
	trainingSprite->setScale(0.25);
	auto sb = ScaleBy::create(1, 1.2);
	auto seq = Sequence::create(sb, sb->reverse(), NULL);
	auto rp = RepeatForever::create(seq);
	trainingSprite->runAction(rp);
	trainingSprite->setPosition(towerSprite->getBoundingBox().size.width-10,0);
	addChild(trainingSprite, 5);
	trainingSprite->setVisible(false);

	produceTime = 0;//初始化生产时间为0
	produceTimeBarBg = Sprite::create("manaBarBg.png");
	produceTimeBarBg->setScale(0.3); //0.45
	produceTimeBarBg->setPosition(towerSprite->getPosition() + Point(0, 65));
	addChild(produceTimeBarBg, 11);

	//待生产士兵的数目
	soldierNum = Label::createWithTTF("", "b.ttf", 10);
	soldierNum->setPosition(produceTimeBarBg->getPosition());
	addChild(soldierNum, 12);


	//生产时间条初始化
	timeBar = ProgressTimer::create(Sprite::create("time.png"));
	timeBar->setScaleX(0.3);
	timeBar->setScaleY(2.2);
	timeBar->setType(ProgressTimer::Type::BAR);
	timeBar->setMidpoint(Point(0, 0.5));
	timeBar->setBarChangeRate(Point(1, 0));
	timeBar->setPercentage(produceTime);
	timeBar->setAnchorPoint(Point(0, 0.5));
	timeBar->setPosition(produceTimeBarBg->getPosition() - Point(produceTimeBarBg->getBoundingBox().size.width / 2 - 5, 0));
	addChild(timeBar, 10);

	//生产时间属性条不可见
	timeBar->setVisible(false);
	produceTimeBarBg->setVisible(false);
	soldierNum->setVisible(false);

	//血量条背景图片
	towerHpSprite = Sprite::create("manaBarBg.png");
	towerHpSprite->setPosition(towerSprite->getPosition() + Point(0, 50));
	towerHpSprite->setScale(0.3);
	addChild(towerHpSprite, 10);
	//炮塔血量
	hp = 4;
	//炮塔血量进度条
	towerHp = ProgressTimer::create(Sprite::create("soldierProduceTimeBar.png")); //参数是一个图片sprite
	towerHp->setScaleX(2);
	towerHp->setScaleY(5.2);
	towerHp->setType(ProgressTimer::Type::BAR);
	towerHp->setMidpoint(Point(0, 0.5f));
	towerHp->setBarChangeRate(Point(1, 0));
	towerHp->setPercentage(100);
	towerHp->setPosition(Point(towerHpSprite->getContentSize().width / 2, towerHpSprite->getContentSize().height / 3 * 2 - 10));
	towerHpSprite->addChild(towerHp, 5);
	//初始化不可见
	towerHp->setVisible(false);
	towerHpSprite->setVisible(false);

	addTouch();//添加触摸事件

	listener->onTouchBegan = [=](Touch* pTouch, Event* pEvent){
		
		
		auto target = static_cast<Sprite*>(pEvent->getCurrentTarget());
		
		Point locationInNode = target->convertTouchToNodeSpace(pTouch);
		
		Size size = target->getContentSize();
		Rect rect = Rect(0, 0, size.width, size.height);
	
		//log("rect size %f %f", size.width, size.height);
		if (rect.containsPoint(locationInNode)) 
		{
			timeBar->setVisible(true);
			produceTimeBarBg->setVisible(true);
			trainingSprite->setVisible(true);
			soldierNum->setVisible(true);

			towerHp->setVisible(true);
			towerHpSprite->setVisible(true);
			return true;
		}
		else if (!isTrainingSpriteSelected)//这样就可以实现点击其他地方隐藏属性条的功能了
		{
			timeBar->setVisible(false);
			produceTimeBarBg->setVisible(false);
			trainingSprite->setVisible(false);
			soldierNum->setVisible(false);
			towerHp->setVisible(false);
			towerHpSprite->setVisible(false);
		}
		
		return false;
	};

	

	trainingButtonListener = EventListenerTouchOneByOne::create();
	trainingButtonListener->onTouchBegan = [=](Touch* pTouch, Event* pEvent){ //这个是特殊声明 用来检测训练士兵按钮的点击,这种用法就是在要点击的图片上加listener

		auto target = static_cast<Sprite*>(pEvent->getCurrentTarget());

		Point locationInNode = target->convertTouchToNodeSpace(pTouch);

		Size size = target->getContentSize();
		Rect rect = Rect(0, 0, size.width, size.height);

		//log("rect size %f %f", size.width, size.height);
		if (rect.containsPoint(locationInNode) && trainingSprite->isVisible() && instance->totalSoldierNum<MAX_SOLDIER_NUM )
		{
			if (instance->getMoney() >= 100)
			{
				isTrainingSpriteSelected = true;
				soldierToProduceCount++;
				instance->totalSoldierNum++;//士兵总数最多10个
				char c[10];
				sprintf(c, "%d", soldierToProduceCount);
				std::string str = c;
				soldierNum->setString(str);

				//金钱减少
				instance->setMoney(instance->getMoney() - 100);
				char c1[10];
				sprintf(c1, "%d", instance->getMoney());
				std::string str1 = c1;
				instance->moneyLabel->setString(str1);
			}
			else //金钱不足
			{
				auto sb = ScaleBy::create(0.3, 1.2);
				auto seq = Sequence::create(sb, sb->reverse(), NULL);
				instance->moneyLabel->runAction(seq);
			}

			return true;
		}
		else
		{
			isTrainingSpriteSelected = false;
			trainingSprite->setVisible(false);
		}
		
		return false;
	};
	_eventDispatcher->addEventListenerWithSceneGraphPriority(trainingButtonListener, this->trainingSprite);

	this->schedule(schedule_selector(TrainingTower::produceSoldier), 0.1);

	return true;
}