mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-16 20:41:47 +01:00
Add more digging logic
This commit is contained in:
@@ -40,6 +40,11 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
if(m_Grounded) {
|
||||
if(m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr) {
|
||||
m_ContactMap[Collision::CollisionDirection::Bottom]->SetTileType(GroundTileTypes::Air);
|
||||
WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Bottom];
|
||||
//center of tile
|
||||
Point2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2;
|
||||
m_Position = Point2f{tileCenter.x - m_Size.x / 2, tileCenter.y - m_Size.y / 2};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -56,12 +61,15 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
Point2f intersectionPoint, normal;
|
||||
|
||||
std::vector<std::pair<int, float>> contactTimes{};
|
||||
|
||||
WorldGridManager& gridManager = level.GetGridManager();
|
||||
|
||||
for (size_t x { 0 }; x < level.WORLD_WIDTH; ++x) {
|
||||
for(size_t y { 0 }; y < level.WORLD_HEIGHT; ++y) {
|
||||
if(level.GetTileAt(Point2f{(float)x,(float)y})->GetTileType() == GroundTileTypes::Dirt) {
|
||||
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, level.GetTileAt(Point2f{(float)x, (float)y})->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
|
||||
contactTimes.push_back(std::pair<int, float>{x + y * level.WORLD_WIDTH, t});
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for(size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
if(gridManager.GetTileAtIndex(x,y)->GetTileType() == GroundTileTypes::Dirt) {
|
||||
gridManager.GetTileAtIndex(x,y)->m_Hightlight = false;
|
||||
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, gridManager.GetTileAtIndex(x, y)->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
|
||||
contactTimes.push_back(std::pair<int, float>{x + y * WORLD_WIDTH, t});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,19 +80,43 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
});
|
||||
|
||||
for (std::pair<int, float> contact_time : contactTimes) {
|
||||
int x = contact_time.first % WorldLevel::WORLD_WIDTH;
|
||||
int y = contact_time.first / WorldLevel::WORLD_WIDTH;
|
||||
WorldTile* world_tile = level.GetTileAt(Point2f{(float) x, (float) y});
|
||||
//check if tile is next to player
|
||||
int x = contact_time.first % WORLD_WIDTH;
|
||||
int y = contact_time.first / WORLD_WIDTH;
|
||||
WorldTile* world_tile = gridManager.GetTileAtIndex(x,y);
|
||||
world_tile->m_Hightlight = true;
|
||||
|
||||
//check if the collision happend beneeth the player
|
||||
if(world_tile->GetCollisionRect().pos.y < m_Position.y) {
|
||||
m_Grounded = true;
|
||||
m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
||||
bool isDiggingCandidate = true;
|
||||
Collision::CollisionRect tileRect = world_tile->GetCollisionRect().getCollisionRect();
|
||||
//check if the tile is above the player
|
||||
if(tileRect.pos.y > m_Position.y + m_Size.y) {
|
||||
isDiggingCandidate = false;
|
||||
}
|
||||
//check if tile is left of player
|
||||
if(tileRect.pos.x > m_Position.x + m_Size.x) {
|
||||
isDiggingCandidate = false;
|
||||
}
|
||||
//check if tile is right of player
|
||||
if(tileRect.pos.x + tileRect.size.x < m_Position.x) {
|
||||
isDiggingCandidate = false;
|
||||
}
|
||||
//check if the collision happend under the player
|
||||
if(tileRect.pos.y < m_Position.y - m_Size.y) {
|
||||
//Tile is under player
|
||||
|
||||
//check if the center of the player is between the centers of the tiles left and right
|
||||
if(m_Position.x + m_Size.x / 2 > tileRect.pos.x && m_Position.x + m_Size.x / 2 < tileRect.pos.x + tileRect.size.x) {
|
||||
if(isDiggingCandidate) {
|
||||
//Dig the tile
|
||||
m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
||||
m_Grounded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
||||
//m_Grounded = true;
|
||||
|
||||
Collision::CollisionRect rect = world_tile->GetCollisionRect().getCollisionRect(); //TODO: fix this mess
|
||||
Collision::ResolvePlayerVsRect(*this, elapsedTime, &rect);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user