Guest User

CLevel.h

a guest
Feb 5th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.10 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "SDL_util.h"
  4. #include "tinyxml2.h"
  5.  
  6. #include "Entity.h"
  7. #include "Vector2.h"
  8. #include "Trigger.h"
  9. #include "Renderer.h"
  10. #include "QuadTree.h"
  11. #include "EntityTile.h"
  12. #include "LevelGraph.h"
  13. #include "BoundingBox.h"
  14. #include "SpriteSheet.h"
  15. #include "LevelLoader.h"
  16. #include "ContentLoader.h"
  17. #include "LevelLayerTMX.h"
  18. #include "LevelLoaderTMX.h"
  19. #include "LevelTilesetTMX.h"
  20. #include "LevelObjectGroupTMX.h"
  21.  
  22. #include <string>
  23. #include <vector>
  24.  
  25. // Forward-declares the entity class as there is a circular dependency between the entity
  26. // and the level.
  27. class CEntity;
  28.  
  29. /*
  30. A functor to sort the entity sets depending on their Z-index.
  31. */
  32. struct SEntitySort
  33. {
  34.     bool operator() (CEntity* item1, CEntity* item2)
  35.     {
  36.         return item1->GetZ() < item2->GetZ();
  37.     }
  38. };
  39.  
  40. /*
  41. Stores the entities in the level.
  42. */
  43. struct SLevelObjectStorage
  44. {
  45.     // Stores all the entities in the level.
  46.     std::vector<CEntity*> m_Entities;
  47.     // Stores the entities that won't get updated.
  48.     std::vector<CEntity*> m_StaticEntities;
  49.     // Stores the entities that will be updated.
  50.     std::vector<CEntity*> m_DynamicEntities;
  51.  
  52.     // Stores the entities that have been removed, but now deleted.
  53.     std::vector<CEntity*> m_RemovedEntities;
  54.  
  55.     // Stores the triggers in the level.
  56.     std::vector<CTrigger*> m_Triggers;
  57.     // Stores the collision-boxes in the level.
  58.     std::vector<CBoundingBox*> m_BoundingBoxes;
  59. };
  60.  
  61. /*
  62. Stores general information and statistics about the level.
  63. */
  64. struct SLevelStats
  65. {
  66.     // The file where the level was loaded from.
  67.     std::string m_LevelFileName;
  68.  
  69.     // The size of the level in pixels.
  70.     TVector2 m_LevelSize;
  71.     // The offset of the level.
  72.     TVector2 m_LevelOffset;
  73.     // Indicates if the offset should be clamped to the level size or not.
  74.     bool m_LevelOffsetClampEnabled;
  75.  
  76.     // Stores the current Z index for entities to be created.
  77.     int m_LevelLayerZ = 0;
  78.     // Stores the size of a tile in the level.
  79.     int m_LevelTileSize = 16;
  80.  
  81.     // Indicates if the level has been loaded or not.
  82.     bool m_LevelLoaded = false;
  83.  
  84.     // The color used for rendering the level grid.
  85.     TColor m_LevelGridColor;
  86.  
  87.     // Indicates if the level should render various debug info.
  88.     bool m_DebugState = false;
  89. };
  90.  
  91. // Forward-declaration for the parallax layer as the container needs them and the layer needs the container.
  92. struct SLevelParallax;
  93.  
  94. /*
  95. Struct for keeping the parallax data.
  96. */
  97. struct SLevelParallaxStats
  98. {
  99.     // The background color of the level, the layers are colored with a factor
  100.     // of this color.
  101.     TColor m_ParallaxSkyColor;
  102.     // Stores the layers in the level.
  103.     std::vector<SLevelParallax*> m_ParallaxLayers;
  104. };
  105.  
  106. /*
  107. Struct for parallax scrolling layer.
  108. */
  109. struct SLevelParallax
  110. {
  111.     // The Z-index of the layer.
  112.     int m_LayerZ = 0;
  113.     // The factor applied to the scrolling.
  114.     float m_ParallaxFactor = 1.0f;
  115.     // The intensity of the parent's color.
  116.     float m_ColorIntensity = 1.0f;
  117.     // The render description of the layer.
  118.     CRenderDescription* m_ParallaxRenderDescription;
  119.  
  120.     SLevelParallax(SLevelParallaxStats* parent, std::string parallaxTextureContentName, SRect* sourceRect, float colorIntensity, float parallaxFactor, float x = 0.0f, float y = 0.0f)
  121.         : m_ParallaxFactor(parallaxFactor), m_ColorIntensity(colorIntensity)
  122.     {
  123.         // Creates a render description for the texture of the parallax layer.
  124.         m_ParallaxRenderDescription = new CRenderDescription(CContentLoader::LoadTexture(parallaxTextureContentName), sourceRect, TVector2());
  125.         // Sets the shade of the layer.
  126.         m_ParallaxRenderDescription->SetShadeColor(parent->m_ParallaxSkyColor * colorIntensity);
  127.         // Sets the position of the layer.
  128.         m_ParallaxRenderDescription->SetPosition(TVector2(x, y));
  129.     }
  130.  
  131.     SLevelParallax(SLevelParallax& other)
  132.     {
  133.         m_ParallaxFactor = other.m_ParallaxFactor;
  134.         m_ParallaxRenderDescription = new CRenderDescription(*other.m_ParallaxRenderDescription);
  135.     }
  136.  
  137.     ~SLevelParallax()
  138.     {
  139.         delete m_ParallaxRenderDescription;
  140.     }
  141.  
  142.     /*
  143.     Recalculates the shade of the parallax layer.
  144.     */
  145.     void ReShade(SLevelParallaxStats* parent)
  146.     {
  147.         m_ParallaxRenderDescription->SetShadeColor(parent->m_ParallaxSkyColor * m_ColorIntensity);
  148.     }
  149. };
  150.  
  151. /*
  152. A functor-struct to sort parallax layer depending on their given Z-index.
  153. */
  154. struct SLevelParallaxSort
  155. {
  156.     bool operator() (SLevelParallax* item1, SLevelParallax* item2)
  157.     {
  158.         return item1->m_LayerZ < item2->m_LayerZ;
  159.     }
  160. };
  161.  
  162. /*
  163. Stores the different level formats that the level can be loaded from.
  164. */
  165. enum class ELevelFileFormat
  166. {
  167.     TMX
  168. };
  169.  
  170. // Maximum amount of layers in a level, used to generate a safe overlay amount
  171. // for entities.
  172. #define MAX_LAYER_COUNT 1000
  173.  
  174. // Values for entity storage flags.
  175. #define STATIC 1
  176. #define DYNAMIC 2
  177. #define NOCOLLISION 4
  178. #define COLLISION 8
  179.  
  180. // Bitwise-ORs some of the entity flags together to allow for easier initialization.
  181. #define STATIC_NOCOLLISION NOCOLLISION | STATIC
  182. #define STATIC_COLLISION COLLISION | STATIC
  183. #define DYNAMIC_NOCOLLISION NOCOLLISION | DYNAMIC
  184. #define DYNAMIC_COLLISION COLLISION | DYNAMIC
  185.  
  186. // The type for the entity flags defined above.
  187. typedef const int DEntityStorageType;
  188.  
  189. /*
  190. Creates a new level. The level handles the tiles, and the entities in the level.
  191. Level also loads the level from a TMX file, using tinyxml2 for the access to
  192. XML.
  193. */
  194. class CLevel
  195. {
  196.     friend class CLevelEditor;
  197. public:
  198.     CLevel();
  199.     CLevel(const std::string& levelFileName, CLevelLoader* levelLoader = NULL);
  200.     ~CLevel();
  201.  
  202.     // Handles passing events to the entities in the level.
  203.     void EventLevel(SDL_Event* event);
  204.     // Updates the entities in the level.
  205.     void UpdateLevel(float deltaTime);
  206.     // Renders the entities in the level.
  207.     void RenderLevel(CRenderer* renderer);
  208.     // Renders the level grid (used for debugging).
  209.     void RenderLevelGrid(CRenderer* renderer);
  210.  
  211.     // Removes an entity from the level.
  212.     void RemoveEntity(CEntity* entity);
  213.     // Adds an entity to the level.
  214.     void AddEntity(CEntity* entity, DEntityStorageType storageType, int zModifier = 0);
  215.     // Adds a parallax layer to the level.
  216.     void AddParallaxLayer(SLevelParallax* parallaxLayer, int z);
  217.  
  218.     // Returns the size of the level in pixel size.
  219.     TVector2 GetLevelSize();
  220.     // Returns the offset of the level that is clamped to a limit if wanted.
  221.     TVector2 GetLevelOffset();
  222.     // Sets the size of the level.
  223.     void SetLevelSize(TVector2 size);
  224.     // Sets the offset of the level.
  225.     void SetLevelOffset(TVector2 offset);
  226.  
  227.     // Returns the object storage struct that holds the entities and collision
  228.     // boxes in the level.
  229.     SLevelObjectStorage* GetLevelObjectStorage();
  230.  
  231.     // Sets the debug state of the level. true means that some debug info is rendered.
  232.     void SetDebugState(bool value);
  233.     // Sets if the offset of the level will be clamped.
  234.     void SetOffsetClamp(bool value);
  235.  
  236.     // Clears all the entities that have been removed during the running frame.
  237.     // This is public because levels can also be just rendered, so in case any entities
  238.     // are removed the UpdateLevel()-method won't call this.
  239.     void ClearRemovedEntities();
  240. protected:
  241. private:
  242.     // Initializes the level values, this allows multiple different constructors
  243.     // and minimal code-duplication.
  244.     void Initialize();
  245.  
  246.     // Loads the level from a filename with a specific level format.
  247.     void LoadLevel(const std::string& levelFileName, ELevelFileFormat levelFileFormat);
  248.     // Loads a layer.
  249.     void LoadLayer(CLevelLayerTMX* layer);
  250.     // Loads a tileset.
  251.     void LoadTileset(CLevelTilesetTMX* tileset);
  252.     // Loads an object group.
  253.     void LoadObjectGroup(CLevelObjectGroupTMX* objectGroup);
  254.  
  255.     // Converts an object in an object group into a bounding box.
  256.     CBoundingBox* ObjectToBoundingBox(CLevelObjectTMX* object);
  257.  
  258.     // Sorts the entity containers that hold the entities in the level.
  259.     void SortLevelObjectStorage();
  260.  
  261.     // Stores general information and statistics about the level.
  262.     SLevelStats* m_LevelStats;
  263.     // Stores the parallax layer -related data.
  264.     SLevelParallaxStats* m_LevelParallaxStats;
  265.     // Stores the entities and collision boxes in the level.
  266.     SLevelObjectStorage* m_LevelObjectStorage;
  267.  
  268.     // The class that will handle loading objects into entities.
  269.     CLevelLoader* m_LevelLoader;
  270. };
Advertisement
Add Comment
Please, Sign In to add comment