
#include "tilebase.hpp"

// ----------------- Map -----------------
Map::Map()
  : worldtime(0), DEBUG(false) {
  for (int i = 0; i < 4; i++) {
    layers[i] = Layer(i, this);
  }
}

// ----------------- Layer -----------------
Layer::Layer(ID Id, Map* Owner)
  : id(Id), DEBUG(false), owner(Owner) {}

Layer::Layer()
  : id(0), DEBUG(false), owner(nullptr) {}

// ----------------- Chunk -----------------
Chunk::Chunk(Layer* Owner)
  : position(0, 0), owner(Owner) {
}

Chunk::Chunk()
  : position(0, 0), owner(nullptr) {}

// ----------------- Tile -----------------
Tile::Tile(ID Id, ID Spriteid, Chunk* Owner)
  : id(Id), spriteid(Spriteid), animationframe(0), owner(Owner) {}

Tile::Tile()
  : id(0), spriteid(1), animationframe(0), owner(nullptr) {}

// ----------------- Vector2Hash / Eq -----------------
std::size_t Vector2Hash::operator()(const Vector2& v) const noexcept {
  std::size_t h1 = std::hash<float>{}(v.x);
  std::size_t h2 = std::hash<float>{}(v.y);
  return h1 ^ (h2 << 1);
}

bool Vector2Eq::operator()(const Vector2& a, const Vector2& b) const noexcept {
  return (a.x == b.x && a.y == b.y);
}

void Tile::Draw() {
  int posx = owner->position.x * 64 * 8 + id % 8;
  int posy = owner->position.y * 64 * 8 + id / 8;

  DrawTextureEx(tileTextures.GetTexture(spriteid), Vector2({(float)posx,(float)posy}), 0, 1, WHITE);

}

void Chunk::Draw() {
  for (int i = 0; i < 64; i++) {
    tiles[i].Draw();
  }
}

void Layer::Draw() {
  for (auto it = chunks.begin(); it != chunks.end(); ++it) {
    it->second.Draw();
  }
}

void Map::Draw() {
  for (int i = 0; i < 4; i++) {
    layers[i].Draw();
  }
}
