...
 
Commits (2)
......@@ -8,7 +8,7 @@
class HUD{
public :
enum Dir{ TL , TR , BL , BR };
enum Dir{ TL , TR , BL , BR , Last};
struct Meter{
int cv; int mv; Agent::Type upType,downType;
};
......
......@@ -7,12 +7,13 @@
class Client{
public :
Client(std::string str);
Client(std::string str, int poff);
void connect();
void send(std::string, int length);
private :
std::string ip;
int port_offset;
sf::TcpSocket socket;
sf::Thread thread;
bool connected;
......@@ -21,7 +22,7 @@ class Client{
class Game{
public :
Game(std::string ip);
Game(std::vector<std::string> ips);
void run();
void update(sf::Time deltaTime);
void render();
......@@ -31,10 +32,10 @@ public :
private :
sf::RenderWindow mWindow;
World *mWorld;
std::array<HUD*,2> mHUD;
std::vector<HUD*> mHUD;
sf::Time TimePerFrame;
std::set<sf::Keyboard::Key> pressed, released;
Client client;
std::vector<Client*> client;
};
......
......@@ -23,8 +23,8 @@ class KeyInput : public Input {
class TCPInput : public Input{
public :
TCPInput();
~TCPInput();
TCPInput(int poff);
virtual bool isKeyPressed(sf::Keyboard::Key k);
virtual void update();
......@@ -33,10 +33,10 @@ class TCPInput : public Input{
void release(sf::Keyboard::Key k);
private :
void connect();
std::set<sf::Keyboard::Key> pressed;
sf::TcpListener listener;
sf::TcpSocket client;
sf::Thread thread;
bool listening, clienting;
int port_offset;
};
......@@ -23,7 +23,7 @@
class World : private sf::NonCopyable{
public:
explicit World(sf::RenderWindow& window);
explicit World(sf::RenderWindow& window, int era);
~World();
void update(sf::Time dt);
......@@ -49,8 +49,8 @@ private:
Carte mCarte;
SceneNode mSceneGraph;
std::array<Player*,2> mPlayer;
std::vector<Player*> mPlayer;
Room* mRoom;
std::array<HUD::HUD_data*,2> mData;
std::vector<HUD::HUD_data*> mData;
};
......@@ -15,15 +15,19 @@ HUD::HUD(sf::RenderWindow& window, HUD::HUD_data* data, HUD::Dir d)
mRoot.attachChild(mContent[i][j]);
switch(d){
case TL :
mContent[i][j]->move(mView.getSize().x-32*j-16,32*i+16);
std::cout << "t1\n";
mContent[i][j]->move(32*j+16,32*i+16);
break;
case TR :
mContent[i][j]->move(32*j+16,32*i+16);
std::cout << "t2\n";
mContent[i][j]->move(mView.getSize().x-32*j-16,32*i+16);
break;
case BL :
std::cout << "t3\n";
mContent[i][j]->move(32*j+16,mView.getSize().y-32*i-16);
break;
case BR :
std::cout << "r4\n";
mContent[i][j]->move(mView.getSize().x-32*j-16,mView.getSize().y-32*i-16);
break;
}
......
#include "../headers/game.hpp"
#include "../headers/HUD.hpp"
Game::Game(std::string str) : mWindow(sf::VideoMode(1920, 1080), "LSV Fighters"),
mWorld(new World(mWindow)), client(str){
mHUD[0] = new HUD(mWindow,mWorld->getHUDLocator(0), HUD::TL);
mHUD[1] = new HUD(mWindow,mWorld->getHUDLocator(1), HUD::TR);
Game::Game(std::vector<std::string> ips)
: mWindow(sf::VideoMode(1920, 1080), "LSV Fighters"),
mWorld(new World(mWindow, ips.size())){
int index = 0;
for(auto ip : ips) client.push_back(new Client(ip,index++));
mHUD.push_back(new HUD(mWindow,mWorld->getHUDLocator(0), HUD::TL));
index = 1;
for(auto ip : ips){ std::cout << ip << ' ';
mHUD.push_back(new HUD(mWindow,mWorld->getHUDLocator(index),
(HUD::Dir)(index % ((int) HUD::Last))));
std::cout << (HUD::Dir)(index % ((int) HUD::Last)) << '\n';
++index;
}
TimePerFrame = sf::seconds(1.f/60.f);
}
......@@ -31,7 +40,7 @@ void Game::streamEvents(){
isstr<<"R"<<it<<"\n";
}
const char* data = isstr.str().c_str();
client.send(data, 1000);
for(auto cli : client) cli->send(data, 1000);
pressed.clear();
released.clear();
}
......@@ -57,36 +66,40 @@ 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);
mHUD[0]->updateHUDlocator(mWorld->getHUDLocator(0));
mHUD[1]->updateHUDlocator(mWorld->getHUDLocator(1));
delete mWorld; mWorld = new World(mWindow, client.size());
auto index = 0;
for(auto hud : mHUD){
hud->updateHUDlocator(mWorld->getHUDLocator(index));
++index;
}
}
}
void Game::update(sf::Time deltaTime){
mWorld->update(deltaTime);
mHUD[0]->update(deltaTime);
mHUD[1]->update(deltaTime);
for(auto hud : mHUD)
hud->update(deltaTime);
}
void Game::render(){
mWindow.clear();
mWorld->draw();
mHUD[0]->draw();
mHUD[1]->draw();
for(auto hud : mHUD) hud->draw();
mWindow.display();
}
Client::Client(std::string str) : ip(str), thread(&Client::connect, this){
Client::Client(std::string str, int poff) : ip(str),
port_offset(poff), thread(&Client::connect, this){
connected = false;
thread.launch();
}
void Client::connect(){
sf::Socket::Status status = socket.connect(ip.c_str(), PORT, sf::seconds(5.f));
sf::Socket::Status status = socket.connect(ip.c_str(),
PORT+port_offset, sf::seconds(5.f));
while(status != sf::Socket::Done){
status = socket.connect(ip.c_str(), PORT, sf::seconds(1.f));
status = socket.connect(ip.c_str(), PORT+port_offset, sf::seconds(1.f));
sf::sleep(sf::seconds(1.f));
}
connected = true;
......@@ -97,6 +110,3 @@ void Client::send(std::string msg, int length){
socket.send(msg.c_str(),length);
}
}
......@@ -19,19 +19,15 @@ TCPInput::~TCPInput(){
void Input::update(){
}
TCPInput::TCPInput() : listening(false), clienting(false), thread(&TCPInput::update,this){
TCPInput::TCPInput(int poff) :
listening(false), clienting(false), thread(&TCPInput::update,this),
port_offset(poff){
thread.launch();
}
void TCPInput::connect(){
listening = listening || listener.listen(PORT) == sf::Socket::Done;
clienting = clienting || listener.accept(client) == sf::Socket::Done;
}
void TCPInput::update(){
sf::TcpListener listener;
if (listener.listen(PORT) != sf::Socket::Done){
if (listener.listen(PORT + port_offset) != sf::Socket::Done){
std::cout << "err\n";
}
sf::TcpSocket client;
......@@ -53,18 +49,6 @@ void TCPInput::update(){
}
sf::sleep(sf::seconds(0.01f));
}
/* connect();
std::cout << "t\n";
while(true){
char data[1000];
std::size_t received;
if(client.receive(data,1000,received) != sf::Socket::Done){
std::cout << "u\n";
std::cout << data;
}
sf::sleep(sf::seconds(1.f));
}*/
}
bool Input::isKeyPressed(sf::Keyboard::Key k){
......
......@@ -4,11 +4,10 @@
int main(int argc, char** argv){
if(argc >= 2)
srand(atoi(argv[2]));
else
srand(time(NULL));
Game game(argv[1]);
srand(atoi(argv[argc-1]));
std::vector<std::string> ips(argv+1,argv+argc-1);
std::cout << argc -3 << '\n';
Game game(ips);
game.run();
return 0;
}
#include "../headers/world.hpp"
World::World(sf::RenderWindow& window)
World::World(sf::RenderWindow& window, int era)
: 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){
mCarte(mTextures,mWorldBounds,0.9f,0.9f,0.4f,20,this),
mPlayer(era+1), mData(era+1){
mRoom = mCarte.checkIn();
spawnPlayer(0,mRoom, new KeyInput());
spawnPlayer(1,mRoom, new TCPInput());
for(int i = 0 ; i < era ; ++i) spawnPlayer(i+1,mRoom, new TCPInput(i));
mSceneGraph.attachChild(mRoom);
makeData(0); makeData(1);
for(int i = 0 ; i < era+1 ; ++i) makeData(i);
}
World::~World(){
delete mData[0], mData[1];
for(int i = 0 ; i < mData.size() ; ++i) delete mData[i];
}
void World::makeData(int i){
......@@ -56,35 +57,36 @@ void World::update(sf::Time dt){
mSceneGraph.update(dt);
mSceneGraph.removeWrecks(bounds);
checkCollision();
fillData(0); fillData(1);
for(int i = 0 ; i < mData.size() ; ++i) fillData(i);
}
void World::checkCollision(){
std::set<SceneNode::Pair> stack;
mSceneGraph.checkSceneCollision(mSceneGraph, stack);
bool apri = false;
bool zenua = false;
for(auto &pair : stack){
pair.first->onCollideWith(*pair.second);
pair.second->onCollideWith(*pair.first);
}
for(auto &pair : stack){
if(!apri && pair.first->identify() == Category::Player
&& pair.second->identify() == Category::Door
&& static_cast<Door&>(*pair.second).lockLevel()
<= static_cast<Player&>(*pair.first).getKeys()){
mPlayer[0] = static_cast<Player*>(mRoom->detachPlayer(*mPlayer[0]));
mPlayer[1] = static_cast<Player*>(mRoom->detachPlayer(*mPlayer[1]));
Room* room = static_cast<Room*>(mSceneGraph.detachChild(*mRoom));
mCarte.checkOut(room);
mCarte.move(static_cast<Door&>(*pair.second).getDir());
mRoom->reset();
mRoom = mCarte.checkIn();
mRoom->spawnAt(static_cast<Door&>(*pair.second).getDir());
mRoom->attachPlayer(mPlayer[0]);
mRoom->attachPlayer(mPlayer[1]);
mSceneGraph.attachChild(mRoom);
apri = true;
if(pair.first->identify() == Category::Player
&& pair.second->identify() == Category::Door
&& static_cast<Door&>(*pair.second).lockLevel()
<= static_cast<Player&>(*pair.first).getKeys()
&& !zenua){ zenua = true;
for(int i = 0 ; i < mData.size() ; ++i)
mPlayer[i] = static_cast<Player*>(mRoom->detachPlayer(*mPlayer[i]));
Room* room = static_cast<Room*>(mSceneGraph.detachChild(*mRoom));
mCarte.checkOut(room);
mCarte.move(static_cast<Door&>(*pair.second).getDir());
mRoom->reset();
mRoom = mCarte.checkIn();
mRoom->spawnAt(static_cast<Door&>(*pair.second).getDir());
for(int i = 0 ; i < mData.size() ; ++i)
mRoom->attachPlayer(mPlayer[i]);
mSceneGraph.attachChild(mRoom);
}
}
}