首页 > 代码库 > Cocos2d-x 3.2 大富翁游戏项目开发-第十八部分 问号随机事件

Cocos2d-x 3.2 大富翁游戏项目开发-第十八部分 问号随机事件

角色走完要求的步数后,先查看停留位置是否有问号,如果有,先处理问号事件,处理完毕后,再处理相邻周边上下左右地块问题,购买、升级或缴纳过路费。

 

问号随机事件主要是如下事件:

政府鼓励投资,返还税金10000

政府严查账务,补交税金20000

喝到假酒,上吐下泻,体力耗光

吃了大补丸,体力恢复

投资获利,分红20000

投资失败,亏损30000

由于还没有进行国际化处理,在用中文的时候,会出现乱码,我们暂时先都用英文处理一下,后期统一做国际化处理

 

1、与之对应的变量定义在ConstUtil.h文件中:

const int TAX_REBATES_TAG = 1; //政府鼓励投资,返还税金10000
const int PAY_TAXES_TAG = 2; //政府严查账务,补交税金20000
const int LOSS_STRENGTH_TAG =3;//喝到假酒,上吐下泻,体力耗光
const int PHYSICAL_RECOVERY_TAG =4;//吃了大补丸,体力恢复
const int INVESTMENT_DIVIDENDS_TAG = 5 ;//投资获利,分红20000
const int INVESTMENT_LOSS_TAG = 6; //投资失败,亏损30000

#define  TAX_REBATES  "The government encourages investment, tax rebates 10000"
#define  PAY_TAXES "Government scrutiny of accounts, pay taxes 20000"
#define  LOSS_STRENGTH "To drink alcohol, diarrhea, loss of light strength"
#define  PHYSICAL_RECOVERY "Eat cake, physical recovery"
#define  INVESTMENT_DIVIDENDS "Investment profits, dividends 20000"
#define  INVESTMENT_LOSS "Investment failure, loss 30000"

2、在GameBaseScene.cpp初始化随机事件Map容器

void GameBaseScene::initRandomAskEvent()
{
	randomAskEventMap.insert(TAX_REBATES_TAG,__String::create(TAX_REBATES));
	randomAskEventMap.insert(PAY_TAXES_TAG,__String::create(PAY_TAXES));
	randomAskEventMap.insert(LOSS_STRENGTH_TAG,__String::create(LOSS_STRENGTH));
	randomAskEventMap.insert(PHYSICAL_RECOVERY_TAG,__String::create(PHYSICAL_RECOVERY));
	randomAskEventMap.insert(INVESTMENT_DIVIDENDS_TAG,__String::create(INVESTMENT_DIVIDENDS));
	randomAskEventMap.insert(INVESTMENT_LOSS_TAG,__String::create(INVESTMENT_LOSS));
}

3、当角色走完步数后,会调用到RicherGameController的handlePropEvent方法。

在该方法中我们判断角色停留的位置是否有问号,如果有,就发送一个问号信息MSG_RANDOM_ASK_EVENT,同时方法返回。如果没有问号,就调用aroundLandEvent()方法,这个方法是处理角色周边相邻上下左右地块信息的方法,我们把以前的代码抽取成了该方法,便于后期重复使用

void RicherGameController::handlePropEvent()
{
	oneRoundDone =false;
	float playerEnd_X = _colVector[stepsCount]*32;
        float playerEnd_Y = _rowVector[stepsCount]*32 + 32;

	 Point pointInMap = Util::GL2map(Vec2(playerEnd_X,playerEnd_Y), GameBaseScene::_map);
	 int endId = GameBaseScene::wayLayer->getTileGIDAt(pointInMap);
	 if(endId == GameBaseScene::randomEvent_tiledID)
	 {
		 String * str = String::createWithFormat("%d-%f-%f-%d",MSG_RANDOM_ASK_EVENT_TAG,pointInMap.x,pointInMap.y,_richerPlayer->getTag());
		 NotificationCenter::getInstance()->postNotification(MSG_RANDOM_ASK_EVENT,str);
		 return;
	 }

	 aroundLandEvent();
}

4、GameBaseScene.cpp中注册了对问号信息MSG_RANDOM_ASK_EVENT的处理

void GameBaseScene::registerNotificationObserver()
{
 ………………………..
NotificationCenter::getInstance()->addObserver(
		this,
		callfuncO_selector(GameBaseScene::receivedNotificationOMsg),
		MSG_RANDOM_ASK_EVENT,
		NULL);	
…………………….
}

处理方法在receivedNotificationOMsg()方法中,当接收到问号信息,根据角色调用doRandomAskEvent()方法,处理问号信息,

处理完毕后调用sendMSGDealAroundLand()方法发送MSG_AROUND_LAND消息,处理角色相邻上下左右地块的消息,缴纳过路费或购买升级地块。

void GameBaseScene::receivedNotificationOMsg(Object* data)
{
……………..
	case MSG_RANDOM_ASK_EVENT_TAG:
		{
		
			int playerTag = messageVector.at(3)->intValue();
			switch(playerTag)
			{
				case PLAYER_1_TAG:
				{
					doRandomAskEvent(player1);
					scheduleOnce(schedule_selector( GameBaseScene::sendMSGDealAroundLand),TOAST_SHOW_TIME);
					break;
				}
				case PLAYER_2_TAG:
				{
					doRandomAskEvent(player2);
					scheduleOnce(schedule_selector( GameBaseScene::sendMSGDealAroundLand),TOAST_SHOW_TIME);
					break;
				}				
			}
			
			break;
		}
…………….
}
void GameBaseScene::sendMSGDealAroundLand(float dt)
{
	NotificationCenter::getInstance()->postNotification(MSG_AROUND_LAND,String::createWithFormat("%d",MSG_AROUND_LAND_TAG));
}

5、处理问号信息的方法doRandomAskEvent(),就是从问号容器中随机取出一个,根据事件进行资金或体力的增减,并Toast提示事件信息

void GameBaseScene::doRandomAskEvent(RicherPlayer* player)
{
			int randomNumber = rand()%(randomAskEventMap.size()) + 1; 
			__String * str = randomAskEventMap.at(randomNumber);

			switch(randomNumber)
			{
			case TAX_REBATES_TAG:
				{
					refreshMoneyLabel(player,10000);					
					break;
				}
			case PAY_TAXES_TAG:
				{
					refreshMoneyLabel(player,-20000);
					break;
				}

			case LOSS_STRENGTH_TAG:
				{
					refreshStrengthLabel(player,-100);
					break;
				}
			case PHYSICAL_RECOVERY_TAG:
				{
					refreshStrengthLabel(player,100);
					break;
				}
			case INVESTMENT_DIVIDENDS_TAG:
				{
					refreshMoneyLabel(player,20000);
					break;
				}
			case INVESTMENT_LOSS_TAG:
				{
					refreshMoneyLabel(player,-30000);
					break;
				}

			}
			CocosToast::createToast(this, str->getCString(), TOAST_SHOW_TIME,player->getPosition());
}

6、RicherGameController.cpp中注册了MSG_AROUND_LAND消息观察者,收到该消息后调用aroundLandEvent() ,处理过路费或购买地块等问题

void RicherGameController::registerNotificationObserver()
{
	…………………….

	NotificationCenter::getInstance()->addObserver(
		this,
		callfuncO_selector(RicherGameController::receivedMsg),
		MSG_AROUND_LAND,
		NULL);
}

void RicherGameController::receivedMsg(Object* data)
{
	……………………

	if(retMsgType == MSG_AROUND_LAND_TAG)
	{	
		aroundLandEvent();
	}
	
}

点击下载代码   http://download.csdn.net/detail/lideguo1979/8339065

未完待续...............

Cocos2d-x 3.2 大富翁游戏项目开发-第十八部分 问号随机事件