This commit is contained in:
Tamir Atias 2012-02-08 22:03:49 +02:00
commit 6e83a0fa77

View File

@ -5,183 +5,183 @@
#include "../UI/EventHistory.h" #include "../UI/EventHistory.h"
WorldManager::WorldManager(LevelGen* level) { WorldManager::WorldManager(LevelGen* level) {
_level = level; _level = level;
} }
WorldManager::~WorldManager(void) { WorldManager::~WorldManager(void) {
for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) { for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) {
NPC* npc = (*i); NPC* npc = (*i);
delete npc; delete npc;
} }
} }
void WorldManager::Update(void) { void WorldManager::Update(void) {
for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) { for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) {
NPC* npc = (*i); NPC* npc = (*i);
npc->Update(); npc->Update();
} }
} }
void WorldManager::Render(void) { void WorldManager::Render(void) {
for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) { for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) {
NPC* npc = (*i); NPC* npc = (*i);
npc->Render(); npc->Render();
} }
} }
void WorldManager::AddNPC(NPC* npc) { void WorldManager::AddNPC(NPC* npc) {
_npcs.push_back(npc); _npcs.push_back(npc);
} }
void WorldManager::RemoveNPC(int index) { void WorldManager::RemoveNPC(int index) {
int npcsIndex = 0; int npcsIndex = 0;
for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) { for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) {
NPC* npc = (*i); NPC* npc = (*i);
if(npcsIndex == index) { if(npcsIndex == index) {
_npcs.erase(i); _npcs.erase(i);
delete npc; delete npc;
} }
npcsIndex++; npcsIndex++;
} }
} }
NPC* WorldManager::GetNPC(int index) { NPC* WorldManager::GetNPC(int index) {
int npcsIndex = 0; int npcsIndex = 0;
for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) { for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) {
NPC* npc = (*i); NPC* npc = (*i);
if(npcsIndex == index) { if(npcsIndex == index) {
return npc; return npc;
} }
npcsIndex++; npcsIndex++;
} }
return NULL; return NULL;
} }
NPC* WorldManager::GetNPCAt(int xArg, int yArg) { NPC* WorldManager::GetNPCAt(int xArg, int yArg) {
for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) { for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) {
NPC* npc = (*i); NPC* npc = (*i);
if(xArg >= npc->GetX() && xArg <= (npc->GetX() + npc->GetWidth()) && if(xArg >= npc->GetX() && xArg <= (npc->GetX() + npc->GetWidth()) &&
yArg >= npc->GetY() && yArg <= (npc->GetY() + npc->GetHeight())) { yArg >= npc->GetY() && yArg <= (npc->GetY() + npc->GetHeight())) {
return npc; return npc;
} }
} }
return NULL; return NULL;
} }
void WorldManager::CreateNPC(int x, int y) { void WorldManager::CreateNPC(int x, int y) {
NPC* npc = new NPC(_level); NPC* npc = new NPC(_level);
npc->SetXY(x, y); npc->SetXY(x, y);
npc->LoadSprites("../Data/Media/Images/Characters/template.png", 40,45); npc->LoadSprites("../Data/Media/Images/Characters/template.png", 40,45);
_npcs.push_back(npc); _npcs.push_back(npc);
} }
bool WorldManager::CheckCollision(const SDL_Rect& charRect, Character* exclude) { bool WorldManager::CheckCollision(const SDL_Rect& charRect, Character* exclude) {
for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) { for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) {
NPC* npc = (*i); NPC* npc = (*i);
if(npc == exclude) { if(npc == exclude) {
continue; continue;
} }
SDL_Rect npcRect; SDL_Rect npcRect;
npcRect.x = npc->GetX(); npcRect.x = npc->GetX();
npcRect.y = npc->GetY(); npcRect.y = npc->GetY();
npcRect.w = npc->GetWidth(); npcRect.w = npc->GetWidth();
npcRect.h = npc->GetHeight(); npcRect.h = npc->GetHeight();
if(CheckCollisionRect(npcRect, charRect)) { if(CheckCollisionRect(npcRect, charRect)) {
return true; return true;
} }
} }
return false; return false;
} }
void WorldManager::OnPlayerAttack(Player* player) { void WorldManager::OnPlayerAttack(Player* player) {
int playerX = (int)(player->GetX() / 32.0f); int playerX = (int)(player->GetX() / 32.0f);
int playerY = (int)(player->GetY() / 32.0f); int playerY = (int)(player->GetY() / 32.0f);
int playerDir = player->GetDirectionFacing(); int playerDir = player->GetDirectionFacing();
std::list<NPC*>::iterator i = _npcs.begin(); std::list<NPC*>::iterator i = _npcs.begin();
while(i != _npcs.end()) { while(i != _npcs.end()) {
NPC* npc = (*i); NPC* npc = (*i);
int npcX = (int)(npc->GetX() / 32.0f); int npcX = (int)(npc->GetX() / 32.0f);
int npcY = (int)(npc->GetY() / 32.0f); int npcY = (int)(npc->GetY() / 32.0f);
int diffX = npcX - playerX; int diffX = npcX - playerX;
int diffY = npcY - playerY; int diffY = npcY - playerY;
// Not in player's line of sight. // Not in player's line of sight.
if(diffX != 0 && diffY != 0) { if(diffX != 0 && diffY != 0) {
++i; ++i;
continue; continue;
} }
// Distance is greater than 2. // Distance is greater than 2.
if(abs(diffX) > 2 || abs(diffY) > 2) { if(abs(diffX) > 2 || abs(diffY) > 2) {
++i; ++i;
continue; continue;
} }
// Player not facing the npc. // Player not facing the npc.
if((diffX < 0 && playerDir != Character::FACING_LEFT) || if((diffX < 0 && playerDir != Character::FACING_LEFT) ||
(diffX > 0 && playerDir != Character::FACING_RIGHT) || (diffX > 0 && playerDir != Character::FACING_RIGHT) ||
(diffY < 0 && playerDir != Character::FACING_UP) || (diffY < 0 && playerDir != Character::FACING_UP) ||
(diffY > 0 && playerDir != Character::FACING_DOWN)) (diffY > 0 && playerDir != Character::FACING_DOWN))
{ {
++i; ++i;
continue; continue;
} }
npc->SetHealth(npc->GetHealth() - 20); npc->SetHealth(npc->GetHealth() - 20);
npc->OnAttack(); npc->OnAttack();
if(npc->GetHealth() <= 0) { if(npc->GetHealth() <= 0) {
// Please note: // Please note:
// Naked dudes are known to be sensitive to spicy food. // Naked dudes are known to be sensitive to spicy food.
char* waysOfDeath[] = { char* waysOfDeath[] = {
"Choked Naked Dude!", "Choked Naked Dude!",
"Stabbed Naked Dude!", "Stabbed Naked Dude!",
"Urinated Acid on Naked Dude!", "Urinated Acid on Naked Dude!",
"Killed Naked Dude with a dildo!", "Killed Naked Dude with a dildo!",
"Poured Tabasco on Naked Dude!", "Poured Tabasco on Naked Dude!",
"Threw Acid on Naked Dude!", "Threw Acid on Naked Dude!",
"Slapped Naked Dude with Dead Fish!", "Slapped Naked Dude with Dead Fish!",
"Killed Naked Dude with a Pistol!", "Killed Naked Dude with a Pistol!",
"Ate Naked Dude's brain!", "Ate Naked Dude's brain!",
"Slaughtered Naked Dude!", "Slaughtered Naked Dude!",
"Roasted Naked Dude!", "Roasted Naked Dude!",
"Paper Sprayed Naked Dude!", "Pepper Sprayed Naked Dude!",
"Stoned Naked Dude!", "Stoned Naked Dude!",
"Slayed Naked Dude with a Katana!", "Slayed Naked Dude with a Katana!",
"Thew Chili Peppers on Naked Dude!", "Threw Chili Peppers on Naked Dude!",
"Used Karate on Naked Dude!", "Used Karate on Naked Dude!",
"Beat the shit out of Naked Dude!", "Beat the shit out of Naked Dude!",
"FUS RO DAH!" "FUS RO DAH!"
}; };
eventHistory->LogEvent(waysOfDeath[rand() % (sizeof(waysOfDeath)/sizeof(char*))]); eventHistory->LogEvent(waysOfDeath[rand() % (sizeof(waysOfDeath)/sizeof(char*))]);
int expGain = 3 + (rand() % 2); int expGain = 3 + (rand() % 2);
player->SetExp(player->GetExp() + expGain); player->SetExp(player->GetExp() + expGain);
i = _npcs.erase(i); i = _npcs.erase(i);
delete npc; delete npc;
if(_npcs.empty()) { if(_npcs.empty()) {
_level->Load("map"); _level->Load("map");
int spawnX; int spawnX;
int spawnY; int spawnY;
_level->FindSpawnPoint(spawnX, spawnY, player->GetWidth(),player->GetHeight()); _level->FindSpawnPoint(spawnX, spawnY, player->GetWidth(),player->GetHeight());
player->SetXY(spawnX, spawnY); player->SetXY(spawnX, spawnY);
} }
} }
else { else {
++i; ++i;
} }
} }
} }