//
// 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->