axmol/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp

966 lines
26 KiB
C++
Raw Normal View History

2013-06-06 12:02:54 +08:00
#include "ArmatureScene.h"
#include "../../testResource.h"
2013-09-13 18:07:37 +08:00
2013-06-06 12:02:54 +08:00
using namespace cocos2d;
using namespace cocos2d::extension::armature;
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
Layer *NextTest();
Layer *BackTest();
Layer *RestartTest();
2013-06-06 12:02:54 +08:00
static int s_nActionIdx = -1;
Layer *CreateLayer(int index)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
Layer *pLayer = NULL;
switch(index)
{
case TEST_ASYNCHRONOUS_LOADING:
pLayer = new TestAsynchronousLoading();
break;
case TEST_DRAGON_BONES_2_0:
pLayer = new TestDragonBones20();
break;
case TEST_COCOSTUDIO_WITH_SKELETON:
pLayer = new TestCSWithSkeleton();
break;
case TEST_PERFORMANCE:
pLayer = new TestPerformance();
break;
case TEST_CHANGE_ZORDER:
pLayer = new TestChangeZorder();
break;
case TEST_ANIMATION_EVENT:
pLayer = new TestAnimationEvent();
break;
case TEST_PARTICLE_DISPLAY:
pLayer = new TestParticleDisplay();
break;
case TEST_USE_DIFFERENT_PICTURE:
pLayer = new TestUseMutiplePicture();
break;
case TEST_BCOLLIDER_DETECTOR:
pLayer = new TestColliderDetector();
break;
case TEST_BOUDINGBOX:
pLayer = new TestBoundingBox();
break;
case TEST_ANCHORPOINT:
pLayer = new TestAnchorPoint();
break;
case TEST_ARMATURE_NESTING:
pLayer = new TestArmatureNesting();
break;
default:
break;
}
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
return pLayer;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
Layer *NextTest()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
++s_nActionIdx;
s_nActionIdx = s_nActionIdx % TEST_LAYER_COUNT;
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
Layer *pLayer = CreateLayer(s_nActionIdx);
pLayer->autorelease();
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
return pLayer;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
Layer *BackTest()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
--s_nActionIdx;
if( s_nActionIdx < 0 )
s_nActionIdx += TEST_LAYER_COUNT;
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
Layer *pLayer = CreateLayer(s_nActionIdx);
pLayer->autorelease();
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
return pLayer;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
Layer *RestartTest()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
Layer *pLayer = CreateLayer(s_nActionIdx);
pLayer->autorelease();
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
return pLayer;
2013-06-06 12:02:54 +08:00
}
ArmatureTestScene::ArmatureTestScene(bool bPortrait)
{
2013-09-13 18:07:37 +08:00
TestScene::init();
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
Sprite *bg = Sprite::create("armature/bg.jpg");
bg->setPosition(VisibleRect::center());
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
float scaleX = VisibleRect::getVisibleRect().size.width / bg->getContentSize().width;
float scaleY = VisibleRect::getVisibleRect().size.height / bg->getContentSize().height;
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
bg->setScaleX(scaleX);
bg->setScaleY(scaleY);
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
addChild(bg);
2013-06-06 12:02:54 +08:00
}
void ArmatureTestScene::runThisTest()
{
2013-09-13 18:07:37 +08:00
s_nActionIdx = -1;
addChild(NextTest());
Director::getInstance()->replaceScene(this);
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void ArmatureTestScene::MainMenuCallback(Object *pSender)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
//TestScene::MainMenuCallback(pSender);
removeAllChildren();
ArmatureDataManager::purge();
2013-06-06 12:02:54 +08:00
}
void ArmatureTestLayer::onEnter()
{
2013-09-13 18:07:37 +08:00
Layer::onEnter();
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
// add title and subtitle
std::string str = title();
const char *pTitle = str.c_str();
LabelTTF *label = LabelTTF::create(pTitle, "Arial", 18);
label->setColor(Color3B(0, 0, 0));
addChild(label, 1, 10000);
label->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y - 30) );
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
std::string strSubtitle = subtitle();
if( ! strSubtitle.empty() )
{
LabelTTF *l = LabelTTF::create(strSubtitle.c_str(), "Arial", 18);
l->setColor(Color3B(0, 0, 0));
addChild(l, 1, 10001);
l->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y - 60) );
}
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
// add menu
backItem = MenuItemImage::create(s_pathB1, s_pathB2, CC_CALLBACK_1(ArmatureTestLayer::backCallback, this) );
restartItem = MenuItemImage::create(s_pathR1, s_pathR2, CC_CALLBACK_1(ArmatureTestLayer::restartCallback, this) );
nextItem = MenuItemImage::create(s_pathF1, s_pathF2, CC_CALLBACK_1(ArmatureTestLayer::nextCallback, this) );
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
Menu *menu = Menu::create(backItem, restartItem, nextItem, NULL);
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
menu->setPosition(Point::ZERO);
backItem->setPosition(Point(VisibleRect::center().x - restartItem->getContentSize().width * 2, VisibleRect::bottom().y + restartItem->getContentSize().height / 2));
restartItem->setPosition(Point(VisibleRect::center().x, VisibleRect::bottom().y + restartItem->getContentSize().height / 2));
nextItem->setPosition(Point(VisibleRect::center().x + restartItem->getContentSize().width * 2, VisibleRect::bottom().y + restartItem->getContentSize().height / 2));
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
addChild(menu, 100);
2013-06-06 12:02:54 +08:00
setShaderProgram(ShaderCache::getInstance()->programForKey(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
2013-06-06 12:02:54 +08:00
}
void ArmatureTestLayer::onExit()
{
2013-09-13 18:07:37 +08:00
removeAllChildren();
backItem = restartItem = nextItem = NULL;
2013-06-06 12:02:54 +08:00
}
std::string ArmatureTestLayer::title()
{
return "Armature Test Bed";
2013-06-06 12:02:54 +08:00
}
std::string ArmatureTestLayer::subtitle()
{
2013-09-13 18:07:37 +08:00
return "";
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void ArmatureTestLayer::restartCallback(Object *pSender)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
Scene *s = new ArmatureTestScene();
s->addChild( RestartTest() );
Director::getInstance()->replaceScene(s);
s->release();
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void ArmatureTestLayer::nextCallback(Object *pSender)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
Scene *s = new ArmatureTestScene();
s->addChild( NextTest() );
Director::getInstance()->replaceScene(s);
s->release();
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void ArmatureTestLayer::backCallback(Object *pSender)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
Scene *s = new ArmatureTestScene();
s->addChild( BackTest() );
Director::getInstance()->replaceScene(s);
s->release();
2013-06-06 12:02:54 +08:00
}
void ArmatureTestLayer::draw()
{
2013-09-13 18:07:37 +08:00
Layer::draw();
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void TestAsynchronousLoading::onEnter()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
ArmatureTestLayer::onEnter();
//CCLOG("armature version : %s", armatureVersion());
2013-09-13 18:07:37 +08:00
backItem->setEnabled(false);
restartItem->setEnabled(false);
nextItem->setEnabled(false);
char pszPercent[255];
sprintf(pszPercent, "%s %f", subtitle().c_str(), 0.0f);
LabelTTF *label = (LabelTTF *)getChildByTag(10001);
label->setString(pszPercent);
//! create a new thread to load data
ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/knight.png", "armature/knight.plist", "armature/knight.xml", this, schedule_selector(TestAsynchronousLoading::dataLoaded));
ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/weapon.png", "armature/weapon.plist", "armature/weapon.xml", this, schedule_selector(TestAsynchronousLoading::dataLoaded));
ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/robot.png", "armature/robot.plist", "armature/robot.xml", this, schedule_selector(TestAsynchronousLoading::dataLoaded));
ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/cyborg.png", "armature/cyborg.plist", "armature/cyborg.xml", this, schedule_selector(TestAsynchronousLoading::dataLoaded));
ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/Dragon.png", "armature/Dragon.plist", "armature/Dragon.xml", this, schedule_selector(TestAsynchronousLoading::dataLoaded));
ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/Cowboy.ExportJson", this, schedule_selector(TestAsynchronousLoading::dataLoaded));
2013-09-13 18:07:37 +08:00
//! load data directly
// ArmatureDataManager::getInstance()->addArmatureFileInfo("armature/knight.png", "armature/knight.plist", "armature/knight.xml");
// ArmatureDataManager::getInstance()->addArmatureFileInfo("armature/weapon.png", "armature/weapon.plist", "armature/weapon.xml");
// ArmatureDataManager::getInstance()->addArmatureFileInfo("armature/robot.png", "armature/robot.plist", "armature/robot.xml");
// ArmatureDataManager::getInstance()->addArmatureFileInfo("armature/cyborg.png", "armature/cyborg.plist", "armature/cyborg.xml");
// ArmatureDataManager::getInstance()->addArmatureFileInfo("armature/Dragon.png", "armature/Dragon.plist", "armature/Dragon.xml");
// ArmatureDataManager::getInstance()->addArmatureFileInfo("armature/Cowboy.ExportJson");
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
std::string TestAsynchronousLoading::title()
{
return "Test Asynchronous Loading";
}
std::string TestAsynchronousLoading::subtitle()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
return "current percent : ";
}
void TestAsynchronousLoading::dataLoaded(float percent)
{
LabelTTF *label = (LabelTTF *)getChildByTag(10001);
if (label)
{
char pszPercent[255];
sprintf(pszPercent, "%s %f", subtitle().c_str(), percent * 100);
label->setString(pszPercent);
}
if (percent >= 1 && backItem && restartItem && nextItem)
{
backItem->setEnabled(true);
restartItem->setEnabled(true);
nextItem->setEnabled(true);
}
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
2013-06-06 12:02:54 +08:00
void TestCSWithSkeleton::onEnter()
{
2013-09-13 18:07:37 +08:00
ArmatureTestLayer::onEnter();
Armature *armature = NULL;
armature = Armature::create("Cowboy");
2013-09-13 18:07:37 +08:00
armature->getAnimation()->playByIndex(0);
armature->setScale(0.2f);
armature->setPosition(Point(VisibleRect::center().x, VisibleRect::center().y/*-100*/));
addChild(armature);
2013-06-06 12:02:54 +08:00
}
std::string TestCSWithSkeleton::title()
{
2013-09-13 18:07:37 +08:00
return "Test Export From CocoStudio With Skeleton Effect";
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void TestDragonBones20::onEnter()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
ArmatureTestLayer::onEnter();
Armature *armature = NULL;
armature = Armature::create("Dragon");
2013-09-13 18:07:37 +08:00
armature->getAnimation()->playByIndex(1);
armature->getAnimation()->setSpeedScale(0.4f);
armature->setPosition(VisibleRect::center().x, VisibleRect::center().y * 0.3f);
armature->setScale(0.6f);
addChild(armature);
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
std::string TestDragonBones20::title()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
return "Test Export From DragonBones version 2.0";
2013-06-06 12:02:54 +08:00
}
TestPerformance::~TestPerformance()
{
}
void TestPerformance::onEnter()
{
2013-09-13 18:07:37 +08:00
ArmatureTestLayer::onEnter();
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
armatureCount = frames = times = lastTimes = 0;
generated = false;
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
scheduleUpdate();
2013-06-06 12:02:54 +08:00
}
2013-06-06 12:02:54 +08:00
std::string TestPerformance::title()
{
2013-09-13 18:07:37 +08:00
return "Test Performance";
2013-06-06 12:02:54 +08:00
}
std::string TestPerformance::subtitle()
{
return "Current Armature Count : ";
2013-06-06 12:02:54 +08:00
}
void TestPerformance::addArmature(Armature *armature)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
armatureCount++;
addChild(armature, armatureCount);
2013-06-06 12:02:54 +08:00
}
void TestPerformance::update(float delta)
{
2013-09-13 18:07:37 +08:00
frames ++;
times += delta;
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
if (frames / times > 58)
{
Armature *armature = NULL;
armature = new Armature();
2013-09-13 18:07:37 +08:00
armature->init("Knight_f/Knight");
armature->getAnimation()->playByIndex(0);
armature->setPosition(50 + armatureCount * 2, 150);
armature->setScale(0.6f);
addArmature(armature);
armature->release();
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
char pszCount[255];
sprintf(pszCount, "%s %i", subtitle().c_str(), armatureCount);
LabelTTF *label = (LabelTTF *)getChildByTag(10001);
label->setString(pszCount);
}
2013-06-06 12:02:54 +08:00
}
void TestChangeZorder::onEnter()
{
2013-09-13 18:07:37 +08:00
ArmatureTestLayer::onEnter();
2013-06-06 12:02:54 +08:00
Armature *armature = NULL;
2013-09-13 18:07:37 +08:00
currentTag = -1;
2013-06-06 12:02:54 +08:00
armature = Armature::create("Knight_f/Knight");
2013-09-13 18:07:37 +08:00
armature->getAnimation()->playByIndex(0);
armature->setPosition(Point(VisibleRect::center().x, VisibleRect::center().y - 100));
++currentTag;
armature->setScale(0.6f);
addChild(armature, currentTag, currentTag);
2013-06-06 12:02:54 +08:00
armature = Armature::create("Cowboy");
2013-09-13 18:07:37 +08:00
armature->getAnimation()->playByIndex(0);
armature->setScale(0.24f);
armature->setPosition(Point(VisibleRect::center().x, VisibleRect::center().y - 100));
++currentTag;
addChild(armature, currentTag, currentTag);
2013-06-06 12:02:54 +08:00
armature = Armature::create("Dragon");
2013-09-13 18:07:37 +08:00
armature->getAnimation()->playByIndex(0);
armature->setPosition(Point(VisibleRect::center().x , VisibleRect::center().y - 100));
++currentTag;
armature->setScale(0.6f);
addChild(armature, currentTag, currentTag);
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
schedule( schedule_selector(TestChangeZorder::changeZorder), 1);
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
currentTag = 0;
2013-06-06 12:02:54 +08:00
}
std::string TestChangeZorder::title()
{
return "Test Change ZOrder Of Different Armature";
2013-06-06 12:02:54 +08:00
}
void TestChangeZorder::changeZorder(float dt)
{
2013-09-13 18:07:37 +08:00
Node *node = getChildByTag(currentTag);
node->setZOrder(CCRANDOM_0_1() * 3);
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
currentTag ++;
currentTag = currentTag % 3;
2013-06-06 12:02:54 +08:00
}
void TestAnimationEvent::onEnter()
{
2013-09-13 18:07:37 +08:00
ArmatureTestLayer::onEnter();
armature = Armature::create("Cowboy");
2013-09-13 18:07:37 +08:00
armature->getAnimation()->play("Fire");
armature->setScaleX(-0.24f);
armature->setScaleY(0.24f);
armature->setPosition(Point(VisibleRect::left().x + 50, VisibleRect::left().y));
/*
* Set armature's movement event callback function
* To disconnect this event, just setMovementEventCallFunc(NULL, NULL);
*/
armature->getAnimation()->setMovementEventCallFunc(this, movementEvent_selector(TestAnimationEvent::animationEvent));
addChild(armature);
2013-06-06 12:02:54 +08:00
}
std::string TestAnimationEvent::title()
{
return "Test Armature Animation Event";
2013-09-13 18:07:37 +08:00
}
void TestAnimationEvent::animationEvent(Armature *armature, MovementEventType movementType, const char *movementID)
2013-09-13 18:07:37 +08:00
{
std::string id = movementID;
if (movementType == LOOP_COMPLETE)
{
if (id.compare("Fire") == 0)
{
ActionInterval *actionToRight = MoveTo::create(2, Point(VisibleRect::right().x - 50, VisibleRect::right().y));
armature->stopAllActions();
armature->runAction(Sequence::create(actionToRight, CallFunc::create( CC_CALLBACK_0(TestAnimationEvent::callback1, this)), NULL));
armature->getAnimation()->play("Walk");
}
else if (id.compare("FireMax") == 0)
{
ActionInterval *actionToLeft = MoveTo::create(2, Point(VisibleRect::left().x + 50, VisibleRect::left().y));
armature->stopAllActions();
armature->runAction(Sequence::create(actionToLeft, CallFunc::create( CC_CALLBACK_0(TestAnimationEvent::callback2, this)), NULL));
armature->getAnimation()->play("Walk");
}
}
2013-06-06 12:02:54 +08:00
}
void TestAnimationEvent::callback1()
{
2013-09-13 18:07:37 +08:00
armature->runAction(ScaleTo::create(0.3f, 0.24f, 0.24f));
armature->getAnimation()->play("FireMax", 10);
2013-06-06 12:02:54 +08:00
}
void TestAnimationEvent::callback2()
{
2013-09-13 18:07:37 +08:00
armature->runAction(ScaleTo::create(0.3f, -0.24f, 0.24f));
armature->getAnimation()->play("Fire", 10);
2013-06-06 12:02:54 +08:00
}
void TestParticleDisplay::onEnter()
{
2013-09-13 18:07:37 +08:00
ArmatureTestLayer::onEnter();
setTouchEnabled(true);
animationID = 0;
armature = Armature::create("robot");
2013-09-13 18:07:37 +08:00
armature->getAnimation()->playByIndex(0);
armature->setPosition(VisibleRect::center());
armature->setScale(0.48f);
armature->getAnimation()->setSpeedScale(0.5f);
addChild(armature);
ParticleSystem *p1 = CCParticleSystemQuad::create("Particles/SmallSun.plist");
ParticleSystem *p2 = CCParticleSystemQuad::create("Particles/SmallSun.plist");
Bone *bone = Bone::create("p1");
2013-09-13 18:07:37 +08:00
bone->addDisplay(p1, 0);
bone->changeDisplayByIndex(0, true);
bone->setIgnoreMovementBoneData(true);
bone->setZOrder(100);
bone->setScale(1.2f);
armature->addBone(bone, "bady-a3");
bone = Bone::create("p2");
2013-09-13 18:07:37 +08:00
bone->addDisplay(p2, 0);
bone->changeDisplayByIndex(0, true);
bone->setIgnoreMovementBoneData(true);
bone->setZOrder(100);
bone->setScale(1.2f);
armature->addBone(bone, "bady-a30");
}
2013-09-16 19:27:34 +08:00
2013-09-13 18:07:37 +08:00
void TestParticleDisplay::onExit()
{
ArmatureTestLayer::onExit();
2013-06-06 12:02:54 +08:00
}
2013-09-16 19:27:34 +08:00
2013-06-06 12:02:54 +08:00
std::string TestParticleDisplay::title()
{
2013-09-13 18:07:37 +08:00
return "Test Particle Display";
2013-06-06 12:02:54 +08:00
}
2013-09-16 19:27:34 +08:00
2013-06-06 12:02:54 +08:00
std::string TestParticleDisplay::subtitle()
{
2013-09-13 18:07:37 +08:00
return "Touch to change animation";
2013-06-06 12:02:54 +08:00
}
2013-09-16 19:27:34 +08:00
void TestParticleDisplay::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
++animationID;
animationID = animationID % armature->getAnimation()->getMovementCount();
armature->getAnimation()->playByIndex(animationID);
2013-06-06 12:02:54 +08:00
}
void TestUseMutiplePicture::onEnter()
{
2013-09-13 18:07:37 +08:00
ArmatureTestLayer::onEnter();
setTouchEnabled(true);
displayIndex = 0;
armature = Armature::create("Knight_f/Knight");
2013-09-13 18:07:37 +08:00
armature->getAnimation()->playByIndex(0);
armature->setPosition(Point(VisibleRect::center().x, VisibleRect::left().y));
armature->setScale(1.2f);
addChild(armature);
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
std::string weapon[] = {"weapon_f-sword.png", "weapon_f-sword2.png", "weapon_f-sword3.png", "weapon_f-sword4.png", "weapon_f-sword5.png", "weapon_f-knife.png", "weapon_f-hammer.png"};
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
for (int i = 0; i < 7; i++)
{
Skin *skin = Skin::createWithSpriteFrameName(weapon[i].c_str());
2013-09-13 18:07:37 +08:00
armature->getBone("weapon")->addDisplay(skin, i);
}
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
// CCSpriteDisplayData displayData;
// for (int i = 0; i < 7; i++)
// {
// displayData.setParam(weapon[i].c_str());
// armature->getBone("weapon")->addDisplay(&displayData, i);
// }
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
LabelTTF *l = LabelTTF::create("This is a weapon!", "Arial", 18);
l->setAnchorPoint(Point(0.2f, 0.5f));
armature->getBone("weapon")->addDisplay(l, 7);
}
2013-09-16 19:27:34 +08:00
2013-09-13 18:07:37 +08:00
void TestUseMutiplePicture::onExit()
{
ArmatureTestLayer::onExit();
2013-06-06 12:02:54 +08:00
}
2013-09-16 19:27:34 +08:00
2013-06-06 12:02:54 +08:00
std::string TestUseMutiplePicture::title()
{
return "Test One Armature Use Different Picture";
2013-06-06 12:02:54 +08:00
}
2013-09-16 19:27:34 +08:00
2013-06-06 12:02:54 +08:00
std::string TestUseMutiplePicture::subtitle()
{
2013-09-13 18:07:37 +08:00
return "weapon and armature are in different picture";
2013-06-06 12:02:54 +08:00
}
2013-09-16 19:27:34 +08:00
void TestUseMutiplePicture::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
++displayIndex;
displayIndex = (displayIndex) % 8;
armature->getBone("weapon")->changeDisplayByIndex(displayIndex, true);
}
TestColliderDetector::~TestColliderDetector()
{
}
void TestColliderDetector::onEnter()
{
ArmatureTestLayer::onEnter();
scheduleUpdate();
armature = Armature::create("Cowboy");
2013-09-13 18:07:37 +08:00
armature->getAnimation()->play("FireWithoutBullet");
armature->getAnimation()->setSpeedScale(0.2f);
armature->setScaleX(-0.2f);
armature->setScaleY(0.2f);
armature->setPosition(Point(VisibleRect::left().x + 70, VisibleRect::left().y));
/*
* Set armature's frame event callback function
* To disconnect this event, just setFrameEventCallFunc(NULL, NULL);
*/
armature->getAnimation()->setFrameEventCallFunc(this, frameEvent_selector(TestColliderDetector::onFrameEvent));
addChild(armature);
armature2 = Armature::create("Cowboy");
2013-09-13 18:07:37 +08:00
armature2->getAnimation()->play("Walk");
armature2->setScaleX(-0.2f);
armature2->setScaleY(0.2f);
armature2->setPosition(Point(VisibleRect::right().x - 60, VisibleRect::left().y));
addChild(armature2);
bullet = cocos2d::extension::PhysicsSprite::createWithSpriteFrameName("25.png");
addChild(bullet);
initWorld();
}
std::string TestColliderDetector::title()
{
return "Test Collider Detector";
}
void TestColliderDetector::onFrameEvent(Bone *bone, const char *evt, int originFrameIndex, int currentFrameIndex)
2013-09-13 18:07:37 +08:00
{
CCLOG("(%s) emit a frame event (%s) at frame index (%d).", bone->getName().c_str(), evt, currentFrameIndex);
/*
* originFrameIndex is the frame index editted in Action Editor
* currentFrameIndex is the current index animation played to
* frame event may be delay emit, so originFrameIndex may be different from currentFrameIndex.
*/
Point p = armature->getBone("Layer126")->getDisplayRenderNode()->convertToWorldSpaceAR(Point(0, 0));
bullet->setPosition(Point(p.x + 60, p.y));
bullet->stopAllActions();
bullet->runAction(CCMoveBy::create(1.5f, Point(350, 0)));
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
#if ENABLE_PHYSICS_BOX2D_DETECT
class Contact
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
public:
b2Fixture *fixtureA;
b2Fixture *fixtureB;
};
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
class ContactListener : public b2ContactListener
{
//! Callbacks for derived classes.
virtual void BeginContact(b2Contact *contact)
{
if (contact)
{
Contact c;
c.fixtureA = contact->GetFixtureA();
c.fixtureB = contact->GetFixtureB();
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
contact_list.push_back(c);
}
B2_NOT_USED(contact);
}
virtual void EndContact(b2Contact *contact)
{
contact_list.clear();
B2_NOT_USED(contact);
}
virtual void PreSolve(b2Contact *contact, const b2Manifold *oldManifold)
{
B2_NOT_USED(contact);
B2_NOT_USED(oldManifold);
}
virtual void PostSolve(const b2Contact *contact, const b2ContactImpulse *impulse)
{
B2_NOT_USED(contact);
B2_NOT_USED(impulse);
}
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
public:
std::list<Contact> contact_list;
};
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
void TestColliderDetector::onExit()
{
CC_SAFE_DELETE(world);
CC_SAFE_DELETE(listener);
CC_SAFE_DELETE(debugDraw);
ArmatureTestLayer::onExit();
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void TestColliderDetector::draw()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
kmGLPushMatrix();
world->DrawDebugData();
kmGLPopMatrix();
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void TestColliderDetector::update(float delta)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
armature2->setVisible(true);
world->Step(delta, 0, 0);
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
for (std::list<Contact>::iterator it = listener->contact_list.begin(); it != listener->contact_list.end(); ++it)
{
Contact &contact = *it;
2013-06-06 12:02:54 +08:00
Bone *ba = (Bone *)contact.fixtureA->GetUserData();
Bone *bb = (Bone *)contact.fixtureB->GetUserData();
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
bb->getArmature()->setVisible(false);
}
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void TestColliderDetector::initWorld()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
b2Vec2 noGravity(0, 0);
world = new b2World(noGravity);
world->SetAllowSleeping(true);
listener = new ContactListener();
world->SetContactListener(listener);
debugDraw = new GLESDebugDraw( PT_RATIO );
world->SetDebugDraw(debugDraw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
// flags += b2Draw::e_jointBit;
// flags += b2Draw::e_aabbBit;
// flags += b2Draw::e_pairBit;
// flags += b2Draw::e_centerOfMassBit;
debugDraw->SetFlags(flags);
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.isSensor = true;
body->CreateFixture(&fixtureDef);
bullet->setB2Body(body);
bullet->setPTMRatio(PT_RATIO);
bullet->setPosition( Point( -100, -100) );
body = world->CreateBody(&bodyDef);
armature2->setBody(body);
}
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
enum ColliderType
{
eBulletTag,
eEnemyTag
};
int TestColliderDetector::beginHit(cpArbiter *arb, cpSpace *space, void *unused)
{
CP_ARBITER_GET_SHAPES(arb, a, b);
Bone *bone = (Bone *)a->data;
2013-09-13 18:07:37 +08:00
bone->getArmature()->setVisible(false);
return 0;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void TestColliderDetector::endHit(cpArbiter *arb, cpSpace *space, void *unused)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
CP_ARBITER_GET_SHAPES(arb, a, b);
Bone *bone = (Bone *)a->data;
2013-09-13 18:07:37 +08:00
bone->getArmature()->setVisible(true);
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void TestColliderDetector::destroyCPBody(cpBody *body)
{
cpShape *shape = body->shapeList_private;
while(shape)
{
cpShape *temp = shape->next_private;
cpSpaceRemoveShape(space, shape);
cpShapeFree(shape);
shape = temp;
}
cpSpaceRemoveBody(space, body);
cpBodyFree(body);
}
void TestColliderDetector::onExit()
{
destroyCPBody(armature2->getBody());
destroyCPBody(bullet->getCPBody());
cpSpaceFree(space);
ArmatureTestLayer::onExit();
}
void TestColliderDetector::update(float delta)
{
cpSpaceStep(space, delta);
}
void TestColliderDetector::initWorld()
{
space = cpSpaceNew();
space->gravity = cpv(0, 0);
// Physics debug layer
cocos2d::extension::PhysicsDebugNode *debugLayer = cocos2d::extension::PhysicsDebugNode::create(space);
this->addChild(debugLayer, INT_MAX);
Size size = bullet->getContentSize();
int num = 4;
cpVect verts[] =
{
cpv(-size.width / 2, -size.height / 2),
cpv(-size.width / 2, size.height / 2),
cpv(size.width / 2, size.height / 2),
cpv(size.width / 2, -size.height / 2),
};
cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero));
cpSpaceAddBody(space, body);
cpShape *shape = cpPolyShapeNew(body, num, verts, cpvzero);
shape->collision_type = eBulletTag;
cpSpaceAddShape(space, shape);
bullet->setCPBody(body);
body = cpBodyNew(INFINITY, INFINITY);
cpSpaceAddBody(space, body);
armature2->setBody(body);
shape = armature2->getShapeList();
while(shape)
{
cpShape *next = shape->next_private;
shape->collision_type = eEnemyTag;
shape = next;
}
cpSpaceAddCollisionHandler(space, eEnemyTag, eBulletTag, beginHit, NULL, NULL, endHit, NULL);
}
#endif
2013-06-06 12:02:54 +08:00
void TestBoundingBox::onEnter()
{
2013-09-13 18:07:37 +08:00
ArmatureTestLayer::onEnter();
armature = Armature::create("Cowboy");
2013-09-13 18:07:37 +08:00
armature->getAnimation()->playByIndex(0);
armature->setPosition(VisibleRect::center());
armature->setScale(0.2f);
addChild(armature);
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
Sprite *sprite = Sprite::create("Images/background3.png");
armature->addChild(sprite);
2013-06-06 12:02:54 +08:00
}
std::string TestBoundingBox::title()
{
2013-09-13 18:07:37 +08:00
return "Test BoundingBox";
2013-06-06 12:02:54 +08:00
}
void TestBoundingBox::draw()
{
2013-09-13 18:07:37 +08:00
CC_NODE_DRAW_SETUP();
rect = RectApplyAffineTransform(armature->getBoundingBox(), armature->getNodeToParentTransform());
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
DrawPrimitives::setDrawColor4B(100, 100, 100, 255);
DrawPrimitives::drawRect(rect.origin, Point(rect.getMaxX(), rect.getMaxY()));
2013-06-06 12:02:54 +08:00
}
void TestAnchorPoint::onEnter()
{
2013-09-13 18:07:37 +08:00
ArmatureTestLayer::onEnter();
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
for (int i = 0; i < 5; i++)
{
Armature *armature = Armature::create("Cowboy");
2013-09-13 18:07:37 +08:00
armature->getAnimation()->playByIndex(0);
armature->setPosition(VisibleRect::center());
armature->setScale(0.2f);
addChild(armature, 0, i);
}
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
getChildByTag(0)->setAnchorPoint(Point(0, 0));
getChildByTag(1)->setAnchorPoint(Point(0, 1));
getChildByTag(2)->setAnchorPoint(Point(1, 0));
getChildByTag(3)->setAnchorPoint(Point(1, 1));
getChildByTag(4)->setAnchorPoint(Point(0.5, 0.5));
2013-06-06 12:02:54 +08:00
}
std::string TestAnchorPoint::title()
{
2013-09-13 18:07:37 +08:00
return "Test Set AnchorPoint";
2013-06-06 12:02:54 +08:00
}
void TestArmatureNesting::onEnter()
{
2013-09-13 18:07:37 +08:00
ArmatureTestLayer::onEnter();
setTouchEnabled(true);
2013-06-06 12:02:54 +08:00
armature = Armature::create("cyborg");
2013-09-13 18:07:37 +08:00
armature->getAnimation()->playByIndex(1);
armature->setPosition(VisibleRect::center());
armature->setScale(1.2f);
armature->getAnimation()->setSpeedScale(0.4f);
addChild(armature);
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
weaponIndex = 0;
}
2013-09-16 19:27:34 +08:00
2013-09-13 18:07:37 +08:00
void TestArmatureNesting::onExit()
{
ArmatureTestLayer::onExit();
2013-06-06 12:02:54 +08:00
}
2013-09-16 19:27:34 +08:00
2013-06-06 12:02:54 +08:00
std::string TestArmatureNesting::title()
{
return "Test Armature Nesting";
2013-06-06 12:02:54 +08:00
}
2013-09-16 19:27:34 +08:00
void TestArmatureNesting::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
++weaponIndex;
weaponIndex = weaponIndex % 4;
if(armature != NULL)
{
armature->getBone("armInside")->getChildArmature()->getAnimation()->playByIndex(weaponIndex);
armature->getBone("armOutside")->getChildArmature()->getAnimation()->playByIndex(weaponIndex);
}
2013-06-06 12:02:54 +08:00
}