monkeycar_area.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "monkeycar_area.h"
  2. #include "monkeycar_bus.h"
  3. #include "monkeycar_person.h"
  4. #include "card.h"
  5. #include "log.h"
  6. #include "common.h"
  7. uint64_t monkey_area::m_startTime=time(NULL)*1000;
  8. monkey_area::monkey_area(std::shared_ptr<db_area> dap,int area_id,int over_count_person,int over_time_person,double scale,int32_t mapid,int32_t type)
  9. :area(area_id,over_count_person,over_time_person,scale,mapid,type)
  10. ,m_path(dap->m_point.begin(),dap->m_point.end())
  11. {
  12. log_info("monkeycar _area init ");
  13. std::shared_ptr<monkey_bus> bus = std::make_shared<monkey_bus>(dap->m_default_speed,NEGTIVE_DIRECTION);
  14. bus_.push_back(bus);
  15. bus = std::make_shared<monkey_bus>(dap->m_default_speed * -1.0,POSTIVE_DIRECTION);
  16. bus_.push_back(bus);
  17. }
  18. std::shared_ptr<monkey_person> monkey_area::find(uint32_t cardid)
  19. {
  20. std::lock_guard<std::mutex> lk(m_mtx);
  21. auto it = card_map_.find(cardid);
  22. if(it == card_map_.end())
  23. return nullptr;
  24. return it->second;
  25. }
  26. void monkey_area::on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  27. {
  28. area::on_hover(a,c);
  29. if(c->m_type != CT_PERSON)return;
  30. st_coord pt;
  31. pt.x_=c->x;
  32. pt.y_=c->y;
  33. pt.m_ct = c->m_ct;
  34. pt.gen_time_ = c->m_time-m_startTime;
  35. on_card_move(c->m_id,pt,c->m_type);
  36. }
  37. void monkey_area::on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  38. {
  39. area::on_enter(a,c);
  40. if(c->m_type != CT_PERSON)return;
  41. on_card_enter(c);
  42. }
  43. void monkey_area::on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  44. {
  45. area::on_leave(a,c);
  46. if(c->m_type != CT_PERSON)return;
  47. on_card_leave(c->m_id);
  48. }
  49. void monkey_area::on_card_leave(uint32_t cardid)
  50. {
  51. log_info("[monkeycar on_card_leave]cardId:%d",cardid);
  52. std::lock_guard<std::mutex> lk(m_mtx);
  53. auto it = card_map_.find(cardid);
  54. if (it != card_map_.end())
  55. {
  56. if(auto tbus = it->second->getBus())
  57. tbus->getOffTheBus(it->second);
  58. card_map_.erase(it);
  59. }
  60. }
  61. void monkey_area::on_card_enter(const std::shared_ptr<card_location_base> &card)
  62. {
  63. log_info("[monkeycar on_card_enter]cardId:%d",card->m_id);
  64. auto me = shared_from_this();
  65. std::shared_ptr<monkey_person> mp = std::make_shared<monkey_person>(me);
  66. card->reset(mp);
  67. std::lock_guard<std::mutex> lk(m_mtx);
  68. card_map_.insert(std::make_pair(card->m_id,mp));
  69. }
  70. void monkey_area::on_card_move(uint32_t cardid,const st_coord & st,uint64_t type)
  71. {
  72. log_info("[monkeycar on_card_move]cardId:%d",cardid);
  73. //auto me = shared_from_this();
  74. auto mp=find(cardid);
  75. if (!mp)
  76. {
  77. //if not exist ,create it .because may be restart the system.then area has not changed.
  78. uint64_t id=type<<32|cardid;
  79. if(auto c=card_list::instance()->get(id))
  80. on_card_enter(c);
  81. return ;
  82. }
  83. bool ret = mp->on_step_map(st);
  84. if (!ret)
  85. return;
  86. double speed=0;
  87. bool b=mp->fit_speed(&speed);
  88. if (!b || speed == DBL_MAX)
  89. return ;
  90. //on bus
  91. speed *= 1000;
  92. log_info("monkeycar speed :cardid:%d,speed:%.2f",cardid,speed);
  93. if(auto tbus = mp->getBus())
  94. {
  95. if(!tbus->test_get_off(mp,speed))
  96. {
  97. log_info("monkeycar get off id:%d",cardid);
  98. tbus->getOffTheBus(mp);
  99. }
  100. }
  101. else
  102. {
  103. std::shared_ptr<monkey_bus> bus=bus_[speed>0?0:1];
  104. if(bus->test_get_on(speed))
  105. {
  106. log_info("monkeycar get onid:%d",cardid);
  107. bus->getOnTheBus(mp,bus);
  108. }
  109. }
  110. }