[Fix] Sped up characters.

[Fix] Load game.
[Fix] Enemy collisions.
[Add] Enemies attacking the player.
[Add] GAME OVER!!! :D
This commit is contained in:
Tamir Atias 2012-02-22 01:27:46 +02:00
parent 57e3229baf
commit 120be24f96
9 changed files with 74 additions and 29 deletions

View File

@ -219,6 +219,18 @@ void Game::UpdateGame(void) {
if(_ingameMenu.GetStatus() == false) {
_map.Update();
_player->Update();
if(gameOver) {
gameOver = false;
_map.New();
_player->SetHealth(100);
_player->SetLevelLiteral(1);
_player->SetExpLiteral(0);
New(_saveGameID);
}
} else {
// :D
}

View File

@ -16,3 +16,5 @@ int plrKeyeft;
int plrKeyRight;
int plrBtnSpell;
bool gameOver = false;

View File

@ -11,3 +11,5 @@ extern bool debugEnabled;
class EventHistory;
extern EventHistory* eventHistory;
extern bool gameOver;

View File

@ -1,7 +1,7 @@
#include "Character.h"
// Pixels * 60 / sec
const float Character::CHARACTER_SPEED = 1.0f;
const float Character::CHARACTER_SPEED = 2.0f;
Character::Character(LevelGen* mapArg) {
map = mapArg;

View File

@ -46,7 +46,6 @@ public:
void OnAttack(void);
// Overload new and delete operators to utilize MemManager.
/*
inline void* operator new(size_t size) {
return gMemManager.Allocate(size);
}
@ -62,7 +61,6 @@ public:
inline void operator delete [](void* object) {
gMemManager.Free(object);
}
*/
enum {
FACING_UP,

View File

@ -31,23 +31,11 @@ void NPC::Update(void) {
}
void NPC::Move(void) {
Character::HealthBarScroll();
xVel = 0.0f;
yVel = 0.0f;
if(!_walkInPath) {
return;
}
Vec2 realPos(x, y);
Character* player = map->GetPlayer();
if(fabs((player->GetX() - x)) > 256 || fabs((player->GetY() - y)) > 256) {
return;
}
SDL_Rect selfRect;
selfRect.x = x - 5;
selfRect.y = y - 5;
@ -60,7 +48,36 @@ void NPC::Move(void) {
playerRect.w = player->GetWidth() + 5;
playerRect.h = player->GetHeight() + 5;
if(CheckCollisionRect(selfRect, playerRect)) {
bool isNearPlayer = CheckCollisionRect(selfRect, playerRect);
if(isNearPlayer) {
if(!attackTimer.IsStarted()) {
attackTimer.Start();
} else {
if(attackTimer.GetTicks() >= ATTACK_FREQUENCY) {
attackTimer.Start();
AttackPlayer();
}
}
} else {
if(attackTimer.IsStarted()) {
attackTimer.Stop();
}
}
Character::HealthBarScroll();
if(!_walkInPath) {
return;
}
Vec2 realPos(x, y);
if(fabs((player->GetX() - x)) > 256 || fabs((player->GetY() - y)) > 256) {
return;
}
if(isNearPlayer) {
_walkInPath = false;
return;
}
@ -77,7 +94,7 @@ void NPC::Move(void) {
else if(dx < 0.0f) {
xVel = -CHARACTER_SPEED;
}
if(dy > 0.0f) {
if(dy > 0.0f) {
yVel = CHARACTER_SPEED;
}
else if(dy < 0.0f) {
@ -87,7 +104,9 @@ void NPC::Move(void) {
if(xVel != 0.0f || yVel != 0.0f) {
map->MoveIfPossible(this, xVel, yVel, false);
}
else {
if(dx >= -CHARACTER_SPEED && dx <= CHARACTER_SPEED &&
dy >= -CHARACTER_SPEED && dy <= CHARACTER_SPEED) {
_target = _astar.GetSolutionNext();
if(_target == NULL || _target == _lastTarget) {
@ -136,3 +155,11 @@ void NPC::OnPlayerMove(Player* player) {
}
}
}
void NPC::AttackPlayer() {
map->GetPlayer()->SetHealth(map->GetPlayer()->GetHealth() - (rand() % 3));
map->GetPlayer()->OnAttack();
if(map->GetPlayer()->GetHealth() < 0) {
gameOver = true;
}
}

View File

@ -3,6 +3,7 @@
#include "Character.h"
#include "Pathfinding.h"
#include "../LevelGen/AStarTile.h"
#include "../System/Timer.h"
class Player;
@ -18,6 +19,7 @@ public:
protected:
void Move(void);
void AttackPlayer(void);
private:
bool _moving;
@ -26,4 +28,8 @@ private:
bool _walkInPath;
AStarTile* _target;
AStarTile* _lastTarget;
Timer _attackTimer;
static const int ATTACK_FREQUENCY = 1000;
};

View File

@ -86,9 +86,9 @@ bool WorldManager::CheckCollision(const SDL_Rect& charRect, Character* exclude)
SDL_Rect npcRect;
npcRect.x = npc->GetX();
npcRect.y = npc->GetY() + (npc->GetHeight() / 4) * 3;
npcRect.y = npc->GetY()/* + (npc->GetHeight() / 4) * 3*/;
npcRect.w = npc->GetWidth();
npcRect.h = npc->GetHeight() / 4;
npcRect.h = npc->GetHeight()/* / 4*/;
if(CheckCollisionRect(npcRect, charRect)) {
return true;

View File

@ -150,10 +150,10 @@ void LevelGen::Save(const string& filename){
TiXmlElement* rootElem = new TiXmlElement("map");
for(y = 0; y < TILE_ARRAY_WIDTH; y++) {
for(y = 0; y < TILE_ARRAY_HEIGHT; y++) {
TiXmlElement* lineElem = new TiXmlElement("line");
for(x = 0; x < TILE_ARRAY_HEIGHT; x++) {
for(x = 0; x < TILE_ARRAY_WIDTH; x++) {
TiXmlElement* tileElem = new TiXmlElement("tile");
TiXmlElement* tileTextureElem = new TiXmlElement("tileTexture");
@ -339,7 +339,7 @@ void LevelGen::FindSpawnPoint(int& xArg, int& yArg, int objWidth, int objHeight)
}
void LevelGen::GenerateEnemies(void) {
int npcsToGen = 4 + (rand() % ((_player->GetLevel() * 2) + 4));
int npcsToGen = (_player->GetLevel() * 2) + (rand() % 4);
for(int i = 0; i < npcsToGen; i++) {
int spawnX;
@ -394,10 +394,8 @@ void LevelGen::MoveIfPossible(Character* character, float xVel, float yVel, bool
}
}
if(true) {
if(_world.CheckCollision(charRect, character)) {
return;
}
if(_world.CheckCollision(charRect, character)) {
return;
}
if(!isPlayer) {
@ -442,9 +440,9 @@ bool LevelGen::AStarTilePassable(int xArg, int yArg) {
SDL_Rect playerRect;
playerRect.x = _player->GetX();
playerRect.y = _player->GetY() + (_player->GetHeight() / 4) * 3;
playerRect.y = _player->GetY()/* + (_player->GetHeight() / 4) * 3*/;
playerRect.w = _player->GetWidth();
playerRect.h = _player->GetHeight() / 4;
playerRect.h = _player->GetHeight()/* / 4*/;
if(CheckCollisionRect(tileRect, playerRect)) {
return false;