YAPOG  0.0.1
Yet Another Pokemon Online Game
GameplayScreen.cpp
Go to the documentation of this file.
32 
34 #include "World/Map/Player.hpp"
35 #include "World/Map/Map.hpp"
36 #include "Client/User.hpp"
37 #include "Client/Session.hpp"
38 #include "Gui/GameGuiManager.hpp"
39 #include "Gui/MapRootWidget.hpp"
40 #include "Gui/GameMainMenu.hpp"
41 #include "Gui/ChatWidget.hpp"
42 #include "Gui/PokedexWidget.hpp"
46 #include "Pokemon/Pokemon.hpp"
47 #include "Pokemon/PokemonTeam.hpp"
48 #include "Battle/PlayerTrainer.hpp"
49 #include "Battle/PokemonFighter.hpp"
50 #include "Battle/PokemonFighterTeam.hpp"
51 #include "Battle/Battle.hpp"
52 #include "Battle/WildBattle.hpp"
55 
56 namespace debug
57 {
59  {
60  yap::ID staticID = yap::ID (yap::RandomHelper::GetNext (1, 10));
61 
62  if (staticID == yap::ID (10))
63  staticID = yap::ID (16);
64 
65  int level = yap::RandomHelper::GetNext (1, 100);
66 
67  ycl::Pokemon* p = new ycl::Pokemon (staticID, level, false);
68 
69  return p;
70  }
71 }
72 
73 namespace ycl
74 {
76 
78  : BaseScreen (DEFAULT_NAME, context)
79  , world_ ()
80  , worldDrawingPolicy_ (nullptr)
81  , cameraController_ (context.GetCamera ("World"))
82  , player_ (nullptr)
83  , moveController_ ()
84  , lastForce_ ()
85  , fpsDisplayTimer_ ()
86  , gameGuiManager_ (nullptr)
87  , gameWorldGuiManager_ (nullptr)
88  , mapRootWidget_ (nullptr)
89  , mainMenu_ (nullptr)
90  , pokedexWidget_ (nullptr)
91  , pokemonTeamWidget_ (nullptr)
92  , trainerCardWidget_ (nullptr)
93  , chat_ (nullptr)
94  , fpsLabel_ (nullptr)
95  {
96  }
97 
99  {
100  }
101 
103  {
105 
106  session_.GetUser ().OnPlayerCreated += [this] (
107  const User& sender,
108  Player* args)
109  {
110  SetPlayer (args);
111  };
112 
113  session_.GetUser ().OnPlayerWarped += [this] (
114  const User& sender,
115  Player& args)
116  {
118  };
119 
120  session_.GetUser ().OnMessageReceived += [this] (
121  const User& sender,
122  const yap::GameMessage& message)
123  {
124  chat_->AddMessage (message);
125  };
126 
127  session_.GetUser ().OnBattleTriggered += [this] (
128  User& sender,
129  const yap::EmptyEventArgs& args)
130  {
131  BattleParameters* battleParameters = new BattleParameters ();
132 
133  IDrawableBattleEntity* battleEntity =
135 
136  battleParameters->SetOpponent (battleEntity);
137 
138  sender.SetBattleParameters (battleParameters);
139 
140  nextScreen_ = "Battle";
141  };
142 
143  session_.GetUser ().OnPokemonTeamReceived += [this] (
144  const User& sender,
145  const yap::EmptyEventArgs& args)
146  {
147  // Team Manager Widget
152 
154  };
155 
156  session_.GetUser ().OnPlayerDataReceived += [this] (
157  User& sender,
158  const yap::EmptyEventArgs& args)
159  {
160  // Pokedex
161  yap::Pokedex* pokedex = new yap::Pokedex ();
162 
163  for (int i = 1; i < 4; i++)
164  {
166  Create<yap::PokemonInfo> ("PokemonInfo", yap::ID (i));
167 
168  pokedex->AddPokemon (pok);
169  pokedex->AddPokemonSeen (pok);
170  pokedex->AddPokemonCaught (pok);
171  }
172 
174  Create<yap::PokemonInfo> ("PokemonInfo", yap::ID (16));
175 
176  pokedex->AddPokemon (pok);
177  pokedex->AddPokemonSeen (pok);
178  pokedex->AddPokemonCaught (pok);
179 
180  pokedexWidget_ = new PokedexWidget (pokedex);
181  pokedexWidget_->Close ();
182  pokedexWidget_->Init ();
183 
184  sender.GetTrainer ().SetPokedex (pokedex);
185 
187 
188  // Trainer Card
192 
194  };
195 
198 
199  world_.OnMapChanged += [this] (
200  const World& sender,
201  const yap::ChangeEventArgs<Map*>& args)
202  {
203  SetCurrentMap (*args.Current);
204  };
205 
207 
209  context_.GetCamera ("Gui"),
210  context_.GetCamera ("World"),
214 
215  mapRootWidget_ = new MapRootWidget ();
216 
218 
219  // Chat
220  chat_ = new ChatWidget (session_.GetUser ().GetLogin ());
221  chat_->Init ();
222  chat_->Close ();
223 
224  chat_->OnMessageSent +=
225  [this] (ChatWidget& sender, yap::GameMessage& args)
226  {
227  session_.GetUser ().SendGameMessage (args);
228  };
229 
231 
232  // FPS
233  fpsLabel_ = new yap::Label ();
234  fpsLabel_->SetTextSize (18);
235 
237  }
238 
240  const yap::Time& dt,
241  yap::IDrawingContext& context)
242  {
243  if (fpsDisplayTimer_.DelayIsComplete (yap::Time (0.5f), true))
244  fpsLabel_->SetText (
245  "FPS: " +
246  yap::StringHelper::ToString<int> (1.0f / dt.GetValue ()));
247 
248  world_.Update (dt);
249 
251 
252  UpdatePlayer (dt);
253 
254  world_.Draw (context);
255 
256  BaseScreen::HandleRun (dt, context);
257  }
258 
260  {
261  switch (guiEvent.type)
262  {
263  case sf::Event::KeyPressed:
264 
265  switch (guiEvent.key.code)
266  {
267  case sf::Keyboard::Space:
268 
269  if (player_ != nullptr && player_->IsActive ())
270  break;
271 
273 
274  yap::AudioManager::Instance ().PlayMusic ("BGM/SettingMenu.ogg");
275 
276  return true;
277 
278  case sf::Keyboard::C:
279 
280  if (player_ != nullptr && player_->IsActive ())
281  break;
282 
284 
285  return true;
286 
287  default: break;
288  }
289 
290  break;
291 
292  case sf::Event::KeyReleased:
293 
294  switch (guiEvent.key.code)
295  {
296  default: break;
297  }
298 
299  default: break;
300  }
301 
304  guiEvent))
305  {
307  return true;
308  }
309 
312  guiEvent))
313  {
314  moveController_.EnableDirection (yap::Direction::North);
315  return true;
316  }
317 
320  guiEvent))
321  {
323  return true;
324  }
325 
328  guiEvent))
329  {
331  return true;
332  }
333 
336  guiEvent))
337  {
339  return true;
340  }
341 
344  guiEvent))
345  {
346  moveController_.DisableDirection (yap::Direction::North);
347  return true;
348  }
349 
352  guiEvent))
353  {
355  return true;
356  }
357 
360  guiEvent))
361  {
363  return true;
364  }
365 
368  guiEvent))
369  {
371  return true;
372  }
373 
376  guiEvent))
377  {
379  return true;
380  }
381 
382  return false;
383  }
384 
386  {
387  context_.SetTargetClearColor (sf::Color::Black);
388 
389  yap::AudioManager::Instance ().PlayMusic ("BGM/City2.ogg", false);
390  }
391 
393  {
395  }
396 
398  {
399  return world_.GetCurrentMap ();
400  }
401 
403  {
405 
408  yap::Vector2 (),
409  map.GetSize ()));
410  }
411 
413  {
414  player_ = player;
415  SetPlayerName ();
416 
418 
419  cameraController_.SetTarget (*player);
421  }
422 
425  {
426  // player_->SetName (session_.GetUser ().GetLogin ());
427 
428  mainMenu_ = new GameMainMenu ();
429  mainMenu_->Init (player_->GetName ());
431  yap::Vector2 menuPosition (
432  GameData::Resolution ().x - mainMenu_->GetSize ().x, 0);
433  mainMenu_->SetPosition (menuPosition);
434 
435  mainMenu_->OnPokedexItemActivated += [this] (
436  GameMainMenu& sender,
437  const yap::EmptyEventArgs& args)
438  {
439  gameGuiManager_->SetCurrentWidget ("Pokedex");
440  };
441 
442  mainMenu_->OnPokemonItemActivated += [this] (
443  GameMainMenu& sender,
444  const yap::EmptyEventArgs& args)
445  {
446  gameGuiManager_->SetCurrentWidget ("PokemonTeam");
447  };
448 
450  GameMainMenu& sender,
451  const yap::EmptyEventArgs& args)
452  {
453  gameGuiManager_->SetCurrentWidget ("TrainerCard");
454  };
455 
457 
458  mainMenu_->Close ();
459  }
460 
462  {
463  moveController_.DisableDirection (yap::Direction::North);
467  }
468 
470  {
471  if (player_ == nullptr)
472  return;
473 
474  const yap::Vector2& force = moveController_.GetForce ();
475 
476  if (lastForce_ == force)
477  return;
478 
479  SendApplyForce (force);
480 
481 #if YAPOG_MOVE_PREDICTION
482  // in case of client side move prediction player is moved
483  // before server aknowledgement is received
484  player_->ApplyForce (force);
485 #endif // YAPOG_MOVE_PREDICTION
486 
487  lastForce_ = force;
488  }
489 
491  {
493  }
494 
496  {
497  yap::Packet packet;
499 
500  packet.Write (force);
501 
502  session_.SendPacket (packet);
503  }
504 
506  yap::GameInputType gameInputType,
507  bool state)
508  {
509  yap::Packet packet;
511 
512  packet.Write (static_cast<yap::Int16> (gameInputType));
513  packet.Write (state);
514 
515  session_.SendPacket (packet);
516  }
517 
519  {
521  }
522 
524  {
526 
528  }
529 } // namespace ycl