From 4808d8b45dbeda5ff0684d65476ae5b782575e1b Mon Sep 17 00:00:00 2001 From: Tamir Atias Date: Thu, 2 Feb 2012 23:30:40 +0200 Subject: [PATCH] [Add] NPCs now generating randomally. [Add] Map is regenerated after all NPCs are killed. [Fix] Bugs regarding level up system. [Fix] Unuk-QT.pro.user is now ignored. --- .gitignore | 1 + Unuk-QT/Unuk-QT.pro.user | 2 +- src/Unuk/Game.cpp | 7 +-- src/Unuk/Player.cpp | 27 +++++++----- src/Unuk/Player.h | 8 +--- src/libUnuk/Engine/WorldManager.cpp | 16 ++++++- src/libUnuk/Engine/WorldManager.h | 5 ++- src/libUnuk/LevelGen/LevelGen.cpp | 68 ++++++++++------------------- src/libUnuk/LevelGen/LevelGen.h | 4 ++ 9 files changed, 68 insertions(+), 70 deletions(-) diff --git a/.gitignore b/.gitignore index ed57d9f..17ae8d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ UnukQT/Makefile +UnukQT/Unuk-QT.pro.user Win32/Unuk/Debug Win32/Unuk/Release Win32/Unuk/LibUnuk/Debug diff --git a/Unuk-QT/Unuk-QT.pro.user b/Unuk-QT/Unuk-QT.pro.user index 65d5e2e..f95998c 100644 --- a/Unuk-QT/Unuk-QT.pro.user +++ b/Unuk-QT/Unuk-QT.pro.user @@ -1,6 +1,6 @@ - + ProjectExplorer.Project.ActiveTarget diff --git a/src/Unuk/Game.cpp b/src/Unuk/Game.cpp index b50b918..d4aea1d 100644 --- a/src/Unuk/Game.cpp +++ b/src/Unuk/Game.cpp @@ -23,9 +23,6 @@ gameNavVal_t Game::Run(const string savegameIDArg) { LoadSavegame(savegameIDArg); - // some weird bug. player->_exp is set to unreasonable number randomally after LoadSavegame returns. - _player->SetExpNeeded(Player::BASE_EXP_NEEDED); - int fps = 0; int frame = 0; int nextGameTick = SDL_GetTicks(); @@ -115,10 +112,10 @@ gameNavVal_t Game::Run(const string savegameIDArg) { _playerHealthBar.SetProgress((float)_player->GetHealth() / 100.0f); playerExp.str(""); - playerExp << "Player Level " << _player->GetLevel() << " (" << _player->GetExp() << "/" << _player->GetExpNeeded() << ")"; + playerExp << "Player Level " << _player->GetLevel() << " (" << _player->GetExp() << "/" << Player::EXP_TABLE[_player->GetLevel()] << ")"; _playerExp.SetTextBlended(playerExp.str(), vsmall, COLOUR_WHITE); - _playerExpBar.SetProgress((float)_player->GetExp() / (float)_player->GetExpNeeded()); + _playerExpBar.SetProgress((float)_player->GetExp() / (float)Player::EXP_TABLE[_player->GetLevel()]); // Check to see if we are allowed to display debug info. if(debugEnabled) { diff --git a/src/Unuk/Player.cpp b/src/Unuk/Player.cpp index 0444940..c8cb468 100644 --- a/src/Unuk/Player.cpp +++ b/src/Unuk/Player.cpp @@ -5,13 +5,23 @@ // Pixels * 60 / sec. const float Player::PLAYER_SPEED = Character::CHARACTER_SPEED + 0.5f; -// Amount of Exp needed to level up from 1 to 2 -const int Player::BASE_EXP_NEEDED = 10; +// Amount of Exp needed every level +const int Player::EXP_TABLE[10] = { + 10, + 30, + 90, + 150, + 300, + 512, + 1000, + 2000, + 3500, + 5000 +}; Player::Player(LevelGen *mapArg) : Character(mapArg) { _level = 1; _exp = 0; - _expNeeded = BASE_EXP_NEEDED; } Player::~Player(void) { @@ -111,11 +121,10 @@ void Player::Move() { void Player::SetLevel(int level) { _level = level; - _exp = _exp - _expNeeded; + _exp = _exp - EXP_TABLE[level]; if(_exp < 0) { _exp = 0; } - _expNeeded = pow(BASE_EXP_NEEDED, _level); } void Player::SetExp(int exp) { @@ -123,13 +132,9 @@ void Player::SetExp(int exp) { evtMsg << "Gained " << (exp - _exp) << " Experience Points."; eventHistory->LogEvent(evtMsg.str()); - _exp += exp; - if(_exp >= _expNeeded) { + _exp = exp; + if(_level != MAX_LEVEL && _exp >= EXP_TABLE[_level]) { eventHistory->LogEvent("Player leveled up!"); SetLevel(_level + 1); } } - -void Player::SetExpNeeded(int expNeeded) { - _expNeeded = expNeeded; -} diff --git a/src/Unuk/Player.h b/src/Unuk/Player.h index 094246b..00da354 100644 --- a/src/Unuk/Player.h +++ b/src/Unuk/Player.h @@ -23,10 +23,8 @@ public: void SetExp(int exp); int GetExp(void) { return _exp; } - void SetExpNeeded(int expNeeded); - int GetExpNeeded(void) { return _expNeeded; } - - static const int BASE_EXP_NEEDED; + static const int MAX_LEVEL = 10; + static const int EXP_TABLE[MAX_LEVEL]; protected: void Move(void); @@ -37,8 +35,6 @@ private: static const float PLAYER_SPEED; string _name; - //int _health; int _level; int _exp; - int _expNeeded; }; diff --git a/src/libUnuk/Engine/WorldManager.cpp b/src/libUnuk/Engine/WorldManager.cpp index 18b2fe4..e9d157f 100644 --- a/src/libUnuk/Engine/WorldManager.cpp +++ b/src/libUnuk/Engine/WorldManager.cpp @@ -4,7 +4,8 @@ #include "../../Unuk/Globals.h" #include "../UI/EventHistory.h" -WorldManager::WorldManager(void) { +WorldManager::WorldManager(LevelGen* level) { + _level = level; } WorldManager::~WorldManager(void) { @@ -67,6 +68,13 @@ bool WorldManager::HasNPCIn(int xArg, int yArg) { return false; } +void WorldManager::CreateNPC(int x, int y) { + NPC* npc = new NPC(_level); + npc->SetXY(x, y); + npc->LoadSprites("../Data/Media/Images/Characters/template.png", 40,45); + _npcs.push_back(npc); +} + void WorldManager::OnPlayerAttack(Player* player) { int playerX = (int)(player->GetX() / 32.0f); int playerY = (int)(player->GetY() / 32.0f); @@ -105,7 +113,7 @@ void WorldManager::OnPlayerAttack(Player* player) { continue; } - npc->SetHealth(npc->GetHealth() - 5); + npc->SetHealth(npc->GetHealth() - 20); npc->OnAttack(); if(npc->GetHealth() <= 0) { @@ -116,6 +124,10 @@ void WorldManager::OnPlayerAttack(Player* player) { i = _npcs.erase(i); delete npc; + + if(_npcs.empty()) { + _level->Load("map"); + } } else { ++i; diff --git a/src/libUnuk/Engine/WorldManager.h b/src/libUnuk/Engine/WorldManager.h index 9225f41..00d4472 100644 --- a/src/libUnuk/Engine/WorldManager.h +++ b/src/libUnuk/Engine/WorldManager.h @@ -3,10 +3,11 @@ class NPC; class Player; +class LevelGen; class WorldManager { public: - WorldManager(void); + WorldManager(LevelGen* level); ~WorldManager(void); void Update(void); @@ -15,6 +16,7 @@ public: void AddNPC(NPC* npc); void RemoveNPC(int index); NPC* GetNPC(int index); + void CreateNPC(int x, int y); bool HasNPCIn(int xArg, int yArg); @@ -23,5 +25,6 @@ public: void OnPlayerAttack(Player* player); private: + LevelGen* _level; std::list _npcs; }; diff --git a/src/libUnuk/LevelGen/LevelGen.cpp b/src/libUnuk/LevelGen/LevelGen.cpp index ea1af41..98b5c83 100644 --- a/src/libUnuk/LevelGen/LevelGen.cpp +++ b/src/libUnuk/LevelGen/LevelGen.cpp @@ -1,7 +1,7 @@ #include "LevelGen.h" #include "../Engine/NPC.h" -LevelGen::LevelGen(void) { +LevelGen::LevelGen(void) : _world(this) { } @@ -128,42 +128,6 @@ void LevelGen::Load(const string filename) { // procedural generation DoMagic(); - - //character->Load(filename); - - NPC* npc = new NPC(this); - - //int spawnX; - //int spawnY; - //FindSpawnPoint(spawnX, spawnY); - //npc->SetXY(spawnX, spawnY); // try to uncomment this, try to find out what's going on.. --konom - npc->SetXY(100, 230); - npc->LoadSprites("../Data/Media/Images/Characters/template.png", 40,45); - _world.AddNPC(npc); - - npc = new NPC(this); - //FindSpawnPoint(spawnX, spawnY); - npc->SetXY(300, 250); - npc->LoadSprites("../Data/Media/Images/Characters/template.png", 40,45); - _world.AddNPC(npc); - - npc = new NPC(this); - //FindSpawnPoint(spawnX, spawnY); - npc->SetXY(400, 100); - npc->LoadSprites("../Data/Media/Images/Characters/template.png", 40,45); - _world.AddNPC(npc); - - npc = new NPC(this); - //FindSpawnPoint(spawnX, spawnY); - npc->SetXY(200, 400); - npc->LoadSprites("../Data/Media/Images/Characters/template.png", 40,45); - _world.AddNPC(npc); - - npc = new NPC(this); - //FindSpawnPoint(spawnX, spawnY); - npc->SetXY(250, 250); - npc->LoadSprites("../Data/Media/Images/Characters/template.png", 40,45); - _world.AddNPC(npc); } void LevelGen::Update(void) { @@ -213,6 +177,7 @@ void LevelGen::Render(void) { void LevelGen::Unload(void) { _tileTextures.Unload(); _entityTextures.Unload(); + memset(_tile, sizeof(_tile), 0); } void LevelGen::DoMagic(void) { @@ -220,14 +185,15 @@ void LevelGen::DoMagic(void) { GenerateEntities("hedge", 15); GenerateEntities("barrel", 40); MakeWalkingPaths(); + GenerateEnemies(); } void LevelGen::GenerateEntities(const string& name, int frequency) { int nextEntityGen = 1 + (rand() % frequency); std::string filename = "../Data/Media/Images/Entities/" + name + ".png"; - for(int x = 0; x < SCREEN_WIDTH/64 + 1; x++) { - for(int y = 0; y < SCREEN_HEIGHT/64; y++) { + for(int x = 0; x < BOUNDARIES_X; x++) { + for(int y = 0; y < BOUNDARIES_Y; y++) { nextEntityGen--; if(!_tile[x][y].GetTileSolidity() && !_tile[x][y].GetEntitySolitity() && nextEntityGen <= 0) { _tile[x][y].SetEntityTexture(_entityTextures.AddAlpha(filename)); @@ -246,10 +212,10 @@ void LevelGen::GenerateEntities(const string& name, int frequency) { void LevelGen::MakeWalkingPaths(void) { int lastOpenY = rand() % 5; - for(int x = 0; x < SCREEN_WIDTH/64 + 1; x++) { + for(int x = 0; x < BOUNDARIES_X; x++) { bool pathFound = false; - for(int y = 0; y < SCREEN_HEIGHT/64; y++) { + for(int y = 0; y < BOUNDARIES_Y; y++) { if(!_tile[x][y].GetEntitySolitity()) { pathFound = true; break; @@ -266,19 +232,33 @@ void LevelGen::MakeWalkingPaths(void) { } void LevelGen::FindSpawnPoint(int& xArg, int& yArg) { - xArg = rand() % SCREEN_WIDTH; - yArg = rand() % SCREEN_HEIGHT; + xArg = rand() % (BOUNDARIES_X * TILE_WIDTH); + yArg = rand() % (BOUNDARIES_Y * TILE_HEIGHT); int tileX = xArg / 64; int tileY = yArg / 64; - if(!_tile[tileX][tileY].GetEntitySolitity() && !_world.HasNPCIn(xArg, yArg)) { + if(!_tile[tileX][tileY].GetEntitySolitity() && + !_tile[tileX][tileY].GetTileSolidity() && + !_world.HasNPCIn(xArg, yArg)) { return; } FindSpawnPoint(xArg, yArg); } +void LevelGen::GenerateEnemies(void) { + int npcsToGen = 4 + (rand() % 4); + + for(int i = 0; i < npcsToGen; i++) { + int spawnX; + int spawnY; + FindSpawnPoint(spawnX, spawnY); + + _world.CreateNPC(spawnX, spawnY); + } +} + string LevelGen::GetCurrentMap(void) { return _currentMap; } diff --git a/src/libUnuk/LevelGen/LevelGen.h b/src/libUnuk/LevelGen/LevelGen.h index eb81430..8d67b3d 100644 --- a/src/libUnuk/LevelGen/LevelGen.h +++ b/src/libUnuk/LevelGen/LevelGen.h @@ -52,6 +52,7 @@ private: void DoMagic(void); void GenerateEntities(const std::string& name, int frequency); void MakeWalkingPaths(void); + void GenerateEnemies(void); string _currentMap; int x; @@ -59,6 +60,9 @@ private: static const int TILE_ARRAY_SIZE = 150; MapTile _tile[TILE_ARRAY_SIZE][TILE_ARRAY_SIZE]; + + static const int BOUNDARIES_X = (SCREEN_WIDTH / TILE_WIDTH) - 2; + static const int BOUNDARIES_Y = (SCREEN_HEIGHT / TILE_HEIGHT) - 2; TextureManager _tileTextures; TextureManager _entityTextures;