World.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "Types.h"
  25. #include "HeapAllocator.h"
  26. #include "IdArray.h"
  27. #include "LinearAllocator.h"
  28. #include "Unit.h"
  29. #include "Camera.h"
  30. #include "Vector.h"
  31. #include "RenderWorld.h"
  32. #include "SoundRenderer.h"
  33. #include "PoolAllocator.h"
  34. namespace crown
  35. {
  36. #define MAX_UNITS 65000
  37. #define MAX_SOUNDS 64
  38. #define MAX_CAMERAS 16
  39. typedef Id UnitId;
  40. typedef Id CameraId;
  41. typedef Id MeshId;
  42. typedef Id SoundInstanceId;
  43. typedef Id SpriteId;
  44. struct SoundInstance
  45. {
  46. SoundId sound;
  47. Matrix4x4 world;
  48. float volume;
  49. float range;
  50. bool loop : 1;
  51. bool playing : 1;
  52. };
  53. struct UnitToCamera
  54. {
  55. UnitId unit;
  56. int32_t node;
  57. CameraId camera;
  58. };
  59. struct UnitToSoundInstance
  60. {
  61. UnitId unit;
  62. SoundInstanceId sound;
  63. int32_t node;
  64. };
  65. struct UnitToSprite
  66. {
  67. UnitId unit;
  68. SpriteId sprite;
  69. int32_t node;
  70. };
  71. class Mesh;
  72. class Sprite;
  73. class Vector3;
  74. class Quaternion;
  75. class World
  76. {
  77. public:
  78. World();
  79. UnitId spawn_unit(const char* name, const Vector3& pos = Vector3::ZERO, const Quaternion& rot = Quaternion(Vector3(0, 1, 0), 0.0f));
  80. void destroy_unit(UnitId unit);
  81. void destroy_unit(Unit* unit);
  82. void link_unit(UnitId child, UnitId parent, int32_t node);
  83. void unlink_unit(UnitId unit);
  84. void link_camera(CameraId camera, UnitId unit, int32_t node);
  85. void unlink_camera(CameraId camera);
  86. void link_sprite(SpriteId sprite, UnitId unit, int32_t node);
  87. void unlink_sprite(SpriteId sprite);
  88. Unit* lookup_unit(UnitId unit);
  89. Camera* lookup_camera(CameraId camera);
  90. Mesh* lookup_mesh(MeshId mesh);
  91. Sprite* lookup_sprite(SpriteId sprite);
  92. RenderWorld& render_world();
  93. void update(Camera& camera, float dt);
  94. CameraId create_camera(int32_t node, const Vector3& pos = Vector3::ZERO, const Quaternion& rot = Quaternion::IDENTITY);
  95. void destroy_camera(CameraId camera);
  96. MeshId create_mesh(ResourceId id, int32_t node, const Vector3& pos = Vector3::ZERO, const Quaternion& rot = Quaternion::IDENTITY);
  97. void destroy_mesh(MeshId id);
  98. SpriteId create_sprite(ResourceId id, int32_t node = -1, const Vector3& pos = Vector3::ZERO, const Quaternion& rot = Quaternion::IDENTITY);
  99. void destroy_sprite(SpriteId id);
  100. SoundInstanceId play_sound(const char* name, const bool loop = false, const float volume = 1.0f, const Vector3& pos = Vector3::ZERO, const float range = 50.0f);
  101. void pause_sound(SoundInstanceId sound);
  102. void link_sound(SoundInstanceId sound, Unit* unit, int32_t node);
  103. void set_listener(const Vector3& pos, const Vector3& vel, const Vector3& or_up, const Vector3& or_at);
  104. void set_sound_position(SoundInstanceId sound, const Vector3& pos);
  105. void set_sound_range(SoundInstanceId sound, const float range);
  106. void set_sound_volume(SoundInstanceId sound, const float vol);
  107. private:
  108. PoolAllocator m_unit_pool;
  109. PoolAllocator m_camera_pool;
  110. IdArray<MAX_UNITS, Unit*> m_units;
  111. IdArray<MAX_CAMERAS, Camera*> m_camera;
  112. IdArray<MAX_SOUNDS, SoundInstance> m_sounds;
  113. // Connections
  114. List<UnitToCamera> m_unit_to_camera;
  115. List<UnitToSoundInstance> m_unit_to_sound_instance;
  116. List<UnitToSprite> m_unit_to_sprite;
  117. RenderWorld m_render_world;
  118. };
  119. } // namespace crown