diff --git a/Shooter.cpp b/Shooter.cpp index 4754115..ccf4239 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -172,13 +172,14 @@ void Shooter::update() { if (inGame) { screen->setTitle(ShooterConsts::PROJECT_NAME); + if (isTypingMessage) { string symbols = screen->getInputSymbols(); for (char s : symbols) { if (s == (char)8) { message = message.substr(0, message.size() - 1); } - else { + else if (message.length() < ShooterConsts::MAX_MESSAGE_LENGTH && s!=(char)13) { message += s; } } @@ -202,11 +203,15 @@ void Shooter::update() { } void Shooter::drawChat() { - sf::Color chatColor = sf::Color(0, 0, 0, chat->update(Time::deltaTime())); - string chatText = chat->getChat(); - screen->drawText(chatText, Vec2D{ 0, (double)screen->height()/2 }, 20, chatColor); - if (isTypingMessage) { - screen->drawText(message, Vec2D{ (double)screen->width()/4, (double)screen->height() / 1.5 }, 40, sf::Color(0, 0, 0, 255)); + sf::Color chatColor; + string chatText; + if (!isTypingMessage) { chatColor = sf::Color(0, 0, 0, chat->update(Time::deltaTime())); chatText = chat->getChatPreview(); } + else { chatColor = sf::Color(60, 60, 60, 200); chatText = chat->getChat();} + + screen->drawText(chatText, Vec2D{ 0, (double)screen->height()*0.25 }, 20, chatColor); + + if (isTypingMessage){ + screen->drawText(message+"_", Vec2D{(double)screen->width() * 0.05, (double)screen->height() / 1.5}, 30, sf::Color(0, 0, 0, 255)); } } diff --git a/ShooterConsts.h b/ShooterConsts.h index 35de1ad..e8d2a18 100644 --- a/ShooterConsts.h +++ b/ShooterConsts.h @@ -15,6 +15,7 @@ namespace ShooterConsts { const double SLOW_MO_COEFFICIENT = 5; const double FIRE_DISTANCE = 1000; const double BONUS_RECHARGE_TIME = 30; + const int MAX_MESSAGE_LENGTH = 70; const std::string PLAYER_NAME = "Player"; const std::string PROJECT_NAME = "Shooter"; diff --git a/network/Chat.cpp b/network/Chat.cpp index 06b59d8..860c964 100644 --- a/network/Chat.cpp +++ b/network/Chat.cpp @@ -6,28 +6,34 @@ void ChatManager::addNewMessage(std::string author, std::string message) { messages.push_back(message); authors.push_back(author); isChatUpdate = true; - if (messages.size() > 6) { + if (messages.size() > 20) { messages.erase(messages.begin()); } } int ChatManager::update(double delta) { hide = std::max(hide-delta, 0.0); - if (hide < 0.0000001) { - messages.clear(); - authors.clear(); - } return std::min((int)(hide * 255.0), 255); } std::string ChatManager::getChat() { + updateChat(); return chatStr; +} +std::string ChatManager::getChatPreview() { + updateChat(); return chatStrPrev; +} + +void ChatManager::updateChat() { if (isChatUpdate) { isChatUpdate = false; int size = messages.size(); chatStr = ""; - for (int messageIndex = messages.size()-1; messageIndex >= 0; messageIndex--) + chatStrPrev = ""; + for (int messageIndex = size - 1; messageIndex >= 0; messageIndex--) { + if (messageIndex > size - 6) { + chatStrPrev += authors[messageIndex] + ": " + messages[messageIndex] + ";\n"; + } chatStr += authors[messageIndex] + ": " + messages[messageIndex] + ";\n"; } } - return chatStr; } diff --git a/network/Chat.h b/network/Chat.h index f8ddf5b..9a568f5 100644 --- a/network/Chat.h +++ b/network/Chat.h @@ -11,10 +11,13 @@ private: std::vector authors; bool isChatUpdate = true; std::string chatStr = ""; + std::string chatStrPrev = ""; double hide = 0.0; + void updateChat(); public: void addNewMessage(std::string author, std::string message); int update(double delta); std::string getChat(); + std::string getChatPreview(); }; #endif \ No newline at end of file diff --git a/network/ShooterClient.cpp b/network/ShooterClient.cpp index a902cb2..3ffa60a 100644 --- a/network/ShooterClient.cpp +++ b/network/ShooterClient.cpp @@ -121,6 +121,9 @@ void ShooterClient::processDisconnect(sf::Uint16 targetId) { } void ShooterClient::sendMessage(string message){ + + if (message.length() == 0) + return; chatManager->addNewMessage(_player->playerNickName(), message); sf::Packet packet; packet << MsgType::Custom << ShooterMsgType::newMessage << message; diff --git a/network/ShooterServer.cpp b/network/ShooterServer.cpp index 2f593e0..ebec713 100644 --- a/network/ShooterServer.cpp +++ b/network/ShooterServer.cpp @@ -146,6 +146,8 @@ void ShooterServer::processCustomPacket(sf::Packet &packet, sf::Uint16 senderId) packet >> message; sendPacket << MsgType::Custom << ShooterMsgType::newMessage << _players[senderId]->playerNickName() << message; + if (message.length() == 0) + break; for (auto& player : _players) { if (player.first != senderId) { _socket.send(sendPacket, player.first);