[Fix] NPCs and Player shall no longer spawn inside entities!

This commit is contained in:
Tamir Atias 2012-02-04 23:51:45 +02:00
parent 228371376d
commit 90e3ff7a22
4 changed files with 42 additions and 16 deletions

View File

@ -252,10 +252,10 @@ void Game::NewSavegame(const string savegameIDArg) {
TiXmlElement* nameElement = new TiXmlElement("name"); TiXmlElement* nameElement = new TiXmlElement("name");
TiXmlText* nameText = new TiXmlText("Allanis"); //TODO: replace with _player->GetName() when it works. --konom TiXmlText* nameText = new TiXmlText("Allanis"); //TODO: replace with _player->GetName() when it works. --konom
nameElement->LinkEndChild(nameText); nameElement->LinkEndChild(nameText);
int spawnX; int spawnX;
int spawnY; int spawnY;
_map.FindSpawnPoint(spawnX, spawnY); _map.FindSpawnPoint(spawnX, spawnY, _player->GetWidth(), _player->GetHeight());
_player->SetXY(spawnX, spawnY); _player->SetXY(spawnX, spawnY);

View File

@ -130,7 +130,7 @@ void WorldManager::OnPlayerAttack(Player* player) {
int spawnX; int spawnX;
int spawnY; int spawnY;
_level->FindSpawnPoint(spawnX, spawnY); _level->FindSpawnPoint(spawnX, spawnY, player->GetWidth(),player->GetHeight());
player->SetXY(spawnX, spawnY); player->SetXY(spawnX, spawnY);
} }
} }

View File

@ -1,3 +1,5 @@
#include <list>
#include "LevelGen.h" #include "LevelGen.h"
#include "../Engine/NPC.h" #include "../Engine/NPC.h"
@ -235,20 +237,44 @@ void LevelGen::MakeWalkingPaths(void) {
} }
} }
void LevelGen::FindSpawnPoint(int& xArg, int& yArg) { void LevelGen::FindSpawnPoint(int& xArg, int& yArg, int objWidth, int objHeight) {
xArg = rand() % (BOUNDARIES_X * TILE_WIDTH); xArg = rand() % (BOUNDARIES_X * TILE_WIDTH);
yArg = rand() % (BOUNDARIES_Y * TILE_HEIGHT); yArg = rand() % (BOUNDARIES_Y * TILE_HEIGHT);
if(_world.HasNPCIn(xArg, yArg)) {
goto findNext;
}
SDL_Rect objRect;
objRect.x = xArg;
objRect.y = yArg;
objRect.w = objWidth;
objRect.h = objHeight;
for(int x = 0; x < BOUNDARIES_X; x++) {
for(int y = 0; y < BOUNDARIES_Y; y++) {
if(_tile[x][y].GetTileSolidity()) {
goto findNext;
}
if(_tile[x][y].GetEntitySolitity()) {
SDL_Rect entityRect;
entityRect.x = _tile[x][y].GetEntityX();
entityRect.y = _tile[x][y].GetEntityY();
entityRect.w = _tile[x][y].GetEntityWidth();
entityRect.h = _tile[x][y].GetEntityHeight();
if(CheckCollisionRect(entityRect, objRect)) {
goto findNext;
}
}
}
}
return;
int tileX = xArg / 64; findNext:
int tileY = yArg / 64; FindSpawnPoint(xArg, yArg, objWidth, objHeight);
if(!_tile[tileX][tileY].GetEntitySolitity() &&
!_tile[tileX][tileY].GetTileSolidity() &&
!_world.HasNPCIn(xArg, yArg)) {
return;
}
FindSpawnPoint(xArg, yArg);
} }
void LevelGen::GenerateEnemies(void) { void LevelGen::GenerateEnemies(void) {
@ -257,7 +283,7 @@ void LevelGen::GenerateEnemies(void) {
for(int i = 0; i < npcsToGen; i++) { for(int i = 0; i < npcsToGen; i++) {
int spawnX; int spawnX;
int spawnY; int spawnY;
FindSpawnPoint(spawnX, spawnY); FindSpawnPoint(spawnX, spawnY, 40,45);
_world.CreateNPC(spawnX, spawnY); _world.CreateNPC(spawnX, spawnY);
} }

View File

@ -25,7 +25,7 @@ public:
void Update(void); void Update(void);
void Render(void); void Render(void);
void FindSpawnPoint(int& xArg, int& yArg); void FindSpawnPoint(int& xArg, int& yArg, int objWidth, int objHeight);
bool GetTileSolidity(int xArg, int yArg); bool GetTileSolidity(int xArg, int yArg);
int GetTileX(int xArg, int yArg); int GetTileX(int xArg, int yArg);