Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #pragma once
  2. #include <vector>
  3. #include <memory>
  4. #include "basicobject.h"
  5.  
  6.  
  7.  
  8. class objcontainer
  9. {
  10. using objpair = std::pair<std::unique_ptr<basicobject>, bool >;
  11. std::vector < objpair> m_objects;
  12. public:
  13. objcontainer();
  14. ~objcontainer();
  15. void Draw(drawinfo* info);
  16. void Run(float deltatime, tickinfo* info);
  17. void Remove(class basicobject* obj);
  18.  
  19. void ShowInfo(std::string info);
  20.  
  21. void setActive();
  22. void Possess(class playable* victim);
  23.  
  24. class playable* m_possessed = nullptr;
  25.  
  26. class basicobject* MakeObject(class basicobject* obj, bool ShouldDraw = true);
  27.  
  28. /*
  29. template<class obj, typename... us> obj* MakeObject(us... args)
  30. {
  31. m_objects.push_back(new obj(args...));
  32. return dynamic_cast<obj*>(m_objects.back());
  33. }*/
  34. };
  35.  
  36. namespace usefullstuff
  37. {
  38. extern objcontainer* container;
  39. extern objmanager* manager;
  40.  
  41. class basicobject* MakeObject(class basicobject* obj, bool ShouldDraw = true);
  42. void Remove(class basicobject* obj);
  43. }
  44.  
  45. #include "objcontainer.h"
  46. #include "entity.h"
  47. #include "playable.h"
  48. #include "objmanager.h"
  49. #include "chunk.h"
  50.  
  51. #include <iostream>
  52. #include "basicobject.h"
  53.  
  54. namespace usefullstuff
  55. {
  56. objcontainer* container =nullptr;
  57. objmanager* manager = nullptr;
  58. basicobject* MakeObject(basicobject* obj, bool ShouldDraw)
  59. {
  60. return container->MakeObject(obj, ShouldDraw);
  61. }
  62. void Remove(basicobject* obj)
  63. {
  64. container->Remove(obj);
  65. }
  66. }
  67. objcontainer::objcontainer()
  68. {
  69. }
  70.  
  71. objcontainer::~objcontainer()
  72. {
  73.  
  74. }
  75.  
  76. void objcontainer::Draw(drawinfo* info)
  77. {
  78. for (int i = 0; i < m_objects.size(); i++)
  79. m_objects[i].first->Draw(info);
  80. }
  81.  
  82. void objcontainer::Run(float deltatime, tickinfo* info)
  83. {
  84. for (int i = 0; i < m_objects.size(); i++)
  85. m_objects[i].first->Tick(deltatime, info);
  86. }
  87.  
  88.  
  89. /*
  90. Deletes pointer, and deletes it from its container if it has any
  91. */
  92. void objcontainer::Remove(basicobject* obj)
  93. {
  94.  
  95. /*
  96. int debug = 0;
  97. for (int i = 0; i < m_objects.size(); i++)
  98. if (dynamic_cast<chunk*>(m_objects[i].first))
  99. debug++;
  100.  
  101. std::cout << "DELETING: \n";*/
  102.  
  103. /*
  104. for (int i = 0; i < m_objects.size(); i++)
  105. if (obj == m_objects[i].first.get())
  106. {
  107. m_objects.erase(m_objects.begin() + i);
  108. return;
  109. }*/
  110. std::cout << m_objects.size();
  111. auto object =
  112. find_if(m_objects.begin(), m_objects.end(), [&](objpair& ob) { return ob.first.get() == obj; });
  113.  
  114. m_objects.erase(std::remove(m_objects.begin(), m_objects.end(), *object));
  115.  
  116. std::cout << ' ' << m_objects.size() << std::endl;
  117.  
  118. //delete obj;
  119. }
  120.  
  121. void objcontainer::ShowInfo(std::string info)
  122. {
  123. std::cout << info << m_objects.size() << std::endl;
  124. }
  125.  
  126. void objcontainer::setActive()
  127. {
  128. usefullstuff::container = this;
  129. }
  130.  
  131. void objcontainer::Possess(playable* victim)
  132. {
  133. m_possessed = victim;
  134. }
  135.  
  136. basicobject* objcontainer::MakeObject(basicobject* obj, bool ShouldDraw)
  137. {
  138. m_objects.push_back(objpair(std::unique_ptr<basicobject>(obj), ShouldDraw));
  139. return obj;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement