Implemented SharedParameters system in the client (read more)
Here are more tweaks: * InWorld's quit event now exits to the main menu, bypassing a relayed disconnect message * InWorld's disconnect hander no longer throws exceptions (dropped creation packet bugfix) * CombatData's internals now point to std::pair objects * Enemies are stored in a global list
This commit is contained in:
@@ -119,13 +119,13 @@ void ClientApplication::LoadScene(SceneList sceneIndex) {
|
||||
activeScene = new OptionsMenu(&config);
|
||||
break;
|
||||
case SceneList::LOBBYMENU:
|
||||
activeScene = new LobbyMenu(&config, &network, &clientIndex, &accountIndex, &characterIndex);
|
||||
activeScene = new LobbyMenu(&config, &network, ¶ms);
|
||||
break;
|
||||
case SceneList::INWORLD:
|
||||
activeScene = new InWorld(&config, &network, &clientIndex, &accountIndex, &characterIndex);
|
||||
activeScene = new InWorld(&config, &network, ¶ms);
|
||||
break;
|
||||
case SceneList::INCOMBAT:
|
||||
activeScene = new InCombat();
|
||||
activeScene = new InCombat(&config, &network, ¶ms);
|
||||
break;
|
||||
default:
|
||||
throw(std::logic_error("Failed to recognize the scene index"));
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "scene_list.hpp"
|
||||
#include "base_scene.hpp"
|
||||
|
||||
#include "shared_parameters.hpp"
|
||||
#include "config_utility.hpp"
|
||||
#include "udp_network_utility.hpp"
|
||||
|
||||
@@ -47,9 +48,7 @@ private:
|
||||
//shared parameters
|
||||
ConfigUtility config;
|
||||
UDPNetworkUtility network;
|
||||
int clientIndex = -1;
|
||||
int accountIndex = -1;
|
||||
int characterIndex = -1;
|
||||
SharedParameters params;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -25,7 +25,11 @@
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
InCombat::InCombat() {
|
||||
InCombat::InCombat(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, SharedParameters* const argParams):
|
||||
config(*argConfig),
|
||||
network(*argNetwork),
|
||||
params(*argParams)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
@@ -57,6 +61,10 @@ void InCombat::Render(SDL_Surface* const screen) {
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void InCombat::QuitEvent() {
|
||||
//
|
||||
}
|
||||
|
||||
void InCombat::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
//
|
||||
}
|
||||
@@ -80,3 +88,7 @@ void InCombat::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
void InCombat::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
|
||||
void InCombat::HandlePacket(SerialPacket& packet) {
|
||||
//
|
||||
}
|
||||
@@ -22,12 +22,25 @@
|
||||
#ifndef INCOMBAT_HPP_
|
||||
#define INCOMBAT_HPP_
|
||||
|
||||
//graphics & utilities
|
||||
#include "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "button.hpp"
|
||||
#include "config_utility.hpp"
|
||||
#include "shared_parameters.hpp"
|
||||
|
||||
//network
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "serial_packet.hpp"
|
||||
#include "serial.hpp"
|
||||
|
||||
//client
|
||||
#include "base_scene.hpp"
|
||||
|
||||
class InCombat : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
InCombat();
|
||||
InCombat(ConfigUtility* const, UDPNetworkUtility* const, SharedParameters* const);
|
||||
~InCombat();
|
||||
|
||||
protected:
|
||||
@@ -38,11 +51,20 @@ protected:
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
//Event handlers
|
||||
void QuitEvent();
|
||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//Network handlers
|
||||
void HandlePacket(SerialPacket&);
|
||||
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
UDPNetworkUtility& network;
|
||||
SharedParameters& params;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+26
-31
@@ -31,12 +31,10 @@
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex):
|
||||
InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, SharedParameters* const argParams):
|
||||
config(*argConfig),
|
||||
network(*argNetwork),
|
||||
clientIndex(*argClientIndex),
|
||||
accountIndex(*argAccountIndex),
|
||||
characterIndex(*argCharacterIndex)
|
||||
params(*argParams)
|
||||
{
|
||||
//setup the utility objects
|
||||
buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
||||
@@ -68,9 +66,9 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet
|
||||
SerialPacket packet;
|
||||
char buffer[PACKET_STRING_SIZE];
|
||||
packet.meta.type = SerialPacket::Type::SYNCHRONIZE;
|
||||
packet.clientInfo.clientIndex = clientIndex;
|
||||
packet.clientInfo.accountIndex = accountIndex;
|
||||
packet.clientInfo.characterIndex = characterIndex;
|
||||
packet.clientInfo.clientIndex = params.clientIndex;
|
||||
packet.clientInfo.accountIndex = params.accountIndex;
|
||||
packet.clientInfo.characterIndex = params.characterIndex;
|
||||
serialize(&packet, buffer);
|
||||
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
|
||||
|
||||
@@ -151,6 +149,7 @@ void InWorld::Render(SDL_Surface* const screen) {
|
||||
void InWorld::QuitEvent() {
|
||||
//exit the game AND the server
|
||||
RequestDisconnect();
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
}
|
||||
|
||||
void InWorld::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
@@ -273,17 +272,15 @@ void InWorld::HandlePacket(SerialPacket packet) {
|
||||
|
||||
void InWorld::HandleDisconnect(SerialPacket packet) {
|
||||
network.Unbind(Channels::SERVER);
|
||||
clientIndex = -1;
|
||||
accountIndex = -1;
|
||||
characterIndex = -1;
|
||||
params.clientIndex = -1;
|
||||
params.accountIndex = -1;
|
||||
params.characterIndex = -1;
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
}
|
||||
|
||||
void InWorld::HandleRegionContent(SerialPacket packet) {
|
||||
//replace existing regions
|
||||
if (regionPager.FindRegion(packet.regionInfo.x, packet.regionInfo.y)) {
|
||||
regionPager.UnloadRegion(packet.regionInfo.x, packet.regionInfo.y);
|
||||
}
|
||||
regionPager.UnloadRegion(packet.regionInfo.x, packet.regionInfo.y);
|
||||
regionPager.PushRegion(packet.regionInfo.region);
|
||||
packet.regionInfo.region = nullptr;
|
||||
}
|
||||
@@ -295,7 +292,7 @@ void InWorld::HandleCharacterUpdate(SerialPacket packet) {
|
||||
}
|
||||
|
||||
//update only if the message didn't originate from here
|
||||
if (packet.characterInfo.clientIndex != clientIndex) {
|
||||
if (packet.characterInfo.clientIndex != params.clientIndex) {
|
||||
playerCharacters[packet.characterInfo.characterIndex].SetPosition(packet.characterInfo.position);
|
||||
playerCharacters[packet.characterInfo.characterIndex].SetMotion(packet.characterInfo.motion);
|
||||
}
|
||||
@@ -308,14 +305,15 @@ void InWorld::HandleCharacterNew(SerialPacket packet) {
|
||||
}
|
||||
|
||||
//TODO: set the player's handle
|
||||
//TODO: use a reference, don't use a lookup for every call
|
||||
playerCharacters[packet.characterInfo.characterIndex].GetSprite()->LoadSurface(config["dir.sprites"] + packet.characterInfo.avatar, 4, 4);
|
||||
playerCharacters[packet.characterInfo.characterIndex].SetPosition(packet.characterInfo.position);
|
||||
playerCharacters[packet.characterInfo.characterIndex].SetMotion(packet.characterInfo.motion);
|
||||
playerCharacters[packet.characterInfo.characterIndex].ResetDirection();
|
||||
|
||||
//catch this client's player object
|
||||
if (packet.characterInfo.characterIndex == characterIndex && !localCharacter) {
|
||||
localCharacter = &playerCharacters[characterIndex];
|
||||
if (packet.characterInfo.characterIndex == params.characterIndex && !localCharacter) {
|
||||
localCharacter = &playerCharacters[params.characterIndex];
|
||||
|
||||
//setup the camera
|
||||
camera.width = GetScreen()->w;
|
||||
@@ -327,16 +325,13 @@ void InWorld::HandleCharacterNew(SerialPacket packet) {
|
||||
}
|
||||
|
||||
void InWorld::HandleCharacterDelete(SerialPacket packet) {
|
||||
//TODO: authenticate
|
||||
if (playerCharacters.find(packet.characterInfo.characterIndex) == playerCharacters.end()) {
|
||||
throw(std::runtime_error("Cannot delete non-existant characters"));
|
||||
}
|
||||
//TODO: authenticate when own character is being deleted
|
||||
|
||||
playerCharacters.erase(packet.characterInfo.characterIndex);
|
||||
|
||||
//catch this client's player object
|
||||
if (packet.characterInfo.characterIndex == characterIndex) {
|
||||
characterIndex = -1;
|
||||
if (packet.characterInfo.characterIndex == params.characterIndex) {
|
||||
params.characterIndex = -1;
|
||||
localCharacter = nullptr;
|
||||
}
|
||||
}
|
||||
@@ -351,9 +346,9 @@ void InWorld::SendPlayerUpdate() {
|
||||
|
||||
//pack the packet
|
||||
packet.meta.type = SerialPacket::Type::CHARACTER_UPDATE;
|
||||
packet.characterInfo.clientIndex = clientIndex;
|
||||
packet.characterInfo.accountIndex = accountIndex;
|
||||
packet.characterInfo.characterIndex = characterIndex;
|
||||
packet.characterInfo.clientIndex = params.clientIndex;
|
||||
packet.characterInfo.accountIndex = params.accountIndex;
|
||||
packet.characterInfo.characterIndex = params.characterIndex;
|
||||
packet.characterInfo.position = localCharacter->GetPosition();
|
||||
packet.characterInfo.motion = localCharacter->GetMotion();
|
||||
|
||||
@@ -367,9 +362,9 @@ void InWorld::RequestDisconnect() {
|
||||
|
||||
//send a disconnect request
|
||||
packet.meta.type = SerialPacket::Type::DISCONNECT;
|
||||
packet.clientInfo.clientIndex = clientIndex;
|
||||
packet.clientInfo.accountIndex = accountIndex;
|
||||
packet.clientInfo.characterIndex = characterIndex;
|
||||
packet.clientInfo.clientIndex = params.clientIndex;
|
||||
packet.clientInfo.accountIndex = params.accountIndex;
|
||||
packet.clientInfo.characterIndex = params.characterIndex;
|
||||
serialize(&packet, buffer);
|
||||
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
|
||||
}
|
||||
@@ -380,9 +375,9 @@ void InWorld::RequestShutDown() {
|
||||
|
||||
//send a shutdown request
|
||||
packet.meta.type = SerialPacket::Type::SHUTDOWN;
|
||||
packet.clientInfo.clientIndex = clientIndex;
|
||||
packet.clientInfo.accountIndex = accountIndex;
|
||||
packet.clientInfo.characterIndex = characterIndex;
|
||||
packet.clientInfo.clientIndex = params.clientIndex;
|
||||
packet.clientInfo.accountIndex = params.accountIndex;
|
||||
packet.clientInfo.characterIndex = params.characterIndex;
|
||||
serialize(&packet, buffer);
|
||||
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
//client
|
||||
#include "base_scene.hpp"
|
||||
#include "player_character.hpp"
|
||||
#include "shared_parameters.hpp"
|
||||
|
||||
//STL
|
||||
#include <map>
|
||||
@@ -52,7 +53,7 @@
|
||||
class InWorld : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
InWorld(ConfigUtility* const, UDPNetworkUtility* const, int* const, int* const, int* const);
|
||||
InWorld(ConfigUtility* const, UDPNetworkUtility* const, SharedParameters* const);
|
||||
~InWorld();
|
||||
|
||||
protected:
|
||||
@@ -91,9 +92,7 @@ protected:
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
UDPNetworkUtility& network;
|
||||
int& clientIndex;
|
||||
int& accountIndex;
|
||||
int& characterIndex;
|
||||
SharedParameters& params;
|
||||
|
||||
//graphics
|
||||
Image buttonImage;
|
||||
|
||||
@@ -30,12 +30,10 @@
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
LobbyMenu::LobbyMenu(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex):
|
||||
LobbyMenu::LobbyMenu(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, SharedParameters* const argParams):
|
||||
config(*argConfig),
|
||||
network(*argNetwork),
|
||||
clientIndex(*argClientIndex),
|
||||
accountIndex(*argAccountIndex),
|
||||
characterIndex(*argCharacterIndex)
|
||||
params(*argParams)
|
||||
{
|
||||
//setup the utility objects
|
||||
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
||||
@@ -94,6 +92,7 @@ void LobbyMenu::FrameEnd() {
|
||||
}
|
||||
|
||||
void LobbyMenu::Render(SDL_Surface* const screen) {
|
||||
//TODO: this needs rewriting
|
||||
//TODO: I need a proper UI system for the entire client and the editor
|
||||
//UI
|
||||
search.DrawTo(screen);
|
||||
@@ -221,9 +220,9 @@ void LobbyMenu::HandlePacket(SerialPacket packet) {
|
||||
}
|
||||
break;
|
||||
case SerialPacket::Type::JOIN_RESPONSE:
|
||||
clientIndex = packet.clientInfo.clientIndex;
|
||||
accountIndex = packet.clientInfo.accountIndex;
|
||||
characterIndex = packet.clientInfo.characterIndex;
|
||||
params.clientIndex = packet.clientInfo.clientIndex;
|
||||
params.accountIndex = packet.clientInfo.accountIndex;
|
||||
params.characterIndex = packet.clientInfo.characterIndex;
|
||||
network.Bind(&packet.meta.srcAddress, Channels::SERVER);
|
||||
SetNextScene(SceneList::INWORLD);
|
||||
break;
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "raster_font.hpp"
|
||||
#include "button.hpp"
|
||||
#include "config_utility.hpp"
|
||||
#include "shared_parameters.hpp"
|
||||
|
||||
//network
|
||||
#include "udp_network_utility.hpp"
|
||||
@@ -42,7 +43,7 @@
|
||||
class LobbyMenu : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const, int* const, int* const, int* const);
|
||||
LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const, SharedParameters* const);
|
||||
~LobbyMenu();
|
||||
|
||||
protected:
|
||||
@@ -64,9 +65,7 @@ protected:
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
UDPNetworkUtility& network;
|
||||
int& clientIndex;
|
||||
int& accountIndex;
|
||||
int& characterIndex;
|
||||
SharedParameters& params;
|
||||
|
||||
//members
|
||||
Image image;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* 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 SHAREDPARAMETERS_HPP_
|
||||
#define SHAREDPARAMETERS_HPP_
|
||||
|
||||
struct SharedParameters {
|
||||
int clientIndex = -1;
|
||||
int accountIndex = -1;
|
||||
int characterIndex = -1;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user