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 0000000..e168583 Binary files /dev/null and b/screenshots/the beatles.png differ