added tileson still debugging

This commit is contained in:
Alan Youngblood
2023-07-20 12:31:37 -04:00
parent 352034790c
commit 9432b68efe
18 changed files with 15930 additions and 294 deletions

76
src/assetmgr/Map.cpp Normal file
View File

@ -0,0 +1,76 @@
/*
* Map.cpp
*
* Created on: Feb 13, 2020
* Author: ayoungblood
*/
#include "Map.h"
#include "../game/Game.hpp"
#include <fstream>
#include "../ecs/ECS.h"
#include "../ecs/Components.h"
#include <string>
#include <iostream>
extern Manager manager;
Map::Map(std::string tID, int ms, int ts) : texID(tID), mapScale(ms), tileSize(ts)
{
scaledSize = ms* ts;
width = 0;
height = 0;
tSize = ts;
}
Map::~Map()
{
}
void Map::LoadMap(std::string path, int sizeX, int sizeY, int scale)
{
char c;
std::fstream mapFile;
mapFile.open(path);
int srcX, srcY;
width = tSize*scale*sizeX;
height = tSize*scale*sizeY;
for (int y = 0; y < sizeY; y++)
{
for (int x = 0; x < sizeX; x++)
{
mapFile.get(c);
srcY = atoi(&c) * tileSize;
mapFile.get(c);
srcX = atoi(&c) * tileSize;
mapFile.ignore(2,',');
}
}
mapFile.ignore();
// colliders
for (int y =0; y < sizeY; y++)
{
for (int x = 0; x < sizeX; x++)
{
mapFile.get(c);
if (c == '1')
{
auto& tcol(manager.addEntity());
tcol.addComponent<ColliderComponent>("terrain",x*scaledSize,y*scaledSize,tileSize,scale,texID);
tcol.addGroup(Game::groupColliders);
}
mapFile.ignore(2,',');
}
}
mapFile.close();
}
void Map::AddTile(int srcX, int srcY, int xpos, int ypos)
{
auto& tile(manager.addEntity());
tile.addComponent<TileComponent>(srcX,srcY,xpos,ypos,tileSize, mapScale, texID);
tile.addGroup(Game::groupMap);
}

33
src/assetmgr/Map.h Normal file
View File

@ -0,0 +1,33 @@
/*
* Map.h
*
* Created on: Feb 13, 2020
* Author: ayoungblood
*/
#ifndef SRC_MAP_H_
#define SRC_MAP_H_
#include <string>
class Map
{
public:
Map(std::string tID, int ms, int ts);
~Map();
void LoadMap(std::string path, int sizeX, int sizeY, int scale);
void AddTile(int srcX, int srcY, int xpos, int ypos);
int width;
int height;
int tSize;
private:
std::string texID;
int mapScale;
int tileSize;
int scaledSize;
};
#endif /* SRC_MAP_H_ */

View File

@ -76,12 +76,11 @@ int last_time;
int current_time;
int diff_time;
//tson::Tileson t;
int Game::pTileX = 0;
int Game::pTileY = 0;
tson::Tileson t;
std::unique_ptr<tson::Map> map = t.parse(std::filesystem::path("../../assets/maps/testmap.json"));
std::string Game::BoolToString(bool b) {
std::string myString;
if (b) {
@ -247,30 +246,35 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
// ---------------------------------------
// Tileson tryout
printf("Trying out Tileson, does it work?");
if(map->getStatus() == tson::ParseStatus::OK)
{
printf("Tileson successfully parsed the tilemap");
//Gets the layer called "Object Layer" from the "ultimate_demo.json map
tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is an Object Layer
//Example from a Tile Layer
//I know for a fact that this is a Tile Layer, but you can check it this way to be sure.
if(tileLayer->getType() == tson::LayerType::TileLayer)
{
//pos = position in tile units
printf("Tileson found layer of tiles");
/*for(auto &[pos, tileObject] : tileLayer->getTileObjects()) //Loops through absolutely all existing tileObjects
{
tson::Tileset *tileset = tileObject.getTile()->getTileset();
tson::Rect drawingRect = tileObject.getDrawingRect();
tson::Vector2f position = tileObject.getPosition();
}*/
}
} else {
printf("Failed to load Tileson map");
}
// printf("Trying out Tileson, does it work?");
//
//
// //std::unique_ptr<tson::Map> map = t.parse(std::filesystem::path("../../assets/maps/testmap.json"));
// std::unique_ptr<tson::Map> map = t.parse(tson_files::_ULTIMATE_TEST_JSON, tson_files::_ULTIMATE_TEST_JSON_SIZE);
//
// if(map->getStatus() == tson::ParseStatus::OK)
// {
// printf("Tileson successfully parsed the tilemap");
// //Gets the layer called "Object Layer" from the "ultimate_demo.json map
// //tson::Layer *tileLayer = map->getLayer("Tile Layer 1"); //This is an Object Layer
//
// //Example from a Tile Layer
// //I know for a fact that this is a Tile Layer, but you can check it this way to be sure.
// // if(tileLayer->getType() == tson::LayerType::TileLayer)
// // {
// // //pos = position in tile units
// // printf("Tileson found layer of tiles");
// // /*for(auto &[pos, tileObject] : tileLayer->getTileObjects()) //Loops through absolutely all existing tileObjects
// // {
// // tson::Tileset *tileset = tileObject.getTile()->getTileset();
// // tson::Rect drawingRect = tileObject.getDrawingRect();
// // tson::Vector2f position = tileObject.getPosition();
// // }*/
// // }
//
// } else {
// printf("Failed to load Tileson map");
// }
playerPosition = Vector2D().Zero();
pVel = Vector2D().Zero();
@ -326,8 +330,8 @@ void Game::update()
if (Mix_PlayingMusic() == 0 && gsm->currentState == GameStateManager::ST_COREGAME)
{
std::cout << "Play Music Now" << std::endl;
Mix_PlayMusic(assets->GetMusicTrack("simonZ"), -1);
// std::cout << "Play Music Now" << std::endl;
// Mix_PlayMusic(assets->GetMusicTrack("simonZ"), -1);
}
if (Mix_PlayingMusic() != 0 && gsm->currentState != GameStateManager::ST_COREGAME)
{

8992
src/tileson/tileson.hpp Normal file

File diff suppressed because it is too large Load Diff