void KarbonCalligraphicShape::simplifyPath() { if (m_points.count() < 2) return; close(); // add final cap addCap(m_points.count() - 2, m_points.count() - 1, pointCount() / 2); // TODO: the error should be proportional to the width // and it shouldn't be a magic number karbonSimplifyPath(this, 0.3); }
void KarbonCalligraphicShape:: appendPointToPath(const KarbonCalligraphicPoint &p) { qreal dx = std::cos(p.angle()) * p.width(); qreal dy = std::sin(p.angle()) * p.width(); // find the outline points QPointF p1 = p.point() - QPointF(dx / 2, dy / 2); QPointF p2 = p.point() + QPointF(dx / 2, dy / 2); if (pointCount() == 0) { moveTo(p1); lineTo(p2); normalize(); return; } // pointCount > 0 bool flip = (pointCount() >= 2) ? flipDetected(p1, p2) : false; // if there was a flip add additional points if (flip) { appendPointsToPathAux(p2, p1); if (pointCount() > 4) smoothLastPoints(); } appendPointsToPathAux(p1, p2); if (pointCount() > 4) { smoothLastPoints(); if (flip) { int index = pointCount() / 2; // find the last two points KoPathPoint *last1 = pointByIndex(KoPathPointIndex(0, index - 1)); KoPathPoint *last2 = pointByIndex(KoPathPointIndex(0, index)); last1->removeControlPoint1(); last1->removeControlPoint2(); last2->removeControlPoint1(); last2->removeControlPoint2(); m_lastWasFlip = true; } if (m_lastWasFlip) { int index = pointCount() / 2; // find the previous two points KoPathPoint *prev1 = pointByIndex(KoPathPointIndex(0, index - 2)); KoPathPoint *prev2 = pointByIndex(KoPathPointIndex(0, index + 1)); prev1->removeControlPoint1(); prev1->removeControlPoint2(); prev2->removeControlPoint1(); prev2->removeControlPoint2(); if (! flip) m_lastWasFlip = false; } } normalize(); // add initial cap if it's the fourth added point // this code is here because this function is called from different places // pointCount() == 8 may causes crashes because it doesn't take possible // flips into account if (m_points.count() >= 4 && &p == m_points[3]) { kDebug(38000) << "Adding caps!!!!!!!!!!!!!!!!" << m_points.count(); addCap(3, 0, 0, true); // duplicate the last point to make the points remain "balanced" // needed to keep all indexes code (else I would need to change // everything in the code...) KoPathPoint *last = pointByIndex(KoPathPointIndex(0, pointCount() - 1)); KoPathPoint *newPoint = new KoPathPoint(this, last->point()); insertPoint(newPoint, KoPathPointIndex(0, pointCount())); close(); } }
void CArmySprite::createSprite(ARMY_KINDS eKind) { //std::string tString = CGlobalData::getSingleton()->getNameByEnum(eKind); mScreenSize = CCDirector::sharedDirector()->getWinSize(); m_sCurData = CGlobalData::getSingleton()->getDataByKind(eKind); m_pCurCache = CCSpriteFrameCache::sharedSpriteFrameCache(); char szName[128] = {0}; //sprintf(szName, "%s%02d_01.png", "Plane", m_sCurData);NPC_Tank01_01 setContentSize(CCSizeMake(98,176)); if (CCRANDOM_0_1() > 0.5f && m_sCurData.mKind == AK_DEFAULT || m_sCurData.mKind == AK_MISSILEARMY) { m_bIsHorizontal = true; } sprintf(szName, "%s%02d_%02d.png", "NPC_Tank", m_sCurData.mIndex, 1); if (m_bIsHorizontal) { sprintf(szName, "%s%02d_%02d.png", "NPC_Tank", m_sCurData.mIndex, 2); } if (m_pCurCache->spriteFrameByName(szName) == NULL) { return ; } m_pBaseSprite = CCSprite::createWithSpriteFrame(m_pCurCache->spriteFrameByName(szName)); m_pBaseSprite->setPosition(ccp(getContentSize().width * 0.5f, getContentSize().height * 0.5f)); addChild(m_pBaseSprite); sprintf(szName, "%s%02d_%02d.png", "NPC_Tank", m_sCurData.mIndex, 3); if (m_pCurCache->spriteFrameByName(szName) == NULL) { return ; } m_pTurretSprite = CCSprite::createWithSpriteFrame(m_pCurCache->spriteFrameByName(szName)); //各地面武器偏移位置和基准大小 CCPoint mBase = CCPointMake(0,0); CCPoint mOffset = CCPointMake(0,0); switch(eKind) { case AK_DEFAULT: mBase = ccp(0.5f, 0.4f); mOffset = ccp(0.5f, 0.75f); break; case AK_MISSILEARMY: mBase = ccp(0.5f, 0.5f); mOffset = ccp(0.5f, 0.714f); break; case AK_GREENGUN01: mBase = ccp(0.75f, 0.7f); mOffset = ccp(0.5f, 0.78f); break; case AK_GREENGUN02: mBase = ccp(0.75f, 0.5f); mOffset = ccp(0.5f, 0.68f); break; case AK_BATTERY01: mBase = ccp(0.55f, 0.5f); mOffset = ccp(0.5f, 0.7f); break; case AK_BATTERY02: mBase = ccp(0.5f, 0.5f); mOffset = ccp(0.5f, 0.7f); break; case AK_BUNKER01: mBase = ccp(0.5f, 0.5f); mOffset = ccp(0.5f, 0.5f); break; case AK_BUNKER02: mBase = ccp(0.5f, 0.5f); mOffset = ccp(0.5f, 0.5f); break; } m_pTurretSprite->setAnchorPoint(mOffset); m_pTurretSprite->setPosition(ccp(m_pBaseSprite->getContentSize().width * mBase.x, m_pBaseSprite->getContentSize().height * mBase.y)); m_pBaseSprite->addChild(m_pTurretSprite); sprintf(szName, "%s%02d_%02d.png", "NPC_Tank", m_sCurData.mIndex, 4); if (m_bIsHorizontal) { sprintf(szName, "%s%02d_%02d.png", "NPC_Tank", m_sCurData.mIndex, 5); } m_pWreckSprite = CCSprite::createWithSpriteFrame(m_pCurCache->spriteFrameByName(szName)); m_pWreckSprite->setPosition(ccp(getContentSize().width * 0.5f, getContentSize().height * 0.5f)); m_pWreckSprite->setVisible(false); addChild(m_pWreckSprite); m_pEffectSprite = CCSprite::create(); m_pEffectSprite->setPosition(ccp(getContentSize().width * 0.5f, getContentSize().height * 0.5f)); addChild(m_pEffectSprite); //create and mount weapon //m_pPlayerWeapon = CPlayerWeapon::createWeapon(); //addChild(m_pPlayerWeapon); setPosition(m_sCurLandData.mPosition); setAnchorPoint(ccp(0.5f,0.5f)); //setScale(0.282f); m_fFireTime = CCRANDOM_0_1() + 2.0f; m_eCurState = AS_MOVE; if (m_sCurData.mKind == AK_GREENGUN01 || m_sCurData.mKind == AK_GREENGUN02) { addCap(); m_eCurState = AS_READY; } this->schedule(schedule_selector(CArmySprite::enemyUpdate)); }