Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool TileSet::Collider(sf::Sprite &playerSprite, sf::Vector2f direction)
- {
- playerSprite.move(direction);
- for (int i = 0; i < (int)currentSet.size(); i++)
- {
- if (currentSet.at(i).canStep == false)
- {
- if (playerSprite.getGlobalBounds().intersects(currentSet.at(i).GetBounds()))
- {
- playerSprite.move(-direction);
- return false;
- }
- }
- }
- playerSprite.move(-direction);
- return true;
- }
- //Loads tiles from default.bin
- bool TileSet::LoadTiles()
- {
- InitializeTiles();
- tileTexture.loadFromFile("tiles/basictiles.png");
- //Opens file and loads size of map
- std::ifstream tileStream;
- tileStream.open("default.bin");
- if (tileStream.is_open() == false) return false;
- tileStream >> sizeX >> sizeY;
- currentSet.reserve(sizeX*sizeY);
- float x_size = displaysizeX / sizeX;
- float y_size = displaysizeY / sizeY;
- //Loads tiles
- int currentX = 0, currentY = 0;
- while (true)
- {
- int tileType;
- tileStream >> tileType;
- TileType type;
- if (tileType == 1) type = tileTrain;//TODO: optimalization
- else if (tileType == 2) type = tileTrack;
- //Dispatch type of tile to tile constructor
- Tile tile(type,tileTexture);
- //Sets scale & position on screen
- tile.SetScale(x_size, y_size);
- tile.SetPosition(currentX, currentY);
- //Push created tile to tile list in map
- currentSet.push_back(tile);
- //Counts current x and y offset for tile
- currentX++;
- if (currentX == sizeX)
- {
- currentX = 0;
- currentY++;
- }
- if (tileStream.eof() == true) return true;
- }
- return true;
- }
- //Tile is "Tile" nested class constructor, which is inside "TileSet" class, and it needs one parameter,
- //which is of type "TileType" struct, defined in "TileSet" class, called "type"
- TileSet::Tile::Tile(TileSet::TileType type,sf::Texture & texture)
- {
- canStep = type.stepable;
- tileSprite.setTexture(texture);
- sf::IntRect offsetRectangle;
- offsetRectangle.left = type.offsetX * tileSize;
- offsetRectangle.top = type.offsetY * tileSize;
- offsetRectangle.height = tileSize;
- offsetRectangle.width = tileSize;
- tileSprite.setTextureRect(offsetRectangle);
- }
- //Draws specified tile
- void TileSet::Tile::Draw(sf::RenderWindow &window)
- {
- window.draw(tileSprite);
- }
- //Sets offset of tile
- void TileSet::Tile::SetPosition(int x, int y)
- {
- tileSprite.setPosition(tileSprite.getScale().x * (float)x * tileSize, tileSize * (float)y * tileSprite.getScale().y);
- }
- //Sets scale of tile - to fullfill window
- void TileSet::Tile::SetScale(float x, float y)
- {
- tileSprite.setScale(x / (float)tileSize, y / (float)tileSize);
- }
- sf::FloatRect TileSet::Tile::GetBounds()
- {
- return tileSprite.getGlobalBounds();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement