A 2D tile-based sandbox game.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Chunk.h 1003B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. #include <SFML/Graphics/Texture.hpp>
  3. #include <string.h>
  4. #include "common.h"
  5. #include "Tile.h"
  6. #include "TileMap.h"
  7. namespace Swan {
  8. class Chunk {
  9. public:
  10. using ChunkPos = Vector2<int>;
  11. using RelPos = Vector2<int>;
  12. ChunkPos pos_;
  13. bool dirty_ = false;
  14. Tile::ID tiles_[CHUNK_WIDTH][CHUNK_HEIGHT];
  15. sf::Texture texture_;
  16. sf::Sprite sprite_;
  17. Chunk(ChunkPos pos): pos_(pos) {
  18. texture_.create(CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE);
  19. sprite_ = sf::Sprite(texture_);
  20. }
  21. void setTileID(TileMap &tmap, RelPos pos, Tile::ID id) {
  22. tiles_[pos.x_][pos.y_] = id;
  23. drawBlock(tmap, pos, id);
  24. }
  25. Tile &getTile(TileMap &tmap, RelPos pos) {
  26. return tmap.get(tiles_[pos.x_][pos.y_]);
  27. }
  28. void drawBlock(RelPos pos, const Tile &t) {
  29. texture_.update(t.image_, pos.x_ * TILE_SIZE, pos.y_ * TILE_SIZE);
  30. dirty_ = true;
  31. }
  32. void drawBlock(TileMap &tmap, RelPos pos, Tile::ID id) {
  33. drawBlock(pos, tmap.get(id));
  34. }
  35. void redraw(TileMap &tmap);
  36. void draw(Win &win);
  37. };
  38. }