cocos2dx 植物大战僵尸 10 铲子的实现

铲子的作用是移除植物,而且铲子是和卡片互斥的,即点击了卡片,再点击铲子时会取消卡片的选中,反之亦然,接下来看看铲子的实现吧

class Shovel : public Entity
{
private:
	//当前是否处于选中状态
	bool m_bSelected;
public:
	Shovel();
	~Shovel();
	CREATE_FUNC(Shovel);
	bool init();
	//是否选中
	bool isSelected()const;
	//设置为选中状态
	void selected();
	//设置为没选中状态
	void unselected();
};
Shovel::Shovel()
	:m_bSelected(false)
{
}
Shovel::~Shovel()
{
}
bool Shovel::init()
{
	auto spriteName = STATIC_DATA_STRING("shovel_sprite_name");//使用plist对应着铲子的精灵名称
	this->bindSpriteWithSpriteFrameName(spriteName);

	return true;
}

bool Shovel::isSelected()const
{
	return m_bSelected;
}

void Shovel::selected()
{
	m_bSelected = true;
	//选中时会开始闪烁动作
	auto fade = FadeOut::create(0.5f);
	auto sequence = static_cast<ActionInterval*>(Sequence::create(fade,fade->reverse(),nullptr));
	auto repeat = RepeatForever::create(sequence);
	repeat->setTag(1);

	m_pSprite->runAction(repeat);
}

void Shovel::unselected()
{
	m_bSelected = false;
	//停止闪烁动作
	m_pSprite->stopActionByTag(1);
	m_pSprite->setOpacity(255);
}
铲子是保存在PanelLayer中的,在init添加一个对铲子的初始化

	//初始化铲子
	m_pShovel = Shovel::create();
	m_pShovel->setPosition(m_pShovelBg->getPosition());

	this->addChild(m_pShovel);
void PanelLayer::seletcShovel()
{
	m_pShovel->selected();
}

void PanelLayer::unselectShovel()
{
	if (m_pShovel->isSelected())
	{
		m_pShovel->unselected();
	}
}

bool PanelLayer::isClickShovel(const Point&pos)const
{
	auto rect = m_pShovel->getBoundingBox();

	if (rect.containsPoint(pos))
		return true;
	return false;
}

bool PanelLayer::isSelectedShovel()
{
	return m_pShovel->isSelected();
}
这些方法都是调用了铲子的方法,接下来就是使用地方了

在onTouchBegan方法

bool GameScene::onTouchBegan(Touch*touch,SDL_Event*event)
{
	auto pos = touch->getLocation();
	bool bRet = false;
	//是否点击了卡片层
	auto clickedCard = m_pCardLayer->getClickedCard(pos);

	if (clickedCard && clickedCard->isEnabled())
	{
		auto selectedCard = m_pCardLayer->getSelectedCard();
		//取消选中
		if (clickedCard == selectedCard)
		{
			m_pCardLayer->unselectedCard();
		}
		else//重新设置选中
		{
			m_pCardLayer->setSelectedCard(clickedCard);
		}
		bRet = true;
		//与铲子互斥
		m_pPanelLayer->unselectShovel();
	}
	//点击了铲子
	else if (m_pPanelLayer->isClickShovel(pos))
	{
		//已经选中铲子,取消选中
		if (m_pPanelLayer->isSelectedShovel())
		{
			m_pPanelLayer->unselectShovel();
		}
		else
		{
			m_pPanelLayer->seletcShovel();
		}
		bRet = true;
		//与卡片层互斥
		m_pCardLayer->unselectedCard();
	}

	return bRet;
}
上面实现了铲子的点击,卡片的点击和他们之间的互斥

void GameScene::onTouchEnded(Touch*touch,SDL_Event*event)
{
	auto pos = touch->getLocation();
	auto nodePos = m_pLevelLayer->convertToNodeSpace(pos);
	//是否点击了ProductLayer
	auto product = m_pProductLayer->getClickedProduct(pos);
	if (product)
	{
		product->setClicked(true);
		this->collectProduct(product);

		return;
	}
	//是否点击了某一个terrain
	auto terrain = m_pLevelLayer->getClickedTerrain(nodePos);
	//所有操作对要经过terrain
	if (terrain == nullptr)
		return;

	auto selectedCard = m_pCardLayer->getSelectedCard();
	//存在选中的卡片,则阳光足够,cd完成
	if (selectedCard)
	{
		bool bRet = false;
		
		bRet = this->tryPlanting(selectedCard,terrain);
		//创建完成
		if (bRet)
		{
			//减少阳光值
			this->subSun(selectedCard->getWorth());
			//取消点击
			m_pCardLayer->unselectedCard();
			//该卡片开始cd
			selectedCard->setCurCD(selectedCard->getCD());
		}
	}//选中了铲子
	else if (m_pPanelLayer->isSelectedShovel())
	{
		//获取顶层植物
		auto topPlant = m_pLevelLayer->getTopPlant(terrain);
		if (topPlant != nullptr)
		{
			//获取顶层植物的容器
			auto vessel = m_pLevelLayer->getVesselOfPlant(terrain,topPlant);

			vessel->clearInnerPlant();
			//移除该植物
			this->removePlant(topPlant);
			//取消铲子的选中
			m_pPanelLayer->unselectShovel();
		}
	}
}
在onTouchEnded中如果点击了铲子,则进行删除植物,另外删除最上层的植物

void GameScene::removePlant(Plant*plant)
{
	//TODO
	m_pPlantLayer->removePlant(plant);
	plant->removeFromParent();
}
植物的移除方法,作扩展使用。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值