void Camera::lookAt(PointF target, PointI mapSize) { int screenLeft = target.getX() - geometry.getWidth() / 2; int screenTop = target.getY() - geometry.getHeight() / 2; int screenRight = target.getX() + geometry.getWidth() / 2; int screenBottom = target.getY() + geometry.getHeight() / 2; scroll_2(screenLeft, screenRight, screenTop, screenBottom, mapSize); geometry.setX(screenLeft); geometry.setY(screenTop); }
void AttractionAffector::updateParticle(const ParticlePtr& particle, float elapsedTime) { if(!m_active) return; PointF pPosition = particle->getPosition(); PointF d = PointF(m_position.x - pPosition.x, pPosition.y - m_position.y); if(d.length() == 0) return; PointF direction = PointF(1, 1); if(m_repelish) direction = PointF(-1, -1); PointF pVelocity = particle->getVelocity() + (d / d.length() * m_acceleration * elapsedTime) * direction; particle->setVelocity(pVelocity - pVelocity * m_reduction/100.0 * elapsedTime); }
void Camera::scroll(PointF position, PointF speed, PointI mapSize) { int centerHoriz = geometry.getLeftSide() + position.getX() + speed.getX() + lockedPosition.getX(); int centerVert = geometry.getTopSide() + position.getY() + speed.getY() + lockedPosition.getY(); int screenLeft = centerHoriz - geometry.getWidth() / 2; int screenRight = centerHoriz + geometry.getWidth() / 2; int screenTop = centerVert - geometry.getHeight() / 2; int screenBottom = centerVert + geometry.getHeight() / 2; scroll_2(screenLeft, screenRight, screenTop, screenBottom, mapSize); if ( isLockedX() ) { if ( scrollingX ) { lockedPosition.addX(speed.getX()); } } else { geometry.setX(screenLeft); } if ( isLockedY() ) { if ( scrollingY ) lockedPosition.addY(speed.getY()); } else { geometry.setY(screenTop); } }
float PointF::distance(const PointF other) { return sqrt((x() - other.x()) * (x() - other.x()) + (y() - other.y()) * (y() - other.y())); }
PointF::PointF(const PointF &p) { mX = p.x(); mY = p.y(); }
void Walker::_walk() { if( !_d->action.valid() || !_pathway().isValid() ) { // nothing to do _noWay(); _changeDirection(); return; } float speedKoeff = 1.f; switch( _d->action.direction ) { case direction::north: case direction::south: case direction::east: case direction::west: break; case direction::northEast: case direction::southWest: case direction::southEast: case direction::northWest: speedKoeff = 0.7f; break; default: Logger::warning( "Walker: invalid move direction: %d", _d->action.direction ); _d->action.action = acNone; _d->action.direction = direction::none; return; break; } PointF delta = _d->speed.vector; delta.rx() = delta.x() * _d->speed.current() * speedKoeff; delta.ry() = delta.y() * _d->speed.current() * speedKoeff; PointF tmp = _d->world.pos; const int wcell = tilemap::cellSize().height(); TilePos saveMpos( tmp.x() / wcell , tmp.y() / wcell ); _d->world.pos += delta; _updateMappos(); tmp = _d->world.pos; TilePos currentMapPos( tmp.x() / wcell, tmp.y() / wcell ); if( !_d->map.inCenter ) { float crntDst = _d->world.dst2next(); if( crntDst < _d->world.lastDst ) { _d->world.lastDst = crntDst; } else { _d->map.path.next(); _centerTile(); } } if( saveMpos != currentMapPos ) { _d->map.tile = &_d->city->tilemap().at( currentMapPos ); _changeTile(); } }
std::string Json::serialize(const Variant &data, bool &success, const std::string& tab) { std::string str; success = true; if( data.isNull() ) // invalid or null? { return "null"; } switch( data.type() ) { case Variant::List: case Variant::NStringArray: // variant is a list? { StringArray values; const VariantList rlist = data.toList(); std::string serializedValue; serializedValue.reserve( 512 ); for( auto item : rlist ) { serializedValue = serialize( item, "" ); if( serializedValue.empty() ) { success = false; break; } values.push_back( serializedValue ); } str = "[ " + join( values, ", " ) + " ]"; } break; case Variant::Map: // variant is a map? { VariantMap vmap = data.toMap(); if( vmap.empty() ) { str = "{}"; } else { str = "{ \n"; StringArray pairs; std::string serializedValue; serializedValue.reserve( 512 ); for( auto item : vmap ) { serializedValue = serialize( item.second, tab + " "); if( serializedValue.empty()) { //success = false; pairs.push_back( tab + sanitizeString( item.first ) + std::string( " : \"nonSerializableValue\"" ) ); continue; } pairs.push_back( tab + sanitizeString( item.first ) + " : " + serializedValue ); } str += join(pairs, ",\n"); std::string rtab( tab ); rtab.resize( std::max<int>( 0, tab.size() - 2 ) ); str += std::string( "\n" ) + rtab + "}"; } } break; case Variant::String: case Variant::NByteArray: // a string or a byte array? { str = sanitizeString( data.toString() ); } break; case Variant::Double: case Variant::Float: // double? { // TODO: cheap hack - almost locale independent double formatting str = utils::format( 0xff, "\"%f\"", data.toDouble() ); str = utils::replace(str, ",", "."); if( str.find(".") == std::string::npos && str.find("e") == std::string::npos ) { str += ".0"; } } break; case Variant::NTilePos: { const TilePos& pos = data.toTilePos(); str = utils::format( 0xff, "[ %d, %d ]", pos.i(), pos.j() ); } break; case Variant::NSize: { const Size& size = data.toSize(); str = utils::format( 0xff, "[ %d, %d ]", size.width(), size.height() ); } break; case Variant::NPoint: { const Point& pos = data.toPoint(); str = utils::format( 0xff, "[ %d, %d ]", pos.x(), pos.y() ); } break; case Variant::NPointF: { PointF pos = data.toPointF(); // TODO: cheap hack - almost locale independent double formatting std::string posX = utils::replace(utils::format( 0xff, "%f", pos.x()), ",", "."); std::string posY = utils::replace(utils::format( 0xff, "%f", pos.y()), ",", "."); str = utils::format( 0xff, "[ \"%s\", \"%s\" ]", posX.c_str(), posY.c_str() ); } break; case Variant::Bool: // boolean value? { str = data.toBool() ? "true" : "false"; } break; case Variant::ULongLong: // large unsigned number? { str = utils::format( 0xff, "%u", data.toULongLong() ); } break; case Variant::Int: // simple int? { str = utils::format( 0xff, "%d", data.toInt() ); } break; case Variant::UInt: { str = utils::format( 0xff, "%d", data.toInt() ); } break; default: if ( data.canConvert( Variant::LongLong ) ) // any signed number? { str = utils::format( 0xff, "%d", data.toLongLong() ); } else if (data.canConvert( Variant::Long )) { str = utils::format( 0xff, "%d", data.toLongLong() ); } else if (data.canConvert( Variant::String ) ) // can value be converted to string? { // this will catch Date, DateTime, Url, ... str = sanitizeString( data.toString() ); } else { success = false; } break; } return success ? str : std::string(); }
std::string Json::serialize(const Variant &data, bool &success, const std::string& tab) { std::string str; success = true; if( !data.isValid() ) // invalid or null? { str = "null"; } else if( (data.type() == Variant::List) || (data.type() == Variant::NStringArray) ) // variant is a list? { StringArray values; const VariantList rlist = data.toList(); for( VariantList::const_iterator it = rlist.begin(); it != rlist.end(); it++) { std::string serializedValue = serialize( *it, "" ); if( serializedValue.empty() ) { success = false; break; } values.push_back( serializedValue ); } str = "[ " + join( values, ", " ) + " ]"; } // else if(data.type() == Variant::Hash) // variant is a hash? // { // const VariantHash vhash = data.toHash(); // QHashIterator<std::string, Variant> it( vhash ); // str = "{ "; // QList<QByteArray> pairs; // // while(it.hasNext()) // { // it.next(); // QByteArray serializedValue = serialize(it.value(), ""); // // if(serializedValue.isNull()) // { // success = false; // break; // } // // pairs << tab.toAscii() + sanitizeString(it.key()).toUtf8() + " : " + serializedValue; // } // // str += join(pairs, ", "); // str += " }"; // } else if(data.type() == Variant::Map) // variant is a map? { VariantMap vmap = data.toMap(); str = "{ \n"; StringArray pairs; for( VariantMap::iterator it = vmap.begin(); it != vmap.end(); it++ ) { std::string serializedValue = serialize( it->second, tab + " "); if( serializedValue.empty()) { //success = false; pairs.push_back( tab + sanitizeString( it->first ) + std::string( " : \"nonSerializableValue\"" ) ); continue; } pairs.push_back( tab + sanitizeString( it->first ) + " : " + serializedValue ); } str += join(pairs, ",\n"); std::string rtab( tab ); rtab.resize( std::max<int>( 0, tab.size() - 2 ) ); str += std::string( "\n" ) + rtab + "}"; } else if((data.type() == Variant::String) || (data.type() == Variant::NByteArray)) // a string or a byte array? { str = sanitizeString( data.toString() ); } else if(data.type() == Variant::Double || data.type() == Variant::Float) // double? { str = StringHelper::format( 0xff, "\"%f\"", data.toDouble() ); if( str.find(".") == std::string::npos && str.find("e") == std::string::npos ) { str += ".0"; } } else if( data.type() == Variant::NTilePos) { TilePos pos = data.toTilePos(); str = StringHelper::format( 0xff, "[ %d, %d ]", pos.getI(), pos.getJ() ); } else if( data.type() == Variant::NSize) { Size size = data.toSize(); str = StringHelper::format( 0xff, "[ %d, %d ]", size.getWidth(), size.getHeight() ); } else if( data.type() == Variant::NPoint) { Point pos = data.toPoint(); str = StringHelper::format( 0xff, "[ %d, %d ]", pos.getX(), pos.getY() ); } else if( data.type() == Variant::NPointF) { PointF pos = data.toPointF(); str = StringHelper::format( 0xff, "[ \"%f\", \"%f\" ]", pos.getX(), pos.getY() ); } else if (data.type() == Variant::Bool) // boolean value? { str = data.toBool() ? "true" : "false"; } else if (data.type() == Variant::ULongLong) // large unsigned number? { str = StringHelper::format( 0xff, "%u", data.toULongLong() ); } else if ( data.canConvert( Variant::LongLong ) ) // any signed number? { str = StringHelper::format( 0xff, "%d", data.toLongLong() ); } else if (data.canConvert( Variant::Long )) { str = StringHelper::format( 0xff, "%d", data.toLongLong() ); } else if (data.canConvert( Variant::String ) ) // can value be converted to string? { // this will catch Date, DateTime, Url, ... str = sanitizeString( data.toString() ); } else { success = false; } if (success) { return str; } else { return std::string(); } }