//
// GameLayer.cpp
// MoonWarriorsx
//
// Created by 王 磊 on 12-12-29.
//
//
#include "GameLayer.h"
#include "SimpleAudioEngine.h"
#include "Bullet.h"
#include "Resource.h"
#include "Config.h"
#include "Enemy.h"
#include "Effect.h"
#include "GameOver.h"
#include "PauseLayer.h"
using namespace cocos2d;
using namespace CocosDenshion;
bool isPaused = false;
GameLayer::GameLayer():m_state(statePlaying),m_time(0),m_ship(NULL),m_backSky(NULL),m_backSkyHeight(0),m_backSkyRe(NULL),m_backTileMap(NULL),m_backTileMapHeight(0),m_backTileMapRe(NULL),m_isBackSkyReload(false),m_isBackTileReload(false),m_lbScore(NULL),m_lifeCount(NULL),
m_tempScore(0)
{
}
GameLayer::~GameLayer()
{
if (m_levelManager) {
delete m_levelManager;
}
play_bullet->release();
enemy_bullet->release();
enemy_items->release();
}
bool GameLayer::init()
{
if (!CCLayer::init()) {
return false;
}
// 开启触摸
this->setTouchEnabled(true);
// 创建数组,需要retain一下
play_bullet = CCArray::create();
play_bullet->retain();
enemy_bullet = CCArray::create();
enemy_bullet->retain();
enemy_items = CCArray::create();
enemy_items->retain();
m_state = statePlaying;
Enemy::sharedEnemy();
Effect::sharedExplosion();
Config::sharedConfig()->resetConfig();
winSize = CCDirector::sharedDirector()->getWinSize();
m_levelManager = new LevelManager(this);
//初始化背景
initBackground();
m_screenRec = CCRectMake(0, 0, winSize.width, winSize.height + 10);
// score
m_lbScore = CCLabelBMFont::create("Score:0", s_arial14_fnt);
m_lbScore->setAnchorPoint(ccp(1, 0));
m_lbScore->setAlignment(kCCTextAlignmentRight);
addChild(m_lbScore, 1000);
m_lbScore->setPosition(winSize.width - 5, winSize.height - 30);
// ship life
CCTexture2D *shipTexture = CCTextureCache::sharedTextureCache()->addImage(s_ship01);
CCSprite *life = CCSprite::createWithTexture(shipTexture, CCRectMake(0, 0, 60, 38));
life->setScale(0.6f);
life->setPosition(ccp(30,winSize.height-23));
addChild(life, 1, 5);
// ship life count
char lifecount[2];
sprintf(lifecount, "%d",Config::sharedConfig()->getLifeCount());
m_lifeCount = CCLabelTTF::create(lifecount, "Arial", 20);
m_lifeCount->setPosition(ccp(60, winSize.height-20));
m_lifeCount->setColor(ccc3(255,0, 0));
addChild(m_lifeCount, 1000);
// ship
m_ship = Ship::create();
addChild(m_ship, m_ship->getZoder(), 1001);
CCMenuItemImage *pause = CCMenuItemImage::create("pause.png", "pause.png", this, menu_selector(GameLayer::doPause));
pause->setAnchorPoint(ccp(1, 0));
pause->setPosition(ccp(winSize.width, 0));
CCMenu *menu = CCMenu::create(pause, NULL);
menu->setAnchorPoint(ccp(0, 0));
addChild(menu, 1, 10);
menu->setPosition(CCPointZero);
// 调 update函数
scheduleUpdate();
// 每秒调一次 scoreCounter函数
schedule(schedule_selector(GameLayer::scoreCounter), 1);
if (Config::sharedConfig()->getAudioState()) {
SimpleAudioEngine::sharedEngine()->playBackgroundMusic(s_bgMusic, true);
}
return true;
}
void GameLayer::update(float dt)
{
if (m_state == statePlaying) {
checkIsCollide();
removeInactiveUnit(dt);
checkIsReborn();
updateUI();
}
}
void GameLayer::scoreCounter(float dt)
{
if (m_state == statePlaying) {
m_time++;
m_levelManager->loadLevelResource(m_time);
}
}
void GameLayer::checkIsCollide()
{
CCObject *units;
CCObject *bullets;
CCObject *enemybs;
CCARRAY_FOREACH(enemy_items, units)
{
UnitSprite *enemy = dynamic_cast<UnitSprite*>(units);
CCARRAY_FOREACH(play_bullet, bullets)
{
UnitSprite *bullet = dynamic_cast<UnitSprite*>(bullets);
if (this->collide(enemy, bullet)) {
enemy->hurt();
bullet->hurt();
}
if (!(m_screenRec.intersectsRect(bullet->boundingBox()))) {
bullet->destroy();
}
}
if (collide(enemy, m_ship)) {
if (m_ship->isActive()) {
enemy->hurt();
m_ship->hurt();
}
}
if (!(m_screenRec.intersectsRect(enemy->boundingBox()))) {
enemy->destroy();
}
}
CCARRAY_FOREACH(enemy_bullet, enemybs)
{
UnitSprite *enemyb = dynamic_cast<UnitSprite*>(enemybs);
if (enemyb) {
if (collide(enemyb, m_ship)) {
if (m_ship->isActive()) {
enemyb->hurt();
m_ship->hurt();
}
}
if (!m_screenRec.intersectsRect(enemyb->boundingBox())) {
enemyb->destroy();
}
}
}
}
void GameLayer::removeInactiveUnit(float dt)
{
CCArray *children = this->getChildren();
for (unsigned int i = 0; i < children->count(); ++i) {
CCSprite *selChild = dynamic_cast<CCSprite*>(children->objectAtIndex(i));
if (selChild) {
selChild->update(dt);
int tag = selChild->getTag();
if (( tag == 900) || (tag == 901 )|| (tag == 1000)) {
if (!((UnitSprite*)selChild)->isActive()) {
((UnitSprite*)selChild)->destroy();
}
}
}
}
if (m_ship) {
if (!m_ship->isActive()) {
m_ship->destroy();
m_ship = NULL;
}
}
}
void GameLayer::checkIsReborn()
{
if (Config::sharedConfig()->getLifeCount() > 0) {
if (!m_ship) {
m_ship = Ship::create();
this->addChild(m_ship, m_ship->getZoder(), 1001);
}
}
else if (Config::sharedConfig()->getLifeCount() <= 0 ) {
m_state = stateGameOver;
m_ship = NULL;
CCCallFunc *gameOver = CCCallFunc::create(this, callfunc_selector(GameLayer::gameOver));
this->runAction(CCSequence::create(CCDelayTime::create(0.2f), gameOver, NULL));
}
}
bool GameLayer::collide(UnitSprite *a, UnitSprite *b)
{
if(!a || !b)
{
return false;
}
CCRect aRect = a->collideRect();
CCRect bRect = b->collideRect();
if (aRect.intersectsRect(bRect)) {
return true;
}
return false;
}
void GameLayer::updateUI()
{
if (m_tempScore < Config::sharedConfig()->getScoreValue()) {
m_tempScore += 5;
}
char score[20];
sprintf(score, "Score:%d", m_tempScore);
m_lbScore->setString(score);
char lifecount[2];
sprintf(lifecount, "%d",Config::sharedConfig()->getLifeCount());
m_lifeCount->setString(lifecount);
}
void GameLayer::onEnter()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
CCLayer::onEnter();
}
void GameLayer::onExit()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
}
bool GameLayer::ccTouchBegan(CCTouch *touch, CCEvent *event)
{
// CCLog("touch began!");
return true;
}
void GameLayer::ccTouchMoved(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)
{
if ((m_state == statePlaying) && m_ship) {
CCPoint pos = touch->getDelta();
CCPoint currentPos = m_ship->getPosition();
currentPos = ccpAdd(currentPos, pos);
currentPos = ccpClamp(currentPos, CCPointZero, ccp(winSize.width, winSize.height));
m_ship->setPosition(currentPos);
}
}
void GameLayer::ccTouchEnded(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)
{
// CCLog("touch end!");
}
// 无限滚动地图,采用两张图循环加载滚动
void GameLayer::initBackground()
{
m_backSky = CCSprite::create(s_bg01);
m_backSky->setAnchorPoint(ccp(0, 0));
m_backSkyHeight = m_backSky->getContentSize().height;
addChild(m_backSky, -10);
// Tile map
m_backTileMap = CCTMXTiledMap::create(s_level01);
addChild(m_backTileMap, -9);
m_backTileMapHeight = m_backTileMap->getMapSize().height * m_backTileMap->getTileSize().height;
m_backSkyHeight -= 48;
m_backTileMapHeight -= 200;
m_backSky->runAction(CCMoveBy::create(3, ccp(0, -48)));
m_backTileMap->runAction(CCMoveBy::create(3, ccp(0, -200)));
schedule(schedule_selector(GameLayer:: movingBackground),3);
}
// 这里就是视差背景了
void GameLayer::movingBackground(float dt)
{
m_backSky->runAction(CCMoveBy::create(3, ccp(0, -48)));
m_backTileMap->runAction(CCMoveBy::create(3, ccp(0, -200)));
// 每次移动48
m_backSkyHeight -= 48;
// 每次移动200
m_backTileMapHeight -= 200;
// 图的顶部到达屏幕顶部时
if (m_backSkyHeight <= winSize.height) {
if (!m_isBackSkyReload) {
// 如果另一张图还没加载则create一个
m_backSkyRe = CCSprite::create(s_bg01);
m_backSkyRe->setAnchorPoint(ccp(0, 0));
addChild(m_backSkyRe, -10);
没有合适的资源?快使用搜索试试~ 我知道了~
cocos2d-x 2.0版雷电(win32工程)
共127个文件
png:25个
tlog:18个
h:17个
4星 · 超过85%的资源 需积分: 15 47 下载量 47 浏览量
2013-02-13
10:32:03
上传
评论 1
收藏 4.31MB 7Z 举报
温馨提示
网上有一份mac版的,在win下用的不方便,改成了win32工程,vs可直接添加现有项目
资源推荐
资源详情
资源评论
收起资源包目录
cocos2d-x 2.0版雷电(win32工程) (127个子文件)
GameLayer.cpp 9KB
Effect.cpp 5KB
StartMenu.cpp 5KB
Ship.cpp 4KB
HelloWorldScene.cpp 3KB
Options.cpp 3KB
Config.cpp 3KB
LevelManager.cpp 3KB
Enemy.cpp 2KB
About.cpp 2KB
GameOver.cpp 2KB
Bullet.cpp 2KB
PauseLayer.cpp 2KB
AppDelegate.cpp 2KB
main.cpp 863B
UnitSprite.cpp 255B
Moon1.win32.vcxproj.filters 4KB
arial-14.fnt 12KB
bitmapFontTest.fnt 11KB
arial-14.GlyphProject 3KB
resource.h 2KB
GameLayer.h 1KB
AppDelegate.h 1KB
Config.h 1KB
Ship.h 1KB
StartMenu.h 1008B
Enemy.h 804B
Bullet.h 729B
PauseLayer.h 634B
Effect.h 632B
HelloWorldScene.h 631B
LevelManager.h 625B
UnitSprite.h 563B
Options.h 421B
GameOver.h 395B
About.h 372B
main.h 270B
Moon1.ico 47KB
vc100.idb 2.33MB
iTunesArtwork 61KB
flare.jpg 25KB
bg01.jpg 17KB
explode2.jpg 3KB
explode3.jpg 2KB
explode1.jpg 2KB
hit.jpg 2KB
Moon1.win32.lastbuildstate 50B
Moon1.win32.log 4KB
Moon1.win32.exe.embed.manifest 406B
Moon1.win32.exe.intermediate.manifest 381B
bgMusic.mp3 1.93MB
mainMainMusic.mp3 748KB
shipDestroyEffect.mp3 23KB
buttonEffet.mp3 8KB
explodeEffect.mp3 4KB
fireEffect.mp3 2KB
Config.obj 363KB
LevelManager.obj 339KB
GameLayer.obj 167KB
Enemy.obj 161KB
StartMenu.obj 135KB
Ship.obj 124KB
GameOver.obj 112KB
Effect.obj 111KB
Options.obj 110KB
Bullet.obj 109KB
About.obj 108KB
HelloWorldScene.obj 103KB
UnitSprite.obj 94KB
PauseLayer.obj 89KB
AppDelegate.obj 86KB
main.obj 40KB
vc100.pdb 684KB
explosion.plist 17KB
Enemy.plist 3KB
bullet.plist 2KB
Info.plist 1KB
explosion.png 202KB
loading.png 162KB
bitmapFontTest.png 140KB
HelloWorld.png 136KB
b01.png 123KB
Default.png 87KB
logo.png 59KB
arial-14.png 39KB
gameOver.png 29KB
Enemy.png 25KB
menu.png 24KB
cocos2d-html5.png 16KB
Icon@2x.png 16KB
menuTitle.png 14KB
Icon-72.png 12KB
ship01.png 11KB
Icon.png 9KB
Icon-Small@2x.png 8KB
Icon-Small-50.png 7KB
CloseNormal.png 6KB
play.png 6KB
CloseSelected.png 5KB
Icon-Small.png 5KB
共 127 条
- 1
- 2
areyouaboy
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 微信自动抢红包APP.zip
- 微信抢红包神器-红包鸟(可以抢多种情况下的红包,支持手机息屏抢红包、屏蔽群抢红包).zip
- 微信抢红包,支持xposed与免root,xposed支持最新版,免root需自己实现.zip
- 一维卷积神经网络英语电影评论情感分类项目功能实现-Embedding层
- 微信 自动抢红包 插件外挂(Android),实现了监控通知栏,自动点击红包等功能,帮助大家快速抢到红包.zip
- 满满的干货:分享二十个Python学习资源材料.zip
- USD ISO14229-1-2013 中文版
- STM32单片机FPGA毕设电路原理论文报告模糊控制器在无线监控机群系统中的应用
- OpenWrt-Toolchain-ipq
- STM32单片机FPGA毕设电路原理论文报告模糊控制逆变弧焊电源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页