Started object construction and parsing

Simplified some structures
Missing : Entity construction
This commit is contained in:
trotFunky 2019-06-07 02:59:27 +02:00
parent 67435debe6
commit 46d26b6e61
10 changed files with 146 additions and 5 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 43 KiB

6
resources/test_level.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version = "1.0"?>
<Level w="10" h="10" textureId="0">
<Entity x="0" y="0" type="Citizen"/>
<Entity x="0" y="1" type="Citizen"/>
<Entity x="1" y="1" type="House"/>
</Level>

View file

@ -4,7 +4,7 @@ project(project_maat)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH}) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
add_library(engine Level.cpp Level.h Entity.cpp Entity.h Game.cpp Game.h) add_library(engine Level.cpp Level.h Entity.cpp Entity.h Game.cpp Game.h Utils.h Utils.h)
target_link_libraries(engine target_link_libraries(engine
sfml-window sfml-window

View file

@ -3,3 +3,39 @@
// //
#include "Entity.h" #include "Entity.h"
const std::map<std::string,EntityType> Entity::entityTypeLookup = {
{"Citizen",EntityType::Citizen},
{"Player",EntityType::Player},
{"House",EntityType::House},
{"Car",EntityType::Car}};
Entity::Entity(int x, int y, EntityType type, int width, int height)
{
}
Entity::Entity(const pugi::xml_node& entityNode)
{
}
void Entity::render(sf::RenderWindow& renderWindow) const
{
}
void Entity::move()
{
}
void Entity::update()
{
}
const sf::RectangleShape& Entity::getShape() const
{
return(shape);
}

View file

@ -6,6 +6,9 @@
#define SRC_ENTITY_H #define SRC_ENTITY_H
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <pugixml.hpp>
#include "Utils.h"
enum class EntityType enum class EntityType
@ -36,15 +39,21 @@ enum class Orientation
class Entity class Entity
{ {
public: public:
Entity(); /// x,y, width and height are in grid coordinates
Entity(int x, int y, EntityType type, int width = 1, int height = 1);
Entity(const pugi::xml_node& entityNode);
void render(sf::RenderWindow& renderWindow) const; void render(sf::RenderWindow& renderWindow) const;
void move(); void move();
void update(); void update();
const sf::RectangleShape& getShape() const;
/// Position of the target of the current action on the map /// Position of the target of the current action on the map
sf::Vector2i target; sf::Vector2i target;
private: private:
static const std::map<std::string,EntityType> entityTypeLookup;
// As it contains position, size and orientation, we do not need anything more // As it contains position, size and orientation, we do not need anything more
sf::RectangleShape shape; sf::RectangleShape shape;

View file

@ -1,6 +1,44 @@
#include <memory>
// //
// Created by trotfunky on 06/06/19. // Created by trotfunky on 06/06/19.
// //
#include "Game.h" #include "Game.h"
Game::Game(const std::vector<std::string>& levels, const std::vector<std::string>& textures)
{
loadTextures();
}
void Game::loadLevel(int levelId)
{
pugi::xml_document document;
pugi::xml_parse_result result = document.load_file(levelFiles.at(levelId).c_str());
if(!result)
{
std::cerr << "Error while loading level :\n" << "\t" << result.description() << std::endl;
exit(-1);
}
currentLevel = std::make_unique<Level>(document,textures);
}
void Game::loadTextures()
{
textures.reserve(textureFiles.size());
for(const std::string& filePath : textureFiles)
{
textures.emplace_back(std::make_unique<sf::Texture>());
textures.back()->loadFromFile(filePath);
}
}
void Game::runGame()
{
}

View file

@ -7,8 +7,10 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <vector> #include <vector>
#include <iostream>
#include "Level.h" #include "Level.h"
#include "Entity.h"
// Used for convenience // Used for convenience
@ -19,9 +21,13 @@ public:
Game(const std::vector<std::string>& levels, const std::vector<std::string>& textures); Game(const std::vector<std::string>& levels, const std::vector<std::string>& textures);
/// Loads the level of corresponding ID from Game::levelFiles /// Loads the level of corresponding ID from Game::levelFiles
/// Closes the program if there is a fatal error (Missing file, bad file...)
void loadLevel(int levelId); void loadLevel(int levelId);
Level currentLevel; std::unique_ptr<Level> currentLevel;
// This should not be called before a level has been loaded
void runGame();
private: private:
/// Store the paths to level files /// Store the paths to level files

View file

@ -3,3 +3,28 @@
// //
#include "Level.h" #include "Level.h"
Level::Level(const pugi::xml_document& xmlDoc, const TextureStore& textureStore)
: textures(textureStore),
size(xmlDoc.child("Level").attribute("width").as_int(),xmlDoc.child("Level").attribute("width").as_int())
{
pugi::xml_node levelNode = xmlDoc.child("Level");
for(const pugi::xml_node& child : levelNode.children())
{
if(!strncmp(child.name(),"Entity",6))
{
entities.emplace_back(child);
}
}
}
void Level::render(sf::RenderWindow& renderWindow) const
{
}
void Level::runStep() const
{
}

View file

@ -6,15 +6,18 @@
#define SRC_LEVEL_H #define SRC_LEVEL_H
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <pugixml.hpp>
#include <vector> #include <vector>
#include <cstring>
#include "Utils.h"
#include "Entity.h" #include "Entity.h"
#include "Game.h"
using TextureStore = std::vector<std::unique_ptr<sf::Texture>>;
class Level { class Level {
public: public:
Level(const TextureStore& textureStore); Level(const pugi::xml_document& xmlDoc, const TextureStore& textureStore);
void render(sf::RenderWindow& renderWindow) const; void render(sf::RenderWindow& renderWindow) const;
void runStep() const; void runStep() const;

18
src/Utils.h Normal file
View file

@ -0,0 +1,18 @@
//
// Created by trotfunky on 07/06/19.
//
#ifndef PROJECT_MAAT_UTILS_H
#define PROJECT_MAAT_UTILS_H
#include <cstdint>
namespace pro_maat
{
static constexpr uint8_t pixelsPerUnit = 20;
}
#endif //PROJECT_MAAT_UTILS_H