RemotePlayer

This commit is contained in:
MarcEricMartel 2023-10-27 14:37:53 -04:00
parent 8970bc33a1
commit 61629b3c8e
6 changed files with 30 additions and 11 deletions

View File

@ -180,7 +180,7 @@ void Server::Run() {
Log("Debut de la partie...", false, false);
for (auto& [key, conn]: m_players) { // Creation des instances de joueurs et premier sync.
conn->player = new Player(Vector3f(64., 128., 64.));
conn->player = new Player(Vector3f(32.5f, CHUNK_SIZE_Y + 1.8f, 32.5f));
Player *player = conn->player;
Sync sync;
sync.position = player->GetPosition();

View File

@ -68,9 +68,10 @@ int Connector::Connect(const char* srv_addr, std::string name) {
delete[] buf;
buf = new char[150] {0};
buflen = 150;
int rpack = 0;
bool ready = false;
int errors = 0;
while (rpack < 2) {
netprot::PlayerInfo pinfo{};
while (!ready) {
recv(m_sock_tcp, buf, buflen, 0);
switch (netprot::getType(buf, buflen)) {
@ -79,17 +80,26 @@ int Connector::Connect(const char* srv_addr, std::string name) {
std::cout << "Packet LoginInfo invalide." << std::endl;
return 2;
}
++rpack;
break;
case netprot::PACKET_TYPE::GAMEINFO:
if (!netprot::Deserialize(&m_gameinfo, buf, buflen)) {
std::cout << "Packet GameInfo invalide." << std::endl;
return 3;
}
++rpack;
break;
case netprot::PACKET_TYPE::PLAYINF:
// TODO: Populer un remotePlayer avec ça.
if (!netprot::Deserialize(&pinfo, buf, buflen)) {
std::cout << "Packet PlayerInfo invalide." << std::endl;
return 3;
}
m_players[pinfo.id] = pinfo;
break;
case netprot::PACKET_TYPE::SYNC:
if (!netprot::Deserialize(&m_origin, buf, buflen)) {
std::cout << "Packet Sync invalide." << std::endl;
return 3;
}
ready = true;
break;
default:
++errors;
@ -105,3 +115,5 @@ int Connector::Connect(const char* srv_addr, std::string name) {
uint64_t Connector::getId() const { return m_loginfo.sid; }
unsigned int Connector::getSeed() const { return m_gameinfo.seed; }
netprot::Sync Connector::getOrigin() const { return m_origin; }

View File

@ -14,6 +14,8 @@ public:
int Connect(const char* srv_addr, std::string name);
uint64_t getId() const;
unsigned int getSeed() const;
netprot::Sync getOrigin() const;
//void SendInput();
//int Sync();
@ -24,16 +26,17 @@ public:
// void updateRemotePlayers(std::map<RemotePlayers> rplayers);
std::map<uint64_t, netprot::PlayerInfo> m_players;
private:
#ifdef _WIN32
WSADATA m_wsaData;
#endif
std::map<Timestamp, netprot::Input> m_inputmanifest;
std::map<uint64_t, netprot::PlayerInfo> m_players;
std::map<uint64_t, netprot::TeamInfo> m_teams;
netprot::LoginInfo m_loginfo;
netprot::GameInfo m_gameinfo;
netprot::Sync m_origin;
};
#endif

View File

@ -265,7 +265,6 @@ void Engine::Init() {
m_whoosh[x] = nullptr;
}
if (true) {
std::cout << "Jouer en ligne? [o/N] ";
std::cin >> ch;
std::cout << std::endl;
@ -284,6 +283,11 @@ void Engine::Init() {
// setup jeu en reseau.
std::cout << "ID recu du serveur: " << std::to_string(m_conn.getId()) << "!" << std::endl;
std::cout << "Seed recu du serveur: " << std::to_string(m_conn.getSeed()) << "!" << std::endl;
m_player = Player(m_conn.getOrigin().position);
for (auto& [key, player] : m_conn.m_players)
m_players[key] = new RemotePlayer(player);
seed = m_conn.getSeed();
m_networkgame = true;
}
@ -291,7 +295,6 @@ void Engine::Init() {
}
else std::cout << "Erreur de creation de socket." << std::endl;
}
}
m_world.SetSeed(seed);

View File

@ -3,7 +3,7 @@
#include <cstring>
RemotePlayer::RemotePlayer() : Player(Vector3f(0, 0, 0), 0, 0), m_aminacc(0.0f), m_animstate(Anim::STILL), m_team_id(0), current(), previous() {
RemotePlayer::RemotePlayer(netprot::PlayerInfo pinfo) : Player(Vector3f(0, 0, 0), 0, 0), m_pinfo(pinfo), m_aminacc(0.0f), m_animstate(Anim::STILL), m_team_id(0), current(), previous() {
}

View File

@ -10,13 +10,14 @@ class RemotePlayer : public Player {
public:
enum Anim { STILL = 1, RUNNING = 2, JUMPING = 4, SHOOTING = 8, POWERUP = 16, DEAD = 32 };
RemotePlayer();
RemotePlayer(netprot::PlayerInfo pinfo);
void Init();
void Feed(const netprot::Output out);
private:
netprot::Output current, previous;
netprot::PlayerInfo m_pinfo;
float m_aminacc;
Anim m_animstate;
uint64_t m_team_id;