chat test 0.5
parent
07cab50b6e
commit
fcee9637aa
|
@ -28,6 +28,8 @@ add_executable(${CMAKE_PROJECT_NAME}
|
||||||
ShooterConsts.h
|
ShooterConsts.h
|
||||||
network/ShooterMsgType.h
|
network/ShooterMsgType.h
|
||||||
network/ShooterMsgType.cpp
|
network/ShooterMsgType.cpp
|
||||||
|
network/Chat.cpp
|
||||||
|
network/Chat.h
|
||||||
# 3d engine:
|
# 3d engine:
|
||||||
engine/Consts.h
|
engine/Consts.h
|
||||||
engine/math/Vec4D.h
|
engine/math/Vec4D.h
|
||||||
|
@ -109,8 +111,7 @@ add_executable(${CMAKE_PROJECT_NAME}
|
||||||
engine/network/UDPConnection.cpp
|
engine/network/UDPConnection.cpp
|
||||||
engine/network/UDPConnection.h
|
engine/network/UDPConnection.h
|
||||||
engine/network/UDPSocket.cpp
|
engine/network/UDPSocket.cpp
|
||||||
engine/network/UDPSocket.h
|
engine/network/UDPSocket.h)
|
||||||
)
|
|
||||||
|
|
||||||
if(APPLE OR UNIX)
|
if(APPLE OR UNIX)
|
||||||
include_directories(/usr/local/include)
|
include_directories(/usr/local/include)
|
||||||
|
|
37
Shooter.cpp
37
Shooter.cpp
|
@ -8,9 +8,8 @@
|
||||||
#include "engine/animation/Animations.h"
|
#include "engine/animation/Animations.h"
|
||||||
#include "ShooterConsts.h"
|
#include "ShooterConsts.h"
|
||||||
#include "engine/io/SoundController.h"
|
#include "engine/io/SoundController.h"
|
||||||
|
#include "network/Chat.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// Read server/client settings and start both.
|
// Read server/client settings and start both.
|
||||||
// If client doesn't connect to the localhost - server doesn't start.
|
// If client doesn't connect to the localhost - server doesn't start.
|
||||||
void Shooter::initNetwork() {
|
void Shooter::initNetwork() {
|
||||||
|
@ -64,6 +63,7 @@ void Shooter::initNetwork() {
|
||||||
client->setRemoveBonusCallBack([this](const ObjectNameTag &bonusName) { removeBonus(bonusName); });
|
client->setRemoveBonusCallBack([this](const ObjectNameTag &bonusName) { removeBonus(bonusName); });
|
||||||
client->setChangeEnemyWeaponCallBack(
|
client->setChangeEnemyWeaponCallBack(
|
||||||
[this](const std::string &weaponName, sf::Uint16 id) { changeEnemyWeapon(weaponName, id); });
|
[this](const std::string &weaponName, sf::Uint16 id) { changeEnemyWeapon(weaponName, id); });
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shooter::start() {
|
void Shooter::start() {
|
||||||
|
@ -128,11 +128,11 @@ void Shooter::start() {
|
||||||
server->stop();
|
server->stop();
|
||||||
this->exit();
|
this->exit();
|
||||||
}, "Exit", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255});
|
}, "Exit", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255});
|
||||||
|
client->setChatManager(chat);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shooter::update() {
|
void Shooter::update() {
|
||||||
// This code executed every time step:
|
// This code executed every time step:
|
||||||
|
|
||||||
server->update();
|
server->update();
|
||||||
client->update();
|
client->update();
|
||||||
|
|
||||||
|
@ -162,9 +162,31 @@ void Shooter::update() {
|
||||||
screen->stopRender();
|
screen->stopRender();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (keyboard->isKeyTapped(sf::Keyboard::Enter)) {
|
||||||
|
if (isTypingMessage) {
|
||||||
|
client->sendMessage(message);
|
||||||
|
message = "";
|
||||||
|
}
|
||||||
|
isTypingMessage = !isTypingMessage;
|
||||||
|
}
|
||||||
|
|
||||||
if (inGame) {
|
if (inGame) {
|
||||||
screen->setTitle(ShooterConsts::PROJECT_NAME);
|
screen->setTitle(ShooterConsts::PROJECT_NAME);
|
||||||
playerController->update();
|
if (isTypingMessage) {
|
||||||
|
string msg;
|
||||||
|
cin >> msg;
|
||||||
|
message += msg;
|
||||||
|
|
||||||
|
client->sendMessage(message);
|
||||||
|
message = "";
|
||||||
|
isTypingMessage = false;
|
||||||
|
|
||||||
|
Log::log(chat->getChat());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
playerController->update();
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
mainMenu.update();
|
mainMenu.update();
|
||||||
}
|
}
|
||||||
|
@ -175,6 +197,13 @@ void Shooter::update() {
|
||||||
if (SoundController::getStatus(SoundTag("background")) != sf::Sound::Status::Playing) {
|
if (SoundController::getStatus(SoundTag("background")) != sf::Sound::Status::Playing) {
|
||||||
SoundController::loadAndPlay(SoundTag("background"), ShooterConsts::BACK_NOISE);
|
SoundController::loadAndPlay(SoundTag("background"), ShooterConsts::BACK_NOISE);
|
||||||
}
|
}
|
||||||
|
drawChat();
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shooter::gui() {
|
void Shooter::gui() {
|
||||||
|
|
|
@ -26,6 +26,9 @@ private:
|
||||||
|
|
||||||
std::shared_ptr<ShooterServer> server = std::make_shared<ShooterServer>();
|
std::shared_ptr<ShooterServer> server = std::make_shared<ShooterServer>();
|
||||||
std::shared_ptr<ShooterClient> client = std::make_shared<ShooterClient>(player);
|
std::shared_ptr<ShooterClient> client = std::make_shared<ShooterClient>(player);
|
||||||
|
std::shared_ptr<ChatManager> chat = std::make_shared<ChatManager>();
|
||||||
|
bool isTypingMessage = false;
|
||||||
|
string message = "";
|
||||||
|
|
||||||
bool inGame = false;
|
bool inGame = false;
|
||||||
int fireTraces = 0;
|
int fireTraces = 0;
|
||||||
|
@ -33,6 +36,7 @@ private:
|
||||||
void start() override;
|
void start() override;
|
||||||
void update() override;
|
void update() override;
|
||||||
void gui() override;
|
void gui() override;
|
||||||
|
void drawChat();
|
||||||
void play();
|
void play();
|
||||||
void drawPlayerStats();
|
void drawPlayerStats();
|
||||||
void drawStatsTable();
|
void drawStatsTable();
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
#include "Chat.h"
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
void ChatManager::addNewMessage(std::string author, std::string message) {
|
||||||
|
hide = 3.0;
|
||||||
|
messages.push_back(message);
|
||||||
|
authors.push_back(author);
|
||||||
|
isChatUpdate = true;
|
||||||
|
}
|
||||||
|
int ChatManager::update(double delta) {
|
||||||
|
hide = std::max(hide-delta, 0.0);
|
||||||
|
return std::min((int)(hide * 255.0), 255);
|
||||||
|
|
||||||
|
}
|
||||||
|
std::string ChatManager::getChat() {
|
||||||
|
if (isChatUpdate) {
|
||||||
|
isChatUpdate = false;
|
||||||
|
int size = messages.size();
|
||||||
|
chatStr = "";
|
||||||
|
for (int messageIndex = messages.size()-1; messageIndex > 0 && chatStr.size() < 100; messageIndex--)
|
||||||
|
{
|
||||||
|
chatStr += authors[messageIndex] + ": " + messages[messageIndex] + ";\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return chatStr;
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
//#ifndef SHOOTER_SHOOTERSERVER_H
|
||||||
|
#ifndef CHAT_H
|
||||||
|
#define CHAT_H
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class ChatManager final {
|
||||||
|
private:
|
||||||
|
std::vector<std::string> messages;
|
||||||
|
std::vector<std::string> authors;
|
||||||
|
bool isChatUpdate = false;
|
||||||
|
std::string chatStr = "A: b\nC: D";
|
||||||
|
double hide = 0.0;
|
||||||
|
public:
|
||||||
|
void addNewMessage(std::string author, std::string message);
|
||||||
|
int update(double delta);
|
||||||
|
std::string getChat();
|
||||||
|
};
|
||||||
|
#endif
|
|
@ -27,7 +27,6 @@ void ShooterClient::processInit(sf::Packet &packet) {
|
||||||
if (_spawnPlayerCallBack != nullptr) {
|
if (_spawnPlayerCallBack != nullptr) {
|
||||||
_spawnPlayerCallBack(targetId);
|
_spawnPlayerCallBack(targetId);
|
||||||
}
|
}
|
||||||
|
|
||||||
_players[targetId]->translateToPoint(Vec3D{x, y, z});
|
_players[targetId]->translateToPoint(Vec3D{x, y, z});
|
||||||
_players[targetId]->setHealth(health);
|
_players[targetId]->setHealth(health);
|
||||||
_players[targetId]->setKills(kills);
|
_players[targetId]->setKills(kills);
|
||||||
|
@ -121,6 +120,15 @@ void ShooterClient::processDisconnect(sf::Uint16 targetId) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ShooterClient::sendMessage(string message){
|
||||||
|
chatManager->addNewMessage(_player->playerNickName(), message);
|
||||||
|
sf::Packet packet;
|
||||||
|
packet << MsgType::Custom << ShooterMsgType::newMessage << message;
|
||||||
|
_socket.send(packet, _socket.serverId());
|
||||||
|
}
|
||||||
|
void ShooterClient::newMessage(string message, string name) {
|
||||||
|
chatManager->addNewMessage(name, message);
|
||||||
|
}
|
||||||
|
|
||||||
void ShooterClient::processCustomPacket(sf::Packet &packet) {
|
void ShooterClient::processCustomPacket(sf::Packet &packet) {
|
||||||
sf::Uint16 buffId[2];
|
sf::Uint16 buffId[2];
|
||||||
|
@ -129,6 +137,7 @@ void ShooterClient::processCustomPacket(sf::Packet &packet) {
|
||||||
|
|
||||||
ShooterMsgType type;
|
ShooterMsgType type;
|
||||||
packet >> type;
|
packet >> type;
|
||||||
|
string name, message;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case ShooterMsgType::Kill:
|
case ShooterMsgType::Kill:
|
||||||
|
@ -225,6 +234,11 @@ void ShooterClient::processCustomPacket(sf::Packet &packet) {
|
||||||
_changeEnemyWeaponCallBack(tmp, buffId[0]);
|
_changeEnemyWeaponCallBack(tmp, buffId[0]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case ShooterMsgType::newMessage:
|
||||||
|
|
||||||
|
packet >> name >> message;
|
||||||
|
newMessage(name, message);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
Log::log("ShooterClient::processCustomPacket: unknown message type " +
|
Log::log("ShooterClient::processCustomPacket: unknown message type " +
|
||||||
std::to_string(static_cast<int>(type)));
|
std::to_string(static_cast<int>(type)));
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include "../engine/network/ClientUDP.h"
|
#include "../engine/network/ClientUDP.h"
|
||||||
#include "../player/Player.h"
|
#include "../player/Player.h"
|
||||||
|
#include "Chat.h"
|
||||||
|
|
||||||
class ShooterClient final : public ClientUDP {
|
class ShooterClient final : public ClientUDP {
|
||||||
private:
|
private:
|
||||||
|
@ -21,9 +22,15 @@ private:
|
||||||
std::function<void(const std::string &, const Vec3D &)> _addBonusCallBack;
|
std::function<void(const std::string &, const Vec3D &)> _addBonusCallBack;
|
||||||
std::function<void(const ObjectNameTag &)> _removeBonusCallBack;
|
std::function<void(const ObjectNameTag &)> _removeBonusCallBack;
|
||||||
std::function<void(const std::string &, sf::Uint16)> _changeEnemyWeaponCallBack;
|
std::function<void(const std::string &, sf::Uint16)> _changeEnemyWeaponCallBack;
|
||||||
|
|
||||||
|
std::shared_ptr<ChatManager> chatManager;
|
||||||
public:
|
public:
|
||||||
explicit ShooterClient(std::shared_ptr<Player> player) : _player(player) {};
|
explicit ShooterClient(std::shared_ptr<Player> player) : _player(player) {};
|
||||||
|
|
||||||
|
void sendMessage(std::string message);
|
||||||
|
|
||||||
|
void newMessage(std::string message, std::string name);
|
||||||
|
|
||||||
void updatePacket() override;
|
void updatePacket() override;
|
||||||
|
|
||||||
void setSpawnPlayerCallBack(std::function<void(sf::Uint16)> spawn);
|
void setSpawnPlayerCallBack(std::function<void(sf::Uint16)> spawn);
|
||||||
|
@ -58,6 +65,8 @@ public:
|
||||||
|
|
||||||
void changeWeapon(const std::string &weaponName);
|
void changeWeapon(const std::string &weaponName);
|
||||||
|
|
||||||
|
void setChatManager(std::shared_ptr<ChatManager> chat) { chatManager = chat; };
|
||||||
|
|
||||||
void addPlayer(sf::Uint16 id, std::shared_ptr<Player> player);
|
void addPlayer(sf::Uint16 id, std::shared_ptr<Player> player);
|
||||||
|
|
||||||
[[nodiscard]] std::map<sf::Uint16, std::shared_ptr<Player>> const &players() const { return _players; }
|
[[nodiscard]] std::map<sf::Uint16, std::shared_ptr<Player>> const &players() const { return _players; }
|
||||||
|
|
|
@ -14,7 +14,8 @@ enum class ShooterMsgType {
|
||||||
InitBonuses,
|
InitBonuses,
|
||||||
AddBonus,
|
AddBonus,
|
||||||
RemoveBonus,
|
RemoveBonus,
|
||||||
ChangeWeapon
|
ChangeWeapon,
|
||||||
|
newMessage
|
||||||
};
|
};
|
||||||
|
|
||||||
sf::Packet &operator<<(sf::Packet &packet, ShooterMsgType type);
|
sf::Packet &operator<<(sf::Packet &packet, ShooterMsgType type);
|
||||||
|
|
|
@ -79,6 +79,7 @@ void ShooterServer::processCustomPacket(sf::Packet &packet, sf::Uint16 senderId)
|
||||||
double damage;
|
double damage;
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
double newHealth;
|
double newHealth;
|
||||||
|
std::string message;
|
||||||
|
|
||||||
ShooterMsgType type;
|
ShooterMsgType type;
|
||||||
packet >> type;
|
packet >> type;
|
||||||
|
@ -140,6 +141,16 @@ void ShooterServer::processCustomPacket(sf::Packet &packet, sf::Uint16 senderId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case ShooterMsgType::newMessage:
|
||||||
|
|
||||||
|
packet >> message;
|
||||||
|
sendPacket << MsgType::Custom << ShooterMsgType::ChangeWeapon << _players[senderId]->playerNickName() << message;
|
||||||
|
for (auto& player : _players) {
|
||||||
|
if (player.first != senderId) {
|
||||||
|
_socket.send(sendPacket, player.first);
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Log::log("ShooterServer::processCustomPacket: unknown message type " +
|
Log::log("ShooterServer::processCustomPacket: unknown message type " +
|
||||||
|
|
Loading…
Reference in New Issue