Initial attempts
This commit is contained in:
parent
a9bcdaeb63
commit
207a31d3d0
16 changed files with 294 additions and 7 deletions
|
@ -94,7 +94,11 @@ set(SOURCES
|
|||
src/game/collectables/collection/collectables_collection.cpp
|
||||
src/game/collectables/collection/collectables_collection.hpp
|
||||
src/game/collectables/collection/collectables_depth_collection.cpp
|
||||
src/game/collectables/collection/collectables_depth_collection.hpp src/game/collectables/collectable_in_level.hpp src/game/collectables/collectable_factory.cpp src/game/collectables/collectable_factory.hpp src/game/player/player_collection.cpp src/game/player/player_collection.hpp src/sprites/tilemap/tilemap.cpp src/sprites/tilemap/tilemap.hpp src/sprites/tilemap/tilemap_config.hpp)
|
||||
src/game/collectables/collection/collectables_depth_collection.hpp src/game/collectables/collectable_in_level.hpp src/game/collectables/collectable_factory.cpp src/game/collectables/collectable_factory.hpp src/game/player/player_collection.cpp src/game/player/player_collection.hpp
|
||||
src/levels.hpp
|
||||
src/sprites/tiling/tilemap.cpp
|
||||
src/sprites/tiling/tilemap.hpp
|
||||
src/sprites/tiling/tilemap_config.hpp src/tilemaps.hpp src/sprites/tiling/tileset_config.hpp src/sprites/tiling/tileset.cpp src/sprites/tiling/tileset.hpp)
|
||||
|
||||
set(PHYSICS_00_SOURCES
|
||||
src/prototypes/physics_00.cpp)
|
||||
|
|
BIN
assets/grass_plus.png
Normal file
BIN
assets/grass_plus.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
|
@ -7,22 +7,27 @@
|
|||
#include "../../logging/easylogging++.h"
|
||||
#include "../collectables/collectable_in_level.hpp"
|
||||
#include <vector>
|
||||
#include "../../sprites/tiling/tilemap_config.hpp"
|
||||
|
||||
struct LevelConfig
|
||||
{
|
||||
std::string name;
|
||||
sf::Vector2f worldMapSize = {};
|
||||
TileMapConfig tileMapConfig = {};
|
||||
std::vector<GridCoordinates> playerSpawnPoints = {};
|
||||
std::vector<CollectableInLevel> collectables = {};
|
||||
|
||||
LevelConfig(std::string name,
|
||||
const sf::Vector2f &worldMapSize,
|
||||
const std::string &tileMapName,
|
||||
const std::vector<GridCoordinates> &playerSpawnPoints,
|
||||
const std::vector<CollectableInLevel> &collectables = {})
|
||||
: name(std::move(name)),
|
||||
worldMapSize(worldMapSize),
|
||||
playerSpawnPoints(playerSpawnPoints)
|
||||
{
|
||||
tileMapConfig = TileMapConfig(tileMapName, {});
|
||||
worldMapSize = tileMapConfig.getSize();
|
||||
|
||||
// Remove invalid collectables
|
||||
for (auto &collectable : collectables)
|
||||
{
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#define INITIAL_LEVEL "default"
|
||||
|
||||
std::map<std::string, LevelConfig> const all_levels = {
|
||||
{"default", LevelConfig("Default", {25, 25}, {{0, 0}}, {
|
||||
{"default", LevelConfig("Default", "grass", {{0, 0}}, {
|
||||
CollectableInLevel("box", {5, 5})
|
||||
})}
|
||||
};
|
||||
|
|
|
@ -95,3 +95,53 @@ std::shared_ptr<SpriteSheet> SpriteFactory::createSheet(const std::string& name)
|
|||
LOG(INFO) << "Creating sheet " << name;
|
||||
return std::make_shared<SpriteSheet>(texture, config.columns, config.rows);
|
||||
}
|
||||
|
||||
std::shared_ptr<TileMap> SpriteFactory::createTileMap(const std::string &name)
|
||||
{
|
||||
// Get config
|
||||
auto tilemap_config = all_tilemaps.find(name);
|
||||
|
||||
if (tilemap_config == all_tilemaps.end())
|
||||
{
|
||||
LOG(ERROR) << "Tilemap " << name << " not found. Could not create tilemap.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Construct tilemap
|
||||
auto config = tilemap_config->second;
|
||||
std::shared_ptr<TileSet> tileSet = createTileSet(config.tileSheet);
|
||||
if (sheet == nullptr)
|
||||
{
|
||||
LOG(ERROR) << "Sheet " << config.sheetName << " not found. Could not create tilemap.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
LOG(INFO) << "Creating tilemap " << name;
|
||||
auto tilemap = std::make_shared<TileMap>(sheet, config.tileSize, config.tileCount);
|
||||
tilemap->setTilemap(config.tilemap);
|
||||
return tilemap;
|
||||
}
|
||||
|
||||
std::shared_ptr<TileSet> SpriteFactory::createTileSet(const std::string &name)
|
||||
{
|
||||
// Get config
|
||||
auto tileset_config = all_tilesets.find(name);
|
||||
|
||||
if (tileset_config == all_tilesets.end())
|
||||
{
|
||||
LOG(ERROR) << "Tileset " << name << " not found. Could not create tileset.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Construct tileset
|
||||
auto config = tileset_config->second;
|
||||
auto sheet = createSheet(config.sheetName);
|
||||
if (sheet == nullptr)
|
||||
{
|
||||
LOG(ERROR) << "Sheet " << config.sheetName << " not found. Could not create tileset.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
LOG(INFO) << "Creating tileset " << name;
|
||||
return std::make_shared<TileSet>(sheet, config.tileIndices);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
#include "animated_sprite.hpp"
|
||||
#include "sprite_sheet.hpp"
|
||||
#include "../logging/easylogging++.h"
|
||||
#include "tiling/tilemap.hpp"
|
||||
#include "tiling/tileset.hpp"
|
||||
|
||||
class SpriteFactory
|
||||
{
|
||||
|
@ -15,6 +17,8 @@ public:
|
|||
static std::shared_ptr<SingleSprite> createSingleSprite(const std::string& name, sf::Vector2f size = sf::Vector2f(0, 0));
|
||||
static std::shared_ptr<AnimatedSprite> createAnimatedSprite(const std::string& name, sf::Vector2f size = sf::Vector2f(0, 0));
|
||||
static std::shared_ptr<SpriteSheet> createSheet(const std::string& name);
|
||||
static std::shared_ptr<TileMap> createTileMap(const std::string& name);
|
||||
std::shared_ptr<TileSet> createTileSet(const std::string &name);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -3,6 +3,10 @@
|
|||
|
||||
SpriteSheet::SpriteSheet(const std::shared_ptr<sf::Texture>& texture, int columns, int rows)
|
||||
{
|
||||
this->texture = texture;
|
||||
this->columns = columns;
|
||||
this->rows = rows;
|
||||
|
||||
// Extract sprites from texture
|
||||
sprites = std::vector<sf::Sprite>();
|
||||
int spriteWidth = texture->getSize().x / columns;
|
||||
|
@ -47,3 +51,18 @@ std::shared_ptr<AnimatedSprite> SpriteSheet::getAnimation(int startingSequenceIn
|
|||
}
|
||||
return std::make_shared<AnimatedSprite>(animation);
|
||||
}
|
||||
|
||||
int SpriteSheet::getRows() const
|
||||
{
|
||||
return rows;
|
||||
}
|
||||
|
||||
int SpriteSheet::getColumns() const
|
||||
{
|
||||
return columns;
|
||||
}
|
||||
|
||||
std::shared_ptr<sf::Texture> SpriteSheet::getTexture() const
|
||||
{
|
||||
return texture;
|
||||
}
|
||||
|
|
|
@ -17,7 +17,15 @@ public:
|
|||
|
||||
std::shared_ptr<AnimatedSprite> getAnimation(int startingSequenceIndex, int numberOfFrames) const;
|
||||
|
||||
std::shared_ptr<sf::Texture> getTexture() const;
|
||||
|
||||
int getColumns() const;
|
||||
int getRows() const;
|
||||
|
||||
private:
|
||||
int columns;
|
||||
int rows;
|
||||
std::shared_ptr<sf::Texture> texture;
|
||||
std::vector<sf::Sprite> sprites;
|
||||
};
|
||||
|
||||
|
|
57
src/sprites/tiling/tilemap.cpp
Normal file
57
src/sprites/tiling/tilemap.cpp
Normal file
|
@ -0,0 +1,57 @@
|
|||
#include "tilemap.hpp"
|
||||
|
||||
TileMap::TileMap(std::shared_ptr<TileSet> tileSet, std::vector<std::vector<int>> tiles)
|
||||
{
|
||||
// resize the vertex array to fit the level size
|
||||
vertices.setPrimitiveType(sf::Quads);
|
||||
|
||||
unsigned int size = tiles.size();
|
||||
vertices.resize(size * size * 4);
|
||||
|
||||
// populate the vertex array, with one quad per tile
|
||||
for (unsigned int y = 0; y < size; ++y)
|
||||
for (unsigned int x = 0; x < size; ++x)
|
||||
{
|
||||
// get the current tile number
|
||||
int tileNumber = tiles[size - 1 - y][x];
|
||||
|
||||
// TODO
|
||||
// Follow: https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php#example-tile-map
|
||||
// find its position in the tileset texture
|
||||
int tu = tileNumber % (tileset.getSize().y / tileSize.y);
|
||||
int tv = tileNumber / (tileset.getSize().y / tileSize.y);
|
||||
|
||||
// get a pointer to the current tile's quad
|
||||
sf::Vertex *quad = &vertices[(y + x * width) * 4];
|
||||
|
||||
// define its 4 corners
|
||||
quad[0].position = sf::Vector2f(y * tileSize.y, x * tileSize.y);
|
||||
quad[1].position = sf::Vector2f((y + 1) * tileSize.y, x * tileSize.y);
|
||||
quad[2].position = sf::Vector2f((y + 1) * tileSize.y, (x + 1) * tileSize.y);
|
||||
quad[3].position = sf::Vector2f(y * tileSize.y, (x + 1) * tileSize.y);
|
||||
|
||||
// define its 4 texture coordinates
|
||||
quad[0].texCoords = sf::Vector2f(tu * tileSize.y, tv * tileSize.y);
|
||||
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.y, tv * tileSize.y);
|
||||
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.y, (tv + 1) * tileSize.y);
|
||||
quad[3].texCoords = sf::Vector2f(tu * tileSize.y, (tv + 1) * tileSize.y);
|
||||
}
|
||||
}
|
||||
|
||||
void TileMap::draw(sf::RenderWindow *window)
|
||||
{
|
||||
window->draw(*this);
|
||||
}
|
||||
|
||||
void TileMap::draw(sf::RenderTarget &target, sf::RenderStates states) const
|
||||
{
|
||||
// apply the transform
|
||||
// TODO? NEEDED?
|
||||
// states.transform *= getTransform();
|
||||
|
||||
// apply the tileset texture
|
||||
states.texture = &tileMapTexture;
|
||||
|
||||
// draw the vertex array
|
||||
target.draw(vertices, states);
|
||||
}
|
26
src/sprites/tiling/tilemap.hpp
Normal file
26
src/sprites/tiling/tilemap.hpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
#ifndef HOLESOME_TILEMAP_HPP
|
||||
#define HOLESOME_TILEMAP_HPP
|
||||
|
||||
|
||||
#include <SFML/Graphics/Transformable.hpp>
|
||||
#include <SFML/Graphics/VertexArray.hpp>
|
||||
#include <SFML/Graphics/Texture.hpp>
|
||||
#include "../../game/game_object.h"
|
||||
#include "tileset.hpp"
|
||||
|
||||
class TileMap : public GameObject, public sf::Drawable
|
||||
{
|
||||
public:
|
||||
TileMap(std::shared_ptr<TileSet> tileSet, std::vector<std::vector<int>> tiles);
|
||||
|
||||
void draw(sf::RenderWindow *window) override;
|
||||
|
||||
void draw(sf::RenderTarget &target, sf::RenderStates states) const override;
|
||||
|
||||
private:
|
||||
sf::VertexArray vertices;
|
||||
sf::Texture tileMapTexture;
|
||||
};
|
||||
|
||||
|
||||
#endif //HOLESOME_TILEMAP_HPP
|
45
src/sprites/tiling/tilemap_config.hpp
Normal file
45
src/sprites/tiling/tilemap_config.hpp
Normal file
|
@ -0,0 +1,45 @@
|
|||
#ifndef HOLESOME_TILEMAP_CONFIG_HPP
|
||||
#define HOLESOME_TILEMAP_CONFIG_HPP
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <stdexcept>
|
||||
|
||||
struct TileMapConfig
|
||||
{
|
||||
std::string tileSheet;
|
||||
|
||||
std::vector<int> backgroundTileSelection = {};
|
||||
|
||||
std::vector<std::vector<int>> tiles = {};
|
||||
|
||||
TileMapConfig(std::string tileSheet, std::vector<std::vector<int>> tiles,
|
||||
std::vector<int> backgroundTileSelection = {})
|
||||
{
|
||||
this->tileSheet = std::move(tileSheet);
|
||||
this->backgroundTileSelection = std::move(backgroundTileSelection);
|
||||
this->tiles = std::move(tiles);
|
||||
|
||||
// Make sure all rows are the same length and that the map is square
|
||||
int size = tiles.size();
|
||||
for (auto &row : tiles)
|
||||
{
|
||||
if (row.size() != size)
|
||||
{
|
||||
throw std::runtime_error("Tile map is not square");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TileMapConfig() = default;
|
||||
|
||||
sf::Vector2f getSize() const
|
||||
{
|
||||
int size = tiles.size();
|
||||
return {static_cast<float>(size), static_cast<float>(size)};
|
||||
}
|
||||
};
|
||||
|
||||
#endif //HOLESOME_TILEMAP_CONFIG_HPP
|
6
src/sprites/tiling/tileset.cpp
Normal file
6
src/sprites/tiling/tileset.cpp
Normal file
|
@ -0,0 +1,6 @@
|
|||
#include "tileset.hpp"
|
||||
|
||||
TileSet::TileSet(const std::shared_ptr<SpriteSheet>& sheet, const std::vector<int>& tileIndices)
|
||||
{
|
||||
|
||||
}
|
14
src/sprites/tiling/tileset.hpp
Normal file
14
src/sprites/tiling/tileset.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef HOLESOME_TILESET_HPP
|
||||
#define HOLESOME_TILESET_HPP
|
||||
|
||||
|
||||
#include "../sprite_sheet.hpp"
|
||||
|
||||
class TileSet
|
||||
{
|
||||
public:
|
||||
TileSet(const std::shared_ptr<SpriteSheet>& sheet, const std::vector<int>& tileIndices);
|
||||
};
|
||||
|
||||
|
||||
#endif //HOLESOME_TILESET_HPP
|
21
src/sprites/tiling/tileset_config.hpp
Normal file
21
src/sprites/tiling/tileset_config.hpp
Normal file
|
@ -0,0 +1,21 @@
|
|||
#ifndef HOLESOME_TILESET_CONFIG_HPP
|
||||
#define HOLESOME_TILESET_CONFIG_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct TileSetConfig
|
||||
{
|
||||
std::string sheetName;
|
||||
std::vector<int> tileIndices = {};
|
||||
|
||||
TileSetConfig() = default;
|
||||
|
||||
TileSetConfig(std::string sheetName, std::vector<int> tileIndices)
|
||||
{
|
||||
this->sheetName = std::move(sheetName);
|
||||
this->tileIndices = std::move(tileIndices);
|
||||
}
|
||||
};
|
||||
|
||||
#endif //HOLESOME_TILESET_CONFIG_HPP
|
|
@ -6,6 +6,7 @@
|
|||
#include "sprites/configs/animation_config.hpp"
|
||||
#include "sprites/configs/sheet_config.hpp"
|
||||
#include "sprites/configs/sprite_config.hpp"
|
||||
#include "sprites/tiling/tileset_config.hpp"
|
||||
|
||||
#define PLAYER_SKIN "ring"
|
||||
|
||||
|
@ -15,9 +16,10 @@
|
|||
*/
|
||||
std::map<std::string, std::string> const all_textures = {
|
||||
{"numbers", "assets/numbers.png"},
|
||||
{"64", "assets/64.png"},
|
||||
{"edge", "assets/edge.png"},
|
||||
{"ring", "assets/ring.png"},
|
||||
{"64", "assets/64.png"},
|
||||
{"edge", "assets/edge.png"},
|
||||
{"ring", "assets/ring.png"},
|
||||
{"grasses", "assets/grass_plus.png"},
|
||||
{"hole", "assets/hole.png"}
|
||||
};
|
||||
|
||||
|
@ -26,7 +28,8 @@ std::map<std::string, std::string> const all_textures = {
|
|||
* The key is the name of the sheet, the value is the sheet config.
|
||||
*/
|
||||
std::map<std::string, SheetConfig> const all_sheets = {
|
||||
{"numbers", SheetConfig("numbers", 4, 2)}
|
||||
{"numbers", SheetConfig("numbers", 4, 2)},
|
||||
{"grasses", SheetConfig("grasses", 25, 14)}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -48,4 +51,8 @@ std::map<std::string, SpriteConfig> const all_sprites = {
|
|||
{"hole", SpriteConfig("hole")}
|
||||
};
|
||||
|
||||
std::map<std::string, TileSetConfig> const all_tilesets = {
|
||||
{"grass", TileSetConfig("grasses", {58})}
|
||||
};
|
||||
|
||||
#endif //HOLESOME_TEXTURE_CONFIG_H
|
||||
|
|
21
src/tilemaps.hpp
Normal file
21
src/tilemaps.hpp
Normal file
|
@ -0,0 +1,21 @@
|
|||
#ifndef HOLESOME_TILEMAPS_HPP
|
||||
#define HOLESOME_TILEMAPS_HPP
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "sprites/tiling/tilemap_config.hpp"
|
||||
|
||||
std::map<std::string, TileMapConfig> const all_tilemaps = {
|
||||
{"grass", TileMapConfig("grass", {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},})}
|
||||
};
|
||||
|
||||
#endif //HOLESOME_TILEMAPS_HPP
|
Loading…
Reference in a new issue