chat in multiplayer 1.0

(use ENTER to write message)
master
CREAsTIVE 2022-07-23 02:47:19 +05:00
parent fcee9637aa
commit 7a01776f87
7 changed files with 60 additions and 40 deletions

View File

@ -140,7 +140,14 @@ void Shooter::update() {
if (!screen->hasFocus()) {
return;
}
if (keyboard->isKeyTapped(sf::Keyboard::Enter)) {
if (isTypingMessage) {
client->sendMessage(message);
message = "";
}
isTypingMessage = !isTypingMessage;
}
if (!isTypingMessage) {
if (keyboard->isKeyTapped(sf::Keyboard::Escape)) {
inGame = !inGame;
screen->setMouseCursorVisible(!inGame);
@ -161,27 +168,20 @@ void Shooter::update() {
if (keyboard->isKeyTapped(sf::Keyboard::L)) {
screen->stopRender();
}
if (keyboard->isKeyTapped(sf::Keyboard::Enter)) {
if (isTypingMessage) {
client->sendMessage(message);
message = "";
}
isTypingMessage = !isTypingMessage;
}
if (inGame) {
screen->setTitle(ShooterConsts::PROJECT_NAME);
if (isTypingMessage) {
string msg;
cin >> msg;
message += msg;
client->sendMessage(message);
message = "";
isTypingMessage = false;
Log::log(chat->getChat());
string symbols = screen->getInputSymbols();
for (char s : symbols) {
if (s == (char)8) {
message = message.substr(0, message.size() - 1);
}
else {
message += s;
}
}
}
else {
playerController->update();
@ -203,7 +203,10 @@ 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, 0 }, 10, chatColor);
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));
}
}
void Shooter::gui() {

View File

@ -27,10 +27,16 @@ void Screen::open(int screenWidth, int screenHeight, const std::string &name, bo
void Screen::display() {
sf::Event event{};
inputSymbols = "";
while (_window->pollEvent(event)) {
if (event.type == sf::Event::Closed) {
_window->close();
}
else if (event.type == sf::Event::TextEntered) {
if (event.text.unicode < 128) {
inputSymbols += static_cast<char>(event.text.unicode);
}
}
}
std::string title = _title + " (" + std::to_string(Time::fps()) + " fps)";
@ -61,7 +67,7 @@ void Screen::stopRender() {
Log::log("Screen::stopRender(): stop recording the screen");
Log::log("Screen::stopRender(): start saving .png sequence");
std::string c = "rm film/png/*.png";
popen(c.c_str(), "w");
_popen(c.c_str(), "w");
int i = 0;
for(; i < _renderSequence.size(); i++) {
_renderSequence[i].copyToImage().saveToFile("film/png/" + std::to_string(i) + ".png");
@ -72,7 +78,7 @@ void Screen::stopRender() {
Log::log("Screen::stopRender(): start rendering final video");
// TODO: .png sequence looks better than a final video (poor clarity and desaturated colors)
c = "ffmpeg -stats -r 60 -i film/png/%d.png -vcodec libx264 -crf 1 -pix_fmt yuv420p -frames " + std::to_string(i) + " film/mp4/" + std::to_string(_scene) + "_" + _title + "_" + std::to_string(rand()) + ".mp4";
popen(c.c_str(), "w");
_popen(c.c_str(), "w");
_scene++;
_renderVideo = false;
Log::log("Screen::stopRender(): finish rendering final video");

View File

@ -27,6 +27,8 @@ private:
sf::Color _background;
std::string inputSymbols;
const std::shared_ptr<sf::RenderWindow> _window = std::make_shared<sf::RenderWindow>();
public:
void open(int screenWidth = Consts::STANDARD_SCREEN_WIDTH, int screenHeight = Consts::STANDARD_SCREEN_HEIGHT,
@ -75,6 +77,8 @@ public:
void startRender();
void stopRender();
std::string getInputSymbols() { return inputSymbols; };
};

View File

@ -2,13 +2,20 @@
#include <string>
#include <iostream>
void ChatManager::addNewMessage(std::string author, std::string message) {
hide = 3.0;
hide = 7.0;
messages.push_back(message);
authors.push_back(author);
isChatUpdate = true;
if (messages.size() > 6) {
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);
}
@ -17,7 +24,7 @@ std::string ChatManager::getChat() {
isChatUpdate = false;
int size = messages.size();
chatStr = "";
for (int messageIndex = messages.size()-1; messageIndex > 0 && chatStr.size() < 100; messageIndex--)
for (int messageIndex = messages.size()-1; messageIndex >= 0; messageIndex--)
{
chatStr += authors[messageIndex] + ": " + messages[messageIndex] + ";\n";
}

View File

@ -9,8 +9,8 @@ class ChatManager final {
private:
std::vector<std::string> messages;
std::vector<std::string> authors;
bool isChatUpdate = false;
std::string chatStr = "A: b\nC: D";
bool isChatUpdate = true;
std::string chatStr = "";
double hide = 0.0;
public:
void addNewMessage(std::string author, std::string message);

View File

@ -237,7 +237,7 @@ void ShooterClient::processCustomPacket(sf::Packet &packet) {
case ShooterMsgType::newMessage:
packet >> name >> message;
newMessage(name, message);
newMessage(message, name);
break;
default:
Log::log("ShooterClient::processCustomPacket: unknown message type " +

View File

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