Tile* Chunk::GetTileAt(int x,int y) { if (x >= SIZE) { Chunk* current = m_WorldPtr->GetChunkAt(m_X+1, m_Y); if (current == nullptr) return nullptr; return current->GetTileAt(x-SIZE, y); } if (x < 0) { Chunk* current = m_WorldPtr->GetChunkAt(m_X-1, m_Y); if (current == nullptr) return nullptr; return current->GetTileAt(x+ SIZE, y ); } if (y >= SIZE) { Chunk* current = m_WorldPtr->GetChunkAt(m_X, m_Y + 1); if (current == nullptr) return nullptr; return current->GetTileAt(x, y - SIZE); } if(y < 0) { Chunk* current = m_WorldPtr->GetChunkAt(m_X, m_Y - 1); if (current == nullptr) return nullptr; return current->GetTileAt(x, y + SIZE); } return m_TileArrPtr[x*SIZE + y]; }
void ItemEntity::DoCollision(World* world, double deltaTime) { m_pos.x += m_motion.x; m_pos.y += m_motion.y; int toprightX = (int)(m_pos.x - m_size.x / 2.0); int toprightY = (int)(m_pos.y - m_size.y / 2.0); Chunk* chunkPtr = world->GetChunkAt(toprightX / (Chunk::TILESIZE*Chunk::SIZE), toprightY / (Chunk::TILESIZE*Chunk::SIZE)); if (chunkPtr == nullptr) { m_motion.y += 1 * deltaTime; return; } int blockposX = toprightX / (Chunk::TILESIZE) - chunkPtr->GetX()*Chunk::SIZE; int blockposY = (toprightY + m_size.y/2.0) / (Chunk::TILESIZE) - chunkPtr->GetY()*Chunk::SIZE; Tile* tilePtr = chunkPtr->GetTileAt(blockposX, blockposY); double topRightLocX = toprightX - chunkPtr->GetX()* Chunk::TILESIZE* Chunk::SIZE; double topRightLocY = toprightY - chunkPtr->GetY()* Chunk::TILESIZE* Chunk::SIZE; if (tilePtr != nullptr) { if (tilePtr->type != Chunk::Type::AIR) { m_pos.y = blockposY * Chunk::TILESIZE + chunkPtr->GetY()* Chunk::TILESIZE* Chunk::SIZE - m_size.y / 2; m_motion.y = 0; } else { m_motion.y += 1 * deltaTime; } } }