integrating tileson with the project for tmx parsing

This commit is contained in:
Alan Youngblood
2023-07-19 17:10:10 -04:00
parent 8aa79741e4
commit 352034790c
19 changed files with 4944 additions and 154 deletions

View File

@ -1,76 +0,0 @@
/*
* 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);
}

View File

@ -1,33 +0,0 @@
/*
* 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

@ -1,6 +1,6 @@
{
"GameName":"Beagle Rescue",
"Developers": "Alan Youngblood, Simon Zaleski, Daniel Rinaldi",
"LibraryDevelopers": "Sam Lantinga, Dave Gamble, Carl Birch, Job Vranish, David Lafreniere, Bayle Jonathan",
"LibraryDevelopers": "Sam Lantinga, Dave Gamble, Carl Birch, Job Vranish, David Lafreniere, Bayle Jonathan, Robin Berg Pettersen",
"SpecialThanks":"Nic Allen, Brian Lhota, Rodrigo Monteiro"
}

View File

@ -15,6 +15,7 @@
#include <iostream>
#include <string>
#include <tuple>
#include "../tileson/tileson.hpp"
//#include "../../libtmx-parser/src/tmxparser.h"
class TileMapComponent : public Component
@ -23,6 +24,7 @@ public:
SDL_Texture* texture;
SDL_Rect srcRect, destRect;
//tmxparser::TmxMap map;
//tileson::Tileson map;
int globalScale;
std::vector<SDL_Rect> tileSet;
std::vector<SDL_Rect> destRects;
@ -40,8 +42,23 @@ public:
SDL_DestroyTexture(texture);
}
TileMapComponent(/*tmxparser::TmxMap loadedMap,*/ int gScale, int offsetX, int offsetY)
TileMapComponent(/*tileson::Tileson loadedMap,*/ int gScale, int offsetX, int offsetY)
{
// ***********************************************************************************
// TILESON ~~~~~~~~~~~
tson::Tileson t;
std::unique_ptr<tson::Map> map = t.parse(fs::path("../../assets/maps/testmap.json"));
if(map->getStatus() == tson::ParseStatus::OK)
{
printf("Parsed the map file okay.");
//Gets the layer called "Object Layer" from the "ultimate_demo.json map
tson::Layer *objectLayer = map->getLayer("Object Layer"); //This is an Object Layer
}
// *******************************************************************************************
/*map = loadedMap;
std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png";
Game::assets->AddTexture(map.tilesetCollection[0].name, texturePath.c_str());

View File

@ -10,7 +10,7 @@
#include "Game.hpp"
#include "../assetmgr/TextureManager.h"
// #include "../assetmgr/Map.h"
#include "../assetmgr/Map.h"
#include "../ecs/Components.h"
#include "Collision.h"
#include <string>
@ -21,9 +21,11 @@
#include "../ui/UIText.h"
#include "../ui/UINineSlice.h"
#include "../cjson/cJSON.h"
#include "../tileson/tileson.hpp"
#include <cmath>
// tmxparser::TmxMap map;
//tileson::Tileson map;
Manager manager;
UINineSlice* my9Slice;
UINineSlice* scoreboard9Slice;
@ -77,6 +79,9 @@ int diff_time;
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) {
@ -132,7 +137,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Couldn't initialize SDL audio mixer!, Error: %s", SDL_GetError());
}
// ---------------------------------------
// Trying out the halsafar/libtmx-parser
/* tmxparser::TmxReturn error;
tmxparser::TmxMap map;
@ -166,7 +171,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
assets->AddSoundClip("bark1","assets/audio/sfx/Bark1.wav");
assets->AddSoundClip("bark2","assets/audio/sfx/Bark2.wav");
// map = new Map("terrain",globalScale,16);
// map = new Map("terrain",globalScale,16);
// std::string myText = "Find lost puppies!\nThey need your help!";
std::string myText = "Press U to Start";
@ -186,7 +191,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
uiInfo.addComponent<TransformComponent>(camera.w/gScale-94,10,72*gScale,96*gScale,gScale);
// uiInfo.addComponent<UITextComponent>("font", "CollisionHori: Vert: Jump: P.y : P.dy: YVec: ", 8, 12, 1);
uiInfo.addComponent<UITextComponent>("font", "Player PTiX: PTiY: P.x: P.y : coll: Px2 bnd ", 8, 12, gScale);
uiInfo.addComponent<UITextComponent>("font", "Player PTiX: PTiY: P.x: P.y : coll: Px2 tson: ", 8, 12, gScale);
uiInfo.addGroup(groupUI_Layer3);
uiCamXInfo.addComponent<TransformComponent>(camera.w/gScale-48,23,40*gScale,12*gScale,gScale);
@ -239,6 +244,33 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
gameScene.addComponent<TileMapComponent>(/*map,*/gScale,player.getComponent<TransformComponent>().position.x+player.getComponent<TransformComponent>().width/2,player.getComponent<TransformComponent>().position.y+player.getComponent<TransformComponent>().height/2); //150,100
gameScene.addGroup(groupMap);
// ---------------------------------------
// 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");
}
playerPosition = Vector2D().Zero();
pVel = Vector2D().Zero();
@ -294,8 +326,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)
{
@ -323,6 +355,8 @@ void Game::update()
}
}
// Gravity
// if (gravityOnPlayer){
@ -429,10 +463,10 @@ void Game::render()
t->draw();
} */
for (auto& c : colliders)
/*for (auto& c : colliders)
{
c->draw();
}
}*/
for (auto& o : objects)
{
@ -492,7 +526,7 @@ void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blu
int * Game::predictCollisions(){
static int boundaries[3];
// ===== LEFT =====
if (player.getComponent<TransformComponent>().velocity.x<0){
/*if (player.getComponent<TransformComponent>().velocity.x<0){
// ====== For Each Row ====
int i = 0;
for (int r=Game::pTileY-1;r<=Game::pTileY+1;r++){
@ -507,7 +541,7 @@ int * Game::predictCollisions(){
}
}
}
}
}*/
/*
// ====== RIGHT ====
if (player.getComponent<TransformComponent>().velocity.x>0){