用cocos2dx做一个简单的单机捕鱼达人游戏(5)

子弹和鱼都写好了。该写新手村了
用cocos2dx做一个简单的单机捕鱼达人游戏(5)_第1张图片
这里就是新手村截图,为了偷点懒,我就只加了两种鱼,一种大鱼,一种小鱼
鱼是随机从四面八方出来的,自带游泳动画,子弹打中鱼了也有子弹死亡动画也就是变网的动画
下面就是gameLayer的代码
gamelayer.h

#pragma once
#ifndef _GameLayer_H_
#define _GameLayer_H_
#include"cocos2d.h"
class Pao;
class Fish;
class Bullet;
class BulletManage;
class FishManage;
enum PAOTYPE;
using namespace cocos2d;
#include "ui\CocosGUI.h"
using namespace ui;
class GameLayer:public Layer
{
public:
	GameLayer();
	~GameLayer();
	void onEnter() override;
	void onExit() override;
	void onCollision2();//子弹与鱼的碰撞检测2方法
	//静态创建场景函数
	static Scene* createScene();
	//静态函数创建本类对象指针
	static GameLayer* create();
	//初始化
	virtual bool init();
	//生产鱼
	void FishCreate(float dt);
	//默认调度器的update函数
	void update(float dt);
	 int m_iScore;//金币数
private:
	TextAtlas * numScore;

	
	Fish* m_cFish;//鱼
	Pao * m_cPao;//大炮主成员
	Pao *m_pc1;//大炮电脑成员1
	BulletManage *bulletManage;
	FishManage*fishManage;
	bool isTouchScreen;//是否触摸到屏幕
};
#endif // !_GameLayer_H_

gameLayer.cpp

#include "GameLayer.h"
#include"Pao.h"
#include"Fish.h"
#include"Bullet.h"
#include"BulletManage.h"
#include"FishManage.h"
#include"FishFactory.h"
#include"SceneManager.h"
GameLayer::GameLayer()
{
}
GameLayer::~GameLayer()
{
	this->unscheduleUpdate();//关闭默认调度器
}
void GameLayer::onEnter()
{
	Node::onEnter();
	bulletManage = BulletManage::getInstance();
	fishManage = FishManage::getInstance();//
}
void GameLayer::onExit()
{
	Node::onExit();
	delete bulletManage;
	bulletManage = nullptr;
	delete fishManage;
	fishManage = nullptr;//
}

void GameLayer::onCollision2()
{
	//范围for遍历
	for (Bullet* bullet : bulletManage->BulletLive)
	{
		if (bullet->koufei==false)
		{
			bullet->koufei = true;//用于gamelayer中子弹发出扣金币判断
			m_iScore -= bullet->getCostCoin();//发出子弹扣金币
		}

		for (Fish* enemy : fishManage->FishLive)
		{
			//鱼死亡状态
			if (!enemy->getLive()||enemy->isPeng==true)
				continue;
			//碰撞检测
			bool isCrash = bullet->getBoundingBox().
				intersectsRect(enemy->getBoundingBox());
			
			if (isCrash)
			{
				enemy->reduceHp(bullet->getAtt());//鱼减血
				bullet->setLive(false);//子弹死亡
				//鱼死了就开启死亡动画并且回收
				if (enemy->getHp() <= 0)
				{	//关掉update函数 死亡了就停止移动
					enemy->unscheduleUpdate();
					(enemy)->isPeng = true; //增加一个bool值用于阻挡死了的鱼应为要回调播放动画造成的阻碍别的子弹
					m_iScore += enemy->getCoin();//金币增加
					switch (enemy->getFishType())
					{
					case FishTypeOne:
						(enemy)->DeadCartoonOne();//鲸鱼的死亡动画加上回收的回调函数
						break;
					case FishTypeTwo:
						enemy->DeadCartoonTwo();//小鲤鱼的死亡动画加上回收的回调函数
						break;
					case FishTypeThree:
						enemy->DeadCartoonThree();//小金鱼的死亡动画加上回收的回调函数
						break;
					default:
						break;
					}
				}
				break;
			}
			
		}
	}
}
Scene * GameLayer::createScene()
{
	//创建场景
	Scene* pScene = Scene::create();
	GameLayer* pGameLayer = GameLayer::create();
	pScene->addChild(pGameLayer);
	return pScene;
}

GameLayer * GameLayer::create()
{
	GameLayer*pLayer = new GameLayer;
	if (pLayer&&pLayer->init())
	{
		//添加到自动释放池
		pLayer->autorelease();
		return pLayer;
	}
	else
	{
		delete pLayer;
		pLayer = nullptr;
		return nullptr;
	}
}

bool GameLayer::init()
{
	if (!Layer::init())
	{
		return false;
	}

	
	//屏幕大小获取
	Size winSize = Director::getInstance()->getWinSize();
	//背景图片的添加
	Sprite*beijin = Sprite::create("bj01.jpg");
	this->addChild(beijin);
	beijin->setPosition(512,384);
	//游戏下方图片
	Sprite*GameDown = Sprite::create("gamedown.png");
	this->addChild(GameDown);
	GameDown->setPosition(450, 90);
	//大炮添加
	m_cPao = Pao::create(PaoTypeOne);
	this->addChild(m_cPao);
	//将大炮的位置设置为居中
	m_cPao->setPosition(Vec2(winSize.width / 2 + 15, m_cPao->getBoundingBox().size.height / 2 - 20));


	//返回按钮
	Button*back = Button::create("btn_back.png",
		"btn_back.png",
		"btn_back.png");
	this->addChild(back);
	//位置设置
	back->setPosition(Vec2(980, 600));
	//触摸事件响应
	back->addTouchEventListener([&](Ref* ref, Widget::TouchEventType type)
	{
		//切换场景(点击进入菜单界面)
		Director::getInstance()->replaceScene(SceneManager::create(MENU_SCENE));

	});


	//自动开炮按钮
	Button*zidong = Button::create("AutomaticFire_Normal.png",
		"AutomaticFire_Normal.png",
		"AutomaticFire_Normal.png");
	this->addChild(zidong);
	zidong->setScale(0.65f);//缩小为原来的0.5倍
	//位置设置
	zidong->setPosition(Vec2(980, 300));
	//触摸事件响应
	zidong->addTouchEventListener([&](Ref* ref, Widget::TouchEventType type)
	{
		//调用pao的自定义调度器  自动发炮
		m_cPao->schedule(schedule_selector(Pao::zidong), 1, -1, 0);

	});

	//手动开炮按钮
	Button*shoudong = Button::create("AutomaticFire_Normal2.png",
		"AutomaticFire_Normal2.png",
		"AutomaticFire_Normal2.png");
	this->addChild(shoudong);
	shoudong->setScale(0.65f);//缩小为原来的0.5倍
							//位置设置
	shoudong->setPosition(Vec2(980, 220));
	//触摸事件响应
	shoudong->addTouchEventListener([&](Ref* ref, Widget::TouchEventType type)
	{
		//调用pao的自定义调度器  自动发炮
		m_cPao->unschedule(schedule_selector(Pao::zidong));

	});
	//炮的等级增加按钮
	Button*addPao = Button::create("fish_review_btn_add_multiple_02.png",
		"fish_review_btn_add_multiple_02.png",
		"fish_review_btn_add_multiple_02.png");
	this->addChild(addPao);
	//位置设置
	addPao->setPosition(Vec2(600, 25));
	//触摸事件响应
	addPao->addTouchEventListener([&](Ref* ref, Widget::TouchEventType type)
	{
		if(type== cocos2d::ui::Widget::TouchEventType::ENDED)
		m_cPao->addPao();//炮等级增加

	});
	//炮的等级减少按钮
	Button*reducePao = Button::create("fish_review_btn_sub_multiple_01.png",
		"fish_review_btn_sub_multiple_01.png",
		"fish_review_btn_sub_multiple_01.png");
	this->addChild(reducePao);
	//位置设置
	reducePao->setPosition(Vec2(450, 25));
	//触摸事件响应
	reducePao->addTouchEventListener([&](Ref* ref, Widget::TouchEventType type)
	{
		if (type == cocos2d::ui::Widget::TouchEventType::ENDED)
			m_cPao->reducePao();//炮等级减少

	});
	// 艺术数字。参数:数字字符串;图片;单个字符宽;单个字符高;起始字符串字符
	m_iScore = 1000;
	numScore = TextAtlas::create("1000",
		"level_num.png",
		37, 37, "0");
	this->addChild(numScore);
	numScore->setPosition(Vec2(808, 25));
	//开启自定义调度器生产鱼
	schedule(schedule_selector(GameLayer::FishCreate),0.5f,-1,0);
	//开启默认调度器
	scheduleUpdate();
	return true;
}

void GameLayer::FishCreate(float dt)
{
	//屏幕大小获取
	Size winSize = Director::getInstance()->getWinSize();
	int num = rand()%100;
	//按概率创建鱼
	Fish* pFish = nullptr;
	if (num<20)
	{
		pFish = FishFactory::create(FishTypeOne);
	}
	else if(20<=num&&num<50)
	{
		pFish = FishFactory::create(FishTypeTwo);
	}
	else if (50<=num&&num<=100)
	{
		pFish = FishFactory::create(FishTypeTwo);

	}
	//添加到图层
	addChild(pFish);
	//设置鱼的位置
	switch (pFish->birth)
	{
	case 1:
		pFish->setPosition((rand() % 600)+100, 800);
		break;
	case 2:
		pFish->setPosition(winSize.width + pFish->getContentSize().width, rand() % 700);
		break;
	case 3:
		pFish->setPosition(rand() % 700, 0);
		break;
	case 4:
		pFish->setPosition(0, (rand() % 600)+300);
		break;
	default:
		break;
	}
	
}

void GameLayer::update(float dt)
{
	//判断鱼与子弹的碰撞
	onCollision2();
	//默认调度器实时更新金币数
	CCString* str =
		CCString::createWithFormat("%d", m_iScore);
	numScore->setStringValue(str->getCString());

}

你可能感兴趣的:(cocos2dx)