void GameLayer::onEnter() { Layer::onEnter(); auto visibleSize = Director::getInstance()->getVisibleSize(); this->m_origin = Director::getInstance()->getVisibleOrigin(); m_pTiledMap = TMXTiledMap::create("TYCHEs_COFEE.tmx"); m_TiledMapSize.setSize(m_pTiledMap->getMapSize().width * m_pTiledMap->getTileSize().width, m_pTiledMap->getMapSize().height * m_pTiledMap->getTileSize().height); this->addChild(m_pTiledMap); TMXObjectGroup *objects = m_pTiledMap->getObjectGroup("Objects"); CCASSERT(NULL != objects, "'Objects' object group not found"); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("hero.plist"); auto spawnPoint = objects->getObject("SpawnPoint"); CCASSERT(!spawnPoint.empty(), "SpawnPoint object not found"); Point heroInitPos = m_origin + Point(spawnPoint["x"].asFloat(), spawnPoint["y"].asFloat()); m_pHero = Hero::create(); m_pHero->setScale(0.5f); m_pHero->setPosition(heroInitPos); m_pHero->runIdleAction(); m_pHero->setLocalZOrder(visibleSize.height - m_pHero->getPositionY()); m_pHero->setHP(100); m_pHero->setIsAttacking(false); m_pHero->setJumpStage(0); m_pHero->onDeadCallback = CC_CALLBACK_0(GameLayer::onHeroDead, this, m_pHero); m_pHero->attack = CC_CALLBACK_0(GameLayer::onHeroAttack, this); m_pHero->stop = CC_CALLBACK_0(GameLayer::onHeroStop, this); m_pHero->walk = CC_CALLBACK_1(GameLayer::onHeroWalk, this); m_pHero->jump = CC_CALLBACK_1(GameLayer::onHeroJump, this); this->addChild(m_pHero); auto centerOfView = Point(visibleSize.width / 2, visibleSize.height / 2); this->setPosition(centerOfView - m_pHero->getPosition()); m_pForesight = Foresight::create(); this->addChild(m_pForesight); JsonParser* parser = JsonParser::createWithFile("Debug.json"); parser->decodeDebugData(); auto list = parser->getList(); for (auto& v : list) { ValueMap row = v.asValueMap(); for (auto& pair : row) { CCLOG("%s %s", pair.first.c_str(), pair.second.asString().c_str()); if (pair.first.compare("HeroHSpeed") == 0) { float s = pair.second.asFloat(); m_pHero->setWalkVelocity(s); } else if (pair.first.compare("HeroVSpeed") == 0) { m_pHero->setJumpVelocity(pair.second.asFloat()); } else if (pair.first.compare("BulletPower") == 0) { m_pHero->setBullletPower(pair.second.asInt()); } else if (pair.first.compare("BulletSpeed") == 0) { m_pHero->setBulletLaunchVelocity(pair.second.asFloat()); } else if (pair.first.compare("BulletDisappearTime") == 0) { m_pHero->setBulletDisappearTime(pair.second.asFloat()); } else if (pair.first.compare("BulletAngle") == 0) { m_pHero->setBullletAngle(pair.second.asInt()); } else if (pair.first.compare("BulletInterval") == 0) { m_pHero->setBulletInterval(pair.second.asFloat()); } else if (pair.first.compare("WorldG") == 0) { //getScene()->getPhysicsWorld()->setGravity(Vec2(0.f, pair.second.asFloat())); } else if (pair.first.compare("ForesightSpeed") == 0) { initForesight(pair.second.asFloat()); } else if (pair.first.compare("AmmoCapacity") == 0) { m_pHero->setMaxAmmoCapacity(pair.second.asInt()); m_pHero->setAmmoCapacity(pair.second.asInt()); } } } /*const PhysicsMaterial m(1.f, 0.f, 0.f); Size boxSize(m_pTiledMap->getMapSize().width * m_pTiledMap->getTileSize().width, m_pTiledMap->getMapSize().height * m_pTiledMap->getTileSize().height); auto body = PhysicsBody::createEdgeBox(boxSize, m, 3); body->setTag(0); body->setCategoryBitmask(0x04); body->setContactTestBitmask(0x02); body->setCollisionBitmask(0x03); auto edgeNode = Node::create(); edgeNode->setPosition(Point(boxSize.width / 2, boxSize.height / 2)); edgeNode->setPhysicsBody(body); this->addChild(edgeNode);*/ importGroundData(m_pTiledMap); m_shootTime = m_pHero->getBulletInterval(); auto listener = EventListenerCustom::create("bullet_disappear", [this](EventCustom* event) { Bullet* bullet = static_cast<Bullet *>(event->getUserData()); if (bullet) this->removeChild(bullet); }); _eventDispatcher->addEventListenerWithFixedPriority(listener, 1); m_vecEventListener.pushBack(listener); /*auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = [this](PhysicsContact& contact)->bool { if (contact.getShapeA()->getBody()->getCategoryBitmask() == PC_Hero && contact.getShapeB()->getBody()->getCategoryBitmask() == PC_Ground) { Point posA = contact.getShapeA()->getBody()->getPosition(); Point posB = contact.getShapeB()->getBody()->getPosition(); if (posA.y >= posB.y) { Hero* hero = static_cast<Hero *>(contact.getShapeA()->getBody()->getNode()); if (hero->getCurrActionState() == ACTION_STATE_MOVE && hero->isInMoveAction(MOVE_STATE_DOWN)) { if (hero->isInMoveAction(MOVE_STATE_WALK)) { hero->stopMoveAction(MOVE_STATE_DOWN, true); Vec2 v = hero->getPhysicsBody()->getVelocity(); hero->walk(v.x); CCLOG("Hero Walk"); } else { CCLOG("Hero Stop"); hero->stop(); } hero->setJumpStage(0); } return true; } else { return false; } } else if (contact.getShapeA()->getBody()->getCategoryBitmask() == PC_Ground && contact.getShapeB()->getBody()->getCategoryBitmask() == PC_Hero) { Point posA = contact.getShapeA()->getBody()->getPosition(); Point posB = contact.getShapeB()->getBody()->getPosition(); if (posA.y <= posB.y) { Hero* hero = static_cast<Hero *>(contact.getShapeB()->getBody()->getNode()); if (hero->getCurrActionState() == ACTION_STATE_MOVE && hero->isInMoveAction(MOVE_STATE_DOWN)) { if (hero->isInMoveAction(MOVE_STATE_WALK)) { hero->stopMoveAction(MOVE_STATE_DOWN, true); Vec2 v = hero->getPhysicsBody()->getVelocity(); hero->walk(v.x); CCLOG("Hero Walk"); } else { CCLOG("Hero Stop"); hero->stop(); } hero->setJumpStage(0); } return true; } else { return false; } } else if(contact.getShapeA()->getBody()->getCategoryBitmask() == PC_Bullet && contact.getShapeB()->getBody()->getCategoryBitmask() == PC_Ground) { Bullet* bullet = static_cast<Bullet *>(contact.getShapeA()->getBody()->getNode()); if(bullet) { bullet->setIsActive(false); this->removeChild(bullet); } } else if(contact.getShapeA()->getBody()->getCategoryBitmask() == PC_Ground && contact.getShapeB()->getBody()->getCategoryBitmask() == PC_Bullet) { Bullet* bullet = static_cast<Bullet *>(contact.getShapeB()->getBody()->getNode()); if(bullet) { bullet->setIsActive(false); this->removeChild(bullet); } } return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this); m_vecEventListener.pushBack(contactListener);*/ //this->getScene()->getPhysicsWorld()->setAutoStep(false); this->scheduleUpdate(); }