⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameplayer.cpp

📁 网络泡泡被.net管理
💻 CPP
字号:
// GamePlayer.cpp: implementation of the GamePlayer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GamePlayer.h"
#include <math.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

GamePlayer::GamePlayer()
{
	m_ptr_table	= NULL;
	m_ptr_user	= NULL;
	m_num_time_pop	= 0;
	m_num_time_shoot	= 0;
	m_tick_last_shoot	= 0;
}

GamePlayer::~GamePlayer()
{

}

bool GamePlayer::Create(BYTE width, BYTE height)
{
	if(false == BubbleScene::Create(width, height))
		return false;

	current_bubble_type	= 0;//RandomBubble();
	next_bubble_type	= 0;//RandomBubble();
//	m_state	= PLAYER_STATE_NULL;

	return true;
}

bool GamePlayer::on_user_shoot(STRUCT_SHOOT_DATA &shoot)
{
	if(current_bubble_type!=shoot.bubble)
		return false;
	if (m_num_time_shoot>=LIMIT_LEVEL_SHOOT)
	{//蹦了,下压一层
		trigger_add_level();
		trigger_bubble_up(4);
		m_num_time_shoot = 0;
	}
	if (num_bubble_store>30)
	{
		if (num_bubble_store>50)
		{
			if (rand()<RAND_MAX/5)
			{//25%几率漏掉一些泡泡
				num_bubble_store	-= 4;
				trigger_bubble_up(2);
				m_ptr_table->on_auto_handle(this);
			}
		}
		else if (rand()<RAND_MAX/5)
		{//20%几率漏掉一些泡泡
			num_bubble_store	-= 2;
			trigger_bubble_up(1);
			m_ptr_table->on_auto_handle(this);
		}
	}
	if (Sys_GetTime() - m_tick_last_shoot > LIMIT_SHAKE_DELAY && rand()<RAND_MAX/4)
	{//25%几率
		trigger_bubble_up(1);
	}
	bool bRet	= CaleShoot(shoot);
	SetBubble(shoot.x_stop, shoot.y_stop, current_bubble_type);
	BYTE cards_trigger[WAY_SIZE];
	for (int i=0; i<WAY_SIZE; i++)
	{//记录触发的卡片效果
		int idx	= GetIndexAround(shoot.x_stop, shoot.y_stop, i);
		if (idx>=0)
		{
			if (GetBubble(idx)%10==current_bubble_type%10 && GetBubble(idx)>10)
			{//有卡片
				cards_trigger[i]	= GetBubble(idx);
				continue;
			}
		}
		cards_trigger[i]	= 0;
	}
	shoot.num_pop = PopBubble(shoot.x_stop, shoot.y_stop);
	if(shoot.num_pop>=3)
	{
		AddGoodBubble(shoot.num_pop - 3);
		m_num_time_pop ++;
		memcpy(m_cards_trigger, cards_trigger, WAY_SIZE);
	}
	current_bubble_type	= next_bubble_type;
	next_bubble_type	= RandomBubble();
	shoot.bubble	= next_bubble_type;
	shoot.shoot_cmd	= 0;
	if (m_num_time_shoot>=LIMIT_LEVEL_SHOOT-2)
	{//快要蹦了,告诉客户端震动
		shoot.shoot_cmd += 10;
	}
	update_state();

	m_tick_last_shoot	= Sys_GetTime();
	m_num_time_shoot++;
	return bRet;
}

bool GamePlayer::on_auto_shoot(STRUCT_SHOOT_DATA &shoot)
{
	bool bRet	= CaleShoot(shoot);
	SetBubble(shoot.x_stop, shoot.y_stop, shoot.bubble);
	shoot.shoot_cmd	= 1;
	update_state();

	return bRet;
}


NET_Packet* GamePlayer::CreateScenePacket()
{
	if(m_ptr_user==NULL)
		return NULL;
	NET_Packet* pPacket	= new NET_Packet;
	pPacket->setCmd(GSP_SCENE_DATA);
	pPacket->write32(m_ptr_user->uid);
	pPacket->writeData((STRUCT_ADD_USER*)m_ptr_user, sizeof(STRUCT_ADD_USER));
	pPacket->writeData((STRUCT_ADD_SCENE*)this, sizeof(STRUCT_ADD_SCENE));
	pPacket->writeData(m_data, m_width*m_height);
	return pPacket;
}


void GamePlayer::AddLevel()
{
	if(!IsValid())
		return;
	for(int y=0; y<m_height-1; y++)
	{
		memcpy(m_data+y*m_width, m_data+(y+1)*m_width, m_width);
	}
	m_level++;
	if((m_level+m_height)%2)
	{
		for(int x=0; x<m_width; x++)
		{
			m_data[m_width*(m_height-1) + x]	= RandomBubble(20);
		}
	}
	else
	{
		for(int x=0; x<m_width-1; x++)
		{
			m_data[m_width*(m_height-1) + x]	= RandomBubble(20);
		}
		m_data[m_width*(m_height)-1]	= 0;
	}
}

UCHAR GamePlayer::RandomBubble(int cards_oods)
{
	UCHAR ret = (rand()*59.0f/RAND_MAX+10)/10;
	if (cards_oods>0 && rand()*100.0f/RAND_MAX<cards_oods)
	{
		ret += UCHAR((rand()*(ENUM_CARD_NUM*1000-1499)/RAND_MAX+1700)/1000)*10;
	}
	return ret;
}

bool GamePlayer::Start()
{
	current_bubble_type	= RandomBubble();
	next_bubble_type	= RandomBubble();
	m_state	= PLAYER_STATE_PLAYING;
	on_state_change();
	m_tick_last_shoot	= Sys_GetTime();
	m_num_time_pop		= 0;
	m_num_time_shoot	= 0;
	num_bubble_bad		= 0;
	num_bubble_store	= 0;
	for (int i=0; i<WAY_SIZE; i++)
	{
		m_cards_trigger[i]	= 0;
	}

	return true;
	//return false;
}


void GamePlayer::SetTable(GameTable *ptr_table)
{
	m_ptr_table	= ptr_table;
}

void GamePlayer::SetUser(GameUser *ptr_user)
{
	m_ptr_user	= ptr_user;
}


void GamePlayer::on_state_change()		
{ 
	if(m_ptr_user && m_ptr_table)
	{
		NET_Packet*	pPacket	= new NET_Packet;
		pPacket->setCmd(GSP_UPDATE_STATE);
		pPacket->write32(m_ptr_user->uid);
		pPacket->writeData(&m_state, sizeof(ENUM_PLAYER_STATE));
		m_ptr_table->BroadcastPacket(pPacket);
	}
}

void GamePlayer::Reset()
{
	m_state	= PLAYER_STATE_NULL;
	current_bubble_type	= 0;//RandomBubble();
	next_bubble_type	= 0;//RandomBubble();
	num_bubble_bad		= 0;
	num_bubble_store	= 0;
	if (m_num_data>0)
	{
		memset(m_data, 0, m_num_data);
	}
	//on_state_change();
}

void GamePlayer::trigger_add_level(int level)
{
	AddLevel();
	NET_Packet* pPacket;
	if(pPacket	= CreateScenePacket())
		m_ptr_table->BroadcastPacket(pPacket);

}

void GamePlayer::trigger_bubble_up(int num_bubble)
{
	STRUCT_SHOOT_DATA	ssd;
	ssd.ftan	= tan(const_PI_DIV_2);
	int j=0;
	std::vector<LONG> flag;
	for (int i=1; i<=m_width; i++)
	{
		flag.push_back(i);
	}
	std::vector<LONG>::iterator it;
	while (j<num_bubble && flag.size()>0)
	{
		it = flag.begin() + int(flag.size()*rand()/(RAND_MAX+1));
		if (it!=flag.end())
		{
			ssd.x_org	= ((*it)-1)*2 - m_fWidth; //-GetWidth() + i*2;
			ssd.y_org	= 0;
			ssd.bubble	= RandomBubble(50);
			ssd.shoot_cmd	= 1;
			on_auto_shoot(ssd);
			NET_Packet* pPacket = new NET_Packet;
			pPacket->setCmd(GSP_SHOOT);
			pPacket->write32(m_ptr_user->uid);
			pPacket->writeData(&ssd, sizeof(STRUCT_SHOOT_DATA));
			m_ptr_table->BroadcastPacket(pPacket);
			j++;
			flag.erase(it);
		}

	}
	for (i=0; i<SCENE_WIDTH && j<num_bubble; i++)
	{
		if (SCENE_WIDTH-i <= num_bubble-j ||
			rand()>RAND_MAX/3)
		{//50%机会得到魔法泡泡
			ssd.bubble	= RandomBubble(50);
			ssd.x_org	= -GetWidth() + i*2;
			ssd.y_org	= 0;
			ssd.shoot_cmd	= 1;
			on_auto_shoot(ssd);
			NET_Packet* pPacket = new NET_Packet;
			pPacket->setCmd(GSP_SHOOT);
			pPacket->write32(m_ptr_user->uid);
			pPacket->writeData(&ssd, sizeof(STRUCT_SHOOT_DATA));
			m_ptr_table->BroadcastPacket(pPacket);
			j++;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -