#include "Tree.hpp"

namespace {
  static const unsigned int coneMeshN = 7;
}

Tanks::Tree::Mesh::Mesh(
  const unsigned int size,
  const double rad
) : ConeMesh(coneMeshN) {
  static const double h = 1.5;
  static const double d = h / 4.5;
  scale(
    0.25,
    h + d * (size - 1) - d / 2,
    0.25
  ).setLight();
  ::Mesh* crown = new ConeMesh(7);
  crown->setColor(
    Color(Color::Cyan) *= 0.75
  ).setLight().translate(-1);
  for (
    unsigned int i = 0;
    i < size;
    i++
  ) {
    ::Mesh* m = crown->clone();
    const double factor = 1 / (1 + i / 4.0);
    *this += m->scale(
      factor,
      0.9,
      factor
    ).rotateY(i % 2 ? Math::PI / coneMeshN : 0).rotateX(rad).translate(h + i * d);
    delete m;
  }
  delete crown;
  translate(
    - (h + size * d) / 2
  );
}

const Mesh& Tanks::Tree::getMesh() const {
  return mesh;
}

Tanks::Tree::Tree(
  const unsigned int size,
  const double rad
) : mesh(
  size,
  rad
) {
}
