Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

125 rindas
3.0 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #pragma once
  13. //
  14. // The entity class
  15. // ----------------
  16. // Entities are objects that can be ticked by the game loop and/or the render
  17. // loop. Entities are implemented as one or several linked lists. See the
  18. // Ticker class for the ticking logic and the linked list implementation.
  19. //
  20. #include <cstdint>
  21. #include <lol/engine/tickable.h>
  22. namespace lol
  23. {
  24. class entity
  25. {
  26. friend class Scene;
  27. friend class ticker;
  28. friend class ticker_data;
  29. public:
  30. virtual std::string GetName() const;
  31. enum class flags : uint16_t
  32. {
  33. none = 0,
  34. init_game = 1 << 0,
  35. init_draw = 1 << 1,
  36. release_game = 1 << 2,
  37. release_draw = 1 << 4,
  38. destroying = 1 << 5,
  39. autorelease = 1 << 6,
  40. };
  41. inline void add_flags(flags f);
  42. inline void remove_flags(flags f);
  43. inline bool has_flags(flags f);
  44. protected:
  45. entity();
  46. virtual ~entity();
  47. virtual bool init_game() { return true; }
  48. virtual bool init_draw() { return true; }
  49. virtual bool release_game() { return true; }
  50. virtual bool release_draw() { return true; }
  51. virtual void tick_game(float seconds);
  52. virtual void tick_draw(float seconds, class Scene &scene);
  53. #if !LOL_BUILD_RELEASE
  54. tickable::state m_tickstate;
  55. #endif
  56. tickable::group::game m_gamegroup;
  57. tickable::group::draw m_drawgroup;
  58. private:
  59. flags m_flags = flags::none;
  60. int m_ref = 0;
  61. uint64_t m_scene_mask = 0;
  62. };
  63. static inline entity::flags operator |(entity::flags a, entity::flags b)
  64. {
  65. return (entity::flags)((uint16_t)a | (uint16_t)b);
  66. }
  67. static inline entity::flags operator &(entity::flags a, entity::flags b)
  68. {
  69. return (entity::flags)((uint16_t)a & (uint16_t)b);
  70. }
  71. static inline entity::flags operator ^(entity::flags a, entity::flags b)
  72. {
  73. return (entity::flags)((uint16_t)a ^ (uint16_t)b);
  74. }
  75. inline void entity::add_flags(entity::flags f) { m_flags = m_flags | f; }
  76. inline void entity::remove_flags(entity::flags f) { m_flags = (m_flags | f) ^ f; }
  77. inline bool entity::has_flags(entity::flags f) { return (m_flags & f) != flags::none; }
  78. template<typename T> struct entity_dict
  79. {
  80. T *get(std::string const &key)
  81. {
  82. auto it = m_cache1.find(key);
  83. return it != m_cache1.end() ? it->second : nullptr;
  84. }
  85. T *set(std::string const &key, T *entity)
  86. {
  87. m_cache1[key] = entity;
  88. m_cache2[entity] = key;
  89. return entity;
  90. }
  91. void erase(T *entity)
  92. {
  93. // FIXME: temporary; we need Ticker::Ref etc.
  94. m_cache1.erase(m_cache2[entity]);
  95. m_cache2.erase(entity);
  96. }
  97. std::map<std::string, T*> m_cache1;
  98. std::map<T*, std::string> m_cache2;
  99. };
  100. } /* namespace lol */