Advertisement
erzis

Untitled

Dec 31st, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.64 KB | None | 0 0
  1. bool TileSet::Collider(sf::Sprite &playerSprite, sf::Vector2f direction)
  2. {
  3.     playerSprite.move(direction);
  4.  
  5.     for (int i = 0; i < (int)currentSet.size(); i++)
  6.     {
  7.         if (currentSet.at(i).canStep == false)
  8.         {
  9.             if (playerSprite.getGlobalBounds().intersects(currentSet.at(i).GetBounds()))
  10.             {
  11.                 playerSprite.move(-direction);
  12.                 return false;
  13.             }
  14.         }
  15.     }
  16.     playerSprite.move(-direction);
  17.     return true;
  18. }
  19.  
  20. //Loads tiles from default.bin
  21. bool TileSet::LoadTiles()
  22. {
  23.     InitializeTiles();
  24.     tileTexture.loadFromFile("tiles/basictiles.png");
  25.  
  26.     //Opens file and loads size of map
  27.     std::ifstream tileStream;
  28.     tileStream.open("default.bin");
  29.     if (tileStream.is_open() == false) return false;
  30.     tileStream >> sizeX >> sizeY;
  31.     currentSet.reserve(sizeX*sizeY);
  32.     float x_size = displaysizeX / sizeX;
  33.     float y_size = displaysizeY / sizeY;
  34.  
  35.     //Loads tiles
  36.     int currentX = 0, currentY = 0;
  37.     while (true)
  38.     {
  39.         int tileType;
  40.         tileStream >> tileType;
  41.         TileType type;
  42.         if (tileType == 1)  type = tileTrain;//TODO: optimalization
  43.         else if (tileType == 2) type = tileTrack;
  44.  
  45.         //Dispatch type of tile to tile constructor
  46.         Tile tile(type,tileTexture);
  47.  
  48.         //Sets scale & position on screen
  49.         tile.SetScale(x_size, y_size);
  50.         tile.SetPosition(currentX, currentY);
  51.  
  52.         //Push created tile to tile list in map
  53.         currentSet.push_back(tile);
  54.  
  55.         //Counts current x and y offset for tile
  56.         currentX++;
  57.         if (currentX == sizeX)
  58.         {
  59.             currentX = 0;
  60.             currentY++;
  61.         }
  62.         if (tileStream.eof() == true) return true;
  63.     }
  64.     return true;
  65. }
  66.  
  67. //Tile is "Tile" nested class constructor, which is inside "TileSet" class, and it needs one parameter,
  68. //which is of type "TileType" struct, defined in "TileSet" class, called "type"
  69. TileSet::Tile::Tile(TileSet::TileType type,sf::Texture & texture)
  70. {
  71.     canStep = type.stepable;
  72.     tileSprite.setTexture(texture);
  73.     sf::IntRect offsetRectangle;
  74.     offsetRectangle.left = type.offsetX * tileSize;
  75.     offsetRectangle.top = type.offsetY * tileSize;
  76.     offsetRectangle.height = tileSize;
  77.     offsetRectangle.width = tileSize;
  78.     tileSprite.setTextureRect(offsetRectangle);
  79. }
  80.  
  81. //Draws specified tile
  82. void TileSet::Tile::Draw(sf::RenderWindow &window)
  83. {
  84.     window.draw(tileSprite);
  85. }
  86.  
  87. //Sets offset of tile
  88. void TileSet::Tile::SetPosition(int x, int y)
  89. {
  90.     tileSprite.setPosition(tileSprite.getScale().x * (float)x * tileSize, tileSize * (float)y * tileSprite.getScale().y);
  91. }
  92.  
  93. //Sets scale of tile - to fullfill window
  94. void TileSet::Tile::SetScale(float x, float y)
  95. {
  96.     tileSprite.setScale(x / (float)tileSize, y / (float)tileSize);
  97. }
  98.  
  99. sf::FloatRect TileSet::Tile::GetBounds()
  100. {
  101.     return tileSprite.getGlobalBounds();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement