#include "GameScene.h"
GamerObjHero* hero;
GameScene::GameScene(void)
{
}
GameScene::~GameScene(void)
{
}
void GameScene::onEnter()
{
CCLayer::onEnter();
}
void GameScene::onExit()
{
CCLayer::onExit();
}
CCScene* GameScene::scene()
{
CCScene* scene=CCScene::create();
//GameScene* layer=GameScene::create();
scene->addChild(GameScene::create());
return scene;
}
bool GameScene::init()
{
if (!CCLayer::init())
{
return false;
}
isover=false;
CCSize size=CCDirector::sharedDirector()->getWinSize();
//创建背景
bg1=CCSprite::create("bg.png");
bg1->setScale(0.5);
bg1->setAnchorPoint(ccp(0,0));
bg1->setPosition(ccp(0,0));
bg2=CCSprite::create("bg.png");
bg2->setScale(0.5);
bg2->setAnchorPoint(ccp(0,0));
bg2->setPosition(ccp(0,size.height));
this->addChild(bg1,0);
this->addChild(bg2,0);
//创建主角
hero=new GamerObjHero();
hero->setPosition(ccp(size.width/2,-50));
hero->setScale(0.5);
this->addChild(hero,2,1);
hero->runAction(CCMoveBy::create(0.5,ccp(0,150)));
bullets=CCArray::create();
for (int i=0;i<5;i++)
{
GameHeroBullet* mybullet=new GameHeroBullet();
mybullet->setIsNotVisable();
mybullet->setScale(0.5);
bullets->addObject(mybullet);
this->addChild(mybullet);
}
bullets->retain();
//创建敌人
enemys=CCArray::create();
for (int i=0;i<3;i++)
{
int type=i+1;
GameObjEnemy* enemy=new GameObjEnemy();
enemy->setPosition(ccp(size.width/4*type,size.height+50));
enemy->setScale(0.5);
enemy->setType(type);
enemys->addObject(enemy);
addChild(enemy,1);
enemy->movestart();
}
enemys->retain();
//初始化敌人子弹
bullets->retain();
enemybullets=CCArray::create();
for (int i=0;i<10;i++)
{
GameEnemyBullet* mybullet=new GameEnemyBullet();
mybullet->setIsNotVisable();
mybullet->setScale(0.5);
enemybullets->addObject(mybullet);
this->addChild(mybullet,3);
}
enemybullets->retain();
//创建分数
gamemark=new GameMark();
addChild(gamemark,4);
//初始化游戏结束画面以及按钮
gameover=CCSprite::create("gameover.png");
gameover->setAnchorPoint(ccp(0.5,0.5));
gameover->setPosition(ccp(0,0));
gameover->setPosition(ccp(size.width/2,size.height/2+70));
gameover->setVisible(false);
gameover->setScale(0.5);
addChild(gameover,5);
CCMenuItemImage* pCloseItem=CCMenuItemImage::create("back.png","back.png",
this,menu_selector(GameScene::menuBackCallback));
pCloseItem->setPosition(ccp(size.width/2,size.height/2-50));
pCloseItem->setScale(0.5);
CCMenu* pMenu=CCMenu::create(pCloseItem,NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu,5,25);
pMenu->setVisible(false);
pMenu->setEnabled(false);
isreduce=false;
isover=false;
//创建血量ui
blood=3;
CCSpriteBatchNode* ui=CCSpriteBatchNode::create("cat.png");
blood1=CCSprite::createWithTexture(ui->getTexture());
blood1->setPosition(ccp(20,size.height-20));
blood1->setScale(0.2f);
ui->addChild(blood1);
blood2=CCSprite::createWithTexture(ui->getTexture());
blood2->setPosition(ccp(50,size.height-20));
blood2->setScale(0.2f);
ui->addChild(blood2);
blood3=CCSprite::createWithTexture(ui->getTexture());
blood3->setPosition(ccp(80,size.height-20));
blood3->setScale(0.2f);
ui->addChild(blood3);
addChild(ui,4);
scheduleUpdate();//调用系统的帧事件
return true;
}
void GameScene::menuBackCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->replaceScene(GameMenu::scence());
}
void GameScene::update(float delta)
{
bg1->setPosition(ccp(bg1->getPositionX(),bg1->getPositionY()-2));
bg2->setPosition(ccp(bg2->getPositionX(),bg2->getPositionY()-2));
if (bg2->getPosition().y<0)
{
float temp=bg2->getPosition().y+480;
bg1->setPosition(ccp(bg2->getPosition().x,temp));
}
if (bg1->getPosition().y<0)
{
float temp=bg1->getPosition().y+480;
bg2->setPosition(ccp(bg1->getPosition().x,temp));
}
//敌人和主角子弹的碰撞测试
CCPoint hpos=hero->getPosition();
for (int i=0;i<3;i++)
{
GameObjEnemy* enemy=((GameObjEnemy*)enemys->objectAtIndex(i));
CCPoint epos=enemy->getPosition();
if (enemy->islife)
{
for(int i=0;i<int(bullets->count());i++)
{
if (((GameHeroBullet*)bullets->objectAtIndex(i))->getIsvisble())
{
if (isCollion(((GameHeroBullet*)bullets->objectAtIndex(i))->
getPosition(),epos,5,13,21,28))
{
enemy->setdie();
gamemark->addnumber(200);
break;
}
}
}
}
//敌人和主角的碰撞检测
if (!isreduce&&enemy->islife&&isCollion(
hpos,epos,21,22.5,21,28))
{
enemy->setdie();
setherohurt();
}
}
//主角和敌人的子弹的碰撞检测
if(!isreduce)
{
for (int i=0;i<int(enemybullets->count());i++)
{
if (isCollion(hpos,((GameEnemyBullet*)enemybullets->objectAtIndex(i))->
getPosition(),21,22.5,5,13))
{
setherohurt();
}
}
}
gamemark->addnumber(10);
}
void GameScene::releaseheroBullet(int x,int y)
{
CCLog("count=%d, =%d",bullets->count(),bullets->retainCount());
for (int i=0;i<int(bullets->count());i++)
{
if (!((GameHeroBullet*)bullets->objectAtIndex(i))->getIsvisble())
{
CCLog("i=%d\n",i);
((GameHeroBullet*)bullets->objectAtIndex(i))->setPosition(ccp(x,y));
((GameHeroBullet*)bullets->objectAtIndex(i))->setIsVisable();
return;
}
}
}
void GameScene::releaseenemyBullet(int x,int y)
{
for (int i=0;i<int(enemybullets->count());i++)
{
if (!((GameEnemyBullet*)enemybullets->objectAtIndex(i))->getIsvisble())
{
((GameEnemyBullet*)enemybullets->objectAtIndex(i))->setPosition(ccp(x,y));
((GameEnemyBullet*)enemybullets->objectAtIndex(i))->setIsVisable();
break;
}
}
}
void GameScene::setover()
{
unscheduleUpdate();
for (int i=0;i<int(enemys->count());i++)
{
CCLog("enemys over");
((GameObjEnemy*)enemys->objectAtIndex(i))->setdie();
((GameObjEnemy*)enemys->objectAtIndex(i))->unschedule(schedule_selector(GameObjEnemy::releasebullet));
((GameObjEnemy*)enemys->objectAtIndex(i))->stopAllActions();
}
hero->unschedule(schedule_selector(GamerObjHero::releasebullet));
CCMenu* pMenu=(CCMenu*)this->getChildByTag(25);
pMenu->setVisible(true);
pMenu->setEnabled(true);
gameover->setVisible(true);
gameover->setScale(0);
pMenu->setScale(0);
pMenu->runAction(CCScaleTo::create(0.5,1));
gameover->runAction(CCScaleTo::create(0.5,0.5));
}
void GameScene::setherohurt()
{
//主角受伤,减血
hero->stopAllActions();
switch(blood)
{
case 3:
blood1->setVisible(false);
blood--;
break;
case 2:
blood2->setVisible(false);
blood--;
break;
case 1:
blood3->setVisible(false);
blood--;
break;
case 0:
if (!isover)
{
isover=true;
setover();
}
break;
}
CCActionInterval* action=CCBlink::create(5,10);
hero->runAction(action);
schedule(schedule_selector(GameScene::resetreduce),5.0f);
isreduce=true;
}
bool GameScene::isCollion(CCPoint p1,CCPoint p2,int w1,int h1,int w2,int h2)
{
if (abs(p1.x-p2.x)<w1+w2&&abs(p1.y-p2.y)<h1+h2)
{
return true;
}
return false;
}
void GameScene::resetreduce(float dt)
{
isreduce=false;
}