YAPOG  0.0.1
Yet Another Pokemon Online Game
TileLayer.cpp
Go to the documentation of this file.
6 
7 namespace yap
8 {
9  const bool TileLayer::DEFAULT_VISIBLE_STATE = true;
10  const sf::Color TileLayer::DEFAULT_COLOR = sf::Color ();
12 
13  TileLayer::TileLayer (uint width, uint height)
14  : width_ (width)
15  , height_ (height)
16  , tiles_ (width_ * height_, nullptr)
17  , isVisible_ (DEFAULT_VISIBLE_STATE)
18  , color_ (DEFAULT_COLOR)
19  {
20  }
21 
23  {
24  for (const Tile* tile : tiles_)
25  delete tile;
26  }
27 
29  : width_ (copy.width_)
30  , height_ (copy.height_)
31  , tiles_ (width_ * height_, nullptr)
32  , isVisible_ (copy.isVisible_)
33  , color_ (copy.color_)
34  {
36  count < width_ * height_;
37  ++count)
38  tiles_[count] = copy.tiles_[count]->Clone ();
39  }
40 
42  {
43  return new TileLayer (*this);
44  }
45 
46  const Tile& TileLayer::GetTile (uint x, uint y) const
47  {
49 
50  return *tiles_[index];
51  }
52 
54  {
56 
57  return *tiles_[index];
58  }
59 
60  void TileLayer::SetTile (uint x, uint y, Tile* tile)
61  {
63 
64  tiles_[index] = tile;
65 
66  tile->SetPosition (
67  Vector2 (
69  y * Tile::DEFAULT_SIZE));
70  }
71 
72  const uint& TileLayer::GetWidth () const
73  {
74  return width_;
75  }
76 
77  const uint& TileLayer::GetHeight () const
78  {
79  return height_;
80  }
81 
83  {
84  if (!IsVisible ())
85  return;
86 
87  int left =
88  context.GetCamera ().GetTopLeft ().x / Tile::DEFAULT_SIZE -
90  int top =
91  context.GetCamera ().GetTopLeft ().y / Tile::DEFAULT_SIZE -
93  int right =
94  context.GetCamera ().GetBottomRight ().x / Tile::DEFAULT_SIZE +
96  int bottom =
97  context.GetCamera ().GetBottomRight ().y / Tile::DEFAULT_SIZE +
99 
100  int maxX = width_ - 1;
101  int maxY = height_ - 1;
102 
103  uint clampedLeft = MathHelper::Clamp (left, 0, maxX);
104  uint clampedTop = MathHelper::Clamp (top, 0, maxY);
105  uint clampedRight = MathHelper::Clamp (right, 0, maxX);
106  uint clampedBottom = MathHelper::Clamp (bottom, 0, maxY);
107 
108  for (uint y = clampedTop; y <= clampedBottom; ++y)
109  for (uint x = clampedLeft; x <= clampedRight; ++x)
110  tiles_[y * width_ + x]->Draw (context);
111  }
112 
113  bool TileLayer::IsVisible () const
114  {
115  return isVisible_;
116  }
117 
118  void TileLayer::Show (bool isVisible)
119  {
120  isVisible_ = isVisible;
121  }
122 
123  void TileLayer::ChangeColor (const sf::Color& color)
124  {
125  color_ = color;
126 
127  for (Tile* tile : tiles_)
128  tile->ChangeColor (color);
129  }
130 
131  void TileLayer::Update (const Time& dt)
132  {
133  for (Tile* tile : tiles_)
134  tile->Update (dt);
135  }
136 } // namespace yap