Branch 'dev-char' is stable, merging into 'dev'
This commit is contained in:
@@ -89,7 +89,7 @@ void ClientApplication::Init() {
|
||||
flags);
|
||||
|
||||
//initiate the remaining singletons
|
||||
netUtil->Open(0, sizeof(Packet));
|
||||
netUtil->Open(0, sizeof(Packet::Packet));
|
||||
}
|
||||
|
||||
void ClientApplication::Proc() {
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
#include "scene_list.hpp"
|
||||
#include "base_scene.hpp"
|
||||
#include "singleton.hpp"
|
||||
#include "packet_type.hpp"
|
||||
#include "packet.hpp"
|
||||
#include "information_manager.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
|
||||
+42
-12
@@ -35,6 +35,7 @@ InWorld::InWorld() {
|
||||
#endif
|
||||
cout << "Client Index: " << infoMgr->GetClientIndex() << endl;
|
||||
font.SetSurface(surfaceMgr->Get("font"));
|
||||
pc.GetSprite()->SetSurface(surfaceMgr->Get("elliot"), 32, 48);
|
||||
}
|
||||
|
||||
InWorld::~InWorld() {
|
||||
@@ -53,6 +54,7 @@ void InWorld::FrameStart() {
|
||||
|
||||
void InWorld::Update(double delta) {
|
||||
while(HandlePacket(popNetworkPacket()));
|
||||
pc.Update(delta);
|
||||
}
|
||||
|
||||
void InWorld::FrameEnd() {
|
||||
@@ -62,6 +64,8 @@ void InWorld::FrameEnd() {
|
||||
void InWorld::Render(SDL_Surface* const screen) {
|
||||
ClockFrameRate();
|
||||
|
||||
pc.DrawTo(screen);
|
||||
|
||||
//since we're using this twice, make a tmp var
|
||||
string fps = itos(GetFrameRate());
|
||||
font.DrawStringTo(fps, screen, screen->w - fps.size() * font.GetCharW(), 0);
|
||||
@@ -94,29 +98,55 @@ void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
case SDLK_ESCAPE:
|
||||
ExitGame();
|
||||
break;
|
||||
case SDLK_w:
|
||||
pc.MoveDirection(CardinalDirection::NORTH);
|
||||
break;
|
||||
case SDLK_s:
|
||||
pc.MoveDirection(CardinalDirection::SOUTH);
|
||||
break;
|
||||
case SDLK_a:
|
||||
pc.MoveDirection(CardinalDirection::EAST);
|
||||
break;
|
||||
case SDLK_d:
|
||||
pc.MoveDirection(CardinalDirection::WEST);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
//reversed
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_w:
|
||||
pc.MoveDirection(CardinalDirection::SOUTH);
|
||||
break;
|
||||
case SDLK_s:
|
||||
pc.MoveDirection(CardinalDirection::NORTH);
|
||||
break;
|
||||
case SDLK_a:
|
||||
pc.MoveDirection(CardinalDirection::WEST);
|
||||
break;
|
||||
case SDLK_d:
|
||||
pc.MoveDirection(CardinalDirection::EAST);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Utilities
|
||||
//-------------------------
|
||||
|
||||
int InWorld::HandlePacket(Packet p) {
|
||||
int InWorld::HandlePacket(Packet::Packet p) {
|
||||
switch(p.meta.type) {
|
||||
case PacketType::NONE:
|
||||
case Packet::Type::NONE:
|
||||
//DO NOTHING
|
||||
return 0;
|
||||
break;
|
||||
case PacketType::PING:
|
||||
case Packet::Type::PING:
|
||||
//quick pong
|
||||
p.meta.type = PacketType::PONG;
|
||||
netUtil->Send(&p.meta.address, &p, sizeof(Packet));
|
||||
p.meta.type = Packet::Type::PONG;
|
||||
netUtil->Send(&p.meta.address, &p, sizeof(Packet::Packet));
|
||||
break;
|
||||
case PacketType::PONG:
|
||||
case Packet::Type::PONG:
|
||||
//
|
||||
break;
|
||||
// case PacketType::BROADCAST_REQUEST:
|
||||
@@ -131,7 +161,7 @@ int InWorld::HandlePacket(Packet p) {
|
||||
// case PacketType::JOIN_RESPONSE:
|
||||
// //
|
||||
// break;
|
||||
case PacketType::DISCONNECT:
|
||||
case Packet::Type::DISCONNECT:
|
||||
HandleDisconnection(p.disconnect);
|
||||
break;
|
||||
// case PacketType::SYNCHRONIZE:
|
||||
@@ -154,10 +184,10 @@ int InWorld::HandlePacket(Packet p) {
|
||||
|
||||
void InWorld::Disconnect() {
|
||||
//disconnect
|
||||
Packet p;
|
||||
p.meta.type = PacketType::DISCONNECT;
|
||||
Packet::Packet p;
|
||||
p.meta.type = Packet::Type::DISCONNECT;
|
||||
p.disconnect.clientIndex = infoMgr->GetClientIndex();
|
||||
netUtil->Send(GAME_CHANNEL, reinterpret_cast<void*>(&p), sizeof(Packet));
|
||||
netUtil->Send(GAME_CHANNEL, reinterpret_cast<void*>(&p), sizeof(Packet::Packet));
|
||||
netUtil->Unbind(GAME_CHANNEL);
|
||||
endQueueThread();
|
||||
|
||||
@@ -171,7 +201,7 @@ void InWorld::ExitGame() {
|
||||
cout << "The game session has ended" << endl;
|
||||
}
|
||||
|
||||
void InWorld::HandleDisconnection(::Disconnect& disconnect) {
|
||||
void InWorld::HandleDisconnection(Packet::Disconnect& disconnect) {
|
||||
Disconnect();
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
cout << "You have been disconnected" << endl;
|
||||
|
||||
+7
-3
@@ -26,7 +26,7 @@
|
||||
#include "utilities.hpp"
|
||||
#include "defines.hpp"
|
||||
#include "singleton.hpp"
|
||||
#include "packet_type.hpp"
|
||||
#include "packet.hpp"
|
||||
#include "network_queue.hpp"
|
||||
#include "information_manager.hpp"
|
||||
|
||||
@@ -37,6 +37,9 @@
|
||||
#include "raster_font.hpp"
|
||||
#include "frame_rate.hpp"
|
||||
|
||||
//debugging
|
||||
#include "player_character.hpp"
|
||||
|
||||
class InWorld : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
@@ -59,10 +62,10 @@ protected:
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//Utilities
|
||||
int HandlePacket(Packet p);
|
||||
int HandlePacket(Packet::Packet p);
|
||||
void Disconnect();
|
||||
void ExitGame();
|
||||
void HandleDisconnection(::Disconnect&);
|
||||
void HandleDisconnection(Packet::Disconnect&);
|
||||
|
||||
//services
|
||||
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||
@@ -72,6 +75,7 @@ protected:
|
||||
|
||||
//members
|
||||
RasterFont font;
|
||||
PlayerCharacter pc;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+16
-16
@@ -155,30 +155,30 @@ void Lobby::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//Utilities
|
||||
//-------------------------
|
||||
|
||||
int Lobby::HandlePacket(Packet p) {
|
||||
int Lobby::HandlePacket(Packet::Packet p) {
|
||||
switch(p.meta.type) {
|
||||
case PacketType::NONE:
|
||||
case Packet::Type::NONE:
|
||||
//DO NOTHING
|
||||
return 0;
|
||||
break;
|
||||
case PacketType::PING:
|
||||
case Packet::Type::PING:
|
||||
//quick pong
|
||||
p.meta.type = PacketType::PONG;
|
||||
netUtil->Send(&p.meta.address, &p, sizeof(Packet));
|
||||
p.meta.type = Packet::Type::PONG;
|
||||
netUtil->Send(&p.meta.address, &p, sizeof(Packet::Packet));
|
||||
break;
|
||||
case PacketType::PONG:
|
||||
case Packet::Type::PONG:
|
||||
//
|
||||
break;
|
||||
// case PacketType::BROADCAST_REQUEST:
|
||||
// //
|
||||
// break;
|
||||
case PacketType::BROADCAST_RESPONSE:
|
||||
case Packet::Type::BROADCAST_RESPONSE:
|
||||
PushServer(p.broadcastResponse);
|
||||
break;
|
||||
// case PacketType::JOIN_REQUEST:
|
||||
// //
|
||||
// break;
|
||||
case PacketType::JOIN_RESPONSE:
|
||||
case Packet::Type::JOIN_RESPONSE:
|
||||
BeginGame(p.joinResponse);
|
||||
break;
|
||||
// case PacketType::DISCONNECT:
|
||||
@@ -203,13 +203,13 @@ int Lobby::HandlePacket(Packet p) {
|
||||
}
|
||||
|
||||
void Lobby::BroadcastNetwork() {
|
||||
Packet p;
|
||||
p.meta.type = PacketType::BROADCAST_REQUEST;
|
||||
netUtil->Send("255.255.255.255", configUtil->Int("server.port"), &p, sizeof(Packet));
|
||||
Packet::Packet p;
|
||||
p.meta.type = Packet::Type::BROADCAST_REQUEST;
|
||||
netUtil->Send("255.255.255.255", configUtil->Int("server.port"), &p, sizeof(Packet::Packet));
|
||||
serverList.clear();
|
||||
}
|
||||
|
||||
void Lobby::PushServer(BroadcastResponse& bcast) {
|
||||
void Lobby::PushServer(Packet::BroadcastResponse& bcast) {
|
||||
ServerEntry entry;
|
||||
entry.name = bcast.name;
|
||||
entry.address = bcast.meta.address;
|
||||
@@ -221,12 +221,12 @@ void Lobby::ConnectToServer(ServerEntry* server) {
|
||||
if (!server) {
|
||||
throw(runtime_error("No server received"));
|
||||
}
|
||||
Packet p;
|
||||
p.meta.type = PacketType::JOIN_REQUEST;
|
||||
netUtil->Send(&server->address, reinterpret_cast<void*>(&p), sizeof(Packet));
|
||||
Packet::Packet p;
|
||||
p.meta.type = Packet::Type::JOIN_REQUEST;
|
||||
netUtil->Send(&server->address, reinterpret_cast<void*>(&p), sizeof(Packet::Packet));
|
||||
}
|
||||
|
||||
void Lobby::BeginGame(JoinResponse& response) {
|
||||
void Lobby::BeginGame(Packet::JoinResponse& response) {
|
||||
//should be downloading the resources here as well
|
||||
infoMgr->SetClientIndex(response.clientIndex);
|
||||
netUtil->Bind(&response.meta.address, GAME_CHANNEL);
|
||||
|
||||
+7
-9
@@ -23,10 +23,13 @@
|
||||
#define LOBBY_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
|
||||
#include "utilities.hpp"
|
||||
#include "defines.hpp"
|
||||
#include "singleton.hpp"
|
||||
#include "packet_type.hpp"
|
||||
|
||||
#include "server_entry.hpp"
|
||||
#include "packet.hpp"
|
||||
#include "network_queue.hpp"
|
||||
#include "information_manager.hpp"
|
||||
|
||||
@@ -39,11 +42,6 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
struct ServerEntry {
|
||||
std::string name;
|
||||
IPaddress address;
|
||||
};
|
||||
|
||||
class Lobby : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
@@ -65,11 +63,11 @@ protected:
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//utilities
|
||||
int HandlePacket(Packet p);
|
||||
int HandlePacket(Packet::Packet p);
|
||||
void BroadcastNetwork();
|
||||
void PushServer(BroadcastResponse&);
|
||||
void PushServer(Packet::BroadcastResponse&);
|
||||
void ConnectToServer(ServerEntry*);
|
||||
void BeginGame(JoinResponse&);
|
||||
void BeginGame(Packet::JoinResponse&);
|
||||
|
||||
//services
|
||||
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "player_character.hpp"
|
||||
|
||||
void PlayerCharacter::Update(double delta) {
|
||||
if (limitSpeed) {
|
||||
constexpr double d = 1.0/sqrt(2);
|
||||
position += motion * delta * d;
|
||||
}
|
||||
else {
|
||||
position += motion * delta;
|
||||
}
|
||||
sprite.Update(delta);
|
||||
}
|
||||
|
||||
void PlayerCharacter::MoveDirection(CardinalDirection cd) {
|
||||
//shift the movement in this direction
|
||||
switch(cd) {
|
||||
case CardinalDirection::NORTH:
|
||||
if (motion.y >= 0) {
|
||||
motion.y -= WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
case CardinalDirection::SOUTH:
|
||||
if (motion.y <= 0) {
|
||||
motion.y += WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
case CardinalDirection::EAST:
|
||||
if (motion.x >= 0) {
|
||||
motion.x -= WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
case CardinalDirection::WEST:
|
||||
if (motion.x <= 0) {
|
||||
motion.x += WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
}
|
||||
//short cut
|
||||
if (motion.x != 0 && motion.y != 0) {
|
||||
sprite.SetInterval(0.1);
|
||||
limitSpeed = true;
|
||||
}
|
||||
else if (motion.x != 0 || motion.y != 0) {
|
||||
sprite.SetInterval(0.1);
|
||||
limitSpeed = false;
|
||||
}
|
||||
else {
|
||||
sprite.SetInterval(0);
|
||||
sprite.SetCurrentFrame(0);
|
||||
limitSpeed = false;
|
||||
}
|
||||
//face the correct direction
|
||||
FaceDirection();
|
||||
}
|
||||
|
||||
void PlayerCharacter::FaceDirection(CardinalDirection cd) {
|
||||
switch(cd) {
|
||||
case CardinalDirection::NORTH:
|
||||
sprite.SetCurrentStrip(1);
|
||||
break;
|
||||
case CardinalDirection::SOUTH:
|
||||
sprite.SetCurrentStrip(0);
|
||||
break;
|
||||
case CardinalDirection::EAST:
|
||||
sprite.SetCurrentStrip(2);
|
||||
break;
|
||||
case CardinalDirection::WEST:
|
||||
sprite.SetCurrentStrip(3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerCharacter::FaceDirection() {
|
||||
//base the direction on the character's movement
|
||||
if (motion.y > 0) {
|
||||
FaceDirection(CardinalDirection::SOUTH);
|
||||
}
|
||||
else if (motion.y < 0) {
|
||||
FaceDirection(CardinalDirection::NORTH);
|
||||
}
|
||||
else if (motion.x < 0) {
|
||||
FaceDirection(CardinalDirection::EAST);
|
||||
}
|
||||
else if (motion.x > 0) {
|
||||
FaceDirection(CardinalDirection::WEST);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef PLAYERCHARACTER_HPP_
|
||||
#define PLAYERCHARACTER_HPP_
|
||||
|
||||
#include "vector2.hpp"
|
||||
#include "sprite_sheet.hpp"
|
||||
#include "defines.hpp"
|
||||
|
||||
class PlayerCharacter {
|
||||
public:
|
||||
PlayerCharacter() = default;
|
||||
~PlayerCharacter() = default;
|
||||
|
||||
void Update(double delta);
|
||||
|
||||
void MoveDirection(CardinalDirection);
|
||||
void FaceDirection(CardinalDirection);
|
||||
|
||||
void DrawTo(SDL_Surface* const dest) { sprite.DrawTo(dest, position.x, position.y); }
|
||||
|
||||
//accessors and mutators
|
||||
Vector2 SetPosition(Vector2 v) { return position = v; }
|
||||
Vector2 ShiftPosition(Vector2 v) { return position += v; }
|
||||
Vector2 GetPosition() { return position; }
|
||||
|
||||
Vector2 SetMotion(Vector2 v) { return motion = v; }
|
||||
Vector2 ShiftMotion(Vector2 v) { return motion += v; }
|
||||
Vector2 GetMotion() { return motion; }
|
||||
|
||||
SpriteSheet* GetSprite() { return &sprite; }
|
||||
private:
|
||||
void FaceDirection();
|
||||
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
SpriteSheet sprite;
|
||||
|
||||
//for moving diagonal
|
||||
bool limitSpeed = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -19,14 +19,13 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef CHARACTER_HPP_
|
||||
#define CHARACTER_HPP_
|
||||
#ifndef CLIENTENTRY_HPP_
|
||||
#define CLIENTENTRY_HPP_
|
||||
|
||||
class Character {
|
||||
public:
|
||||
Character();
|
||||
~Character();
|
||||
private:
|
||||
struct ClientEntry {
|
||||
int index;
|
||||
int channel;
|
||||
int playerIndex;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -25,4 +25,10 @@
|
||||
#define GAME_CHANNEL 0
|
||||
#define CHAT_CHANNEL 1
|
||||
|
||||
#define WALKING_SPEED 140
|
||||
|
||||
enum class CardinalDirection {
|
||||
NORTH, SOUTH, EAST, WEST
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
static SDL_sem* lock = SDL_CreateSemaphore(1);
|
||||
static SDL_Thread* queueThread = nullptr;
|
||||
|
||||
static std::deque<Packet> queue;
|
||||
static std::deque<Packet::Packet> queue;
|
||||
|
||||
static bool running = false;
|
||||
|
||||
@@ -41,8 +41,8 @@ static int networkQueue(void*) {
|
||||
while(running) {
|
||||
SDL_SemWait(lock);
|
||||
while(netUtil->Receive()) {
|
||||
Packet p;
|
||||
memcpy(&p, netUtil->GetInData(), sizeof(Packet));
|
||||
Packet::Packet p;
|
||||
memcpy(&p, netUtil->GetInData(), sizeof(Packet::Packet));
|
||||
p.meta.address = netUtil->GetInPacket()->address;
|
||||
queue.push_back(p);
|
||||
}
|
||||
@@ -80,19 +80,19 @@ void killQueueThread() {
|
||||
queueThread = nullptr;
|
||||
}
|
||||
|
||||
Packet peekNetworkPacket() {
|
||||
Packet::Packet peekNetworkPacket() {
|
||||
SDL_SemWait(lock);
|
||||
Packet p;
|
||||
Packet::Packet p;
|
||||
if (queue.size() > 0) {
|
||||
Packet p = queue[0];
|
||||
Packet::Packet p = queue[0];
|
||||
}
|
||||
SDL_SemPost(lock);
|
||||
return p;
|
||||
}
|
||||
|
||||
Packet popNetworkPacket() {
|
||||
Packet::Packet popNetworkPacket() {
|
||||
SDL_SemWait(lock);
|
||||
Packet p;
|
||||
Packet::Packet p;
|
||||
if (queue.size() > 0) {
|
||||
p = queue[0];
|
||||
queue.pop_front();
|
||||
|
||||
@@ -22,13 +22,13 @@
|
||||
#ifndef NETWORKQUEUE_HPP_
|
||||
#define NETWORKQUEUE_HPP_
|
||||
|
||||
#include "packet_type.hpp"
|
||||
#include "packet.hpp"
|
||||
|
||||
void beginQueueThread();
|
||||
void endQueueThread();
|
||||
void killQueueThread();
|
||||
Packet peekNetworkPacket();
|
||||
Packet popNetworkPacket();
|
||||
Packet::Packet peekNetworkPacket();
|
||||
Packet::Packet popNetworkPacket();
|
||||
void flushNetworkQueue();
|
||||
|
||||
#endif
|
||||
|
||||
@@ -30,7 +30,9 @@
|
||||
|
||||
#pragma pack(push, 0)
|
||||
|
||||
enum class PacketType {
|
||||
namespace Packet {
|
||||
|
||||
enum class Type {
|
||||
NONE = 0,
|
||||
|
||||
PING = 1,
|
||||
@@ -43,13 +45,11 @@ enum class PacketType {
|
||||
|
||||
SYNCHRONIZE = 8,
|
||||
|
||||
PLAYER_NEW = 9,
|
||||
PLAYER_DELETE = 10,
|
||||
PLAYER_MOVE = 11,
|
||||
PLAYER = 9,
|
||||
};
|
||||
|
||||
struct Metadata {
|
||||
PacketType type;
|
||||
Type type;
|
||||
IPaddress address;
|
||||
};
|
||||
|
||||
@@ -90,27 +90,15 @@ struct Synchronize {
|
||||
Metadata meta;
|
||||
};
|
||||
|
||||
struct PlayerNew {
|
||||
struct Player {
|
||||
Metadata meta;
|
||||
int playerIndex;
|
||||
//TODO Playerdata
|
||||
//player data
|
||||
};
|
||||
|
||||
struct PlayerDelete {
|
||||
Metadata meta;
|
||||
int playerIndex;
|
||||
};
|
||||
|
||||
struct PlayerMove {
|
||||
Metadata meta;
|
||||
int playerIndex;
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
};
|
||||
|
||||
union Packet {
|
||||
Packet() {
|
||||
meta.type = PacketType::NONE;
|
||||
meta.type = Type::NONE;
|
||||
meta.address.host = 0;
|
||||
meta.address.port = 0;
|
||||
};
|
||||
@@ -124,15 +112,17 @@ union Packet {
|
||||
JoinResponse joinResponse;
|
||||
Disconnect disconnect;
|
||||
|
||||
PlayerNew playerNew;
|
||||
PlayerDelete playerDelete;
|
||||
PlayerMove playerMove;
|
||||
Synchronize sync;
|
||||
|
||||
Player player;
|
||||
|
||||
#ifdef DEBUG
|
||||
char buffer[1024];
|
||||
#endif
|
||||
};
|
||||
|
||||
} //namespace Packet
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,38 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef PLAYERENTRY_HPP_
|
||||
#define PLAYERENTRY_HPP_
|
||||
|
||||
#include "vector2.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
struct PlayerEntry {
|
||||
int index;
|
||||
int clientIndex;
|
||||
std::string handle;
|
||||
std::string avatar;
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -19,6 +19,16 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "character.hpp"
|
||||
#ifndef SERVERENTRY_HPP_
|
||||
#define SERVERENTRY_HPP_
|
||||
|
||||
|
||||
#include "SDL_net/SDL_net.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
struct ServerEntry {
|
||||
std::string name;
|
||||
IPaddress address;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -70,7 +70,7 @@ void ServerApplication::Init() {
|
||||
}
|
||||
|
||||
//initiate the remaining singletons
|
||||
netUtil->Open(configUtil->Int("server.port"), sizeof(Packet));
|
||||
netUtil->Open(configUtil->Int("server.port"), sizeof(Packet::Packet));
|
||||
|
||||
//create the threads
|
||||
beginQueueThread();
|
||||
@@ -115,42 +115,40 @@ void ServerApplication::Quit() {
|
||||
//-------------------------
|
||||
|
||||
void ServerApplication::UpdateWorld(double delta) {
|
||||
for (auto it : players) {
|
||||
it.second.Update(delta);
|
||||
}
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Network loop
|
||||
//-------------------------
|
||||
|
||||
int ServerApplication::HandlePacket(Packet p) {
|
||||
int ServerApplication::HandlePacket(Packet::Packet p) {
|
||||
switch(p.meta.type) {
|
||||
case PacketType::NONE:
|
||||
case Packet::Type::NONE:
|
||||
//DO NOTHING
|
||||
return 0;
|
||||
break;
|
||||
case PacketType::PING:
|
||||
case Packet::Type::PING:
|
||||
//quick pong
|
||||
p.meta.type = PacketType::PONG;
|
||||
netUtil->Send(&p.meta.address, &p, sizeof(Packet));
|
||||
p.meta.type = Packet::Type::PONG;
|
||||
netUtil->Send(&p.meta.address, &p, sizeof(Packet::Packet));
|
||||
break;
|
||||
case PacketType::PONG:
|
||||
case Packet::Type::PONG:
|
||||
//
|
||||
break;
|
||||
case PacketType::BROADCAST_REQUEST:
|
||||
case Packet::Type::BROADCAST_REQUEST:
|
||||
HandleBroadcast(p.broadcastRequest);
|
||||
break;
|
||||
// case PacketType::BROADCAST_RESPONSE:
|
||||
// //
|
||||
// break;
|
||||
case PacketType::JOIN_REQUEST:
|
||||
case Packet::Type::JOIN_REQUEST:
|
||||
HandleConnection(p.joinRequest);
|
||||
break;
|
||||
// case PacketType::JOIN_RESPONSE:
|
||||
// //
|
||||
// break;
|
||||
case PacketType::DISCONNECT:
|
||||
case Packet::Type::DISCONNECT:
|
||||
HandleDisconnection(p.disconnect);
|
||||
break;
|
||||
// case PacketType::SYNCHRONIZE:
|
||||
@@ -171,22 +169,22 @@ int ServerApplication::HandlePacket(Packet p) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void ServerApplication::HandleBroadcast(BroadcastRequest& bcast) {
|
||||
void ServerApplication::HandleBroadcast(Packet::BroadcastRequest& bcast) {
|
||||
//respond to a broadcast request with the server's data
|
||||
Packet p;
|
||||
p.meta.type = PacketType::BROADCAST_RESPONSE;
|
||||
Packet::Packet p;
|
||||
p.meta.type = Packet::Type::BROADCAST_RESPONSE;
|
||||
snprintf(p.broadcastResponse.name, PACKET_STRING_SIZE, "%s", configUtil->CString("server.name"));
|
||||
//TODO version information
|
||||
netUtil->Send(&bcast.meta.address, &p, sizeof(Packet));
|
||||
netUtil->Send(&bcast.meta.address, &p, sizeof(Packet::Packet));
|
||||
}
|
||||
|
||||
void ServerApplication::HandleConnection(JoinRequest& request) {
|
||||
void ServerApplication::HandleConnection(Packet::JoinRequest& request) {
|
||||
if (clients.size() >= SDLNET_MAX_UDPCHANNELS) {
|
||||
//ignore the new connection if there's too many clients connected
|
||||
return;
|
||||
}
|
||||
//create the containers
|
||||
ClientData client = { uniqueIndex++ };
|
||||
ClientEntry client = { uniqueIndex++ };
|
||||
|
||||
//bind the address
|
||||
client.channel = netUtil->Bind(&request.meta.address);
|
||||
@@ -195,20 +193,20 @@ void ServerApplication::HandleConnection(JoinRequest& request) {
|
||||
clients[client.index] = client;
|
||||
|
||||
//send the player their information
|
||||
Packet p;
|
||||
p.meta.type = PacketType::JOIN_RESPONSE;
|
||||
Packet::Packet p;
|
||||
p.meta.type = Packet::Type::JOIN_RESPONSE;
|
||||
p.joinResponse.clientIndex = client.index;
|
||||
//TODO: resource list
|
||||
netUtil->Send(client.channel, &p, sizeof(Packet));
|
||||
netUtil->Send(client.channel, &p, sizeof(Packet::Packet));
|
||||
|
||||
//pretty
|
||||
cout << "New connection: index " << client.index << endl;
|
||||
cout << "number of clients: " << clients.size() << endl;
|
||||
}
|
||||
|
||||
void ServerApplication::HandleDisconnection(Disconnect& disconnect) {
|
||||
void ServerApplication::HandleDisconnection(Packet::Disconnect& disconnect) {
|
||||
//disconnect a client (redundant message)
|
||||
netUtil->Send(clients[disconnect.clientIndex].channel, &disconnect, sizeof(Packet));
|
||||
netUtil->Send(clients[disconnect.clientIndex].channel, &disconnect, sizeof(Packet::Packet));
|
||||
netUtil->Unbind(clients[disconnect.clientIndex].channel);
|
||||
clients.erase(disconnect.clientIndex);
|
||||
|
||||
|
||||
@@ -23,10 +23,13 @@
|
||||
#define SERVERAPPLICATION_HPP_
|
||||
|
||||
#include "utilities.hpp"
|
||||
#include "packet_type.hpp"
|
||||
#include "packet.hpp"
|
||||
#include "singleton.hpp"
|
||||
#include "network_queue.hpp"
|
||||
|
||||
#include "client_entry.hpp"
|
||||
#include "player_entry.hpp"
|
||||
|
||||
#include "config_Utility.hpp"
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "vector2.hpp"
|
||||
@@ -40,25 +43,6 @@
|
||||
//lazy
|
||||
typedef std::chrono::high_resolution_clock Clock;
|
||||
|
||||
struct ClientData {
|
||||
int index;
|
||||
int channel;
|
||||
int playerIndex;
|
||||
};
|
||||
|
||||
struct PlayerData {
|
||||
int index;
|
||||
int clientIndex;
|
||||
std::string handle;
|
||||
std::string avatar;
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
|
||||
void Update(double delta) {
|
||||
position += motion * delta;
|
||||
}
|
||||
};
|
||||
|
||||
class ServerApplication {
|
||||
public:
|
||||
ServerApplication();
|
||||
@@ -74,10 +58,10 @@ private:
|
||||
void UpdateWorld(double delta);
|
||||
|
||||
//network loop
|
||||
int HandlePacket(Packet p);
|
||||
void HandleBroadcast(BroadcastRequest&);
|
||||
void HandleConnection(JoinRequest&);
|
||||
void HandleDisconnection(Disconnect&);
|
||||
int HandlePacket(Packet::Packet p);
|
||||
void HandleBroadcast(Packet::BroadcastRequest&);
|
||||
void HandleConnection(Packet::JoinRequest&);
|
||||
void HandleDisconnection(Packet::Disconnect&);
|
||||
|
||||
//services
|
||||
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||
@@ -86,8 +70,8 @@ private:
|
||||
//members
|
||||
Clock::time_point lastTick = Clock::now();
|
||||
|
||||
std::map<int, ClientData> clients;
|
||||
std::map<int, PlayerData> players;
|
||||
std::map<int, ClientEntry> clients;
|
||||
std::map<int, PlayerEntry> players;
|
||||
|
||||
bool running = false;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user