From fcb1d02db01ccd8e077473802d4db103b1f6d46a Mon Sep 17 00:00:00 2001 From: Tamir Atias Date: Sun, 5 Feb 2012 19:37:01 +0200 Subject: [PATCH] [Remove] Speech bubbles. [Fix] NPCs now collide with the player. --- src/Unuk/Game.cpp | 1 + src/Unuk/Player.cpp | 1 + src/libUnuk/Engine/Character.cpp | 121 ------------------------------ src/libUnuk/Engine/Character.h | 13 ---- src/libUnuk/Engine/NPC.cpp | 2 + src/libUnuk/LevelGen/LevelGen.cpp | 17 ++++- src/libUnuk/LevelGen/LevelGen.h | 10 ++- 7 files changed, 26 insertions(+), 139 deletions(-) diff --git a/src/Unuk/Game.cpp b/src/Unuk/Game.cpp index ed2e9f3..6c07f3d 100644 --- a/src/Unuk/Game.cpp +++ b/src/Unuk/Game.cpp @@ -7,6 +7,7 @@ Game::Game(void) { Debug::logger->message("Creating characters.."); _player = new Player(&_map); + _map.SetPlayer(_player); _runGameReturnValue = gameMainMenu; } diff --git a/src/Unuk/Player.cpp b/src/Unuk/Player.cpp index c4baee7..c2a32ff 100644 --- a/src/Unuk/Player.cpp +++ b/src/Unuk/Player.cpp @@ -112,6 +112,7 @@ void Player::SetCamera(void) { } void Player::Move() { + map->MoveIfPossible(this, xVel, yVel, true); Character::Move(); } diff --git a/src/libUnuk/Engine/Character.cpp b/src/libUnuk/Engine/Character.cpp index 5ff3d93..112348e 100644 --- a/src/libUnuk/Engine/Character.cpp +++ b/src/libUnuk/Engine/Character.cpp @@ -59,38 +59,7 @@ void Character::LoadSprites(string filename, int wArg, int hArg) { _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) { - // 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) { ApplySurface((int)x, (int)y, _texture, screen, &_sprites[directionFacing][ANIM_ATTACK]); return; @@ -132,22 +101,6 @@ void Character::Render(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); } @@ -157,79 +110,5 @@ void Character::OnAttack(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)); } - -/* - * 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; -} diff --git a/src/libUnuk/Engine/Character.h b/src/libUnuk/Engine/Character.h index 8b6c23c..bc46387 100644 --- a/src/libUnuk/Engine/Character.h +++ b/src/libUnuk/Engine/Character.h @@ -40,8 +40,6 @@ public: int GetDirectionFacing(void) { return directionFacing; } void SetDirectionFacing(int dir) { directionFacing = dir; } - void AddSpeachBubble(string text); - void Render(void); void Update(void); @@ -73,10 +71,6 @@ public: protected: void Move(void); - bool CheckTileCollisions(void); - bool CheckEntityCollisions(void); - bool CheckCharacterCollisions(void); - float x; float y; float w; @@ -112,8 +106,6 @@ private: static const int ANIMATION_SPEED = 200; static const int ATTACKING_DISPLAY_LEN = 150; - static const int SPEACH_BUBBLE_DISPLAY_LENGTH = 6000; - SDL_Surface* _texture; // [direction][action] @@ -122,9 +114,4 @@ private: Timer _animationTimer; int _animationStage; bool _leftFoot; - - list _speachBubble; - list::iterator _speachBubbleIter; - Timer _speachBubbleTimer; - Text _speachBubbleText; }; diff --git a/src/libUnuk/Engine/NPC.cpp b/src/libUnuk/Engine/NPC.cpp index c4d4e75..2be24de 100644 --- a/src/libUnuk/Engine/NPC.cpp +++ b/src/libUnuk/Engine/NPC.cpp @@ -57,5 +57,7 @@ void NPC::Move(void) { } _moving = true; } + + map->MoveIfPossible(this, xVel, yVel, false); Character::Move(); } diff --git a/src/libUnuk/LevelGen/LevelGen.cpp b/src/libUnuk/LevelGen/LevelGen.cpp index c5a34eb..09a2833 100644 --- a/src/libUnuk/LevelGen/LevelGen.cpp +++ b/src/libUnuk/LevelGen/LevelGen.cpp @@ -2,9 +2,10 @@ #include "LevelGen.h" #include "../Engine/NPC.h" +#include "../../Unuk/Player.h" LevelGen::LevelGen(void) : _world(this) { - + _player = NULL; } 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) { return; } @@ -327,6 +328,18 @@ void LevelGen::MoveIfPossible(Character* character, float xVel, float yVel) { 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); } diff --git a/src/libUnuk/LevelGen/LevelGen.h b/src/libUnuk/LevelGen/LevelGen.h index a1e0f45..60073c3 100644 --- a/src/libUnuk/LevelGen/LevelGen.h +++ b/src/libUnuk/LevelGen/LevelGen.h @@ -17,6 +17,7 @@ using namespace std; class Character; +class Player; class LevelGen { public: @@ -28,7 +29,8 @@ public: void Render(void); 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); int GetTileX(int xArg, int yArg); int GetTileY(int xArg, int yArg); @@ -44,9 +46,9 @@ public: string GetCurrentMap(void); WorldManager& GetWorld(void) { return _world; } - - void MoveIfPossible(Character* character, float xVel, float yVel); + void SetPlayer(Player* player) { _player = player; } + private: void Unload(void); void DoMagic(void); @@ -68,4 +70,6 @@ private: TextureManager _entityTextures; WorldManager _world; + + Player* _player; };