Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
Merge branch 'SocketManagement-and-engine'
Browse files Browse the repository at this point in the history
  • Loading branch information
P0ulpy committed Feb 21, 2022
2 parents 87ef572 + f54ea55 commit 747401b
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/Engine/Engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ struct EngineConfig
sf::VideoMode videoMode = sf::VideoMode(1200, 900);

std::string title = "Render Window";
unsigned int style = sf::Style::Default;
unsigned int style = sf::Style::Fullscreen;
unsigned int framerateLimit = 60; // 0 mean automatic (verticalSyncEnabled = true)

explicit WindowConfig(sf::VideoMode pVideoMode, std::string pTitle = "Render Window", unsigned int pStyle = sf::Style::Default, unsigned int pFramerateLimit = 60)
explicit WindowConfig(sf::VideoMode pVideoMode, std::string pTitle = "Render Window", unsigned int pStyle = sf::Style::Fullscreen, unsigned int pFramerateLimit = 60)
: videoMode(pVideoMode), title(std::move(pTitle)), style(pStyle), framerateLimit(pFramerateLimit) {}
};

Expand Down
38 changes: 10 additions & 28 deletions src/Engine/Scenes/MainGameScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,6 @@ void MainGameScene::initFonts()
}
}

void MainGameScene::checkPlayerPongBallCollision(const PongBall& pongBall) const
{
for (int currPlayerIndex = 0; currPlayerIndex < _players.size(); ++currPlayerIndex)
{
if (pongBall.hitPlayer(_players[currPlayerIndex]->getPosition().x,
_players[currPlayerIndex]->getPosition().y,
_players[currPlayerIndex]->getRadius(),
_players[currPlayerIndex]->getNormalAmmoColor()))
{
_players[currPlayerIndex]->setPlayerAlive(false);
getAudioPlayer()->playSound("Explosion");
_animator->DeathAnimation((sf::Vector2f)_players[currPlayerIndex]->getPosition());
_gameManager->makePlayerWin(currPlayerIndex+1);
}
}
}

void MainGameScene::update(const float& deltaTime)
{
updateInputs(deltaTime);
Expand Down Expand Up @@ -124,7 +107,11 @@ void MainGameScene::render(sf::RenderTarget* target)
_animator->render(*target);
}

PolygonTerrain *const MainGameScene::getPolygonTerrain() const { return _polygonTerrain.get(); }
PolygonTerrain* MainGameScene::getPolygonTerrain() const { return _polygonTerrain.get(); }
std::vector<Character *>& MainGameScene::getPlayers() { return _players; }
AudioPlayer* MainGameScene::getAudioPlayer() const { return _audioPlayer.get(); }
AnimatorManager* MainGameScene::getAnimatorManager() const { return _animator.get(); }
GameManager* MainGameScene::getGameManager() const { return _gameManager.get(); }

void MainGameScene::displayPlayers(bool isDisplayed) const
{
Expand All @@ -135,11 +122,6 @@ void MainGameScene::displayPlayers(bool isDisplayed) const
}
}

AudioPlayer* MainGameScene::getAudioPlayer() const
{
return _audioPlayer.get();
}

void MainGameScene::togglePlayersMovement(bool canTheyMove) const
{
for (const auto player : _players)
Expand Down Expand Up @@ -250,8 +232,8 @@ void MainGameScene::setPlayersToDefaultSpawnPoints(Character* p1, Character* p2)
if (p1)
{
sf::Vector2i spawnPosition(
_poPossibEngin->getRenderWindow().getSize().x / 2 - PLAYERS_SPAWN_POINT_X_OFFSET,
_poPossibEngin->getRenderWindow().getSize().y / 2
(int)_poPossibEngin->getRenderWindow().getSize().x / 2 - PLAYERS_SPAWN_POINT_X_OFFSET,
(int)_poPossibEngin->getRenderWindow().getSize().y / 2
);

p1->setPosition(spawnPosition);
Expand All @@ -260,8 +242,8 @@ void MainGameScene::setPlayersToDefaultSpawnPoints(Character* p1, Character* p2)
if (p2)
{
sf::Vector2i spawnPosition(
_poPossibEngin->getRenderWindow().getSize().x / 2 + PLAYERS_SPAWN_POINT_X_OFFSET,
_poPossibEngin->getRenderWindow().getSize().y / 2
(int)_poPossibEngin->getRenderWindow().getSize().x / 2 + PLAYERS_SPAWN_POINT_X_OFFSET,
(int)_poPossibEngin->getRenderWindow().getSize().y / 2
);

p2->setPosition(spawnPosition);
Expand All @@ -283,4 +265,4 @@ Client::SyncableObject *MainGameScene::createPongBall(const SyncableObjectOption
_controllers.push_back(pongBallController);

return pongBallController;
}
}
9 changes: 5 additions & 4 deletions src/Engine/Scenes/MainGameScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ class MainGameScene : public Scene
void endRound() const;

// Get / Set
PolygonTerrain *const getPolygonTerrain() const;
PolygonTerrain *getPolygonTerrain() const;
void displayPlayers(bool isDisplayed) const;
AudioPlayer* getAudioPlayer() const;
AudioPlayer* getAudioPlayer() const;
AnimatorManager *getAnimatorManager() const;
std::vector<Character*>& getPlayers();
GameManager *getGameManager() const;

Client::SyncableObject* createPlayer(const SyncableObjectOptions& options, const PlayerState& playerState);
Client::SyncableObject* createPongBall(const SyncableObjectOptions& options, const PongBallState& pongBallState);
Expand All @@ -62,14 +65,12 @@ class MainGameScene : public Scene

std::unique_ptr<PolygonTerrain> _polygonTerrain;

private:
//Font, texts and textures..
sf::Font _font;

void initValues();
void initFonts();
void hideAllPongBalls() const;
void checkPlayerPongBallCollision(const PongBall& pongBall) const;

void setPlayersToDefaultSpawnPoints(Character *p1 = nullptr, Character *p2 = nullptr) const;
};
2 changes: 1 addition & 1 deletion src/Engine/Scenes/MainMenuScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class MainMenuScene : public Scene
const std::string host_hostNameTextDefault = "New Lobby";
const std::string host_hostPortTextDefault = "25565";

const std::string join_hostIpTextDefault = "109.215.43.120";
const std::string join_hostIpTextDefault = "127.0.0.1";
const std::string join_hostPortTextDefault = "25565";

const std::string wait_hostIpTextDefault = "127.0.0.1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
#include "../../../../Game/Controllers/LocalCharacterController/LocalCharacterController.hpp"

using namespace Client;
constexpr unsigned int tickDelay = 10;

constexpr unsigned int tickDelay = 30;

std::map<SyncableObjectType, unsigned int> SyncableObjectManager::objectTypesInSceneUpdate;
std::map<SyncableObjectType, unsigned int> SyncableObjectManager::objectTypesInSceneUpdate = std::map<SyncableObjectType, unsigned int>();
bool SyncableObjectManager::isGameStarted = false;

SyncableObjectManager::SyncableObjectManager(ClientSocket *clientSocket)
Expand Down
3 changes: 3 additions & 0 deletions src/Engine/SocketsManagement/ObjectsStates/PongBallState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ struct PongBallState

bool enabled = false;
bool canKill = false;

bool hit = false;
int hitID = -1;
};


Expand Down
51 changes: 50 additions & 1 deletion src/Engine/SocketsManagement/Server/ServerMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bool ServerMain::isReady() const
return _serverSocket->isReady();
}

constexpr unsigned int serverTick = 30;
constexpr unsigned int serverTick = 10;

[[noreturn]] void ServerMain::threadEntry()
{
Expand All @@ -55,6 +55,55 @@ constexpr unsigned int serverTick = 30;
}
}

void checkPingBallCollisions()
{

}

/*bool checkPlayerCollision()
{
auto* mainGameScene = MainGameScene::getInstance();
if(!mainGameScene) return false;
auto& players = mainGameScene->getPlayers();
for (int i = 0; i < players.size(); i++)
{
auto* player = players[i];
if (_pongBall.hitPlayer(
(float)player->getPosition().x,
(float)player->getPosition().y,
player->getRadius(),
player->getNormalAmmoColor()
))
{
player->setPlayerAlive(false);
mainGameScene->getAudioPlayer()->playSound("Explosion");
mainGameScene->getAnimatorManager()->DeathAnimation({
(float)player->getPosition().x,
(float)player->getPosition().y
});
mainGameScene->getGameManager()->makePlayerWin(i + 1);
return true;
}
}
return false;
}
bool hitPlayer(float c2x, float c2y, float c2r, sf::Color color2) const
{
if (_canKill && _isActive && color2 != _ballColor)
{
return Utils::circleCircleCollision(_ballShape.getPosition().x, _ballShape.getPosition().y, _ballShape.getRadius(), c2x, c2y, c2r);
}
return false;
}*/

sf::Packet ServerMain::buildSceneState()
{
sf::Packet sceneState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#include "NetworkPongBallController.hpp"
#include <utility>

#include "../../../Engine/Scenes/MainGameScene.hpp"
#include "../../../Engine/Animator/AnimatorManager.hpp"
#include "../../Entities/Character.hpp"
#include "../../System/Audio/AudioPlayer.hpp"
#include "../../System/GameManager.hpp"

NetworkPongBallController::NetworkPongBallController(SyncableObjectOptions options, PongBall &pongBall, const PongBallState& pongBallState)
: ControllerBase(std::move(options), pongBall)
, _pongBall(pongBall)
Expand Down Expand Up @@ -58,12 +64,47 @@ void NetworkPongBallController::applySync(const PongBallState& state, std::strin

void NetworkPongBallController::update(const float &deltaTime)
{
checkPlayerCollision();
}

bool NetworkPongBallController::checkPlayerCollision()
{
auto* mainGameScene = MainGameScene::getInstance();
if(!mainGameScene) return false;

auto& players = mainGameScene->getPlayers();

for (int i = 0; i < players.size(); i++)
{
auto* player = players[i];

if (_pongBall.hitPlayer(
(float)player->getPosition().x,
(float)player->getPosition().y,
player->getRadius(),
player->getNormalAmmoColor()
)
)
{
player->setPlayerAlive(false);
mainGameScene->getAudioPlayer()->playSound("Explosion");

mainGameScene->getAnimatorManager()->DeathAnimation({
(float)player->getPosition().x,
(float)player->getPosition().y
});

mainGameScene->getGameManager()->makePlayerWin(i + 1);
return true;
}
}

return false;
}

PongBallState NetworkPongBallController::getPongBallState()
{
return PongBallState {
return {
_pongBall.getPosition(),
_pongBall.getVelocity(),
_pongBall.isActive(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class NetworkPongBallController : public Engine::ControllerBase

private:
PongBall& _pongBall;

bool checkPlayerCollision();
};


Expand Down
19 changes: 6 additions & 13 deletions src/Game/Terrains/PolygonTerrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,13 @@ void PolygonTerrain::drawRandomTerrain()
initTerrainOrigin();
}

constexpr int terrainCount = 10;

void PolygonTerrain::selectAndDrawRandomTerrain()
{
//Pick random index between 1 and NUM_TERRAINS, that is different from the previous one
srand(static_cast<unsigned>(time(nullptr)));
int randomTerrainIndex;
do
{
randomTerrainIndex = rand() % NUM_TERRAINS + 1;
} while (randomTerrainIndex == _currentTerrain);

_currentTerrain = randomTerrainIndex;
_currentTerrain = (++_currentTerrain <= terrainCount) ? _currentTerrain : 1;

//Draw random terrain
switch (randomTerrainIndex)
switch (_currentTerrain)
{
case 1: drawTerrain1(); break;
case 2: drawTerrain2(); break;
Expand All @@ -221,13 +214,13 @@ void PolygonTerrain::selectAndDrawRandomTerrain()
case 8: drawTerrain8(); break;
case 9: drawTerrain9(); break;
case 10:drawTerrain10();break;
default:drawTerrain1(); break;
default: drawTerrain1(); break;
}
}

//=========================== DRAWING TERRAINS ===========================

void PolygonTerrain::drawTerrain1()//OCTOGON
void PolygonTerrain::drawTerrain1() //OCTOGON
{
int edgeLenght = 16;

Expand Down

0 comments on commit 747401b

Please sign in to comment.