#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace cocostudio::timeline;
Scene* HelloWorld::createScene()
{
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);//开启线框调试模式
scene->getPhysicsWorld()->setAutoStep(false);//自动步进
auto layer = HelloWorld::create();
PhysicsBody* body = PhysicsBody::createEdgeBox(Director::getInstance()->getWinSizeInPixels()*0.9);
body->setCategoryBitmask(0x3);
body->setCollisionBitmask(0x3);
body->setContactTestBitmask(0x3);
layer->setPhysicsBody(body);
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
scheduleUpdate();
if ( !Layer::init() )
{
return false;
}
setTouchEnabled(true);
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
rootNode = CSLoader::createNode("MainScene.csb");
sprite = rootNode->getChildByName<Sprite*>("Sprite_1");
PhysicsBody* body = PhysicsBody::createBox(sprite->getContentSize());
PhysicsBody* bodyCircle = PhysicsBody::createCircle(sprite->getContentSize().width/2);
//掩码 100001000 A
// 000001000 B
//按位与 >0 A拥有B的特征
bodyCircle->setCategoryBitmask(0x2);//Category类别掩码
bodyCircle->setCollisionBitmask(0x2);//Collision碰撞掩码
bodyCircle->setContactTestBitmask(0x1);//ContactTest连接掩码,触碰触发事件需要设置
bodyCircle->setDynamic(true);//true使用物理模拟,false反物理模拟,反运动运动学
bodyCircle->setGravityEnable(true);//重力
bodyCircle->setName("bird");
sprite->setPhysicsBody(bodyCircle);//向精灵添加刚体
sprite->getPhysicsBody()->setMass(0.5f);
//碰撞检测事件 全局检测
physicsContact = EventListenerPhysicsContact::create();//物理连接事件
physicsContact->onContactBegin = CC_CALLBACK_1(HelloWorld::onCollsionEnter,this);//回调函数事件
getEventDispatcher()->addEventListenerWithSceneGraphPriority(physicsContact, rootNode);//分发事件
addChild(rootNode);
return true;
}
void HelloWorld::update(float dt)
{
/**********优化物体穿模***********/
for (int i = 0; i < 3; i++)
{
this->getScene()->getPhysicsWorld()->step(1 / 180.0f);
}
/********************************/
}
bool HelloWorld::onTouchBegan(Touch * touch, Event * unused_event)
{
bool isSelect = false;
getScene()->getPhysicsWorld()->queryPoint(CC_CALLBACK_3(HelloWorld::onPickUpScreen,this),touch->getLocation(),&isSelect);//获取包含点的物理形状
if (!isSelect)
{
cloneSprite(touch->getLocation());
}
//Todo 1 、onTouchBegan
return true;
}
void HelloWorld::onTouchMoved(Touch * touch, Event * unused_event)
{
//Todo onTouchMoved
log("onTouchMoved");
if (selectBird)
{
Node* pin = getChildByName("pin");
pin->setPosition(touch->getLocation());
}
}
void HelloWorld::onTouchEnded(Touch * touch, Event * unused_event)
{
//Todo onTouchEnded 移除弹簧
selectBird = NULL;
Node* pin = getChildByName("pin");
if (pin!= NULL)
{
pin->removeFromParent();
this->getScene()->getPhysicsWorld()->removeAllJoints();
}
}
//bool HelloWorld::onContactBegin(PhysicsContact & contact)
//{
// return true;
//}
bool HelloWorld::onCollsionEnter(cocos2d::PhysicsContact &contact)
{
//Todo onCollsionEnter碰撞物体类型
std :: string bodyA = contact.getShapeA()->getBody()->getName();
std :: string bodyB = contact.getShapeB()->getBody()->getName();
if (bodyA == "bird")
{
//contact.getShapeA()->getBody()->getOwner()->setVisible(false);
//contact.getShapeA()->getBody()->setEnabled(false);//关闭刚体
}
else if (bodyB == "clone")
{
FIXME an
//contact.getShapeB()->getBody()->getOwner()->setVisible(false);
//contact.getShapeB()->getBody()->setEnabled(false);//关闭刚体
}
return true;
}
void HelloWorld::onCollsionStay(PhysicsContact & contact, const cocos2d::PhysicsContactPostSolve & solve)
{
}
bool HelloWorld::onPickUpScreen(PhysicsWorld & world, PhysicsShape & shape, void * data)
{
log ("onPickUpScreen");
if (shape.getBody()->getName() == "clone")
{
bool* isSelect = (bool*)data;
*isSelect = true;
selectBird = shape.getBody()->getOwner();
Vec2 pos = selectBird->getPosition();
Node* pin = Node::create();
pin->setName("pin");
pin->setPosition(pos);
PhysicsBody* body = PhysicsBody::create();
pin->setPhysicsBody(body);
this->addChild(pin);
// Todo 弹簧的创建
PhysicsJointPin* jointPin = PhysicsJointPin::construct(body, shape.getBody(), pos);
Director::getInstance()->getRunningScene()->getPhysicsWorld()->addJoint(jointPin);
}
//Todo 2 、onPickUpScreen
return false;
}
void HelloWorld::cloneSprite(Vec2 pos)
{
//Todo 3、cloneSprite
Sprite* clone = Sprite::createWithTexture(sprite->getTexture());
PhysicsBody* bodyClone = PhysicsBody::createBox(clone->getContentSize());
clone->setPosition(pos);
bodyClone->setCategoryBitmask(0x1);
bodyClone->setCollisionBitmask(0x1);
bodyClone->setContactTestBitmask(0x2);
bodyClone->setMass(sprite->getPhysicsBody()->getMass());
bodyClone->setDynamic(true);
bodyClone->setGravityEnable(true);
bodyClone->setName("clone");
clone->setPhysicsBody(bodyClone);
this->addChild(clone);
}