[Add] Player can now attack NPCs.

This commit is contained in:
Tamir Atias 2012-01-14 06:44:23 +02:00
parent 1feac5012d
commit cb3c463261
5 changed files with 75 additions and 6 deletions

View File

@ -50,7 +50,7 @@ gameNavVal_t Game::Run(const string savegameIDArg) {
stringstream npcHealth; stringstream npcHealth;
_npcHealth.SetXY(10, 100); _npcHealth.SetXY(10, 100);
_npcHealth.SetTextBlended("NPC Health - XX", vsmall, COLOUR_BLACK); _npcHealth.SetTextBlended("NPC 0 Health - XX", vsmall, COLOUR_BLACK);
_gameRunning = true; _gameRunning = true;
while(_gameRunning) { while(_gameRunning) {
@ -103,8 +103,16 @@ gameNavVal_t Game::Run(const string savegameIDArg) {
playerHealth << "Player Health: " << _player->GetHealth(); playerHealth << "Player Health: " << _player->GetHealth();
_playerHealth.SetTextBlended(playerHealth.str(), vsmall, COLOUR_BLACK); _playerHealth.SetTextBlended(playerHealth.str(), vsmall, COLOUR_BLACK);
int npc0Health = 0;
if(_map.GetWorld().GetNPCCount() == 0) {
npc0Health = 0;
}
else {
npc0Health = _map.GetWorld().GetNPC(0)->GetHealth();
}
npcHealth.str(""); npcHealth.str("");
npcHealth << "NPC Health: " << _map.GetWorld().GetNPC(0)->GetHealth(); npcHealth << "NPC 0 Health: " << npc0Health;
_npcHealth.SetTextBlended(npcHealth.str(), vsmall, COLOUR_BLACK); _npcHealth.SetTextBlended(npcHealth.str(), vsmall, COLOUR_BLACK);
} }
} }

View File

@ -41,6 +41,7 @@ void Player::HandleInput(void) {
case SDLK_SPACE: case SDLK_SPACE:
attacking = true; attacking = true;
attackTimer.Start(); attackTimer.Start();
map->GetWorld().OnPlayerAttack(this);
break; break;
default: default:
break; break;
@ -59,6 +60,7 @@ void Player::HandleInput(void) {
if(event.button.button == SDL_BUTTON_LEFT) { if(event.button.button == SDL_BUTTON_LEFT) {
attacking = true; attacking = true;
attackTimer.Start(); attackTimer.Start();
map->GetWorld().OnPlayerAttack(this);
} }
} }
} }

View File

@ -36,6 +36,9 @@ public:
void SetHealth(int health) { _health = health; } void SetHealth(int health) { _health = health; }
int GetHealth(void) { return _health; } int GetHealth(void) { return _health; }
int GetDirectionFacing(void) { return directionFacing; }
void SetDirectionFacing(int dir) { directionFacing = dir; }
void AddSpeachBubble(string text); void AddSpeachBubble(string text);
void Render(void); void Render(void);
@ -56,6 +59,11 @@ public:
inline void operator delete [](void* object) { inline void operator delete [](void* object) {
gMemManager.Free(object); gMemManager.Free(object);
} }
static const int FACING_UP = 0;
static const int FACING_RIGHT = 1;
static const int FACING_DOWN = 2;
static const int FACING_LEFT = 3;
protected: protected:
void Move(void); void Move(void);
@ -85,10 +93,6 @@ protected:
static const float CHARACTER_SPEED; static const float CHARACTER_SPEED;
int directionFacing; int directionFacing;
static const int FACING_UP = 0;
static const int FACING_RIGHT = 1;
static const int FACING_DOWN = 2;
static const int FACING_LEFT = 3;
static const int ANIM_LEFT_FOOT = 0; static const int ANIM_LEFT_FOOT = 0;
static const int ANIM_NO_FOOT = 1; static const int ANIM_NO_FOOT = 1;

View File

@ -52,3 +52,53 @@ NPC* WorldManager::GetNPC(int index) {
} }
return NULL; return NULL;
} }
void WorldManager::OnPlayerAttack(Character* player) {
int playerX = (int)(player->GetX() / 32.0f);
int playerY = (int)(player->GetY() / 32.0f);
int playerDir = player->GetDirectionFacing();
std::list<NPC*>::iterator i = _npcs.begin();
while(i != _npcs.end()) {
NPC* npc = (*i);
int npcX = (int)(npc->GetX() / 32.0f);
int npcY = (int)(npc->GetY() / 32.0f);
int diffX = npcX - playerX;
int diffY = npcY - playerY;
// Not in player's line of sight.
if(diffX != 0 && diffY != 0) {
++i;
continue;
}
// Distance is greater than 2.
if(diffX > 2 || diffY > 2) {
++i;
continue;
}
// Player not facing the npc.
if((diffX < 0 && playerDir != Character::FACING_LEFT) ||
(diffX > 0 && playerDir != Character::FACING_RIGHT) ||
(diffY < 0 && playerDir != Character::FACING_UP) ||
(diffY > 0 && playerDir != Character::FACING_DOWN))
{
++i;
continue;
}
npc->SetHealth(npc->GetHealth() - 5);
if(npc->GetHealth() <= 0) {
i = _npcs.erase(i);
delete npc;
}
else {
++i;
}
}
}

View File

@ -2,6 +2,7 @@
#include <list> #include <list>
class NPC; class NPC;
class Character;
class WorldManager { class WorldManager {
public: public:
@ -15,6 +16,10 @@ public:
void RemoveNPC(int index); void RemoveNPC(int index);
NPC* GetNPC(int index); NPC* GetNPC(int index);
int GetNPCCount() { return _npcs.size(); }
void OnPlayerAttack(Character* player);
private: private:
std::list<NPC*> _npcs; std::list<NPC*> _npcs;
}; };