bool CollisionObject::isCollisionWithTileRight() { // iterate through collision layers for (std::vector<TileLayer*>::const_iterator it = m_pCollisionLayers->begin(); it != m_pCollisionLayers->end(); ++it) { TileLayer* pTileLayer = (*it); std::vector<std::vector<int>> tiles = pTileLayer->getTileIDs(); // get this layers position Vector2D layerPos = pTileLayer->getPosition(); int x, y, tileColumn, tileRow, tileid = 0; // calculate position on tile map x = layerPos.getX() / pTileLayer->getTileSize(); y = layerPos.getY() / pTileLayer->getTileSize(); if (getVelocity().getX() > 0) //dreta { tileColumn = ((getPosition().getX() + Camera::Instance()->getPosition().getX() + getWidth()) / pTileLayer->getTileSize()); tileRow = (getPosition().getY() / pTileLayer->getTileSize()); tileid = tiles[tileRow+y][tileColumn + x];//Li restem 1 a la y perque quan estiguem asobre la plataforma no ens doni colisio } if (tileid != 0) // if the tile id not blank then collide { return true; } } return false; }
void CollisionManager::checkPlayerTileCollision(Player* pPlayer, const std::vector<TileLayer*>& collisionLayers) { for(auto it = collisionLayers.begin(); it != collisionLayers.end(); ++it) { TileLayer* pTileLayer = (*it); std::vector<std::vector<int>> tiles = pTileLayer->getTileIDs(); Vector2D layerPos = pTileLayer->getPosition(); int x, y, tileColumn, tileRow, tileid = 0; x = layerPos.getX() / pTileLayer->getTileSize(); y = layerPos.getY() / pTileLayer->getTileSize(); if(pPlayer->getVelocity().getX() >= 0 || pPlayer->getVelocity().getY() >= 0) { tileColumn = ((pPlayer->getPosition().getX() + pPlayer->getWidth()) / pTileLayer->getTileSize()); tileRow = ((pPlayer->getPosition().getY() + pPlayer->getHeight()) / pTileLayer->getTileSize()); tileid = tiles[tileRow + y][tileColumn + x]; } else if(pPlayer->getVelocity().getX() < 0 || pPlayer->getVelocity().getY() < 0) { tileColumn = pPlayer->getPosition().getX() / pTileLayer->getTileSize(); tileRow = pPlayer->getPosition().getY() / pTileLayer->getTileSize(); tileid = tiles[tileRow + y][tileColumn + x]; } if(tileid != 0) { pPlayer->collision(); } } }
void Level::update() { ObjectLayer * pObjects; for (int i = 0; i < m_layers.size(); ++i) { if (m_pPlayer->getPosition()->getX() + m_pPlayer->getWidth() > 635) { TileLayer * pTile = dynamic_cast<TileLayer*>(m_layers[i]); if (pTile && pTile->getNumColumns() + pTile->getColumnIncrement() < pTile->getTileIDs().at(0).size()) { pTile->setColumns(20); m_pPlayer->setPosition(Vector2D(10, 330)); BulletHandler::Instance()->clearAll(); scrollObjects(); } else { m_pPlayer->setVelocity(Vector2D(0.0, 0.0)); } } m_layers[i]->update(); pObjects = dynamic_cast<ObjectLayer*>(m_layers[i]); if (pObjects) { CollisionManager::Instance()->checkEnemyPlayerBulletCollision(BulletHandler::Instance()->getBulletPlayer(), pObjects->getObjects()); } } CollisionManager::Instance()->checkPlayerEnemyBulletCollision(m_pPlayer, BulletHandler::Instance()->getBulletEnemy()); BulletHandler::Instance()->update(); if (m_pPlayer->isDead()) { Game::Instance()->getStateMachine()->changeState(new GameOverState()); } }
bool GameObject::tileCollisions(Vector2D pos){ for(vector<TileLayer*>::iterator i = collisionLayers->begin(); i != collisionLayers->end(); i++){ TileLayer* tileLayer = (*i); vector<vector<int>> tiles = tileLayer->getTileIDs(); Vector2D layerPos = tileLayer->getPosition(); int x, y, tileColumn, tileRow, tileid = 0; x = layerPos.X() / tileLayer->getTileSize(); y = layerPos.Y() / tileLayer->getTileSize(); Vector2D startPos = pos; startPos.setX( startPos.X() + 3); startPos.setY( startPos.Y() + 4); Vector2D endPos(pos.X() + collisionWidth, pos.Y() + collisionHeight - 4); for(int i = startPos.X(); i < endPos.X(); i++) { for(int j = startPos.Y(); j < endPos.Y(); j++) { tileColumn = i / tileLayer->getTileSize(); tileRow = j / tileLayer->getTileSize(); tileid = tiles[tileRow + y][tileColumn + x]; if(tileid != 0) { return true; } } } } return false; }
bool PlatformerObject::checkCollideTile(Vector2D newPos) { if(newPos.m_y + m_height >= TheGame::Instance()->getGameHeight() - 32) { return false; } else { for(std::vector<TileLayer*>::iterator it = m_pCollisionLayers->begin(); it != m_pCollisionLayers->end(); ++it) { TileLayer* pTileLayer = (*it); std::vector<std::vector<int>> tiles = pTileLayer->getTileIDs(); Vector2D layerPos = pTileLayer->getPosition(); int x, y, tileColumn, tileRow, tileid = 0; x = layerPos.getX() / pTileLayer->getTileSize(); y = layerPos.getY() / pTileLayer->getTileSize(); Vector2D startPos = newPos; startPos.m_x += 15; startPos.m_y += 20; Vector2D endPos(newPos.m_x + (m_width - 15), (newPos.m_y) + m_height - 4); for(int i = startPos.m_x; i < endPos.m_x; i++) { for(int j = startPos.m_y; j < endPos.m_y; j++) { tileColumn = i / pTileLayer->getTileSize(); tileRow = j / pTileLayer->getTileSize(); tileid = tiles[tileRow + y][tileColumn + x]; if(tileid != 0) { return true; } } } } return false; } }
bool Actor::checkCollideTile(Vector2D newPos) { for (std::vector<TileLayer*>::iterator it = m_pCollisionLayers->begin(); it != m_pCollisionLayers->end(); ++it) { TileLayer* pTileLayer = (*it); //Check in Map Bounds Before We Bother Checking Collision // Otherwise NO Tiles Stupid! if (newPos.m_x > 0 && newPos.m_x < pTileLayer->getMapWidth() - 100 && newPos.m_y > 0 && newPos.m_y < pTileLayer->getMapHeight()) { std::vector<std::vector<int>> tiles = pTileLayer->getTileIDs(); Vector2D layerPos = pTileLayer->getPosition(); int x, y, tileColumn, tileRow, tileID = 0; // get specific tile x = layerPos.getX() / pTileLayer->getTileSize(); y = layerPos.getY() / pTileLayer->getTileSize(); Vector2D startPos = newPos; startPos.m_x; startPos.m_y; Vector2D endPos(newPos.m_x + (m_width), newPos.m_y + (m_height)); for (int i = startPos.m_x; i < endPos.m_x; ++i) { for (int j = startPos.m_y; j < endPos.m_y; ++j) { tileColumn = i / pTileLayer->getTileSize(); tileRow = j / pTileLayer->getTileSize(); tileID = tiles[tileRow + y][tileColumn + x]; if (tileID != 0) { return true; } } } } } return false; }