Created PlayerManager

This commit is contained in:
Kayne Ruse
2013-05-01 23:40:28 +10:00
parent 80004ca8d4
commit 95f2d212ef
6 changed files with 103 additions and 14 deletions
+33 -13
View File
@@ -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));
}
+2 -1
View File
@@ -39,7 +39,8 @@ protected:
SurfaceManager surfaceMgr;
PlayerManager playerMgr;
Player* player;
int playerCounter;
int currentPlayer;
};
#endif
+51
View File
@@ -1,2 +1,53 @@
#include "player_manager.hpp"
#include <stdexcept>
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<int, Player*>::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<int, Player*>::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();
}
+15
View File
@@ -3,9 +3,24 @@
#include "player.hpp"
#include <map>
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<int, Player*> playerMap;
};
#endif
+2
View File
@@ -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<std::string, SDL_Surface*> mapType;
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB