#include "GameScene.h"
#include "SimpleAudioEngine.h"
#include "Welcome.h"
using namespace CocosDenshion;
USING_NS_CC;
// android effect only support ogg
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#define EFFECT_FILE "pew-pew-lei.wav"
#elif( CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
#define EFFECT_FILE "pew-pew-lei.wav"
#else
#define EFFECT_FILE "pew-pew-lei.wav"
#endif // CC_PLATFORM_ANDROID
CCArray *bulletArray;
CCArray *enemyArray;
CCLabelTTF* pLabel;
int dieEnemy=1;
CCScene* GameScene::scene()
{
CCScene *scene = CCScene::create();
GameScene *layer = GameScene::create();
scene->addChild(layer);
return scene;
}
bool GameScene::init()
{
if ( !CCLayerColor::initWithColor(ccc4( 0,0,0,255)) )
{
return false;
}
bulletArray=new CCArray;
enemyArray=new CCArray;
SimpleAudioEngine::sharedEngine()->preloadEffect(EFFECT_FILE );
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(GameScene::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu);
pLabel = CCLabelTTF::create("LazyGame", "Arial", 24+20);
// position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
// add the label as a child to this layer
this->addChild(pLabel);
// hero
CCSize size=CCDirector::sharedDirector()->getWinSize();
//player =CCSprite::create("Player.png",CCRectMake(0,0,27,40));
player =CCSprite::create("hero/20005_stand_1.png");
player->setPosition(ccp(origin.x+ player->getContentSize().width/2,origin.y+visibleSize.height/2));
this->addChild(player);
this->schedule( schedule_selector(GameScene::gameLogic), 1.0 );
this->setTouchEnabled(true);
this->scheduleUpdate();
return true;
}
void GameScene::gameLogic(float dt)
{
this->addTarget();
}
void GameScene::addTarget()
{
int rIndex=(rand()%3+1);
CCSprite *target;
if(rIndex==1)
{target=CCSprite::create("guai/guai1.png",CCRectMake(0,0,80,72));
}else if(rIndex==2)
{target=CCSprite::create("guai/guai2.png",CCRectMake(0,0,80,72));
}else if(rIndex==3)
{target=CCSprite::create("guai/guai3.png",CCRectMake(0,0,80,72));
}else
{target=CCSprite::create("guai/guai4.png",CCRectMake(0,0,80,72));
}
//target=CCSprite::create("Target.png",CCRectMake(0,0,27,40));
CCSize winSize=CCDirector::sharedDirector()->getWinSize();
int minY=target->getContentSize().height/2;
int maxY=winSize.height;
int rangeY=maxY-minY;
int actualY=(rand()%rangeY)+minY;
target->setPosition(ccp(winSize.width-target->getContentSize().width/2,actualY));
this->addChild(target,0,1);
enemyArray->addObject(target);
int minD=1;
int maxD=20;
int rangeD=maxD-minD;
int actualD=(rand()%rangeD+minD);
CCFiniteTimeAction *actionMove=CCMoveTo::create((float)actualD,ccp(100-target->getContentSize().width,winSize.height/2));
CCFiniteTimeAction* actionMoveDone = CCCallFuncN::create(this,callfuncN_selector(GameScene::spriteMoveFinished));
target->runAction(CCSequence::create(actionMove,actionMoveDone,NULL));
}
void GameScene::ccTouchesEnded(CCSet *touches,CCEvent *event)
{
CCTouch *touch=(CCTouch*)(touches->anyObject());
CCPoint location=touch->getLocation();
CCSize winSize=CCDirector::sharedDirector()->getWinSize();
//add bullet
addBullet(location.x,location.y,winSize.height/2);
int i=35;
addBullet(location.x-i,location.y+i,winSize.height/2);
addBullet(location.x-i,location.y-i,winSize.height/2);
addBullet(location.x-i*2,location.y+i*2,winSize.height/2);
addBullet(location.x-i*2,location.y-i*2,winSize.height/2);
addBullet(location.x-i*2,location.y+i*3,winSize.height/2);
addBullet(location.x-i*2,location.y-i*3,winSize.height/2);
//play anim
player->stopAllActions();
CCAnimation *animationShoot = CCAnimation::create();
for (int i = 1; i <= 23; i+=2)
{
char szImageFileName[128] = {0};
sprintf(szImageFileName, "hero/20005_%d.png", i);
animationShoot->addSpriteFrameWithFileName(szImageFileName);
}
animationShoot->setDelayPerUnit(0.6f / 10.0f);
//animationShoot->setLoops(-1);
CCAnimate *actShoot = CCAnimate::create(animationShoot);
player->runAction(actShoot);
}
void GameScene::addBullet(float x,float y,float startY)
{
CCSprite *bullet=CCSprite::create("Projectile.png",CCRectMake(0,0,20,20));
bullet->setPosition(ccp(60,startY));
this->addChild(bullet,0,2);
this->move(bullet,x,y);
bulletArray->addObject(bullet);
}
void GameScene::move(CCSprite *bullet,float flocationX, float flocationY)
{
CCLOG("flocationX=%f flocationY=%f",flocationX,flocationY);
CCSize winSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
// Determinie offset of location to projectile
float offX = flocationX - bullet->getPosition().x;
float offY = flocationY - bullet->getPosition().y;
// Bail out if we are shooting down or backwards
if (offX <= 0) return;
// Ok to add now - we've double checked position
// Determine where we wish to shoot the projectile to
float realX = origin.x + winSize.width + (bullet->getContentSize().width/2);
float ratio = offY / offX;
float realY = (realX * ratio) + bullet->getPosition().y;
CCPoint realDest = ccp(realX, realY);
// Determine the length of how far we're shooting
float offRealX = realX - bullet->getPosition().x;
float offRealY = realY - bullet->getPosition().y;
float length = sqrtf((offRealX * offRealX) + (offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
// Move projectile to actual endpoint
bullet->runAction( CCSequence::create(
CCMoveTo::create(realMoveDuration, realDest),
CCCallFuncN::create(this,callfuncN_selector(GameScene::spriteMoveFinished)),
NULL) );
}
void GameScene::spriteMoveFinished(CCNode* sender)
{
CCLOG("Sprite tag=%d",sender->getTag());
CCSprite *sprite = (CCSprite *)sender;
this->removeChild(sprite);
//del
if(sender->getTag()==1)
{
enemyArray->removeObject(sprite);
}else if(sender->getTag()==2)
{
bulletArray->removeObject(sprite);
}
}
void GameScene::update(float delta)
{
CCArray *toDel=new CCArray;
CCObject *bulletO=NULL;
CCSprite *bulletS=NULL;
CCRect bulletR;
CCObject *enemyO=NULL;
CCSprite *enemyS=NULL;
CCRect enemyR;
CCLOG("Bullet count = %d Enemy count=%d",bulletArray->count(), enemyArray->count());
CCARRAY_FOREACH(bulletArray,bulletO)
{
bulletS=dynamic_cast<CCSprite*>(bulletO);
bulletR=CCRectMake(bulletS->getPosition().x-bulletS->getContentSize().width/2,
bulletS->getPosition().y-bulletS->getContentSize().height/2,
bulletS->getContentSize().width,
bulletS->getContentSize().height);
CCARRAY_FOREACH(enemyArray,enemyO)
{
enemyS=dynamic_cast<CCSprite*>(enemyO);
enemyR=CCRectMake(
enemyS->getPosition().x-enemyS->getContentSize().width/2,
enemyS->getPosition().y-enemyS->getContentSize().height/2,
enemyS->getContentSize().width,
enemyS->getContentSize().height
);
if(bulletR.intersectsRect(enemyR))
{
CCLOG("BOOM!");
dieEnemy++;
char buffer[20];
sprintf(buffer,"You killed : %d",dieEnemy);
pLabel->setString(buffer);
SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);
toDel->addObject(enemyS);
toDel->addObject(bulletS);
cocos2dx游戏源码《忍者飞镖打幽灵》
5星 · 超过95%的资源 需积分: 33 159 浏览量
2014-03-18
10:45:39
上传
评论 36
收藏 1.08MB ZIP 举报
评论30
最新资源