[Remove] Speech bubbles.

[Fix] NPCs now collide with the player.
This commit is contained in:
Tamir Atias 2012-02-05 19:37:01 +02:00
parent a7367551c6
commit fcb1d02db0
7 changed files with 26 additions and 139 deletions

View File

@ -7,6 +7,7 @@
Game::Game(void) { Game::Game(void) {
Debug::logger->message("Creating characters.."); Debug::logger->message("Creating characters..");
_player = new Player(&_map); _player = new Player(&_map);
_map.SetPlayer(_player);
_runGameReturnValue = gameMainMenu; _runGameReturnValue = gameMainMenu;
} }

View File

@ -112,6 +112,7 @@ void Player::SetCamera(void) {
} }
void Player::Move() { void Player::Move() {
map->MoveIfPossible(this, xVel, yVel, true);
Character::Move(); Character::Move();
} }

View File

@ -59,38 +59,7 @@ void Character::LoadSprites(string filename, int wArg, int hArg) {
_healthBar.SetWidthHeight((int)w, 10); _healthBar.SetWidthHeight((int)w, 10);
} }
void Character::AddSpeachBubble(string text) {
_speachBubble.push_back(text);
_speachBubbleText.SetLineWidth(200);
_speachBubbleText.SetTextBlended(text, small, 0, 0, 0, true);
if(_speachBubbleTimer.IsStarted() == false)
_speachBubbleTimer.Start();
}
void Character::Render(void) { void Character::Render(void) {
// Draw some fancy speach bubbles. It is a bit of a mess, I am playing.
if(_speachBubble.size() != 0) {
if(_speachBubbleTimer.GetTicks() < SPEACH_BUBBLE_DISPLAY_LENGTH) {
roundedBoxRGBA(screen, (Sint16)((x + w / 2) - 100 - camera.x),
(Sint16)(y - 100 - camera.y),
(Sint16)((x + w / 2) + 100 - camera.x),
(Sint16)(y - 35 - camera.y),
5, 255, 255, 255, 255);
filledTrigonRGBA(screen, (Sint16)((x + w / 2) - 100 - camera.x),
(Sint16)(y - 100 - camera.y),
(Sint16)((x + w / 2) - 10 - camera.x),
(Sint16)(y - 40 - camera.y),
(Sint16)((x + w / 2) + 10 - camera.x),
(Sint16)(y - 40 - camera.y),
255, 255, 255, 255);
_speachBubbleText.Render((int)((x + w / 2) - 90), (int)y - 90);
}
}
if(attacking && attackTimer.GetTicks() < ATTACKING_DISPLAY_LEN) { if(attacking && attackTimer.GetTicks() < ATTACKING_DISPLAY_LEN) {
ApplySurface((int)x, (int)y, _texture, screen, &_sprites[directionFacing][ANIM_ATTACK]); ApplySurface((int)x, (int)y, _texture, screen, &_sprites[directionFacing][ANIM_ATTACK]);
return; return;
@ -132,22 +101,6 @@ void Character::Render(void) {
} }
void Character::Update(void) { void Character::Update(void) {
Move();
if(_speachBubble.size() != 0) {
if(_speachBubbleTimer.GetTicks() > SPEACH_BUBBLE_DISPLAY_LENGTH) {
_speachBubble.pop_front();
if(_speachBubble.size() != 0) {
_speachBubbleTimer.Start();
}
} else {
if(_speachBubble.front() != _speachBubbleText.GetText()) {
_speachBubbleText.SetTextBlended(_speachBubble.front(), small, 0, 0, 0);
}
}
}
_healthBar.SetProgress((float)_health / 100.0f); _healthBar.SetProgress((float)_health / 100.0f);
} }
@ -157,79 +110,5 @@ void Character::OnAttack(void) {
} }
void Character::Move(void) { void Character::Move(void) {
map->MoveIfPossible(this, xVel, yVel);
/*
x += xVel;
tileX = (int)(((x + (w / 2)) / TILE_WIDTH));
tileY = (int)(((y + (h / 2)) / TILE_HEIGHT));
// Check collisions.
if((x < 0) || (x + w) > levelWidth || (x + w) > SCREEN_WIDTH) x -= xVel;
if(CheckTileCollisions()) x -= xVel;
if(CheckEntityCollisions()) x -= xVel;
if(CheckCharacterCollisions()) x -= xVel;
y += yVel;
tileX = (int)(((x + (w / 2)) / TILE_WIDTH));
tileY = (int)(((y + (h / 2)) / TILE_HEIGHT));
if((y < 0) || (y + h) > levelHeight || (y + h) > SCREEN_HEIGHT) y -= yVel;
if(CheckTileCollisions()) y -= yVel;
if(CheckEntityCollisions()) y -= yVel;
if(CheckCharacterCollisions()) y -= yVel;
*/
_healthBar.SetXY((int)x, (int)(y - _healthBar.GetHeight() - 5)); _healthBar.SetXY((int)x, (int)(y - _healthBar.GetHeight() - 5));
} }
/*
* Bounds checking only included in map.GetTileSolidity() and
* map.GetEntitySolidity(). Remember to add bounds checking
* if any other map method is used in a similar manner.
*/
bool Character::CheckTileCollisions(void) {
tileX = x / 64;
tileY = y / 64;
for(int i = -1; i < 2; i++) {
for(int j = -1; j < 2; j++) {
if(map->GetTileSolidity(tileX + i, tileY + j))
if(CheckCollisionXY((int)x, (int)y, (int)w, (int)h, map->GetTileX(tileX + i, tileY + j),
map->GetTileY(tileX + i, tileY + j), TILE_WIDTH, TILE_HEIGHT))
return true;
}
}
return false;
}
bool Character::CheckEntityCollisions(void) {
for(int i = -1; i < 2; i++) {
for(int j = -1; j < 2; j++) {
if(map->GetEntitySolidity(tileX + i, tileY + j)) {
if(CheckCollisionXY((int)x, (int)y, (int)w, (int)h, map->GetEntityX(tileX + i, tileY + j),
map->GetEntityY(tileX + i, tileY + j),
map->GetEntityWidth(tileX + i, tileY + j),
map->GetEntityHeight(tileX + i, tileY + j)))
return true;
}
}
}
return false;
}
bool Character::CheckCharacterCollisions(void) {
for(collisionIter = collisionList.begin();
collisionIter != collisionList.end();
collisionIter++) {
if((*collisionIter) != this) {
if(CheckCollisionXY((int)x, (int)y, (int)w, (int)h,
(int)(*collisionIter)->GetX(),
(int)(*collisionIter)->GetY(),
(int)(*collisionIter)->GetWidth(),
(int)(*collisionIter)->GetHeight())) {
return true;
}
}
}
return false;
}

View File

@ -40,8 +40,6 @@ public:
int GetDirectionFacing(void) { return directionFacing; } int GetDirectionFacing(void) { return directionFacing; }
void SetDirectionFacing(int dir) { directionFacing = dir; } void SetDirectionFacing(int dir) { directionFacing = dir; }
void AddSpeachBubble(string text);
void Render(void); void Render(void);
void Update(void); void Update(void);
@ -73,10 +71,6 @@ public:
protected: protected:
void Move(void); void Move(void);
bool CheckTileCollisions(void);
bool CheckEntityCollisions(void);
bool CheckCharacterCollisions(void);
float x; float x;
float y; float y;
float w; float w;
@ -112,8 +106,6 @@ private:
static const int ANIMATION_SPEED = 200; static const int ANIMATION_SPEED = 200;
static const int ATTACKING_DISPLAY_LEN = 150; static const int ATTACKING_DISPLAY_LEN = 150;
static const int SPEACH_BUBBLE_DISPLAY_LENGTH = 6000;
SDL_Surface* _texture; SDL_Surface* _texture;
// [direction][action] // [direction][action]
@ -122,9 +114,4 @@ private:
Timer _animationTimer; Timer _animationTimer;
int _animationStage; int _animationStage;
bool _leftFoot; bool _leftFoot;
list<string> _speachBubble;
list<string>::iterator _speachBubbleIter;
Timer _speachBubbleTimer;
Text _speachBubbleText;
}; };

View File

@ -57,5 +57,7 @@ void NPC::Move(void) {
} }
_moving = true; _moving = true;
} }
map->MoveIfPossible(this, xVel, yVel, false);
Character::Move(); Character::Move();
} }

View File

@ -2,9 +2,10 @@
#include "LevelGen.h" #include "LevelGen.h"
#include "../Engine/NPC.h" #include "../Engine/NPC.h"
#include "../../Unuk/Player.h"
LevelGen::LevelGen(void) : _world(this) { LevelGen::LevelGen(void) : _world(this) {
_player = NULL;
} }
LevelGen::~LevelGen(void) { LevelGen::~LevelGen(void) {
@ -279,7 +280,7 @@ void LevelGen::GenerateEnemies(void) {
} }
} }
void LevelGen::MoveIfPossible(Character* character, float xVel, float yVel) { void LevelGen::MoveIfPossible(Character* character, float xVel, float yVel, bool isPlayer) {
if(xVel == 0.0f && yVel == 0.0f) { if(xVel == 0.0f && yVel == 0.0f) {
return; return;
} }
@ -327,6 +328,18 @@ void LevelGen::MoveIfPossible(Character* character, float xVel, float yVel) {
return; return;
} }
if(!isPlayer) {
SDL_Rect playerRect;
playerRect.x = _player->GetX();
playerRect.y = _player->GetY();
playerRect.w = _player->GetWidth();
playerRect.h = _player->GetHeight();
if(CheckCollisionRect(playerRect, charRect)) {
return;
}
}
character->SetXY(targetX, targetY); character->SetXY(targetX, targetY);
} }

View File

@ -17,6 +17,7 @@
using namespace std; using namespace std;
class Character; class Character;
class Player;
class LevelGen { class LevelGen {
public: public:
@ -28,6 +29,7 @@ public:
void Render(void); void Render(void);
void FindSpawnPoint(int& xArg, int& yArg, int objWidth, int objHeight); void FindSpawnPoint(int& xArg, int& yArg, int objWidth, int objHeight);
void MoveIfPossible(Character* character, float xVel, float yVel, bool isPlayer = false);
bool GetTileSolidity(int xArg, int yArg); bool GetTileSolidity(int xArg, int yArg);
int GetTileX(int xArg, int yArg); int GetTileX(int xArg, int yArg);
@ -45,7 +47,7 @@ public:
WorldManager& GetWorld(void) { return _world; } WorldManager& GetWorld(void) { return _world; }
void MoveIfPossible(Character* character, float xVel, float yVel); void SetPlayer(Player* player) { _player = player; }
private: private:
void Unload(void); void Unload(void);
@ -68,4 +70,6 @@ private:
TextureManager _entityTextures; TextureManager _entityTextures;
WorldManager _world; WorldManager _world;
Player* _player;
}; };