[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) {
|
void Character::Move(void) {
|
||||||
|
map->MoveIfPossible(this, xVel, yVel);
|
||||||
|
|
||||||
|
/*
|
||||||
x += xVel;
|
x += xVel;
|
||||||
tileX = (int)(((x + (w / 2)) / TILE_WIDTH));
|
tileX = (int)(((x + (w / 2)) / TILE_WIDTH));
|
||||||
tileY = (int)(((y + (h / 2)) / TILE_HEIGHT));
|
tileY = (int)(((y + (h / 2)) / TILE_HEIGHT));
|
||||||
@ -175,6 +178,7 @@ void Character::Move(void) {
|
|||||||
if(CheckTileCollisions()) y -= yVel;
|
if(CheckTileCollisions()) y -= yVel;
|
||||||
if(CheckEntityCollisions()) y -= yVel;
|
if(CheckEntityCollisions()) y -= yVel;
|
||||||
if(CheckCharacterCollisions()) y -= yVel;
|
if(CheckCharacterCollisions()) y -= yVel;
|
||||||
|
*/
|
||||||
|
|
||||||
_healthBar.SetXY((int)x, (int)(y - _healthBar.GetHeight() - 5));
|
_healthBar.SetXY((int)x, (int)(y - _healthBar.GetHeight() - 5));
|
||||||
}
|
}
|
||||||
|
@ -63,10 +63,12 @@ public:
|
|||||||
// gMemManager.Free(object);
|
// gMemManager.Free(object);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
static const int FACING_UP = 0;
|
enum {
|
||||||
static const int FACING_RIGHT = 1;
|
FACING_UP,
|
||||||
static const int FACING_DOWN = 2;
|
FACING_RIGHT,
|
||||||
static const int FACING_LEFT = 3;
|
FACING_DOWN,
|
||||||
|
FACING_LEFT
|
||||||
|
};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void Move(void);
|
void Move(void);
|
||||||
|
@ -75,6 +75,23 @@ void WorldManager::CreateNPC(int x, int y) {
|
|||||||
_npcs.push_back(npc);
|
_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) {
|
void WorldManager::OnPlayerAttack(Player* player) {
|
||||||
int playerX = (int)(player->GetX() / 32.0f);
|
int playerX = (int)(player->GetX() / 32.0f);
|
||||||
int playerY = (int)(player->GetY() / 32.0f);
|
int playerY = (int)(player->GetY() / 32.0f);
|
||||||
|
@ -5,6 +5,8 @@ class NPC;
|
|||||||
class Player;
|
class Player;
|
||||||
class LevelGen;
|
class LevelGen;
|
||||||
|
|
||||||
|
#include <SDL/SDL.h>
|
||||||
|
|
||||||
class WorldManager {
|
class WorldManager {
|
||||||
public:
|
public:
|
||||||
WorldManager(LevelGen* level);
|
WorldManager(LevelGen* level);
|
||||||
@ -19,6 +21,7 @@ public:
|
|||||||
NPC* GetNPCAt(int xArg, int yArg);
|
NPC* GetNPCAt(int xArg, int yArg);
|
||||||
void CreateNPC(int x, int y);
|
void CreateNPC(int x, int y);
|
||||||
|
|
||||||
|
bool CheckCollision(const SDL_Rect& charRect);
|
||||||
|
|
||||||
int GetNPCCount() { return _npcs.size(); }
|
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) {
|
string LevelGen::GetCurrentMap(void) {
|
||||||
return _currentMap;
|
return _currentMap;
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,8 @@
|
|||||||
#include "../Engine/WorldManager.h"
|
#include "../Engine/WorldManager.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
class Character;
|
||||||
|
|
||||||
class LevelGen {
|
class LevelGen {
|
||||||
public:
|
public:
|
||||||
LevelGen(void);
|
LevelGen(void);
|
||||||
@ -47,6 +49,8 @@ public:
|
|||||||
|
|
||||||
WorldManager& GetWorld(void) { return _world; }
|
WorldManager& GetWorld(void) { return _world; }
|
||||||
|
|
||||||
|
void MoveIfPossible(Character* character, float xVel, float yVel);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void Unload(void);
|
void Unload(void);
|
||||||
void DoMagic(void);
|
void DoMagic(void);
|
||||||
|
Loading…
Reference in New Issue
Block a user