Merge remote-tracking branch 'origin/InputFields' into SQC_mergeFieldsAndOnline

This commit is contained in:
MarcEricMartel 2023-12-09 15:23:44 -05:00
commit 461a8aa11b
6 changed files with 397 additions and 80 deletions

View File

@ -48,8 +48,9 @@
enum GameState {
MAIN_MENU,
SPLASH,
LOBBY,
OPTIONS,
QUIT,
QUIT,
PLAY,
PAUSE
};

View File

@ -47,66 +47,9 @@ void Engine::Init() {
m_whoosh[x] = nullptr;
}
// Init Chunks
m_world.GetChunks().Reset(nullptr);
char* ch = new char[2];
std::cout << "Jouer en ligne? [o/N] ";
std::cin.getline(ch, 2);
std::cout << std::endl;
if (*ch == 'o' || *ch == 'O') {
char* input = new char[32];
std::string playname, srvname;
while (playname.size() < 1) {
std::cout << "Veuillez entrer un nom de joueur: ";
std::cin.getline(input, 32);
std::cout << std::endl;
playname = input;
if (playname.size() < 1 || playname.size() > 32)
std::puts("Nom invalide.");
}
while (srvname.size() < 1) {
std::cout << "Veuillez entrer une adresse de serveur: ";
std::cin.getline(input, 32);
std::cout << std::endl;
srvname = input;
if (srvname.size() < 1 || srvname.size() > 32)
std::puts("Adresse serveur invalide.");
}
delete[] input;
if (!m_conn.Init()) {
if (!m_conn.Connect(srvname.c_str(), playname)) {
// 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);
RemotePlayer* rt = (RemotePlayer*)m_players[key];
rt->SetPosition(Vector3f(555, 555, 555));
}
m_player.m_username = playname;
seed = 9370707;//m_conn.getSeed();
m_networkgame = true;
}
else std::cout << "Erreur de connexion." << std::endl;
}
else std::cout << "Erreur de creation de socket." << std::endl;
}
delete[] ch;
m_world.SetSeed(seed);
m_startTime = std::chrono::high_resolution_clock::now();
// Gestion de souris.
CenterMouse();
HideCursor();
}
void Engine::DeInit() {}
@ -143,8 +86,9 @@ void Engine::LoadResource() {
LoadTexture(m_textureGun, TEXTURE_PATH "gun01.png", false);
LoadTexture(m_texturePovGun, TEXTURE_PATH "GUN.png", false);
LoadTexture(m_textureLobbyMenu, TEXTURE_PATH "menus/backgrounds/bgLobby.png", false);
LoadTexture(m_textureMainMenu, TEXTURE_PATH "menus/backgrounds/bgMainMenu.png", false);
LoadTexture(m_texturePauseMenu, TEXTURE_PATH "menus/backgrounds/bgPause.png", false);
//LoadTexture(m_texturePauseMenu, TEXTURE_PATH "menus/backgrounds/bgPause.png", false);
LoadTexture(m_textureOptionsMenu, TEXTURE_PATH "menus/backgrounds/bgOptions.png", false);
LoadTexture(m_textureSplashScreen, TEXTURE_PATH "menus/backgrounds/bgSplash.png", false);
@ -162,6 +106,9 @@ void Engine::LoadResource() {
LoadTexture(m_textureOptSensitivity, TEXTURE_PATH "menus/buttons/options/optSensitivity.png", false);
LoadTexture(m_textureOptSfx, TEXTURE_PATH "menus/buttons/options/optSfx.png", false);
LoadTexture(m_textureLobbyServer, TEXTURE_PATH "menus/labels/labelServer.png", false);
LoadTexture(m_textureLobbyIdentify, TEXTURE_PATH "menus/labels/labelIdentify.png", false);
LoadTexture(m_textureHd, TEXTURE_PATH "menus/labels/labelHd.png", false);
LoadTexture(m_textureHd, TEXTURE_PATH "menus/labels/labelHd.png", false);
LoadTexture(m_textureFhd, TEXTURE_PATH "menus/labels/labelFhd.png", false);
LoadTexture(m_textureQhd, TEXTURE_PATH "menus/labels/labelQhd.png", false);
@ -602,14 +549,10 @@ void Engine::DisplayPovGun() {
glLoadIdentity();
glTranslated(xTranslation, 0, 0);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(0, 0);
glTexCoord2f(1, 0);
glVertex2i(quadWidth, 0);
glTexCoord2f(1, 1);
glVertex2i(quadWidth, quadHeight);
glTexCoord2f(0, 1);
glVertex2i(0, quadHeight);
glTexCoord2f(0, 0); glVertex2i(0, 0);
glTexCoord2f(1, 0); glVertex2i(quadWidth, 0);
glTexCoord2f(1, 1); glVertex2i(quadWidth, quadHeight);
glTexCoord2f(0, 1); glVertex2i(0, quadHeight);
glEnd();
// Reset du blend function
@ -666,6 +609,25 @@ int Engine::GetOptionsChoice() {
return m_selectedOption;
}
void Engine::StartMultiplayerGame() {
if (!m_conn.Init()) {
if (!m_conn.Connect(m_serverAddr.c_str(), m_username)) {
// 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;
}
else std::cout << "Erreur de connexion." << std::endl;
}
else std::cout << "Erreur de creation de socket." << std::endl;
}
void Engine::DisplayInfo(float elapsedTime, BlockType bloc) {
m_textureFont.Bind();
std::ostringstream ss;
@ -751,15 +713,81 @@ void Engine::DisplaySplashScreen() {
glPopMatrix();
}
void Engine::DisplayPauseMenu() {
void Engine::DisplayLobbyMenu(float elapsedTime) {
m_texturePauseMenu.Bind();
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, Width(), 0, Height(), -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
m_textureLobbyMenu.Bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2i(0, 0);
glTexCoord2f(1, 0); glVertex2i(Width(), 0);
glTexCoord2f(1, 1); glVertex2i(Width(), Height());
glTexCoord2f(0, 0); glVertex2i(0, 0);
glTexCoord2f(1, 0); glVertex2i(Width(), 0);
glTexCoord2f(1, 1); glVertex2i(Width(), Height());
glTexCoord2f(0, 1); glVertex2i(0, Height());
glEnd();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
if (m_settingUsername) {
SetPlayerUsername(elapsedTime);
}
else if (m_settingServer) {
SetServerAddress(elapsedTime);
}
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
void Engine::SetPlayerUsername(float elapsedTime) {
m_textureLobbyIdentify.Bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2i(Width() * 0.6, Height() * 0.75);
glTexCoord2f(1, 0); glVertex2i(Width() * 0.975, Height() * 0.75);
glTexCoord2f(1, 1); glVertex2i(Width() * 0.975, Height() * 0.95);
glTexCoord2f(0, 1); glVertex2i(Width() * 0.6, Height() * 0.95);
glEnd();
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
HandlePlayerInput(elapsedTime);
glDisable(GL_BLEND);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
void Engine::SetServerAddress(float elapsedTime) {
m_textureLobbyServer.Bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2i(Width() * 0.6, Height() * 0.75);
glTexCoord2f(1, 0); glVertex2i(Width() * 0.975, Height() * 0.75);
glTexCoord2f(1, 1); glVertex2i(Width() * 0.975, Height() * 0.95);
glTexCoord2f(0, 1); glVertex2i(Width() * 0.6, Height() * 0.95);
glEnd();
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
HandlePlayerInput(elapsedTime);
glDisable(GL_BLEND);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
void Engine::DisplayMainMenu() {
@ -1197,14 +1225,18 @@ void Engine::Render(float elapsedTime) {
return;
}
if (m_gamestate == GameState::PAUSE) {
DisplayPauseMenu();
if (m_gamestate == GameState::LOBBY) {
DisplayLobbyMenu(elapsedTime);
if (m_multiReady) {
StartMultiplayerGame();
std::cout << "Starting multiplayer game reached" << std::endl;
}
return;
}
if (m_gamestate == GameState::PLAY) {
HideCursor();
CenterMouse(); //D<>placement de centermouse dans l'action de jouer
CenterMouse();
static irrklang::ISound* step; // Pour les sons de pas.
static float pollTime = 0;
@ -1593,9 +1625,11 @@ void Engine::DrawSlider(float centerX, float centerY, float value, float minVal,
}
}
void Engine::KeyPressEvent(unsigned char key) {
if (m_gamestate == LOBBY) {
m_inputChar = SimulateKeyboard(key);
return;
}
switch (key) {
case 0: // A - Gauche
if (!m_keyA) {
@ -1678,6 +1712,12 @@ void Engine::KeyPressEvent(unsigned char key) {
}
void Engine::KeyReleaseEvent(unsigned char key) {
if (m_gamestate == LOBBY) {
if (key == 38) {
m_keyShift = false;
}
return;
}
switch (key) {
case 0: // A - Stop gauche
m_keyA = false;
@ -1749,12 +1789,265 @@ void Engine::KeyReleaseEvent(unsigned char key) {
case 28: // 2
m_key2 = true;
break;
case 38: // Shift
m_keyShift = false;
break;
case 57: // Espace - Stop sauter
m_keySpace = false;
break;
case 58: // Enter - comfirm input
m_keyEnter = false;
break;
case 59: // backspace - remove char
m_keyBackspace = false;
break;
}
}
char Engine::SimulateKeyboard(unsigned char key) {
switch (key) {
case 0: // A
if (!m_keyShift) { m_inputChar = 'a'; }
else { m_inputChar = 'A'; }
break;
case 1: // B
if (!m_keyShift) { m_inputChar = 'b'; }
else { m_inputChar = 'B'; }
break;
case 2: // C
if (!m_keyShift) { m_inputChar = 'c'; }
else { m_inputChar = 'C'; }
break;
case 3: // D
if (!m_keyShift) { m_inputChar = 'd'; }
else { m_inputChar = 'D'; }
break;
case 4: // E
if (!m_keyShift) { m_inputChar = 'e'; }
else { m_inputChar = 'E'; }
break;
case 5: // F
if (!m_keyShift) { m_inputChar = 'f'; }
else { m_inputChar = 'F'; }
break;
case 6: // G
if (!m_keyShift) { m_inputChar = 'g'; }
else { m_inputChar = 'G'; }
break;
case 7: // H
if (!m_keyShift) { m_inputChar = 'h'; }
else { m_inputChar = 'H'; }
break;
case 8: // I
if (!m_keyShift) { m_inputChar = 'i'; }
else { m_inputChar = 'I'; }
break;
case 9: // J
if (!m_keyShift) { m_inputChar = 'j'; }
else { m_inputChar = 'J'; }
break;
case 10: // K
if (!m_keyShift) { m_inputChar = 'k'; }
else { m_inputChar = 'K'; }
break;
case 11: // L
if (!m_keyShift) { m_inputChar = 'l'; }
else { m_inputChar = 'L'; }
break;
case 12: // M
if (!m_keyShift) { m_inputChar = 'm'; }
else { m_inputChar = 'M'; }
break;
case 13: // N
if (!m_keyShift) { m_inputChar = 'n'; }
else { m_inputChar = 'N'; }
break;
case 14: // O
if (!m_keyShift) { m_inputChar = 'o'; }
else { m_inputChar = 'O'; }
break;
case 15: // P
if (!m_keyShift) { m_inputChar = 'p'; }
else { m_inputChar = 'P'; }
break;
case 16: // Q
if (!m_keyShift) { m_inputChar = 'q'; }
else { m_inputChar = 'Q'; }
break;
case 17: // R
if (!m_keyShift) { m_inputChar = 'r'; }
else { m_inputChar = 'R'; }
break;
case 18: // S
if (!m_keyShift) { m_inputChar = 's'; }
else { m_inputChar = 'S'; }
break;
case 19: // T
if (!m_keyShift) { m_inputChar = 't'; }
else { m_inputChar = 'T'; }
break;
case 20: // U
if (!m_keyShift) { m_inputChar = 'u'; }
else { m_inputChar = 'U'; }
break;
case 21: // V
if (!m_keyShift) { m_inputChar = 'v'; }
else { m_inputChar = 'V'; }
break;
case 22: // W
if (!m_keyShift) { m_inputChar = 'w'; }
else { m_inputChar = 'W'; }
break;
case 23: // X
if (!m_keyShift) { m_inputChar = 'x'; }
else { m_inputChar = 'X'; }
break;
case 24: // Y
if (!m_keyShift) { m_inputChar = 'y'; }
else { m_inputChar = 'Y'; }
break;
case 25: // Z
if (!m_keyShift) { m_inputChar = 'z'; }
else { m_inputChar = 'Z'; }
break;
case 26: // 0
if (!m_keyShift) { m_inputChar = '0'; }
else { m_inputChar = ')'; }
break;
case 27: // 1
if (!m_keyShift) { m_inputChar = '1'; }
else { m_inputChar = '!'; }
break;
case 28: // 2
if (!m_keyShift) { m_inputChar = '2'; }
else { m_inputChar = '\"'; }
break;
case 29: // 3
if (!m_keyShift) { m_inputChar = '3'; }
else { m_inputChar = '/'; }
break;
case 30: // 4
if (!m_keyShift) { m_inputChar = '4'; }
else { m_inputChar = '$'; }
break;
case 31: // 5
if (!m_keyShift) { m_inputChar = '5'; }
else { m_inputChar = '%'; }
break;
case 32: // 6
if (!m_keyShift) { m_inputChar = '6'; }
else { m_inputChar = '?'; }
break;
case 33: // 7
if (!m_keyShift) { m_inputChar = '7'; }
else { m_inputChar = '&'; }
break;
case 34: // 8
if (!m_keyShift) { m_inputChar = '8'; }
else { m_inputChar = '*'; }
break;
case 35: // 9
if (!m_keyShift) { m_inputChar = '9'; }
else { m_inputChar = ')'; }
break;
case 38: // SHIFT
m_keyShift = true;
break;
case 50: // . (Period)
m_inputChar = '.';
break;
case 56: // _ (Underscore)
if (!m_keyShift) { m_inputChar = '-'; }
else { m_inputChar = '_'; }
break;
case 57: // SPACE
m_inputChar = ' ';
break;
case 58: // ENTER
m_keyEnter = true;
break;
case 59: // BACKSPACE
m_keyBackspace = true;
break;
default:
m_invalidChar = true;
break;
}
if ((int)key != 38 && !m_invalidChar) {
m_charChanged = true;
std::cout << "Key pressed: " << (int)key << " (" << m_inputChar << ")" << std::endl;
}
m_invalidChar = false;
return m_inputChar;
}
void Engine::HandlePlayerInput(float elapsedTime) {
static float lobbyTime = 0.0f;
lobbyTime += elapsedTime;
float onInterval = 0.5f;
float offInterval = 1.0f;
m_textureFont.Bind();
std::ostringstream ss;
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
if (m_charChanged) {
if (m_keyBackspace) {
if (!m_currentInputString.empty()) {
m_currentInputString.pop_back();
}
m_keyBackspace = false;
}
else if (m_keyEnter) {
if (m_settingUsername) {
if (!m_currentInputString.empty() &&
m_currentInputString.size() > 1 &&
m_currentInputString.size() < 26) {
m_username = m_currentInputString;
m_currentInputString = "";
m_settingUsername = false;
m_settingServer = true;
}
}
else if (m_settingServer) {
if (!m_currentInputString.empty()) {
m_serverAddr = m_currentInputString;
m_currentInputString = "";
m_settingServer = false;
m_multiReady = true;
m_gamestate = GameState::PLAY;
}
}
m_keyEnter = false;
}
else if (m_settingUsername && m_currentInputString.size() < 26) {
m_currentInputString += m_inputChar;
}
else if (m_settingServer && m_currentInputString.size() < 15) {
m_currentInputString += m_inputChar;
}
}
ss << m_currentInputString;
m_charChanged = false;
if (lobbyTime < onInterval) {
ss << "_";
}
else if (lobbyTime > onInterval && lobbyTime < offInterval) {
ss << " ";
}
else {
lobbyTime = 0.0f;
}
PrintText(Width() * 0.6f, Height() * 0.4f, ss.str(), 2.0f);
}
void Engine::MouseMoveEvent(int x, int y) {
if (m_gamestate == GameState::PLAY) {
m_player.TurnLeftRight(x - (Width() / 2));
@ -1842,7 +2135,8 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) {
}
else if (x > leftButton && x < rightButton && y > bottomSecond && y < topSecond) {
if (m_selectedPlayOptions) {
m_gamestate = GameState::PLAY;
m_gamestate = GameState::LOBBY;
m_settingUsername = true;
}
else {
m_gamestate = GameState::OPTIONS;

View File

@ -46,6 +46,7 @@ private:
int GetFps(float elapsedTime) const;
int GetCountdown(float elapsedTime);
int GetOptionsChoice();
void StartMultiplayerGame();
bool LoadTexture(Texture& texture, const std::string& filename, bool useMipmaps = true, bool stopOnError = true);
@ -62,13 +63,16 @@ private:
void DisplayInfo(float elapsedTime, BlockType bloc);
void DisplaySplashScreen();
void DisplayPauseMenu();
void DisplayMainMenu();
void DrawButtonBackgrounds(float centerX, float centerY, int iterations);
void DrawMainMenuButtons(float centerX, float centerY);
void DrawSingleMultiButtons(float centerX, float centerY);
void DisplayLobbyMenu(float elapsedTime);
void SetPlayerUsername(float elapsedTime);
void SetServerAddress(float elapsedTime);
void DisplayOptionsMenu();
void DisplayAudioMenu(float centerX, float centerY);
void DisplayGraphicsMenu(float centerX, float centerY);
@ -77,9 +81,10 @@ private:
void DisplayBarPercentValue(float centerX, float centerY, float posX, float posY, float minVal, float maxVal, float value);
void DrawSlider(float centerX, float centerY, float value, float minVal, float maxVal, float bottomSideValue, float topSideValue);
void PrintText(float x, float y, const std::string& t, float charSizeMultiplier = 1.0f);
void ProcessNotificationQueue();
char SimulateKeyboard(unsigned char key);
void HandlePlayerInput(float elapsedTime);
Audio m_audio = Audio(AUDIO_PATH "start.wav");
irrklang::ISound* m_powpow, * m_scream;
@ -116,6 +121,7 @@ private:
Texture m_texturePovGun;
Texture m_textureSkybox;
Texture m_textureLobbyMenu;
Texture m_textureMainMenu;
Texture m_textureOptionsMenu;
Texture m_texturePauseMenu;
@ -125,6 +131,9 @@ private:
Texture m_textureFhd;
Texture m_textureQhd;
Texture m_textureUhd;
Texture m_textureLobbyServer;
Texture m_textureLobbyIdentify;
Texture m_textureCheck;
Texture m_textureChecked;
@ -190,6 +199,16 @@ private:
bool m_selectedPlayOptions = false;
bool m_selectedOptions = false;
bool m_selectedQuit = false;
std::string m_currentInputString;
std::string m_username;
std::string m_serverAddr;
char m_inputChar = 0;
bool m_invalidChar = false;
bool m_charChanged = false;
bool m_settingUsername = false;
bool m_settingServer = false;
bool m_multiReady = false;
bool m_key1 = false;
bool m_key2 = false;
@ -199,7 +218,10 @@ private:
bool m_keyA = false;
bool m_keyS = false;
bool m_keyD = false;
bool m_keyEnter = false;
bool m_keySpace = false;
bool m_keyShift = false;
bool m_keyBackspace = false;
bool m_mouseL = false;
bool m_mouseR = false;

Binary file not shown.

After

Width:  |  Height:  |  Size: 505 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB