YAPOG  0.0.1
Yet Another Pokemon Online Game
World.cpp
Go to the documentation of this file.
2 
3 #include "World/World.hpp"
4 #include "World/Map/Map.hpp"
5 
6 namespace ycl
7 {
8  const bool World::DEFAULT_VISIBLE_STATE = true;
9  const sf::Color World::DEFAULT_COLOR = sf::Color ();
10 
12  : yap::World ()
13  , OnMapChanged ()
14  , isVisible_ (DEFAULT_VISIBLE_STATE)
15  , color_ (DEFAULT_COLOR)
16  , drawingPolicy_ (nullptr)
17  , currentMapID_ ()
18  , currentMap_ (nullptr)
19  , maps_ ()
20  , packetHandler_ ()
21  {
22  }
23 
25  {
26  }
27 
28  void World::ChangeMap (const yap::ID& worldID)
29  {
30  if (currentMapID_ == worldID)
31  return;
32 
33  Map* oldMap = currentMap_;
34 
35  if (oldMap != nullptr)
36  {
38  currentMap_->SetParent (nullptr);
39  }
40 
41  currentMapID_ = worldID;
42 
44 
46  currentMap_->SetParent (this);
47 
49  }
50 
52  {
53  return *currentMap_;
54  }
55 
56  void World::AddMap (const yap::ID& worldID, const yap::ID& id)
57  {
58  if (maps_.Contains (worldID))
59  return;
60 
61  Map* map = yap::ObjectFactory::Instance ().Get<Map> ("Map", id);
62  map->SetWorldID (worldID);
63 
64  AddMap (map);
65  }
66 
67  void World::AddMap (Map* map)
68  {
69  if (drawingPolicy_ != nullptr)
71 
72  maps_.Add (map->GetWorldID (), map);
73  }
74 
75  void World::RemoveMap (const yap::ID& worldID)
76  {
77  maps_.Remove (worldID);
78  }
79 
81  {
82  drawingPolicy_ = &drawingPolicy;
83 
84  for (auto& idMapPair : maps_)
85  idMapPair.second->SetWorldDrawingPolicy (*drawingPolicy_);
86  }
87 
89  {
90  if (!IsVisible ())
91  return;
92 
93  HandleDraw (context);
94  }
95 
96  bool World::IsVisible () const
97  {
98  return isVisible_;
99  }
100 
101  void World::Show (bool isVisible)
102  {
103  isVisible_ = isVisible;
104 
105  HandleShow (isVisible);
106  }
107 
108  void World::ChangeColor (const sf::Color& color)
109  {
110  color_ = color;
111 
112  HandleChangeColor (color);
113  }
114 
116  {
117  return packetHandler_.HandlePacket (packet);
118  }
119 
121  {
122  return packetHandler_.SendPacket (packet);
123  }
124 
126  {
127  packetHandler_.AddRelay (relay);
128  }
129 
131  {
132  packetHandler_.RemoveRelay (relay);
133  }
134 
136  {
137  packetHandler_.SetParent (parent);
138  }
139 
141  {
142  if (currentMap_ != nullptr)
143  currentMap_->Update (dt);
144  }
145 
147  {
148  if (currentMap_ != nullptr)
149  currentMap_->Draw (context);
150  }
151 
152  void World::HandleShow (bool isVisible)
153  {
154  }
155 
156  void World::HandleChangeColor (const sf::Color& color)
157  {
158  for (auto& it : maps_)
159  it.second->ChangeColor (color);
160  }
161 } // namespace ycl