Skip to content
Browse files

Multiplayer planetAdded event

  • Loading branch information...
1 parent 303669c commit 0eae0d6bee870627f3c505a4978d3a2900c953b4 @vos committed May 19, 2012
Showing with 75 additions and 1 deletion.
  1. +2 −0 mainwindow.cpp
  2. +14 −0 multiplayerclient.cpp
  3. +3 −0 multiplayerclient.h
  4. +1 −0 multiplayerpacket.cpp
  5. +1 −0 multiplayerpacket.h
  6. +19 −0 multiplayerserver.cpp
  7. +3 −0 multiplayerserver.h
  8. +21 −0 planet.cpp
  9. +11 −1 planet.h
View
2 mainwindow.cpp
@@ -62,6 +62,8 @@ MainWindow::MainWindow(QWidget *parent) :
MainWindow::~MainWindow()
{
+ if (m_client != NULL)
+ delete m_client;
if (m_server != NULL)
delete m_server;
qDeleteAll(m_propertyEditorMap.values());
View
14 multiplayerclient.cpp
@@ -13,6 +13,8 @@ MultiplayerClient::MultiplayerClient(Game *game, Player *player, QObject *parent
connect(this, SIGNAL(disconnected()), SLOT(socket_disconnected()));
connect(this, SIGNAL(readyRead()), SLOT(socket_readyRead()));
connect(this, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(socket_error(QAbstractSocket::SocketError)));
+
+ connect(m_game, SIGNAL(planetAdded(Planet*)), SLOT(game_planetAdded(Planet*)));
}
void MultiplayerClient::socket_connected()
@@ -66,6 +68,11 @@ void MultiplayerClient::socket_readyRead()
case MultiplayerPacket::ConnectionRefused:
// TODO
break;
+ case MultiplayerPacket::PlanetAdded: {
+ Planet *planet = new Planet;
+ in >> *planet;
+ m_game->addPlanet(planet);
+ }
default:
qWarning("MultiplayerClient::socket_readyRead(): Illegal PacketType %i", packetType);
return;
@@ -79,3 +86,10 @@ void MultiplayerClient::socket_error(QAbstractSocket::SocketError error)
qDebug("MultiplayerClient::socket_error(%i) => %s", error, qPrintable(errorString()));
#endif
}
+
+void MultiplayerClient::game_planetAdded(Planet *planet)
+{
+ MultiplayerPacket packet(MultiplayerPacket::PlanetAdded);
+ packet.stream() << *planet;
+ packet.send(this);
+}
View
3 multiplayerclient.h
@@ -5,6 +5,7 @@
class Game;
class Player;
+class Planet;
class MultiplayerClient : public QTcpSocket
{
@@ -19,6 +20,8 @@ private slots:
void socket_readyRead();
void socket_error(QAbstractSocket::SocketError error);
+ void game_planetAdded(Planet *planet);
+
private:
Game *m_game;
Player *m_player;
View
1 multiplayerpacket.cpp
@@ -7,6 +7,7 @@ const char* MultiplayerPacket::PacketTypeNames[] = {
"ConnectionRefused",
"PlayerJoin",
"PlayerDisconnect",
+ "PlanetAdded",
"IllegalPacketType"
};
View
1 multiplayerpacket.h
@@ -14,6 +14,7 @@ class MultiplayerPacket
ConnectionRefused, // server -> client
PlayerJoin, // client -> server
PlayerDisconnect, // client -> server
+ PlanetAdded, // client -> server -> other clients
IllegalPacketType
};
static QString typeString(PacketType type);
View
19 multiplayerserver.cpp
@@ -110,13 +110,32 @@ void MultiplayerServer::client_readyRead()
case MultiplayerPacket::PlayerDisconnect:
socket->disconnectFromHost();
break;
+ case MultiplayerPacket::PlanetAdded: {
+ Planet *planet = new Planet;
+ in >> *planet;
+ m_game->addPlanet(planet);
+ // resend packet to all other clients
+ MultiplayerPacket packet(MultiplayerPacket::PlanetAdded);
+ packet.stream() << *planet;
+ sendPacketToOtherClients(packet, socket);
+ break;
+ }
default:
qWarning("MultiplayerServer::client_readyRead(): Illegal PacketType %i", packetType);
return;
}
}
}
+void MultiplayerServer::sendPacketToOtherClients(MultiplayerPacket &packet, const QTcpSocket *sender)
+{
+ foreach (QTcpSocket *client, m_clients.keys()) {
+ if (client != sender) {
+ packet.send(client);
+ }
+ }
+}
+
void MultiplayerServer::client_error(QAbstractSocket::SocketError error)
{
QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
View
3 multiplayerserver.h
@@ -4,6 +4,8 @@
#include <QTcpServer>
#include <QHash>
+#include "multiplayerpacket.h"
+
class Game;
class Player;
@@ -31,6 +33,7 @@ private slots:
QHash<QTcpSocket*, Client*> m_clients;
void incomingConnection(int socketDescriptor);
+ void sendPacketToOtherClients(MultiplayerPacket &packet, const QTcpSocket *sender);
};
View
21 planet.cpp
@@ -139,3 +139,24 @@ void Planet::draw(QPainter &painter)
painter.setPen(Qt::white);
painter.drawText(boundingRect, Qt::AlignCenter, text);
}
+
+#ifndef QT_NO_DATASTREAM
+QDataStream& operator<<(QDataStream &stream, const Planet &planet)
+{
+ return stream << planet.m_position
+ << planet.m_radius
+ << planet.m_resources
+ << planet.m_color
+ << planet.m_productionFactor;
+}
+
+QDataStream& operator>>(QDataStream &stream, Planet &planet)
+{
+ return stream >> planet.m_position
+ >> planet.m_radius
+ >> planet.m_resources
+ >> planet.m_color
+ >> planet.m_productionFactor;
+}
+
+#endif
View
12 planet.h
@@ -15,7 +15,7 @@ class Planet : public SpaceObject
Q_PROPERTY(bool neutral READ isNeutral DESIGNABLE false)
public:
- Planet(const QVector2D& position, qreal radius = 50.0, qreal resources = 0.0, const QColor &color = Qt::darkGray, Player *parent = NULL);
+ Planet(const QVector2D& position = QVector2D(), qreal radius = 50.0, qreal resources = 0.0, const QColor &color = Qt::darkGray, Player *parent = NULL);
void setPosition(const QVector2D &position);
void setResources(qreal resources);
@@ -44,6 +44,11 @@ class Planet : public SpaceObject
PlayerChange
};
+#ifndef QT_NO_DATASTREAM
+ friend QDataStream& operator<<(QDataStream &stream, const Planet &planet);
+ friend QDataStream& operator>>(QDataStream &stream, Planet &planet);
+#endif
+
signals:
void changed(Planet::ChangeType changeType);
@@ -61,6 +66,11 @@ public slots:
};
+#ifndef QT_NO_DATASTREAM
+QDataStream& operator<<(QDataStream &stream, const Planet &planet);
+QDataStream& operator>>(QDataStream &stream, Planet &planet);
+#endif
+
Q_DECLARE_METATYPE(Planet*)
Q_DECLARE_METATYPE(QSet<Planet*>)

0 comments on commit 0eae0d6

Please sign in to comment.
Something went wrong with that request. Please try again.