Branch 'dev-char' is stable, merging into 'dev'

This commit is contained in:
Kayne Ruse
2013-06-24 10:43:39 +10:00
17 changed files with 363 additions and 136 deletions
-24
View File
@@ -1,24 +0,0 @@
/* 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 "character.hpp"
-32
View File
@@ -1,32 +0,0 @@
/* 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 CHARACTER_HPP_
#define CHARACTER_HPP_
class Character {
public:
Character();
~Character();
private:
};
#endif
+1 -1
View File
@@ -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() {
+1 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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();
+108
View File
@@ -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);
}
}
+62
View File
@@ -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