//
// GameScene.cpp
// example11-1
//
// Created by shuoquan man on 12-10-14.
//
#include "GameAboutScene.h"
#include "GameMenuScene.h"
#include "GameScene.h"
#include "GameObjHero.h"
#include "GameHeroBullet.h"
#include "GameEnemyBullet.h"
using namespace cocos2d;
CCScene* GameMain::scene()
{
CCScene *scene = CCScene::create();
GameMain *layer = GameMain::create();
scene->addChild(layer);
return scene;
}
void GameMain::releaseenemyBullet(int x,int y)
{
for(unsigned int i = 0; i<enemybullets->capacity(); i++){
if(!((GameEnemyBullet *)enemybullets->objectAtIndex(i))->getIsvisble()){
((GameEnemyBullet *)enemybullets->objectAtIndex(i))->setPosition(ccp(x,y));
((GameEnemyBullet *)enemybullets->objectAtIndex(i))->setIsVisable();
break;
}
}
}
void GameMain::releaseheroBullet(int x,int y)
{
for(unsigned int i=0; i<bullets->capacity(); i++){
if(!((GameHeroBullet *)bullets->objectAtIndex(i))->getIsvisble()){
((GameHeroBullet *)bullets->objectAtIndex(i))->setPosition(ccp(x,y));
((GameHeroBullet *)bullets->objectAtIndex(i))->setIsVisable();
break;
}
}
}
bool GameMain::init()
{
if(!CCLayer::init()){
return false;
}
CCSize size=CCDirector::sharedDirector()->getWinSize();
bg1=CCSprite::create("bg.png");
bg1->setScale(0.5);
bg2=CCSprite::create("bg.png");
bg2->setScale(0.5);
bg1->setAnchorPoint(ccp(0,0));
bg2->setAnchorPoint(ccp(0,0));
bg1->setPosition( ccp(0,0) );
bg2->setPosition( ccp(0,size.height) );
this->addChild(bg1, 0);
this->addChild(bg2, 0);
hero = GameObjHero::create();
hero->setPosition(ccp(size.width/2,-50));
hero->setScale(0.5);
addChild(hero,2,1);
hero->runAction(CCMoveBy::create(0.5,ccp(0,150)));
enemys = CCArray::create();
for(int i=0; i<3; i++){
int type = i + 1;
GameObjEnemy* enemy = GameObjEnemy::create();
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();
blood = 3;
CCSpriteBatchNode* ui = CCSpriteBatchNode::create("cat.png");
//CCNode *ui = CCNode::create();
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);
bullets = CCArray::create(/*5*/);
for(unsigned int i=0; i<bullets->capacity(); i++){
GameHeroBullet * mybullet = GameHeroBullet::create();
mybullet->setIsNotVisable(this);
mybullet->setScale(0.5);
bullets->addObject(mybullet);
this->addChild(mybullet,3);
}
bullets->retain();
enemybullets = CCArray::create();//10
for(unsigned int i=0; i<enemybullets->capacity(); i++){
GameEnemyBullet * mybullet = GameEnemyBullet::create();
mybullet->setIsNotVisable(this);
mybullet->setScale(0.5);
enemybullets->addObject(mybullet);
this->addChild(mybullet,3);
}
gamemark = GameMark::create();
addChild(gamemark,4);
enemybullets->retain();
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(GameMain::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;
scheduleUpdate();
return true;
}
void GameMain::menuBackCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->replaceScene(GameMenu::scene());
}
bool GameMain::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 GameMain::setover()
{
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 GameMain::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(GameMain::resetreduce), 5.0f);
isreduce = true;
}
void GameMain::resetreduce(float dt)
{
isreduce = false;
}
void GameMain::update(float time)
{
bg1->setPosition(ccp(bg1->getPosition().x,bg1->getPosition().y - 2));
bg2->setPosition(ccp(bg2->getPosition().x,bg2->getPosition().y - 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(unsigned int i=0; i<bullets->capacity(); 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(unsigned int i=0; i<enemybullets->capacity(); i++){
if(isCollion(hpos,((GameEnemyBullet *)enemybullets->objectAtIndex(i))->getPosition(),21,22.5,5,13)){
setherohurt();
}
}
}
}