void Relation::update(const Gift &gift) { int value = gift.value(); if( value <= 0 ) { Logger::warning( "!!! WARNING: Relation update with 0 value from " + gift.sender() ); value = 1; } int monthFromLastGift = math::clamp<int>( lastGift.date().monthsTo( game::Date::current() ), 0, (int)DateTime::monthsInYear ); float timeKoeff = monthFromLastGift / (float)DateTime::monthsInYear; float affectMoney = (float)lastGift.value() / ( monthFromLastGift + 1 ); float moneyKoeff = math::max<float>( value - affectMoney, 0.f ) / value; float favourUpdate = emperor::maxFavourUpdate * timeKoeff * moneyKoeff; favourUpdate = math::clamp<float>( favourUpdate, 0.f, emperor::maxFavourUpdate); Gift maxValueGift( "", "", math::max<int>( value, lastGift.value() ) ); lastGift = maxValueGift; change( favourUpdate ); }
void GameEngine::step() { // needed to exit from the loop if the arrays that they cicle // change (items get deleted) m_itemsGotDeleted = false; m_dScore *= SCORE_AUTO_DECREASE; foreach (Ball *ball, m_balls) { if (ball->toBeFired) { continue; } // TODO: add function ball->move(speed) ball->moveBy(ball->directionX * m_speed, ball->directionY * m_speed); // collision detection detectBallCollisions(ball); if (m_itemsGotDeleted) { return; } } QMutableListIterator<Gift *> i(m_gifts); while (i.hasNext()) { Gift *gift = i.next(); if (!gift->isVisible()) { continue; // do nothing } qreal giftSpeed = std::sqrt(m_speed / m_gameTimer.interval()); gift->move(giftSpeed, m_gameTimer.interval()); if (gift->getRect().bottom() > BRICK_HEIGHT * HEIGHT) { i.remove(); delete gift; } else if (m_bar.getRect().intersects(gift->getRect())) { gift->execute(this); if (m_itemsGotDeleted) { return; } i.remove(); delete gift; } } }
void LevelLoader::loadGift(QDomElement giftNode, QList< Brick* >& bricks) { // Build list of bricks without a gift QList<Brick *> bricksLeft = bricks; QMutableListIterator<Brick *> i(bricksLeft); while (i.hasNext()) { Brick *brick = i.next(); if (brick->identifier() == "UnbreakableBrick" || brick->hasGift() ){ i.remove(); } } bool nodeTextRead = false; // Reading the brick type QDomAttr attribute = giftNode.attributeNode("Type"); QDomElement attributeNode = giftNode.firstChildElement("Type"); QString giftType; if( !attribute.isNull() ){ giftType = attribute.value(); } else if( !attributeNode.isNull() ){ giftType = attributeNode.text(); nodeTextRead = true; } else { giftType = giftNode.text(); nodeTextRead = true; } // Reading number of gifts to be distributed. If not specified one gift is placed. attribute = giftNode.attributeNode("Count"); attributeNode = giftNode.firstChildElement("Count"); int times = 1; bool ok = true; if( !attribute.isNull() ){ times = attribute.value().toInt( &ok ); } else if( !attributeNode.isNull() ){ times = attributeNode.text().toInt( &ok ); nodeTextRead = true; } else if( !nodeTextRead ){ times = giftNode.text().toInt( &ok ); if( !ok ){ times = 1; } } if( bricksLeft.count() < times ){ qDebug() << "Invalid levelset " << m_levelname << ": In Level " << m_level << " are too many gifts of type " << giftType << endl; } // If only one brick to be placed: see if position is given QPoint position; if( times == 1 ){ attribute = giftNode.attributeNode("Position"); attributeNode = giftNode.firstChildElement("Position"); if( !attribute.isNull() ){ position = positionFromString( attribute.value() ); } else if( !attributeNode.isNull() ){ position = positionFromString( attributeNode.text() ); nodeTextRead = true; } else if( !nodeTextRead && giftNode.text().contains(',') ){ position = positionFromString( giftNode.text() ); nodeTextRead = true; } } if( !position.isNull() ){ // Put gift at given position Brick *giftBrick = brickAt( position, bricks ); if( giftBrick == 0 ){ qDebug() << "Invalid levelset " << m_levelname << ": Can't place gift at position (" << position.x() << "," << position.y() << "). There is no brick.\n"; } else { if( giftBrick->hasGift() ){ // Brick already has a gift -> move this gift to a random position int index = qrand() % bricksLeft.count(); bricksLeft.at(index)->setGift( giftBrick->gift() ); } Gift *newgift = new Gift(giftType); newgift->hide(); giftBrick->setGift(newgift); } } else { // Distribute gifts randomly for( int i = 0; i < times; i++ ){ Gift *gift = new Gift(giftType); gift->hide(); int index = qrand() % bricksLeft.count(); bricksLeft.at(index)->setGift(gift); bricksLeft.removeAt(index); } } }
void Emperor::sendGift(const Gift &gift) { Relation &relation = _dfunc()->relations[gift.sender()]; relation.update(gift); }