From 95f2d212ef5c748f9f5cdd08073a4e58f22d0dea Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 1 May 2013 23:40:28 +1000 Subject: [PATCH] Created PlayerManager --- client/in_game.cpp | 46 +++++++++++++++++++++++--------- client/in_game.hpp | 3 ++- client/player_manager.cpp | 51 ++++++++++++++++++++++++++++++++++++ client/player_manager.hpp | 15 +++++++++++ client/surface_manager.hpp | 2 ++ screenshots/the beatles.png | Bin 0 -> 3771 bytes 6 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 screenshots/the beatles.png diff --git a/client/in_game.cpp b/client/in_game.cpp index d1caff7..37f3f43 100644 --- a/client/in_game.cpp +++ b/client/in_game.cpp @@ -15,11 +15,16 @@ InGame::InGame() { surfaceMgr.Load("player", "rsc/graphics/sprites/elliot2.bmp"); surfaceMgr.Load("flower", "rsc/graphics/sprites/aniflower.bmp"); - player = new Player(surfaceMgr.Get("player"), 32, 48); + playerCounter = currentPlayer = 0; + + playerMgr.New(playerCounter++, surfaceMgr["player"]); + playerMgr.New(playerCounter++, surfaceMgr["player"]); + playerMgr.New(playerCounter++, surfaceMgr["player"]); + playerMgr.New(playerCounter++, surfaceMgr["player"]); } InGame::~InGame() { - delete player; + playerMgr.DeleteAll(); surfaceMgr.FreeAll(); #ifdef DEBUG cout << "leaving InGame" << endl; @@ -40,12 +45,12 @@ void InGame::FrameEnd() { void InGame::Update() { delta.Calculate(); - player->Update(delta.GetDelta()); + playerMgr.UpdateAll(delta.GetDelta()); } void InGame::Render(SDL_Surface* const screen) { SDL_FillRect(screen, 0, 0); - player->DrawTo(screen); + playerMgr.DrawAllTo(screen); } //------------------------- @@ -69,17 +74,31 @@ void InGame::KeyDown(SDL_KeyboardEvent const& key) { case SDLK_ESCAPE: QuitEvent(); break; + case SDLK_w: - player->WalkInDirection(Direction::NORTH); + playerMgr[currentPlayer]->WalkInDirection(Direction::NORTH); break; case SDLK_s: - player->WalkInDirection(Direction::SOUTH); + playerMgr[currentPlayer]->WalkInDirection(Direction::SOUTH); break; case SDLK_a: - player->WalkInDirection(Direction::WEST); + playerMgr[currentPlayer]->WalkInDirection(Direction::WEST); break; case SDLK_d: - player->WalkInDirection(Direction::EAST); + playerMgr[currentPlayer]->WalkInDirection(Direction::EAST); + break; + + case SDLK_1: + currentPlayer = 0; + break; + case SDLK_2: + currentPlayer = 1; + break; + case SDLK_3: + currentPlayer = 2; + break; + case SDLK_4: + currentPlayer = 3; break; } } @@ -87,16 +106,16 @@ void InGame::KeyDown(SDL_KeyboardEvent const& key) { void InGame::KeyUp(SDL_KeyboardEvent const& key) { switch(key.keysym.sym) { case SDLK_w: - player->WalkInDirection(Direction::SOUTH); + playerMgr[currentPlayer]->WalkInDirection(Direction::SOUTH); break; case SDLK_s: - player->WalkInDirection(Direction::NORTH); + playerMgr[currentPlayer]->WalkInDirection(Direction::NORTH); break; case SDLK_a: - player->WalkInDirection(Direction::EAST); + playerMgr[currentPlayer]->WalkInDirection(Direction::EAST); break; case SDLK_d: - player->WalkInDirection(Direction::WEST); + playerMgr[currentPlayer]->WalkInDirection(Direction::WEST); break; } } @@ -106,5 +125,6 @@ void InGame::KeyUp(SDL_KeyboardEvent const& key) { //------------------------- void InGame::NewPlayer(int index, std::string avatarName, int x, int y) { - // + Player* p = playerMgr.New(index, surfaceMgr[avatarName]); + p->SetPosition(Vector2(x, y)); } diff --git a/client/in_game.hpp b/client/in_game.hpp index 5ef9a95..cf83ab7 100644 --- a/client/in_game.hpp +++ b/client/in_game.hpp @@ -39,7 +39,8 @@ protected: SurfaceManager surfaceMgr; PlayerManager playerMgr; - Player* player; + int playerCounter; + int currentPlayer; }; #endif diff --git a/client/player_manager.cpp b/client/player_manager.cpp index 616c934..e217bdf 100644 --- a/client/player_manager.cpp +++ b/client/player_manager.cpp @@ -1,2 +1,53 @@ #include "player_manager.hpp" +#include + +PlayerManager::PlayerManager() { + // +} + +PlayerManager::~PlayerManager() { + DeleteAll(); +} + +Player* PlayerManager::New(int index, SDL_Surface* avatarSheet) { + if (playerMap.find(index) != playerMap.end()) { + throw(std::runtime_error("This player's index is already taken")); + } + + return playerMap[index] = new Player(avatarSheet, avatarSheet->w/4, avatarSheet->h/4); +} + +Player* PlayerManager::Get(int index) { + std::map::iterator it = playerMap.find(index); + if(it == playerMap.end()) { + throw(std::runtime_error("Failed to find a player with that index")); + } + + return it->second; +} + +void PlayerManager::Delete(int index) { + std::map::iterator it = playerMap.find(index); + delete it->second; + playerMap.erase(it); +} + +void PlayerManager::UpdateAll(int delta) { + for (auto it : playerMap) { + it.second->Update(delta); + } +} + +void PlayerManager::DrawAllTo(SDL_Surface* dest) { + for (auto it : playerMap) { + it.second->DrawTo(dest); + } +} + +void PlayerManager::DeleteAll() { + for (auto it : playerMap) { + delete it.second; + } + playerMap.clear(); +} diff --git a/client/player_manager.hpp b/client/player_manager.hpp index 59db6f9..9d5e3cf 100644 --- a/client/player_manager.hpp +++ b/client/player_manager.hpp @@ -3,9 +3,24 @@ #include "player.hpp" +#include + class PlayerManager { public: + PlayerManager(); + ~PlayerManager(); + + Player* New(int index, SDL_Surface* avatarSheet); + Player* Get(int index); + void Delete(int index); + + void UpdateAll(int delta); + void DrawAllTo(SDL_Surface* dest); + void DeleteAll(); + + Player* operator[](int i) { return Get(i); } private: + std::map playerMap; }; #endif diff --git a/client/surface_manager.hpp b/client/surface_manager.hpp index 71768b9..d441f96 100644 --- a/client/surface_manager.hpp +++ b/client/surface_manager.hpp @@ -17,6 +17,8 @@ public: SDL_Surface* Set(std::string key, SDL_Surface* ptr); void Free(std::string key); void FreeAll(); + + SDL_Surface* operator[](std::string key) { return Get(key); }; private: SDL_Surface* LoadSurface(std::string key, std::string fname); typedef std::map mapType; diff --git a/screenshots/the beatles.png b/screenshots/the beatles.png new file mode 100644 index 0000000000000000000000000000000000000000..e168583789b874dbba9f77be5c307f58575e88f0 GIT binary patch literal 3771 zcmeH}`Bzid7RL_+P*A7?!4?HX%M=7EJWvFQGKh)<8E$fg1_O~Xf{2Y$F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