[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.
This commit is contained in:
Tamir Atias 2012-02-02 23:30:40 +02:00
parent 7d1a1b1295
commit 4808d8b45d
9 changed files with 68 additions and 70 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
UnukQT/Makefile
UnukQT/Unuk-QT.pro.user
Win32/Unuk/Debug
Win32/Unuk/Release
Win32/Unuk/LibUnuk/Debug

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by Qt Creator 2.4.1, 2012-02-02T01:31:32. -->
<!-- Written by Qt Creator 2.4.1, 2012-02-02T23:24:13. -->
<qtcreator>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>

View File

@ -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) {

View File

@ -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;
}

View File

@ -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;
};

View File

@ -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;

View File

@ -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<NPC*> _npcs;
};

View File

@ -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;
}

View File

@ -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;