From 1776583e01db4e9dda3a8dd358ac6b65f295e276 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 13 Aug 2014 08:08:16 +1000 Subject: [PATCH] Added the escape hotkey, refined BoundingBox a bit LobbyMenu also searches for a server as soon as you enter, so you don't have to press an extra button, a good feature. --- client/scenes/clean_up.cpp | 6 +- client/scenes/in_combat.cpp.txt | 220 ------------------------------ client/scenes/in_combat.hpp.txt | 94 ------------- client/scenes/in_world.cpp | 7 + client/scenes/lobby_menu.cpp | 74 ++++++---- client/scenes/lobby_menu.hpp | 9 +- client/scenes/options_menu.cpp | 6 +- common/utilities/bounding_box.hpp | 3 +- common/utilities/check_bounds.cpp | 40 ------ common/utilities/check_bounds.hpp | 30 ---- todo.txt | 1 + 11 files changed, 72 insertions(+), 418 deletions(-) delete mode 100644 client/scenes/in_combat.cpp.txt delete mode 100644 client/scenes/in_combat.hpp.txt delete mode 100644 common/utilities/check_bounds.cpp delete mode 100644 common/utilities/check_bounds.hpp diff --git a/client/scenes/clean_up.cpp b/client/scenes/clean_up.cpp index c5b5d0f..416a1c8 100644 --- a/client/scenes/clean_up.cpp +++ b/client/scenes/clean_up.cpp @@ -124,7 +124,11 @@ void CleanUp::MouseButtonUp(SDL_MouseButtonEvent const& button) { } void CleanUp::KeyDown(SDL_KeyboardEvent const& key) { - // + switch(key.keysym.sym) { + case SDLK_ESCAPE: + SetNextScene(SceneList::MAINMENU); + break; + } } void CleanUp::KeyUp(SDL_KeyboardEvent const& key) { diff --git a/client/scenes/in_combat.cpp.txt b/client/scenes/in_combat.cpp.txt deleted file mode 100644 index 358bd08..0000000 --- a/client/scenes/in_combat.cpp.txt +++ /dev/null @@ -1,220 +0,0 @@ -/* 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 - * 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 "in_combat.hpp" - -#include "channels.hpp" -#include "utility.hpp" -#include "config_utility.hpp" - -#include - -//------------------------- -//Public access members -//------------------------- - -InCombat::InCombat( - UDPNetworkUtility* const argNetwork, - int* const argClientIndex, - int* const argAccountIndex, - int* const argCharacterIndex, - CharacterMap* argCharacterMap - ): - network(*argNetwork), - clientIndex(*argClientIndex), - accountIndex(*argAccountIndex), - characterIndex(*argCharacterIndex), - characterMap(*argCharacterMap) -{ -/* //setup the utility objects - buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp"); - buttonImage.SetClipH(buttonImage.GetClipH()/3); - font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp"); - - //pass the utility objects - backButton.SetImage(&buttonImage); - backButton.SetFont(&font); - - //set the button positions - backButton.SetX(50); - backButton.SetY(50 + buttonImage.GetClipH() * 0); - - //set the button texts - backButton.SetText("Back"); - - //request a sync - RequestSynchronize(); -*/ - //debug - // -} - -InCombat::~InCombat() { - // -} - -//------------------------- -//Frame loop -//------------------------- - -void InCombat::FrameStart() { - // -} - -void InCombat::Update(double delta) { - //suck in and process all waiting packets - SerialPacket* packetBuffer = static_cast(malloc(MAX_PACKET_SIZE)); - while(network.Receive(packetBuffer)) { - HandlePacket(packetBuffer); - } - free(static_cast(packetBuffer)); - - //TODO: more -} - -void InCombat::FrameEnd() { - // -} - -void InCombat::RenderFrame() { - SDL_FillRect(GetScreen(), 0, 0); - Render(GetScreen()); - SDL_Flip(GetScreen()); - fps.Calculate(); -} - -void InCombat::Render(SDL_Surface* const screen) { - //TODO: draw the background - - //TODO: draw the characters - - //TODO: draw the enemies - - //TODO: draw the UI -} - -//------------------------- -//Event handlers -//------------------------- - -void InCombat::QuitEvent() { - //exit the game AND the server - RequestDisconnect(); - SetNextScene(SceneList::QUIT); -} - -void InCombat::MouseMotion(SDL_MouseMotionEvent const& motion) { - // -} - -void InCombat::MouseButtonDown(SDL_MouseButtonEvent const& button) { - // -} - -void InCombat::MouseButtonUp(SDL_MouseButtonEvent const& button) { - // -} - -void InCombat::KeyDown(SDL_KeyboardEvent const& key) { - // -} - -void InCombat::KeyUp(SDL_KeyboardEvent const& key) { - // -} - -//------------------------- -//Network handlers -//------------------------- - -void InCombat::HandlePacket(SerialPacket* const argPacket) { - switch(argPacket->type) { - case SerialPacketType::DISCONNECT: - HandleDisconnect(argPacket); - break; - //handle errors - default: - throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in InCombat: " + to_string_custom(static_cast(argPacket->type)) )); - break; - } -} - -void InCombat::HandleDisconnect(SerialPacket* const) { - SetNextScene(SceneList::CLEANUP); -} - -//TODO: more network handlers - -//------------------------- -//Server control -//------------------------- - -void InCombat::RequestSynchronize() { - ClientPacket newPacket; - - //request a sync - newPacket.type = SerialPacketType::SYNCHRONIZE; - newPacket.clientIndex = clientIndex; - newPacket.accountIndex = accountIndex; - - network.SendTo(Channels::SERVER, &newPacket); -} - -void InCombat::SendPlayerUpdate() { - CharacterPacket newPacket; - - //pack the packet - newPacket.type = SerialPacketType::CHARACTER_UPDATE; - - newPacket.characterIndex = characterIndex; - //handle, avatar - newPacket.accountIndex = accountIndex; -// newPacket.roomIndex = localCharacter->roomIndex; -// newPacket.origin = localCharacter->origin; -// newPacket.motion = localCharacter->motion; -// newPacket.stats = localCharacter->stats; - - //TODO: gameplay components: equipment, items, buffs, debuffs - - network.SendTo(Channels::SERVER, &newPacket); -} - -void InCombat::RequestDisconnect() { - ClientPacket newPacket; - - //send a disconnect request - newPacket.type = SerialPacketType::DISCONNECT; - newPacket.clientIndex = clientIndex; - newPacket.accountIndex = accountIndex; - - network.SendTo(Channels::SERVER, &newPacket); -} - -void InCombat::RequestShutdown() { - ClientPacket newPacket; - - //send a shutdown request - newPacket.type = SerialPacketType::SHUTDOWN; - newPacket.clientIndex = clientIndex; - newPacket.accountIndex = accountIndex; - - network.SendTo(Channels::SERVER, &newPacket); -} diff --git a/client/scenes/in_combat.hpp.txt b/client/scenes/in_combat.hpp.txt deleted file mode 100644 index 2f5cd20..0000000 --- a/client/scenes/in_combat.hpp.txt +++ /dev/null @@ -1,94 +0,0 @@ -/* 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 - * 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 INCOMBAT_HPP_ -#define INCOMBAT_HPP_ - -//network -#include "udp_network_utility.hpp" - -//graphics -#include "image.hpp" -#include "raster_font.hpp" -#include "button.hpp" - -//common -#include "frame_rate.hpp" - -#include "character.hpp" - -//client -#include "base_scene.hpp" - -class InCombat : public BaseScene { -public: - //Public access members - InCombat( - UDPNetworkUtility* const argNetwork, - int* const argClientIndex, - int* const argAccountIndex, - int* const argCharacterIndex, - CharacterMap* argCharacterMap - ); - ~InCombat(); - -protected: - //Frame loop - void FrameStart(); - void Update(double delta); - void FrameEnd(); - void RenderFrame(); - 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* const); - void HandleDisconnect(SerialPacket* const); - - //Server control - void RequestSynchronize(); - void SendPlayerUpdate(); - void RequestDisconnect(); - void RequestShutdown(); - - //shared parameters - UDPNetworkUtility& network; - int& clientIndex; - int& accountIndex; - int& characterIndex; - CharacterMap& characterMap; - - //graphics - //TODO: graphics - - //UI - //TODO: UI - FrameRate fps; -}; - -#endif diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 8dbd0e6..ff0f67b 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -205,6 +205,13 @@ void InWorld::KeyDown(SDL_KeyboardEvent const& key) { return; } + //hotkeys + switch(key.keysym.sym) { + case SDLK_ESCAPE: + RequestDisconnect(); + break; + } + //player movement Vector2 motion = localCharacter->GetMotion(); switch(key.keysym.sym) { diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index 4b7b356..d866159 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -65,6 +65,9 @@ LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex): //BUGFIX: Eat incoming packets while(network.Receive()); + + //Initial broadcast + SendBroadcastRequest(); } LobbyMenu::~LobbyMenu() { @@ -104,7 +107,10 @@ void LobbyMenu::Render(SDL_Surface* const screen) { for (int i = 0; i < serverInfo.size(); i++) { //draw the selected server's highlight if (selection == &serverInfo[i]) { - SDL_Rect r = listBox; + SDL_Rect r = { + (Sint16)listBox.x, (Sint16)listBox.y, + (Uint16)listBox.w, (Uint16)listBox.h + }; r.y += i * listBox.h; SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 255, 127, 39)); } @@ -142,39 +148,19 @@ void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) { void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { if (search.MouseButtonUp(button) == Button::State::HOVER) { - //broadcast to the network, or a specific server - SerialPacket packet; - packet.type = SerialPacketType::BROADCAST_REQUEST; - network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet); - - //reset the server list - serverInfo.clear(); - selection = nullptr; + SendBroadcastRequest(); } - else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) { - //pack the packet - ClientPacket packet; - packet.type = SerialPacketType::JOIN_REQUEST; - strncpy(packet.username, config["client.username"].c_str(), PACKET_STRING_SIZE); - - //join the selected server - network.SendTo(&selection->address, &packet); - selection = nullptr; + SendJoinRequest(); } - else if (back.MouseButtonUp(button) == Button::State::HOVER) { SetNextScene(SceneList::MAINMENU); } - else if ( - //has the user selected a server on the list? - //TODO: replace with regular collision checker - button.x > listBox.x && - button.x < listBox.x + listBox.w && - button.y > listBox.y && - button.y < listBox.y + listBox.h * serverInfo.size() - ) { + //has the user selected a server on the list? + BoundingBox tmpBox = listBox; + tmpBox.h *= serverInfo.size(); + if (tmpBox.CheckOverlap({button.x, button.y})) { selection = &serverInfo[(button.y - listBox.y)/listBox.h]; } else { @@ -183,7 +169,11 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { } void LobbyMenu::KeyDown(SDL_KeyboardEvent const& key) { - // + switch(key.keysym.sym) { + case SDLK_ESCAPE: + SetNextScene(SceneList::MAINMENU); + break; + } } void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) { @@ -237,4 +227,30 @@ void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) { strncpy(newPacket.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE); newPacket.accountIndex = accountIndex; network.SendTo(Channels::SERVER, &newPacket); -} \ No newline at end of file +} + +//------------------------- +//server control +//------------------------- + +void LobbyMenu::SendBroadcastRequest() { + //broadcast to the network, or a specific server + SerialPacket packet; + packet.type = SerialPacketType::BROADCAST_REQUEST; + network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet); + + //reset the server list + serverInfo.clear(); + selection = nullptr; +} + +void LobbyMenu::SendJoinRequest() { + //pack the packet + ClientPacket packet; + packet.type = SerialPacketType::JOIN_REQUEST; + strncpy(packet.username, config["client.username"].c_str(), PACKET_STRING_SIZE); + + //join the selected server + network.SendTo(&selection->address, &packet); + selection = nullptr; +} diff --git a/client/scenes/lobby_menu.hpp b/client/scenes/lobby_menu.hpp index 43f2c99..7020ac8 100644 --- a/client/scenes/lobby_menu.hpp +++ b/client/scenes/lobby_menu.hpp @@ -26,6 +26,7 @@ #include "image.hpp" #include "raster_font.hpp" #include "button.hpp" +#include "bounding_box.hpp" //utilities #include "config_utility.hpp" @@ -62,6 +63,10 @@ protected: void HandleBroadcastResponse(ServerPacket* const); void HandleJoinResponse(ClientPacket* const); + //server control + void SendBroadcastRequest(); + void SendJoinRequest(); + //shared parameters ConfigUtility& config = ConfigUtility::GetSingleton(); UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); @@ -86,9 +91,9 @@ protected: std::vector serverInfo; - //a terrible hack, forgive me + //NOTE: a terrible hack //I'd love a proper gui system for this - SDL_Rect listBox; + BoundingBox listBox; ServerInformation* selection = nullptr; }; diff --git a/client/scenes/options_menu.cpp b/client/scenes/options_menu.cpp index 4a991f7..539cd44 100644 --- a/client/scenes/options_menu.cpp +++ b/client/scenes/options_menu.cpp @@ -92,7 +92,11 @@ void OptionsMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { } void OptionsMenu::KeyDown(SDL_KeyboardEvent const& key) { - // + switch(key.keysym.sym) { + case SDLK_ESCAPE: + SetNextScene(SceneList::MAINMENU); + break; + } } void OptionsMenu::KeyUp(SDL_KeyboardEvent const& key) { diff --git a/common/utilities/bounding_box.hpp b/common/utilities/bounding_box.hpp index 1d32887..ac7daf2 100644 --- a/common/utilities/bounding_box.hpp +++ b/common/utilities/bounding_box.hpp @@ -32,6 +32,7 @@ public: int w, h; BoundingBox() = default; + BoundingBox(int i, int j): x(i), y(j), w(0), h(0) {}; BoundingBox(int i, int j, int k, int l): x(i), y(j), w(k), h(l) {}; ~BoundingBox() = default; BoundingBox& operator=(BoundingBox const&) = default; @@ -62,7 +63,7 @@ public: }; //This is explicitly a POD -static_assert(std::is_pod::value, "BoundingBox is not a POD"); +static_assert(std::is_pod::value, "BoundingBox is not a POD"); #include "vector2.hpp" diff --git a/common/utilities/check_bounds.cpp b/common/utilities/check_bounds.cpp deleted file mode 100644 index b0d42f8..0000000 --- a/common/utilities/check_bounds.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* 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. -*/ -#include "check_bounds.hpp" - -bool checkPoint(Vector2 const& origin, Vector2 const& bound, Vector2 const& point) { - return !( - point.x < origin.x || - point.y < origin.y || - point.x >= origin.x + bound.x || - point.y >= origin.y + bound.y - ); -} - -bool checkOverlap(Vector2 const& originOne, Vector2 const& boundOne, Vector2 const& originTwo, Vector2 const& boundTwo) { - return !( - originOne.x >= originTwo.x + boundTwo.x || - originOne.x + boundOne.x >= originTwo.x || - originOne.y >= originTwo.y + boundTwo.y || - originOne.y + boundOne.y >= originTwo.y - ); -} diff --git a/common/utilities/check_bounds.hpp b/common/utilities/check_bounds.hpp deleted file mode 100644 index e02d6cd..0000000 --- a/common/utilities/check_bounds.hpp +++ /dev/null @@ -1,30 +0,0 @@ -/* 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 CHECKBOUNDS_HPP_ -#define CHECKBOUNDS_HPP_ - -#include "vector2.hpp" - -bool checkPoint(Vector2 const& origin, Vector2 const& bound, Vector2 const& point); -bool checkOverlap(Vector2 const& originOne, Vector2 const& boundOne, Vector2 const& originTwo, Vector2 const& boundTwo); - -#endif diff --git a/todo.txt b/todo.txt index c5f5afd..a1f8458 100644 --- a/todo.txt +++ b/todo.txt @@ -1,5 +1,6 @@ TODO: encapsulate the data structures TODO: Get the rooms working +TODO: A proper logging system TODO: make the whole thing more fault tolerant TODO: Authentication