refactoring and addings limits to chat v1.3
parent
068030ecea
commit
dbd4eb4953
15
Shooter.cpp
15
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);
|
||||
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()/4, (double)screen->height() / 1.5 }, 40, sf::Color(0, 0, 0, 255));
|
||||
screen->drawText(message+"_", Vec2D{(double)screen->width() * 0.05, (double)screen->height() / 1.5}, 30, sf::Color(0, 0, 0, 255));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -11,10 +11,13 @@ private:
|
|||
std::vector<std::string> 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
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue