Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "SDL_util.h"
- #include "tinyxml2.h"
- #include "Entity.h"
- #include "Vector2.h"
- #include "Trigger.h"
- #include "Renderer.h"
- #include "QuadTree.h"
- #include "EntityTile.h"
- #include "LevelGraph.h"
- #include "BoundingBox.h"
- #include "SpriteSheet.h"
- #include "LevelLoader.h"
- #include "ContentLoader.h"
- #include "LevelLayerTMX.h"
- #include "LevelLoaderTMX.h"
- #include "LevelTilesetTMX.h"
- #include "LevelObjectGroupTMX.h"
- #include <string>
- #include <vector>
- // Forward-declares the entity class as there is a circular dependency between the entity
- // and the level.
- class CEntity;
- /*
- A functor to sort the entity sets depending on their Z-index.
- */
- struct SEntitySort
- {
- bool operator() (CEntity* item1, CEntity* item2)
- {
- return item1->GetZ() < item2->GetZ();
- }
- };
- /*
- Stores the entities in the level.
- */
- struct SLevelObjectStorage
- {
- // Stores all the entities in the level.
- std::vector<CEntity*> m_Entities;
- // Stores the entities that won't get updated.
- std::vector<CEntity*> m_StaticEntities;
- // Stores the entities that will be updated.
- std::vector<CEntity*> m_DynamicEntities;
- // Stores the entities that have been removed, but now deleted.
- std::vector<CEntity*> m_RemovedEntities;
- // Stores the triggers in the level.
- std::vector<CTrigger*> m_Triggers;
- // Stores the collision-boxes in the level.
- std::vector<CBoundingBox*> m_BoundingBoxes;
- };
- /*
- Stores general information and statistics about the level.
- */
- struct SLevelStats
- {
- // The file where the level was loaded from.
- std::string m_LevelFileName;
- // The size of the level in pixels.
- TVector2 m_LevelSize;
- // The offset of the level.
- TVector2 m_LevelOffset;
- // Indicates if the offset should be clamped to the level size or not.
- bool m_LevelOffsetClampEnabled;
- // Stores the current Z index for entities to be created.
- int m_LevelLayerZ = 0;
- // Stores the size of a tile in the level.
- int m_LevelTileSize = 16;
- // Indicates if the level has been loaded or not.
- bool m_LevelLoaded = false;
- // The color used for rendering the level grid.
- TColor m_LevelGridColor;
- // Indicates if the level should render various debug info.
- bool m_DebugState = false;
- };
- // Forward-declaration for the parallax layer as the container needs them and the layer needs the container.
- struct SLevelParallax;
- /*
- Struct for keeping the parallax data.
- */
- struct SLevelParallaxStats
- {
- // The background color of the level, the layers are colored with a factor
- // of this color.
- TColor m_ParallaxSkyColor;
- // Stores the layers in the level.
- std::vector<SLevelParallax*> m_ParallaxLayers;
- };
- /*
- Struct for parallax scrolling layer.
- */
- struct SLevelParallax
- {
- // The Z-index of the layer.
- int m_LayerZ = 0;
- // The factor applied to the scrolling.
- float m_ParallaxFactor = 1.0f;
- // The intensity of the parent's color.
- float m_ColorIntensity = 1.0f;
- // The render description of the layer.
- CRenderDescription* m_ParallaxRenderDescription;
- SLevelParallax(SLevelParallaxStats* parent, std::string parallaxTextureContentName, SRect* sourceRect, float colorIntensity, float parallaxFactor, float x = 0.0f, float y = 0.0f)
- : m_ParallaxFactor(parallaxFactor), m_ColorIntensity(colorIntensity)
- {
- // Creates a render description for the texture of the parallax layer.
- m_ParallaxRenderDescription = new CRenderDescription(CContentLoader::LoadTexture(parallaxTextureContentName), sourceRect, TVector2());
- // Sets the shade of the layer.
- m_ParallaxRenderDescription->SetShadeColor(parent->m_ParallaxSkyColor * colorIntensity);
- // Sets the position of the layer.
- m_ParallaxRenderDescription->SetPosition(TVector2(x, y));
- }
- SLevelParallax(SLevelParallax& other)
- {
- m_ParallaxFactor = other.m_ParallaxFactor;
- m_ParallaxRenderDescription = new CRenderDescription(*other.m_ParallaxRenderDescription);
- }
- ~SLevelParallax()
- {
- delete m_ParallaxRenderDescription;
- }
- /*
- Recalculates the shade of the parallax layer.
- */
- void ReShade(SLevelParallaxStats* parent)
- {
- m_ParallaxRenderDescription->SetShadeColor(parent->m_ParallaxSkyColor * m_ColorIntensity);
- }
- };
- /*
- A functor-struct to sort parallax layer depending on their given Z-index.
- */
- struct SLevelParallaxSort
- {
- bool operator() (SLevelParallax* item1, SLevelParallax* item2)
- {
- return item1->m_LayerZ < item2->m_LayerZ;
- }
- };
- /*
- Stores the different level formats that the level can be loaded from.
- */
- enum class ELevelFileFormat
- {
- TMX
- };
- // Maximum amount of layers in a level, used to generate a safe overlay amount
- // for entities.
- #define MAX_LAYER_COUNT 1000
- // Values for entity storage flags.
- #define STATIC 1
- #define DYNAMIC 2
- #define NOCOLLISION 4
- #define COLLISION 8
- // Bitwise-ORs some of the entity flags together to allow for easier initialization.
- #define STATIC_NOCOLLISION NOCOLLISION | STATIC
- #define STATIC_COLLISION COLLISION | STATIC
- #define DYNAMIC_NOCOLLISION NOCOLLISION | DYNAMIC
- #define DYNAMIC_COLLISION COLLISION | DYNAMIC
- // The type for the entity flags defined above.
- typedef const int DEntityStorageType;
- /*
- Creates a new level. The level handles the tiles, and the entities in the level.
- Level also loads the level from a TMX file, using tinyxml2 for the access to
- XML.
- */
- class CLevel
- {
- friend class CLevelEditor;
- public:
- CLevel();
- CLevel(const std::string& levelFileName, CLevelLoader* levelLoader = NULL);
- ~CLevel();
- // Handles passing events to the entities in the level.
- void EventLevel(SDL_Event* event);
- // Updates the entities in the level.
- void UpdateLevel(float deltaTime);
- // Renders the entities in the level.
- void RenderLevel(CRenderer* renderer);
- // Renders the level grid (used for debugging).
- void RenderLevelGrid(CRenderer* renderer);
- // Removes an entity from the level.
- void RemoveEntity(CEntity* entity);
- // Adds an entity to the level.
- void AddEntity(CEntity* entity, DEntityStorageType storageType, int zModifier = 0);
- // Adds a parallax layer to the level.
- void AddParallaxLayer(SLevelParallax* parallaxLayer, int z);
- // Returns the size of the level in pixel size.
- TVector2 GetLevelSize();
- // Returns the offset of the level that is clamped to a limit if wanted.
- TVector2 GetLevelOffset();
- // Sets the size of the level.
- void SetLevelSize(TVector2 size);
- // Sets the offset of the level.
- void SetLevelOffset(TVector2 offset);
- // Returns the object storage struct that holds the entities and collision
- // boxes in the level.
- SLevelObjectStorage* GetLevelObjectStorage();
- // Sets the debug state of the level. true means that some debug info is rendered.
- void SetDebugState(bool value);
- // Sets if the offset of the level will be clamped.
- void SetOffsetClamp(bool value);
- // Clears all the entities that have been removed during the running frame.
- // This is public because levels can also be just rendered, so in case any entities
- // are removed the UpdateLevel()-method won't call this.
- void ClearRemovedEntities();
- protected:
- private:
- // Initializes the level values, this allows multiple different constructors
- // and minimal code-duplication.
- void Initialize();
- // Loads the level from a filename with a specific level format.
- void LoadLevel(const std::string& levelFileName, ELevelFileFormat levelFileFormat);
- // Loads a layer.
- void LoadLayer(CLevelLayerTMX* layer);
- // Loads a tileset.
- void LoadTileset(CLevelTilesetTMX* tileset);
- // Loads an object group.
- void LoadObjectGroup(CLevelObjectGroupTMX* objectGroup);
- // Converts an object in an object group into a bounding box.
- CBoundingBox* ObjectToBoundingBox(CLevelObjectTMX* object);
- // Sorts the entity containers that hold the entities in the level.
- void SortLevelObjectStorage();
- // Stores general information and statistics about the level.
- SLevelStats* m_LevelStats;
- // Stores the parallax layer -related data.
- SLevelParallaxStats* m_LevelParallaxStats;
- // Stores the entities and collision boxes in the level.
- SLevelObjectStorage* m_LevelObjectStorage;
- // The class that will handle loading objects into entities.
- CLevelLoader* m_LevelLoader;
- };
Advertisement
Add Comment
Please, Sign In to add comment