//
// GameLayer.cpp
// AngryBirds
//
// Created by MAC on 16/7/17.
//
//
#include "GameLayer.hpp"
int GameLayer::num = 0;
Scene* GameLayer::createScene(int _Charpter ,int _Case)
{
auto scene = Scene::create();
GameLayer* layer = new GameLayer();
if (layer && layer->init(_Charpter, _Case))
{
layer->autorelease();
scene->addChild(layer);
return scene;
}
CC_SAFE_DELETE(layer);
return nullptr;
};
bool GameLayer::init(int _Charpter ,int _Case)
{
if (!Layer::init())
{
return false;
}
star = 0;
Charpter = _Charpter;
Case = _Case;
visabliSize = Director::getInstance()->getVisibleSize();
pigArray = __Array::create();
pigArray->retain();
birdArray = __Array::create();
birdArray->retain();
iceArray = __Array::create();
iceArray->retain();
//分数
auto label = Text::create("CurrentScore:","fonts/Marker Felt.ttf",30);
label->setPosition(Vec2(visabliSize.width/2-120, visabliSize.height-25));
label->setColor(Color3B::RED);
this->addChild(label,5);
score = Text::create("","fonts/Marker Felt.ttf",25);
score->setColor(Color3B::RED);
score->setPosition(Vec2(visabliSize.width/2-10,visabliSize.height-25));
this->addChild(score,5);
//弹弓中点
midPos = Vec2(90, 80);
//添加背景
auto gameBg = Sprite::create("bg.png");
gameBg->setPosition(Vec2(visabliSize.width/2, visabliSize.height/2));
this->addChild(gameBg);
//加弹弓
auto leftSlingShot = Sprite::create("slingshot_front.png");
leftSlingShot->setScale(0.5);
leftSlingShot->setPosition(Vec2(86, 73));
this->addChild(leftSlingShot,20);
auto rightSlingShot = Sprite::create("platform.png");
rightSlingShot->setScale(0.5);
rightSlingShot->setPosition(Vec2(95, 60));
this->addChild(rightSlingShot);
//划线(正常状态下水平的线)
node1 = DrawNode::create();
this->addChild(node1);
node2 = DrawNode::create();
this->addChild(node2);
node1->drawLine(Vec2(97, 80), Vec2(83, 80), Color4F::MAGENTA);
node2->drawLine(Vec2(97, 80), Vec2(83, 80), Color4F::MAGENTA);
this->readJson(_Charpter , _Case);
this->addHighScore();
this->adTouch();
this->creatBoxWorld();
return true;
}
void GameLayer::adTouch()
{
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [=](Touch* t,Event * e){
if (birdArray->count() > 0) {
auto rect = currectBird->getBoundingBox();
if (rect.containsPoint(t->getLocation()) && rect.containsPoint(midPos)) {
return true;
}
}
return false;
};
listener->onTouchMoved = [=](Touch* t,Event * e){
Vec2 point = t->getLocation();
if ((point.distance(midPos)<=80 )&&(point.y > 30 ))
{
node1->clear();
node2->clear();
node1->drawLine(point,Vec2(97, 80), Color4F::MAGENTA);
node2->drawLine(point,Vec2(83, 80), Color4F::MAGENTA);
currectBird->setPosition(point);
}
};
listener->onTouchEnded = [=](Touch* t,Event * e){
node1->clear();
node2->clear();
node1->drawLine(Vec2(97, 80), Vec2(83, 80), Color4F::MAGENTA);
node2->drawLine(Vec2(97, 80), Vec2(83, 80), Color4F::MAGENTA);
Vec2 endpos = t->getLocation();
float x = midPos.x - endpos.x;
float y = midPos.y - endpos.y;
currectBird->setBirdSpeed(x,y, world);
};
listener->onTouchCancelled = [=](Touch* t,Event * e){
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
}
void GameLayer::creatBoxWorld()
{
b2Vec2 gravity; //竖直
gravity.Set(0.f, -20.f);//竖直向下
world = new b2World(gravity);//创建一个物理世界
world->SetAllowSleeping(true);//允许睡眠
world->SetContinuousPhysics(true);//允许碰撞
//创建刚体定义
b2BodyDef groundBodyDef;//创建地面
groundBodyDef.position.Set(0.0f, 0.0f);//原点 刚体定义中位置设置
//创建刚体 world创建刚体
b2Body* groundBody = world->CreateBody(&groundBodyDef);
b2EdgeShape groundBox;
//依次定义盒子的边界
//下
groundBox.Set(b2Vec2(0 / PTM_RATIO, 0.7), b2Vec2(visabliSize.width / PTM_RATIO, 0.7));
groundBody->CreateFixture(&groundBox, 0);
//上
groundBox.Set(b2Vec2(0 / PTM_RATIO, visabliSize.height / PTM_RATIO), b2Vec2(visabliSize.width / PTM_RATIO, visabliSize.height / PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);
//左
groundBox.Set(b2Vec2(0 / PTM_RATIO, 0.7), b2Vec2(0 / PTM_RATIO, visabliSize.height / PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);
//右
groundBox.Set(b2Vec2(visabliSize.width / PTM_RATIO, 0), b2Vec2(visabliSize.width / PTM_RATIO, visabliSize.height / PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);
MyContactListener *listener = new MyContactListener(world, this);
world->SetContactListener(listener);
this->addBirdsAndPig(); //添加猪、小鸟、冰块
}
void GameLayer::update(float t)
{
int timeStep = 8;//助间步
int iterationCount = 2;//迭代次数,控制了约束求解器会遍历多少次世界中的接触
world->Step(1.0/60.0, timeStep, iterationCount);
for (b2Body *b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData()!=nullptr)
{
BaseSprite * sp = (BaseSprite*)b->GetUserData();
sp->setPosition( Vec2( b->GetPosition().x *PTM_RATIO, b->GetPosition().y * PTM_RATIO) );
if (sp->getTYPEID() == BirdID)
{
Bird * bird = (Bird*)sp;
if (!b->IsAwake())
{
bird->destroyBody(world);
world->DestroyBody(b);
birdArray->removeObject(currectBird);
if (birdArray->count() == 0) //鸟没得情况
{
if (pigArray->count() == 0) //鸟没🐷也没
{
cout<<"You Win!"<<endl;
youWin();
}
else
{
cout<<"You Filed!!"<<endl;//鸟没了,🐷还有
youFiled();
}
}
if ((birdArray->count()>0)&&(pigArray->count()>0))
{
this->birdJumpToSlingShot();
}
continue;
}
//
}
if (sp->getTYPEID() == PigId)
{
PIG * pig = (PIG *)sp;
if (pig->getHP() <= 0)
{
num = num + 100;
char str[50];
sprintf(str, "%d",num);
score->setString(str);
pig->destroyBody(world);
world->DestroyBody(b);
pigArray->removeObject(pig);
cout<<"Pig:"<<pigArray->count()<<endl;
if (pigArray->count() == 0)
{
if (birdArray->count()>=0)
{
cout<<"You Win!"<<endl;
youWin();
}
}
else
{
if (birdArray->count() == 0)
{
cout<<"You Filed!!"<<endl;
youFiled();
}
}
continue;
}
}
if (sp->
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
愤怒的小鸟.zip (177个子文件)
GameLayer.cpp 18KB
MenuLayer.cpp 18KB
AppDelegate.cpp 4KB
HelloWorldScene.cpp 4KB
welcomeScene.cpp 3KB
Bird.cpp 2KB
Ice.cpp 1KB
MyContactListener.cpp 1KB
Pig.cpp 1KB
BaseSprite.cpp 473B
BaseUtils.cpp 102B
SelectLevel.csb 5KB
Node.csb 3KB
.DS_Store 10KB
.DS_Store 8KB
.DS_Store 6KB
._.DS_Store 70B
._.DS_Store 70B
._.DS_Store 70B
AppDelegate.h 987B
HelloWorldScene.h 550B
GameLayer.hpp 2KB
MenuLayer.hpp 928B
MyContactListener.hpp 790B
BaseSprite.hpp 648B
welcomeScene.hpp 593B
Bird.hpp 465B
Ice.hpp 443B
Pig.hpp 412B
BaseUtils.hpp 278B
background.jpeg 29KB
._background.jpeg 70B
tishi.jpg 20KB
textfield1.jpg 2KB
charpter2.json 5KB
bg_startgamescene.mp3 284KB
about.mp3 151KB
preloadAssetManifest.plist 10KB
bg0.png 284KB
bg1.png 277KB
loding_bg.png 206KB
gamebg.png 153KB
HelloWorld.png 136KB
mianban.png 125KB
bg.png 83KB
start_bg.png 63KB
playbutton-hd.png 48KB
LEVELSELECTION_1.png 31KB
LEVELSELECTION_3.png 30KB
LEVELSELECTION_2.png 26KB
LEVELSELECTION_9.png 25KB
LEVELSELECTION_6.png 25KB
LEVELSELECTION_7.png 23KB
set.png 22KB
LEVELSELECTION_4.png 22KB
LEVELSELECTION_8.png 22KB
musicoff-hd.png 21KB
musicon-hd.png 21KB
ok.png 20KB
LEVELSELECTION_5.png 19KB
playbutton.png 18KB
duihao1.png 17KB
shelf4.png 15KB
starlabel.png 14KB
pig_4.png 13KB
pig_3.png 13KB
restart.png 13KB
circlewood4.png 13KB
circlewood3.png 13KB
pig1.png 12KB
pig_2.png 12KB
circlewood2.png 12KB
next_right.png 12KB
next_left.png 12KB
platform-hd.png 12KB
pig_44.png 11KB
ice4.png 11KB
menu.png 11KB
circlewood1.png 11KB
pig_55.png 11KB
pig_22.png 10KB
pig_11.png 10KB
shelf2.png 10KB
pig-33.png 10KB
ice3.png 9KB
sanjiao4.png 9KB
musicoff.png 9KB
stone1.png 9KB
8.png 9KB
musicon.png 9KB
0.png 9KB
6.png 9KB
2.png 9KB
stone4.png 8KB
sanjiao3.png 8KB
3.png 8KB
icon.png 8KB
9.png 8KB
100001.png 8KB
5.png 8KB
共 177 条
- 1
- 2
资源评论
qq_28995157
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功