Commit ce232e84 authored by pa's avatar pa

Last commit

parent 46a0fbe0
......@@ -7,13 +7,13 @@
class Client{
public :
Client(std::string str, int poff);
Client(std::string str, int p);
void connect();
void send(std::string, int length);
private :
std::string ip;
int port_offset;
int port;
sf::TcpSocket socket;
sf::Thread thread;
bool connected;
......@@ -22,7 +22,7 @@ class Client{
class Game{
public :
Game(std::vector<std::string> ips);
Game(std::vector<std::string> ips, std::vector<int> p);
void run();
void update(sf::Time deltaTime);
void render();
......@@ -30,6 +30,7 @@ public :
void streamEvents();
private :
std::vector<int> ports;
sf::RenderWindow mWindow;
World *mWorld;
std::vector<HUD*> mHUD;
......
......@@ -24,7 +24,7 @@ class KeyInput : public Input {
class TCPInput : public Input{
public :
~TCPInput();
TCPInput(int poff);
TCPInput(int p);
virtual bool isKeyPressed(sf::Keyboard::Key k);
virtual void update();
......@@ -38,5 +38,5 @@ class TCPInput : public Input{
sf::TcpSocket client;
sf::Thread thread;
bool listening, clienting;
int port_offset;
int port;
};
......@@ -23,7 +23,7 @@
class World : private sf::NonCopyable{
public:
explicit World(sf::RenderWindow& window, int era);
explicit World(sf::RenderWindow& window, std::vector<int> ports);
~World();
void update(sf::Time dt);
......
#include "../headers/game.hpp"
#include "../headers/HUD.hpp"
Game::Game(std::vector<std::string> ips)
: mWindow(sf::VideoMode(1920, 1080), "LSV Fighters"),
mWorld(new World(mWindow, ips.size())){
Game::Game(std::vector<std::string> ips, std::vector<int> port)
: mWindow(sf::VideoMode(1920, 1080), "LSV Fighters"), ports(port),
mWorld(new World(mWindow, ports)){
int index = 0;
for(auto ip : ips) client.push_back(new Client(ip,index++));
for(int i = 0; i < ips.size() ; ++i)
client.push_back(new Client(ips[i],ports[i]));
mHUD.push_back(new HUD(mWindow,mWorld->getHUDLocator(0), HUD::TL));
index = 1;
for(auto ip : ips){
mHUD.push_back(new HUD(mWindow,mWorld->getHUDLocator(index),
(HUD::Dir)(index % ((int) HUD::Last))));
++index;
}
for(int i = 0; i < ips.size() ; ++i)
mHUD.push_back(new HUD(mWindow,mWorld->getHUDLocator(i+1),
(HUD::Dir)((i+1) % ((int) HUD::Last))));
TimePerFrame = sf::seconds(1.f/60.f);
}
......@@ -65,7 +63,7 @@ void Game::processEvents(){
&& sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) mWindow.close();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)
&& sf::Keyboard::isKeyPressed(sf::Keyboard::R)){
delete mWorld; mWorld = new World(mWindow, client.size());
delete mWorld; mWorld = new World(mWindow, ports);
auto index = 0;
for(auto hud : mHUD){
hud->updateHUDlocator(mWorld->getHUDLocator(index));
......@@ -88,17 +86,16 @@ void Game::render(){
}
Client::Client(std::string str, int poff) : ip(str),
port_offset(poff), thread(&Client::connect, this){
Client::Client(std::string str, int p) : ip(str),
port(p), thread(&Client::connect, this){
connected = false;
thread.launch();
}
void Client::connect(){
sf::Socket::Status status = socket.connect(ip.c_str(),
PORT+port_offset, sf::seconds(5.f));
sf::Socket::Status status = socket.connect(ip.c_str(), port, sf::seconds(5.f));
while(status != sf::Socket::Done){
status = socket.connect(ip.c_str(), PORT+port_offset, sf::seconds(1.f));
status = socket.connect(ip.c_str(), port, sf::seconds(1.f));
sf::sleep(sf::seconds(1.f));
}
connected = true;
......
......@@ -19,15 +19,15 @@ TCPInput::~TCPInput(){
void Input::update(){
}
TCPInput::TCPInput(int poff) :
TCPInput::TCPInput(int p) :
listening(false), clienting(false), thread(&TCPInput::update,this),
port_offset(poff){
port(p){
thread.launch();
}
void TCPInput::update(){
sf::TcpListener listener;
if (listener.listen(PORT + port_offset) != sf::Socket::Done){
if (listener.listen(port) != sf::Socket::Done){
std::cout << "err\n";
}
sf::TcpSocket client;
......
......@@ -5,12 +5,15 @@
int main(int argc, char** argv){
if(argc == 1){
std::cout << "Usage : \n\t ./lsv-fighters (<ip1> )* <Seed>\n";
std::cout << "Usage : \n\t ./lsv-fighters (<ip> <port> )* <Seed>\n";
exit(1);
}
srand(atoi(argv[argc-1]));
std::vector<std::string> ips(argv+1,argv+argc-1);
Game game(ips);
std::vector<std::string> ips; std::vector<int> ports;
for(int i = 1 ; i < argc -1 ; i += 2){
ips.push_back(argv[i]); ports.push_back(atoi(argv[i+1]));
}
Game game(ips,ports);
game.run();
return 0;
}
#include "../headers/world.hpp"
World::World(sf::RenderWindow& window, int era)
World::World(sf::RenderWindow& window, std::vector<int> ports)
: mWindow(window), mWorldView(window.getDefaultView()),
mWorldBounds(0.f,0.f,mWorldView.getSize().x,mWorldView.getSize().y),
mCarte(mTextures,mWorldBounds,0.9f,0.9f,0.4f,20,this),
mPlayer(era+1), mData(era+1){
mPlayer(ports.size()+1), mData(ports.size()+1){
mRoom = mCarte.checkIn();
spawnPlayer(0,mRoom, new KeyInput());
for(int i = 0 ; i < era ; ++i) spawnPlayer(i+1,mRoom, new TCPInput(i));
for(int i = 0 ; i < ports.size() ; ++i) spawnPlayer(i+1,mRoom, new TCPInput(ports[i]));
mSceneGraph.attachChild(mRoom);
for(int i = 0 ; i < era+1 ; ++i) makeData(i);
for(int i = 0 ; i < ports.size()+1 ; ++i) makeData(i);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment