game.cpp 2.29 KB
Newer Older
louiz’'s avatar
louiz’ committed
1 2
#include <game/game.hpp>
#include <orders.pb.h>
louiz’'s avatar
louiz’ committed
3
#include <network/message.hpp>
louiz’'s avatar
louiz’ committed
4 5 6 7 8 9 10 11 12 13 14 15 16

Game::Game():
  world(),
  occupants_handler(),
  turn_handler()
{
}

Game::~Game()
{
}


louiz’'s avatar
louiz’ committed
17
void Game::new_entity_callback(Message* message)
louiz’'s avatar
louiz’ committed
18
{
louiz’'s avatar
louiz’ committed
19 20
  log_debug("Game::new_entity_callback");
  auto srl = message->parse_body_to_protobuf_object<ser::order::NewEntity>();
louiz’'s avatar
louiz’ committed
21 22 23 24

  Position pos;
  pos.x.raw() = srl.pos().x();
  pos.y.raw() = srl.pos().y();
25
  this->turn_handler.insert_action(std::bind(&World::do_new_entity, &this->world, srl.type_id(), pos, srl.team()),
louiz’'s avatar
louiz’ committed
26 27 28 29 30 31 32 33 34 35 36 37
                                   srl.turn());
}

void Game::move_callback(Message* message)
{
  log_debug("Game::move_callback");
  auto srl = message->parse_body_to_protobuf_object<ser::order::Move>();

  std::vector<EntityId> ids;
  for (const auto& id: srl.entity_id())
    ids.push_back(id);

38 39 40 41 42
  if (srl.has_pos())
    {
      Position pos;
      pos.x.raw() = srl.mutable_pos()->x();
      pos.y.raw() = srl.mutable_pos()->y();
louiz’'s avatar
louiz’ committed
43

44 45 46 47 48 49 50 51 52 53
      this->turn_handler.insert_action(
          std::bind(&World::do_move, &this->world, ids, pos, srl.queue()),
          srl.turn());
    }
  else
    {
      this->turn_handler.insert_action(
          std::bind(&World::do_follow, &this->world, ids, srl.target(), srl.queue()),
          srl.turn());
    }
louiz’'s avatar
louiz’ committed
54 55
}

louiz’'s avatar
louiz’ committed
56 57 58 59 60 61 62 63 64
void Game::cast_callback(Message* message)
{
  log_debug("Game::cast_callback");
  auto srl = message->parse_body_to_protobuf_object<ser::order::Cast>();

  std::vector<EntityId> ids;
  for (const auto& id: srl.entity_id())
    ids.push_back(id);

65
  AbilityType type = static_cast<AbilityType>(srl.type());
louiz’'s avatar
louiz’ committed
66

67 68 69 70 71 72 73 74 75 76 77
  if (srl.has_target())
    {
      this->turn_handler.insert_action(
          std::bind(&World::do_cast_on_target, &this->world, ids, srl.target(), type, srl.queue()),
          srl.turn());
    }
  else if (srl.has_pos())
    {
      Position pos;
      pos.x.raw() = srl.pos().x();
      pos.y.raw() = srl.pos().y();
louiz’'s avatar
louiz’ committed
78

79 80 81 82
      this->turn_handler.insert_action(
          std::bind(&World::do_cast_on_pos, &this->world, ids, pos, type, srl.queue()),
          srl.turn());
    }
83 84 85 86 87 88
  else
    {
      this->turn_handler.insert_action(
             std::bind(&World::do_cast, &this->world, ids, type, srl.queue()),
             srl.turn());
    }
louiz’'s avatar
louiz’ committed
89
}
90 91 92 93 94

utils::FloatingSeconds Game::current_world_time() const
{
  return this->turn_handler.get_current_time();
}