[Fix] Collision testing is now more accurate.
This commit is contained in:
parent
cc258c35e0
commit
3e9c83428d
@ -157,6 +157,9 @@ 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));
|
||||
@ -175,7 +178,8 @@ void Character::Move(void) {
|
||||
if(CheckTileCollisions()) y -= yVel;
|
||||
if(CheckEntityCollisions()) y -= yVel;
|
||||
if(CheckCharacterCollisions()) y -= yVel;
|
||||
|
||||
*/
|
||||
|
||||
_healthBar.SetXY((int)x, (int)(y - _healthBar.GetHeight() - 5));
|
||||
}
|
||||
|
||||
|
@ -62,11 +62,13 @@ public:
|
||||
// inline void operator delete [](void* object) {
|
||||
// gMemManager.Free(object);
|
||||
// }
|
||||
|
||||
static const int FACING_UP = 0;
|
||||
static const int FACING_RIGHT = 1;
|
||||
static const int FACING_DOWN = 2;
|
||||
static const int FACING_LEFT = 3;
|
||||
|
||||
enum {
|
||||
FACING_UP,
|
||||
FACING_RIGHT,
|
||||
FACING_DOWN,
|
||||
FACING_LEFT
|
||||
};
|
||||
|
||||
protected:
|
||||
void Move(void);
|
||||
|
@ -75,6 +75,23 @@ void WorldManager::CreateNPC(int x, int y) {
|
||||
_npcs.push_back(npc);
|
||||
}
|
||||
|
||||
bool WorldManager::CheckCollision(const SDL_Rect& charRect) {
|
||||
for(std::list<NPC*>::iterator i = _npcs.begin(); i != _npcs.end(); ++i) {
|
||||
NPC* npc = (*i);
|
||||
|
||||
SDL_Rect npcRect;
|
||||
npcRect.x = npc->GetX();
|
||||
npcRect.y = npc->GetY();
|
||||
npcRect.w = npc->GetWidth();
|
||||
npcRect.h = npc->GetHeight();
|
||||
|
||||
if(CheckCollisionRect(npcRect, charRect)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void WorldManager::OnPlayerAttack(Player* player) {
|
||||
int playerX = (int)(player->GetX() / 32.0f);
|
||||
int playerY = (int)(player->GetY() / 32.0f);
|
||||
|
@ -5,6 +5,8 @@ class NPC;
|
||||
class Player;
|
||||
class LevelGen;
|
||||
|
||||
#include <SDL/SDL.h>
|
||||
|
||||
class WorldManager {
|
||||
public:
|
||||
WorldManager(LevelGen* level);
|
||||
@ -19,6 +21,7 @@ public:
|
||||
NPC* GetNPCAt(int xArg, int yArg);
|
||||
void CreateNPC(int x, int y);
|
||||
|
||||
bool CheckCollision(const SDL_Rect& charRect);
|
||||
|
||||
int GetNPCCount() { return _npcs.size(); }
|
||||
|
||||
|
@ -297,6 +297,57 @@ void LevelGen::GenerateEnemies(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void LevelGen::MoveIfPossible(Character* character, float xVel, float yVel) {
|
||||
if(xVel == 0.0f && yVel == 0.0f) {
|
||||
return;
|
||||
}
|
||||
|
||||
int targetX = character->GetX() + xVel;
|
||||
int targetY = character->GetY() + yVel;
|
||||
|
||||
if(targetX < 0 || targetX > (SCREEN_WIDTH - character->GetWidth()) ||
|
||||
targetY < 0 || targetY > (SCREEN_HEIGHT - character->GetHeight())) {
|
||||
return;
|
||||
}
|
||||
|
||||
int targetTileX = targetX / TILE_WIDTH;
|
||||
int targetTileY = targetY / TILE_HEIGHT;
|
||||
|
||||
if(_tile[targetTileX][targetTileY].GetTileSolidity()) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_Rect charRect;
|
||||
charRect.x = targetX;
|
||||
charRect.y = targetY + (character->GetHeight() / 2);
|
||||
charRect.w = character->GetWidth();
|
||||
charRect.h = character->GetHeight() / 2;
|
||||
|
||||
for(int x = 0; x < BOUNDARIES_X; x++) {
|
||||
for(int y = 0; y < BOUNDARIES_Y; y++) {
|
||||
if(!_tile[x][y].GetEntitySolitity()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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, charRect)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(_world.CheckCollision(charRect)) {
|
||||
return;
|
||||
}
|
||||
|
||||
character->SetXY(targetX, targetY);
|
||||
}
|
||||
|
||||
string LevelGen::GetCurrentMap(void) {
|
||||
return _currentMap;
|
||||
}
|
||||
|
@ -16,6 +16,8 @@
|
||||
#include "../Engine/WorldManager.h"
|
||||
using namespace std;
|
||||
|
||||
class Character;
|
||||
|
||||
class LevelGen {
|
||||
public:
|
||||
LevelGen(void);
|
||||
@ -46,6 +48,8 @@ public:
|
||||
string GetCurrentMap(void);
|
||||
|
||||
WorldManager& GetWorld(void) { return _world; }
|
||||
|
||||
void MoveIfPossible(Character* character, float xVel, float yVel);
|
||||
|
||||
private:
|
||||
void Unload(void);
|
||||
|
Loading…
Reference in New Issue
Block a user