A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "Chunk.h"
  2. namespace Swan {
  3. void Chunk::setTileID(TileMap &tmap, RelPos pos, Tile::ID id) {
  4. tiles_[pos.x_][pos.y_] = id;
  5. drawBlock(tmap, pos, id);
  6. }
  7. Tile &Chunk::getTile(TileMap &tmap, RelPos pos) {
  8. return tmap.get(tiles_[pos.x_][pos.y_]);
  9. }
  10. void Chunk::drawBlock(RelPos pos, const Tile &t) {
  11. texture_.update(t.image_, pos.x_ * TILE_SIZE, pos.y_ * TILE_SIZE);
  12. dirty_ = true;
  13. }
  14. void Chunk::drawBlock(TileMap &tmap, RelPos pos, Tile::ID id) {
  15. drawBlock(pos, tmap.get(id));
  16. }
  17. void Chunk::redraw(TileMap &tmap) {
  18. for (int x = 0; x < CHUNK_WIDTH; ++x) {
  19. for (int y = 0; y < CHUNK_HEIGHT; ++y) {
  20. drawBlock(tmap, ChunkPos(x, y), tiles_[x][y]);
  21. }
  22. }
  23. }
  24. void Chunk::draw(Win &win) {
  25. if (dirty_) {
  26. sprite_.setTexture(texture_);
  27. dirty_ = false;
  28. }
  29. win.setPos(Vec2(pos_.x_ * CHUNK_WIDTH * TILE_SIZE, pos_.y_ * CHUNK_HEIGHT * TILE_SIZE));
  30. win.draw(sprite_);
  31. }
  32. }