world.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "camera.h"
  25. #include "id_array.h"
  26. #include "linear_allocator.h"
  27. #include "physics_types.h"
  28. #include "physics_world.h"
  29. #include "pool_allocator.h"
  30. #include "render_world.h"
  31. #include "render_world_types.h"
  32. #include "scene_graph_manager.h"
  33. #include "types.h"
  34. #include "unit.h"
  35. #include "vector.h"
  36. #include "world_types.h"
  37. #include "sound_world.h"
  38. #include "event_stream.h"
  39. #include "sprite_animation_player.h"
  40. namespace crown
  41. {
  42. /// @defgroup World World
  43. /// Represents a game world.
  44. ///
  45. /// @ingroup World
  46. class World
  47. {
  48. public:
  49. World();
  50. ~World();
  51. WorldId id() const;
  52. void set_id(WorldId id);
  53. /// Spawns a new instance of the unit @a name at the given @a position and @a rotation.
  54. UnitId spawn_unit(const char* name, const Vector3& position = vector3::ZERO, const Quaternion& rotation = quaternion::IDENTITY);
  55. UnitId spawn_unit(ResourceId id, const Vector3& pos, const Quaternion& rot);
  56. UnitId spawn_unit(const ResourceId id, UnitResource* ur, const Vector3& pos, const Quaternion& rot);
  57. /// Destroys the unit with the given @a id.
  58. void destroy_unit(UnitId id);
  59. /// Reloads all the units with the associated resource @a old_ur.
  60. void reload_units(UnitResource* old_ur, UnitResource* new_ur);
  61. /// Returns the number of units in the world.
  62. uint32_t num_units() const;
  63. /// Returns all the the units in the world.
  64. void units(Array<UnitId>& units) const;
  65. /// Links the unit @a child to the @a node of the unit @a parent.
  66. /// After this call, @a child will follow the @a parent unit.
  67. void link_unit(UnitId child, UnitId parent, int32_t node);
  68. /// Unlinks the unit @a id from its parent if it has any.
  69. void unlink_unit(UnitId id);
  70. /// Returns the unit @a id.
  71. Unit* get_unit(UnitId id);
  72. /// Returns the camera @a id.
  73. Camera* get_camera(CameraId id);
  74. /// Updates all units and sub-systems with the given @a dt delta time.
  75. void update(float dt);
  76. /// Renders the world form the point of view of the given @a camera.
  77. void render(Camera* camera);
  78. CameraId create_camera(SceneGraph& sg, int32_t node, ProjectionType::Enum type, float near, float far);
  79. /// Destroys the camera @a id.
  80. void destroy_camera(CameraId id);
  81. /// Plays the sound with the given @æ name at the given @a position, with the given
  82. /// @a volume and @a range. @a loop controls whether the sound must loop or not.
  83. SoundInstanceId play_sound(const char* name, bool loop = false, float volume = 1.0f, const Vector3& position = vector3::ZERO, float range = 50.0f);
  84. SoundInstanceId play_sound(ResourceId id, const bool loop, const float volume, const Vector3& pos, const float range);
  85. SoundInstanceId play_sound(SoundResource* sr, const bool loop, const float volume, const Vector3& pos, const float range);
  86. /// Stops the sound with the given @a id.
  87. void stop_sound(SoundInstanceId id);
  88. /// Links the sound @a if to the @a node of the given @æ unit.
  89. /// After this call, the sound @a id will follow the unit @æ unit.
  90. void link_sound(SoundInstanceId id, Unit* unit, int32_t node);
  91. /// Sets the @a pose of the listener.
  92. void set_listener_pose(const Matrix4x4& pose);
  93. /// Sets the @a position of the sound @a id.
  94. void set_sound_position(SoundInstanceId id, const Vector3& position);
  95. /// Sets the @a range of the sound @a id.
  96. void set_sound_range(SoundInstanceId id, float range);
  97. /// Sets the @a volume of the sound @a id.
  98. void set_sound_volume(SoundInstanceId id, float volume);
  99. /// Creates a new window-space Gui of size @width and @a height.
  100. GuiId create_window_gui(uint16_t width, uint16_t height, const char* material);
  101. /// Destroys the gui with the given @a id.
  102. void destroy_gui(GuiId id);
  103. /// Returns the gui @a id.
  104. Gui* get_gui(GuiId id);
  105. DebugLine* create_debug_line(bool depth_test);
  106. void destroy_debug_line(DebugLine* line);
  107. /// Loads the level @a name into the world.
  108. void load_level(const char* name);
  109. SceneGraphManager* scene_graph_manager();
  110. SpriteAnimationPlayer* sprite_animation_player();
  111. /// Returns the rendering sub-world.
  112. RenderWorld* render_world();
  113. /// Returns the physics sub-world.
  114. PhysicsWorld* physics_world();
  115. /// Returns the sound sub-world.
  116. SoundWorld* sound_world();
  117. private:
  118. void process_physics_events();
  119. private:
  120. PoolAllocator m_unit_pool;
  121. PoolAllocator m_camera_pool;
  122. IdArray<CE_MAX_UNITS, Unit*> m_units;
  123. IdArray<CE_MAX_CAMERAS, Camera*> m_cameras;
  124. SceneGraphManager m_scenegraph_manager;
  125. SpriteAnimationPlayer m_sprite_animation_player;
  126. RenderWorld m_render_world;
  127. PhysicsWorld m_physics_world;
  128. SoundWorld* m_sound_world;
  129. WorldId m_id;
  130. EventStream m_events;
  131. };
  132. } // namespace crown