refactoring and addings limits to chat v1.3

master
CREAsTIVE 2022-07-24 17:30:14 +05:00
parent 068030ecea
commit dbd4eb4953
6 changed files with 33 additions and 13 deletions

View File

@ -172,13 +172,14 @@ void Shooter::update() {
if (inGame) { if (inGame) {
screen->setTitle(ShooterConsts::PROJECT_NAME); screen->setTitle(ShooterConsts::PROJECT_NAME);
if (isTypingMessage) { if (isTypingMessage) {
string symbols = screen->getInputSymbols(); string symbols = screen->getInputSymbols();
for (char s : symbols) { for (char s : symbols) {
if (s == (char)8) { if (s == (char)8) {
message = message.substr(0, message.size() - 1); message = message.substr(0, message.size() - 1);
} }
else { else if (message.length() < ShooterConsts::MAX_MESSAGE_LENGTH && s!=(char)13) {
message += s; message += s;
} }
} }
@ -202,11 +203,15 @@ void Shooter::update() {
} }
void Shooter::drawChat() { void Shooter::drawChat() {
sf::Color chatColor = sf::Color(0, 0, 0, chat->update(Time::deltaTime())); sf::Color chatColor;
string chatText = chat->getChat(); string chatText;
screen->drawText(chatText, Vec2D{ 0, (double)screen->height()/2 }, 20, chatColor); if (!isTypingMessage) { chatColor = sf::Color(0, 0, 0, chat->update(Time::deltaTime())); chatText = chat->getChatPreview(); }
if (isTypingMessage) { else { chatColor = sf::Color(60, 60, 60, 200); chatText = chat->getChat();}
screen->drawText(message, Vec2D{ (double)screen->width()/4, (double)screen->height() / 1.5 }, 40, sf::Color(0, 0, 0, 255));
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));
} }
} }

View File

@ -15,6 +15,7 @@ namespace ShooterConsts {
const double SLOW_MO_COEFFICIENT = 5; const double SLOW_MO_COEFFICIENT = 5;
const double FIRE_DISTANCE = 1000; const double FIRE_DISTANCE = 1000;
const double BONUS_RECHARGE_TIME = 30; const double BONUS_RECHARGE_TIME = 30;
const int MAX_MESSAGE_LENGTH = 70;
const std::string PLAYER_NAME = "Player"; const std::string PLAYER_NAME = "Player";
const std::string PROJECT_NAME = "Shooter"; const std::string PROJECT_NAME = "Shooter";

View File

@ -6,28 +6,34 @@ void ChatManager::addNewMessage(std::string author, std::string message) {
messages.push_back(message); messages.push_back(message);
authors.push_back(author); authors.push_back(author);
isChatUpdate = true; isChatUpdate = true;
if (messages.size() > 6) { if (messages.size() > 20) {
messages.erase(messages.begin()); messages.erase(messages.begin());
} }
} }
int ChatManager::update(double delta) { int ChatManager::update(double delta) {
hide = std::max(hide-delta, 0.0); hide = std::max(hide-delta, 0.0);
if (hide < 0.0000001) {
messages.clear();
authors.clear();
}
return std::min((int)(hide * 255.0), 255); return std::min((int)(hide * 255.0), 255);
} }
std::string ChatManager::getChat() { std::string ChatManager::getChat() {
updateChat(); return chatStr;
}
std::string ChatManager::getChatPreview() {
updateChat(); return chatStrPrev;
}
void ChatManager::updateChat() {
if (isChatUpdate) { if (isChatUpdate) {
isChatUpdate = false; isChatUpdate = false;
int size = messages.size(); int size = messages.size();
chatStr = ""; 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"; chatStr += authors[messageIndex] + ": " + messages[messageIndex] + ";\n";
} }
} }
return chatStr;
} }

View File

@ -11,10 +11,13 @@ private:
std::vector<std::string> authors; std::vector<std::string> authors;
bool isChatUpdate = true; bool isChatUpdate = true;
std::string chatStr = ""; std::string chatStr = "";
std::string chatStrPrev = "";
double hide = 0.0; double hide = 0.0;
void updateChat();
public: public:
void addNewMessage(std::string author, std::string message); void addNewMessage(std::string author, std::string message);
int update(double delta); int update(double delta);
std::string getChat(); std::string getChat();
std::string getChatPreview();
}; };
#endif #endif

View File

@ -121,6 +121,9 @@ void ShooterClient::processDisconnect(sf::Uint16 targetId) {
} }
void ShooterClient::sendMessage(string message){ void ShooterClient::sendMessage(string message){
if (message.length() == 0)
return;
chatManager->addNewMessage(_player->playerNickName(), message); chatManager->addNewMessage(_player->playerNickName(), message);
sf::Packet packet; sf::Packet packet;
packet << MsgType::Custom << ShooterMsgType::newMessage << message; packet << MsgType::Custom << ShooterMsgType::newMessage << message;

View File

@ -146,6 +146,8 @@ void ShooterServer::processCustomPacket(sf::Packet &packet, sf::Uint16 senderId)
packet >> message; packet >> message;
sendPacket << MsgType::Custom << ShooterMsgType::newMessage << _players[senderId]->playerNickName() << message; sendPacket << MsgType::Custom << ShooterMsgType::newMessage << _players[senderId]->playerNickName() << message;
if (message.length() == 0)
break;
for (auto& player : _players) { for (auto& player : _players) {
if (player.first != senderId) { if (player.first != senderId) {
_socket.send(sendPacket, player.first); _socket.send(sendPacket, player.first);