Untitled
void Player::sendVariant( std::optional<int> delay = std::nullopt, std::optional<int> netID = std::nullopt, std::initializer_list<std::string> strings = {}, std::initializer_list<int> integers = {}, std::initializer_list<float> floats = {}, std::initializer_list<std::pair<float, float>> float_pairs = {}, std::initializer_list<std::tuple<float, float, float>> float_triplets = {}) { std::vector<uint8_t> packet_data(61, 0); int index = 0; int messageType = 0x4, packetType = 0x1, charState = 0x8; // MessageType, PacketType, CharState ve diğer parametrelerin yazılması std::memcpy(packet_data.data(), &messageType, 4); std::memcpy(packet_data.data() + 4, &packetType, 4); std::memcpy(packet_data.data() + 16, &charState, 4); // Delay ve netID isteğe bağlı ise, varsayılan değerleri kullan if (delay.has_value()) { std::memcpy(packet_data.data() + 24, &delay.value(), 4); } else { int default_delay = 0; // Varsayılan delay std::memcpy(packet_data.data() + 24, &default_delay, 4); } if (netID.has_value()) { std::memcpy(packet_data.data() + 8, &netID.value(), 4); } else { int default_netID = -1; // Varsayılan NetID std::memcpy(packet_data.data() + 8, &default_netID, 4); } // String verilerinin eklenmesi for (const auto& str : strings) { packet_data.push_back(index++); packet_data.push_back(0x2); uint16_t str_length = static_cast<uint16_t>(str.size()); const uint8_t* len_ptr = reinterpret_cast<const uint8_t*>(&str_length); packet_data.insert(packet_data.end(), len_ptr, len_ptr + 2); packet_data.insert(packet_data.end(), str.begin(), str.end()); } // Integer verilerinin eklenmesi for (const auto& integer : integers) { packet_data.push_back(index++); packet_data.push_back(0x9); const uint8_t* int_ptr = reinterpret_cast<const uint8_t*>(&integer); packet_data.insert(packet_data.end(), int_ptr, int_ptr + sizeof(int)); } // Float verilerinin eklenmesi for (const auto& value : floats) { packet_data.push_back(index++); packet_data.push_back(0x1); const uint8_t* float_ptr = reinterpret_cast<const uint8_t*>(&value); packet_data.insert(packet_data.end(), float_ptr, float_ptr + sizeof(float)); } // Float pair verilerinin eklenmesi for (const auto& pair : float_pairs) { packet_data.push_back(index++); packet_data.push_back(0x3); const float* pair_values = &pair.first; const uint8_t* pair_ptr = reinterpret_cast<const uint8_t*>(pair_values); packet_data.insert(packet_data.end(), pair_ptr, pair_ptr + 2 * sizeof(float)); } // Float triplet verilerinin eklenmesi for (const auto& triplet : float_triplets) { packet_data.push_back(index++); packet_data.push_back(0x4); const float* triplet_values = &std::get<0>(triplet); const uint8_t* triplet_ptr = reinterpret_cast<const uint8_t*>(triplet_values); packet_data.insert(packet_data.end(), triplet_ptr, triplet_ptr + 3 * sizeof(float)); } // ENet paketinin oluşturulması ve gönderilmesi ENetPacket* packet = enet_packet_create(packet_data.data(), packet_data.size(), ENET_PACKET_FLAG_RELIABLE); enet_peer_send(this->m_peer, 0, packet); }
Leave a Comment