From 120be24f9658c746fafee549126976a2faa4dc4d Mon Sep 17 00:00:00 2001 From: Tamir Atias Date: Wed, 22 Feb 2012 01:27:46 +0200 Subject: [PATCH] [Fix] Sped up characters. [Fix] Load game. [Fix] Enemy collisions. [Add] Enemies attacking the player. [Add] GAME OVER!!! :D --- src/Unuk/Game.cpp | 12 ++++++ src/Unuk/Globals.cpp | 2 + src/Unuk/Globals.h | 2 + src/libUnuk/Engine/Character.cpp | 2 +- src/libUnuk/Engine/Character.h | 2 - src/libUnuk/Engine/NPC.cpp | 57 +++++++++++++++++++++-------- src/libUnuk/Engine/NPC.h | 6 +++ src/libUnuk/Engine/WorldManager.cpp | 4 +- src/libUnuk/LevelGen/LevelGen.cpp | 16 ++++---- 9 files changed, 74 insertions(+), 29 deletions(-) diff --git a/src/Unuk/Game.cpp b/src/Unuk/Game.cpp index 48be3d4..a22b88e 100644 --- a/src/Unuk/Game.cpp +++ b/src/Unuk/Game.cpp @@ -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 } diff --git a/src/Unuk/Globals.cpp b/src/Unuk/Globals.cpp index 72a4579..3906ae9 100644 --- a/src/Unuk/Globals.cpp +++ b/src/Unuk/Globals.cpp @@ -16,3 +16,5 @@ int plrKeyeft; int plrKeyRight; int plrBtnSpell; + +bool gameOver = false; diff --git a/src/Unuk/Globals.h b/src/Unuk/Globals.h index 5e1768b..9079580 100644 --- a/src/Unuk/Globals.h +++ b/src/Unuk/Globals.h @@ -11,3 +11,5 @@ extern bool debugEnabled; class EventHistory; extern EventHistory* eventHistory; + +extern bool gameOver; diff --git a/src/libUnuk/Engine/Character.cpp b/src/libUnuk/Engine/Character.cpp index fbb1f31..ec1e3f7 100644 --- a/src/libUnuk/Engine/Character.cpp +++ b/src/libUnuk/Engine/Character.cpp @@ -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; diff --git a/src/libUnuk/Engine/Character.h b/src/libUnuk/Engine/Character.h index 00557ef..6843146 100644 --- a/src/libUnuk/Engine/Character.h +++ b/src/libUnuk/Engine/Character.h @@ -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, diff --git a/src/libUnuk/Engine/NPC.cpp b/src/libUnuk/Engine/NPC.cpp index 74da69b..f7375a3 100644 --- a/src/libUnuk/Engine/NPC.cpp +++ b/src/libUnuk/Engine/NPC.cpp @@ -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; + } +} diff --git a/src/libUnuk/Engine/NPC.h b/src/libUnuk/Engine/NPC.h index b11cc0c..cb64994 100644 --- a/src/libUnuk/Engine/NPC.h +++ b/src/libUnuk/Engine/NPC.h @@ -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; }; diff --git a/src/libUnuk/Engine/WorldManager.cpp b/src/libUnuk/Engine/WorldManager.cpp index 3bf8650..4e3ecdb 100644 --- a/src/libUnuk/Engine/WorldManager.cpp +++ b/src/libUnuk/Engine/WorldManager.cpp @@ -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; diff --git a/src/libUnuk/LevelGen/LevelGen.cpp b/src/libUnuk/LevelGen/LevelGen.cpp index a6930a5..ddb01d9 100644 --- a/src/libUnuk/LevelGen/LevelGen.cpp +++ b/src/libUnuk/LevelGen/LevelGen.cpp @@ -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;