Compare commits

..

10 Commits

Author SHA1 Message Date
mduval76
09979f1e72 Push connexion multiplayer. 2023-12-09 00:00:52 -05:00
mduval76
c88143c2bc Push inputs username et server address fonctionnels. 2023-12-08 23:42:01 -05:00
mduval76
ff10207257 Push backspace fonctionnel 2023-12-08 23:06:33 -05:00
mduval76
06cd890506 Push input rejette touches non valides. 2023-12-08 22:34:17 -05:00
mduval76
850f5a59d8 Push string accumule comme il faut 2023-12-08 22:15:10 -05:00
mduval76
9562dc00b8 Push SimulateKeyboard différencie MAJ et MIN 2023-12-08 21:45:19 -05:00
mduval76
d6b1870b5b Push fake keyboard - Simulatekeyboard 2023-12-08 19:55:59 -05:00
mduval76
721fef57e2 Push lobby labels. 2023-12-07 16:31:43 -05:00
mduval76
c66e49ab0c Push de quoi 2023-12-07 13:55:44 -05:00
mduval76
63d8a62429 Push tin toé 2023-12-06 13:14:51 -05:00
8 changed files with 469 additions and 266 deletions

View File

@@ -1,83 +1,55 @@
#include "chunk.h"
#include "world.h"
#include <random>
Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y) {
//std::ostringstream pos; // V<>rifie l'existence d'un fichier .chunk avec sa position.
//std::ostringstream pos; // V<>rifie l'existence d'un fichier .chunk avec sa position.
//pos << CHUNK_PATH << x << '_' << y << ".chunk";
//std::ifstream input(pos.str(), std::fstream::binary);
//if (input.fail()) {
OpenSimplexNoise::Noise simplex = OpenSimplexNoise::Noise(seed);
m_blocks.Reset(BTYPE_AIR);
int ratio = 0;
ratio = x * y % 7;
m_blocks.Reset(BTYPE_AIR);
#pragma region Montagnes et Grass des montagnes
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix)
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // Montagnes
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
float xnoiz, ynoiz;
xnoiz = (double)(ix + x * CHUNK_SIZE_X) / 4796.;
ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / 4796.;
xnoiz = (double)(ix + x * CHUNK_SIZE_X) / 4096.;
ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / 4096.;
double height = 0;
for (int x = 0; x < 39; ++x) {
height += simplex.eval(xnoiz, ynoiz);
height *= .79;
xnoiz *= 1.1305;
ynoiz *= 1.1305;
xnoiz *= 1.139;
ynoiz *= 1.139;
}
height = height * 2000. * simplex.eval((double)(ix + x * CHUNK_SIZE_X) / 512., (double)(iz + y * CHUNK_SIZE_Z) / 512.);
height /= (CHUNK_SIZE_Y / 1.9);
height += 15.;
for (int iy = 0; iy <= (int)height % CHUNK_SIZE_Y; ++iy)
SetBlock(ix, iy, iz, BTYPE_METAL, nullptr);
}
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // Collines
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
float xnoiz, ynoiz;
xnoiz = (double)(ix + x * CHUNK_SIZE_X) / 512.;
ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / 512.;
float height = simplex.eval(xnoiz, ynoiz) * 50.f;// +1.f;
for (int iy = 0; iy <= (int)height % CHUNK_SIZE_Y; ++iy) {
if (iy < 20)
{
//std::cout << "" << ynoiz << std::endl;
SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr);
}
else if (iy == 20 || iy == 21) {
double fractionalPart = ynoiz - static_cast<int>(ynoiz);
if (iy == 20) {
if (fractionalPart < 0.3) {
SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr);
}
else {
SetBlock(ix, iy, iz, BTYPE_METAL, nullptr);
if (GetBlock(ix, iy, iz) == BTYPE_AIR)
SetBlock(ix, iy, iz, BTYPE_GREENGRASS, nullptr);
}
}
else if (iy == 21) {
if (fractionalPart < 0.6) {
SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr);
}
else {
SetBlock(ix, iy, iz, BTYPE_METAL, nullptr);
}
}
}
else
{
SetBlock(ix, iy, iz, BTYPE_METAL, nullptr);
}
}
}
#pragma endregion
#pragma region Lacs
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix)
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // "Lacs"
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
for (int iy = 0; iy < 13; ++iy) {
if (iy < 5 && GetBlock(ix, iy, iz) == BTYPE_AIR) {
SetBlock(ix, iy, iz, BTYPE_ICE, nullptr);
}
else if (iy >= 5 && GetBlock(ix, iy, iz) == BTYPE_AIR) {
if (GetBlock(ix, iy, iz) == BTYPE_AIR)
SetBlock(ix, iy, iz, BTYPE_ICE, nullptr);
}
}
}
#pragma endregion
//int rnd = rand() % 15;
if (ratio == 1)
@@ -109,146 +81,38 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y
}
//for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // "Arbres"
// for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
// float xnoiz, ynoiz;
// xnoiz = (double)(iz * CHUNK_SIZE_Y + x * CHUNK_SIZE_X) / 256.;
// ynoiz = (double)(ix * CHUNK_SIZE_Y + y * CHUNK_SIZE_Z) / 256.;
// bool tree = (int)(abs(simplex.eval(xnoiz, ynoiz)) * 17933.f) % CHUNK_SIZE_Y > 126 ? true : false;
// for (int iy = 0; iy < CHUNK_SIZE_Y - 10; ++iy)
// if (GetBlock(ix, iy, iz) == BTYPE_AIR)
// if (GetBlock(ix, iy - 1, iz) == BTYPE_GRASS)
// if (tree) {
// for (int i = 0; i < (int)(abs(simplex.eval(xnoiz, ynoiz) * 4)) % 42 + 1; ++i)
// SetBlock(ix, iy + i, iz, BTYPE_DIRT, nullptr);
// break;
// }
// }
/* }
else {
input.seekg(0, std::ios_base::end);
int size = input.tellg();
input.seekg(0, std::ios_base::beg);
char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
input.read(data, size);
input.close();
#pragma region Arbre
double valeurRnd = 0;
int treeheight = 10;
int lastTreeX = -1;
int lastTreeZ = -1;
int minDistanceBetweenTrees = 10; // Définir la distance minimale entre les arbres
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) {
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
if (GetBlock(ix, 0, iz) != BTYPE_ICE) {
float xnoiz = (double)(ix + x * CHUNK_SIZE_X) / 4796.;
float ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / 4796.;
double height = 0;
for (int i = 0; i < 39; ++i) {
height += simplex.eval(xnoiz, ynoiz);
height *= .79;
xnoiz *= 1.1305;
ynoiz *= 1.1305;
}
height = height * 2000. * simplex.eval((double)(ix + x * CHUNK_SIZE_X) / 512., (double)(iz + y * CHUNK_SIZE_Z) / 512.);
height /= (CHUNK_SIZE_Y / 1.9);
height += 15.;
if (GetBlock(ix, (int)height, iz) == BTYPE_GRASS || (GetBlock(ix, (int)height, iz) == BTYPE_METAL)) {
valeurRnd = simplex.eval(xnoiz, ynoiz);
int distanceThreshold = 20;
// Vérifie si l'emplacement n'est pas à l'intérieur des lacs
bool isInsideLake = false;
for (int iy = 0; iy < 13; ++iy) {
if (GetBlock(ix, iy, iz) == BTYPE_ICE) {
isInsideLake = true;
break;
}
}
if (!isInsideLake && ((valeurRnd > -0.4 && valeurRnd < -0.38) || (valeurRnd > -0.35 && valeurRnd < -0.31)
|| (valeurRnd > 0.3 && valeurRnd < 0.32) || (valeurRnd > 0.37 && valeurRnd < 0.39))
) {
if (lastTreeX == -1 || abs(ix - lastTreeX) > minDistanceBetweenTrees || abs(iz - lastTreeZ) > minDistanceBetweenTrees) {
if (valeurRnd < 0.1)
treeheight = 10;
else {
treeheight = valeurRnd * 20;
if (treeheight < 5)
treeheight = 5;
}
PlaceTree(ix, height, iz, treeheight);
lastTreeX = ix;
lastTreeZ = iz;
}
}
}
}
}
}
#pragma endregion
//else {
// input.seekg(0, std::ios_base::end);
// int size = input.tellg();
// input.seekg(0, std::ios_base::beg);
// char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
// input.read(data, size);
// input.close();
// for (int ix = 0; ix < CHUNK_SIZE_X; ++ix)
// for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz)
// for (int iy = 0; iy < CHUNK_SIZE_Y; ++iy)
// m_blocks.Set(ix, iy, iz, data[ix + (iz * CHUNK_SIZE_X) + (iy * CHUNK_SIZE_Z * CHUNK_SIZE_X)]);
//}*/
//for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // Collines
// for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
// float xnoiz, ynoiz;
// xnoiz = (double)(ix + x * CHUNK_SIZE_X) / 512.;
// ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / 512.;
// float height = simplex.eval(xnoiz, ynoiz) * 50.f;// +1.f;
// for (int iy = 0; iy <= (int)height % CHUNK_SIZE_Y; ++iy) {
// if (iy < 10 && GetBlock(ix, iy, iz) == BTYPE_AIR) {
// SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); // Collines
// }
// else if (iy >= 10 && GetBlock(ix, iy, iz) == BTYPE_AIR) {
// SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); // Grass des collines
// }
// }
// }
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix)
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz)
for (int iy = 0; iy < CHUNK_SIZE_Y; ++iy)
m_blocks.Set(ix, iy, iz, data[ix + (iz * CHUNK_SIZE_X) + (iy * CHUNK_SIZE_Z * CHUNK_SIZE_X)]);
}*/
}
void Chunk::PlaceTree(int x, int y, int z, int height) {
// Vérifie si les coordonnées x, y, z sont dans les limites du chunk
if (x < 0 || x >= CHUNK_SIZE_X || y < 0 || y >= CHUNK_SIZE_Y || z < 0 || z >= CHUNK_SIZE_Z) {
// Coordonnées hors limites du chunk, sortie anticipée pour éviter tout accès non valide
return;
}
// Place la tige de l'arbre
for (int iy = 0; iy < height + 1; ++iy) {
if (y + iy < CHUNK_SIZE_Y) { // Vérifie si la hauteur est à l'intérieur des limites du chunk
SetBlock(x, y + iy, z, BTYPE_DIRT, nullptr);
}
}
// Place les feuilles de l'arbre
int foliageHeight = height / 2;
for (int dy = 0; dy < foliageHeight; ++dy) {
for (int dx = -4; dx <= 4; ++dx) {
for (int dz = -4; dz <= 4; ++dz) {
// Vérifie que les coordonnées se trouvent à l'intérieur des limites du chunk
if (x + dx >= 0 && x + dx < CHUNK_SIZE_X && y + height + dy >= 0 && y + height + dy < CHUNK_SIZE_Y &&
z + dz >= 0 && z + dz < CHUNK_SIZE_Z) {
// Vérifie si le bloc est à une distance acceptable du centre des feuilles pour les placer
double distanceSquared = dx * dx + dy * dy + dz * dz;
if (distanceSquared < 20) {
SetBlock(x + dx, y + height + dy, z + dz, BTYPE_GREENGRASS, nullptr);
}
}
}
}
}
}
Chunk::~Chunk() {
/*if (m_isModified) {
char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
@@ -275,7 +139,7 @@ void Chunk::RemoveBlock(int x, int y, int z, World* world) {
void Chunk::SetBlock(int x, int y, int z, BlockType type, World* world) {
m_blocks.Set(x, y, z, type);
if (world) CheckNeighbors(x, z, world); // Si nullptr, ne pas v<>rifier les chunks voisines.
if (world) CheckNeighbors(x, z, world); // Si nullptr, ne pas v<>rifier les chunks voisines.
m_isDirty = true;
}
@@ -303,10 +167,6 @@ void Chunk::CheckNeighbors(unsigned int x, unsigned int z, World* world) {
void Chunk::GetPosition(unsigned int& x, unsigned int& y) const { x = m_posX; y = m_posY; }
bool Chunk::IsDirty() const { return m_isDirty; }
void Chunk::MakeDirty() { m_isDirty = true; }
@@ -318,8 +178,28 @@ void Chunk::MakeModified() { m_isModified = true; }
void Chunk::Structure(int x, int y, int z,int height)
{
for (int i = 0; i < height; i++)
{
SetBlock(x, i + y, z, BTYPE_GRASS, nullptr);
}
}

View File

@@ -27,7 +27,6 @@ class Chunk {
BlockType GetBlock(int x, int y, int z);
void CheckNeighbors(unsigned int x, unsigned int z, World* world);
void GetPosition(unsigned int& x, unsigned int& y) const;
void PlaceTree(int x, int y, int z, int height);
void Structure(int x, int y, int z, int height);
bool IsDirty() const;

View File

@@ -39,6 +39,7 @@
enum GameState {
MAIN_MENU,
SPLASH,
LOBBY,
OPTIONS,
QUIT,
PLAY,

View File

@@ -70,61 +70,12 @@ void Engine::Init() {
m_whoosh[x] = 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);
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;
}
delete[] ch;
m_world.SetSeed(seed);
// Init Chunks
m_world.GetChunks().Reset(nullptr);
m_startTime = std::chrono::high_resolution_clock::now();
m_remotePlayer.SetPosition(Vector3f(.5, CHUNK_SIZE_Y + 10., .5));
}
void Engine::DeInit() {}
@@ -136,8 +87,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);
@@ -155,6 +107,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);
@@ -168,8 +123,8 @@ void Engine::LoadResource() {
LoadTexture(m_textureMenuQuit, TEXTURE_PATH "menus/buttons/main/mainQuit.png", false);
LoadTexture(m_textureMenuSingle, TEXTURE_PATH "menus/buttons/main/mainSingle.png", false);
TextureAtlas::TextureIndex texDirtIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "metal2.png");
TextureAtlas::TextureIndex texIceIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "metal3.png");
TextureAtlas::TextureIndex texDirtIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "metal3.png");
TextureAtlas::TextureIndex texIceIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "metal2.png");
TextureAtlas::TextureIndex texGrassIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "grass.png");
TextureAtlas::TextureIndex texMetalIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "dirt.png");
TextureAtlas::TextureIndex texGreenGrassIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "greengrass.png");
@@ -550,14 +505,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
@@ -624,6 +575,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;
@@ -709,15 +679,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, 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() {
@@ -1155,16 +1191,19 @@ 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 float gameTime = elapsedTime;
static irrklang::ISound* step; // Pour les sons de pas.
static float pollTime = 0;
static float bulletTime = 0;
@@ -1450,9 +1489,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) {
@@ -1533,6 +1574,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;
@@ -1604,12 +1651,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));
@@ -1697,7 +1997,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

@@ -45,6 +45,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);
@@ -61,13 +62,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);
@@ -76,9 +80,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);
Connector m_conn;
@@ -122,6 +127,7 @@ private:
Texture m_texturePovGun;
Texture m_textureSkybox;
Texture m_textureLobbyMenu;
Texture m_textureMainMenu;
Texture m_textureOptionsMenu;
Texture m_texturePauseMenu;
@@ -131,6 +137,9 @@ private:
Texture m_textureFhd;
Texture m_textureQhd;
Texture m_textureUhd;
Texture m_textureLobbyServer;
Texture m_textureLobbyIdentify;
Texture m_textureCheck;
Texture m_textureChecked;
@@ -197,6 +206,16 @@ private:
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;
bool m_keyK = false;
@@ -205,7 +224,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