Skip to content

Commit

Permalink
type map added
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinmayMittal committed Apr 12, 2022
1 parent f7a2941 commit e6307eb
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 26 deletions.
14 changes: 14 additions & 0 deletions Entity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<string>
#include<functional>
#include"Entity.h"

Entity :: Entity( std::string nameOfEntity , std:: function<void(void)> onCollidefunciton )
{
name = nameOfEntity ;
onCollide = onCollidefunciton ;
}

std::string Entity::getName()
{
return name ;
}
17 changes: 17 additions & 0 deletions Entity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<string>
#include<functional>

class Entity{

public :

Entity( std::string nameOfEntity , std:: function<void(void)> onCollidefunciton ) ;
std::string getName() ;


private :

std::string name ;
std::function<void(void)> onCollide ;

} ;
43 changes: 32 additions & 11 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,54 +91,75 @@ bool LGame::initObjs()

bool LGame::setTiles()
{
// Open the map
// Open the map and the type map
std::ifstream mapStream("resources/map2.tmx");
std:: ifstream mapTypeStream("resources/mapTypes.tmx") ;

// If the map couldn't be loaded
if (mapStream.fail())
{
printf("Unable to load map file!\n");
return false;
}
if (mapTypeStream.fail())
{
printf("Unable to load type map file!\n");
return false;
}

std::stringstream buffer;
std::stringstream buffer , mapTypeBuffer ;
buffer << mapStream.rdbuf();
mapTypeBuffer << mapTypeStream.rdbuf() ;

std::string xmlText = buffer.str();
std :: string mapTypeXMLText = mapTypeBuffer.str() ;

rapidxml::xml_document<> doc; // character type defaults to char
rapidxml::xml_document<> doc , typedoc; // character type defaults to char
doc.parse<0>(const_cast<char *>(xmlText.c_str()));
typedoc.parse<0>( const_cast < char * >(mapTypeXMLText.c_str())) ;

rapidxml::xml_node<> *mapNode = doc.first_node("map");
rapidxml::xml_node<> *typeMapNode = typedoc.first_node("map");

int tileWidth = std::stoi(mapNode->first_attribute("tilewidth")->value());
int tileHeight = std::stoi(mapNode->first_attribute("tileheight")->value());

int numTilesX = std::stoi(mapNode->first_attribute("width")->value());
int numTilesY = std::stoi(mapNode->first_attribute("height")->value());

std::string tilesData = mapNode->first_node("layer")->first_node("data")->value();
int j = 0;

std :: string tilesTypeData = typeMapNode->first_node("layer")->first_node("data")->value() ;
int j = 0 , k = 0 ;
// Initialize the tiles
for (int i = 0; i < numTilesX * numTilesY; ++i)
{
// Read tile from map file
std::string token;
std::string token , typeToken ;
while (j < tilesData.size() && tilesData[j] != ',')
{
token += tilesData[j];
j++;
}
while (k < tilesTypeData.size() && tilesTypeData[k] != ',')
{
typeToken += tilesTypeData[k];
k++;
}
j++;
k ++ ;

int tileType = std::stoi(token);
int tileID = std::stoi(token);
int tileType = std::stoi( typeToken ) ;

Tile myTile(tilesTexture, *this, (i % numTilesX) * tileWidth, (i / numTilesX) * tileHeight, tileWidth, tileHeight, tileType);
Tile myTile(tilesTexture, *this, (i % numTilesX) * tileWidth, (i / numTilesX) * tileHeight, tileWidth, tileHeight, tileID);
myTile.setType( tileType ) ;
std:: cout << myTile.getType() << " " ;
tiles.push_back(myTile);
}

// Close the file
// Close the files
mapStream.close();
mapTypeStream.close() ;

mTilesX = numTilesX;
mTilesY = numTilesY;
Expand All @@ -148,9 +169,9 @@ bool LGame::setTiles()
return true;
}

SDL_Rect LGame::getTileClip(int tileType)
SDL_Rect LGame::getTileClip(int tileID)
{
return tileAtlas.tileClips[tileType - 1];
return tileAtlas.tileClips[tileID - 1];
}

int LGame::getLevelWidth()
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CXX = g++

#OBJS specifies which files to compile as part of the project
OBJS = Button.cpp collision.cpp Game.cpp main.cpp MainMenu.cpp MyTexture.cpp MyWindow.cpp Player.cpp Screen.cpp Text.cpp Tile.cpp TileAtlas.cpp Timer.cpp SoundEffect.cpp
OBJS = Button.cpp collision.cpp Game.cpp main.cpp MainMenu.cpp MyTexture.cpp MyWindow.cpp Player.cpp Screen.cpp Text.cpp Tile.cpp TileAtlas.cpp Timer.cpp SoundEffect.cpp Entity.cpp

#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
Expand Down
4 changes: 2 additions & 2 deletions SoundEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ bool SoundEffect::loadMusic( std :: string filename )
{
// get rid of pre-existing music
free() ;
std::cout << "loading music...\n" ;
// std::cout << "loading music...\n" ;
mMusic = Mix_LoadWAV( filename.c_str() ) ;
if( mMusic == NULL ) {
printf( "Failed to load music!\n" );
std::cout << Mix_GetError() << "\n" ;
return false ;
}
std::cout << "loaded music...\n" ;
// std::cout << "loaded music...\n" ;
return true ;

}
Expand Down
21 changes: 15 additions & 6 deletions Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "TileAtlas.h"
#include "Game.h"

Tile::Tile(LTexture &texture, LGame &game, int x, int y, int width, int height, int tileType) : mTexture(texture), mGame(game)
Tile::Tile(LTexture &texture, LGame &game, int x, int y, int width, int height, int tileID) : mTexture(texture), mGame(game)
{
// Get the offsets
mBox.x = x;
Expand All @@ -15,8 +15,9 @@ Tile::Tile(LTexture &texture, LGame &game, int x, int y, int width, int height,
mBox.w = width;
mBox.h = height;

// Get the tile type
mType = tileType;
// Get the tile id
mID = tileID ;
mType = -1 ;
}

int Tile::render(SDL_Renderer *renderer, SDL_Rect &camera)
Expand All @@ -25,15 +26,23 @@ int Tile::render(SDL_Renderer *renderer, SDL_Rect &camera)
if (checkCollision(camera, mBox))
{
// Show the tile
SDL_Rect clipRect = mGame.getTileClip(mType);
SDL_Rect clipRect = mGame.getTileClip(mID);
mTexture.render(renderer, mBox.x - camera.x, mBox.y - camera.y, &clipRect);
}
return 0;
}
void Tile :: setType( int type )
{
this-> mType = type ;
}

int Tile::getType()
int Tile :: getType( )
{
return mType;
}
int Tile::getID()
{
return mType;
return mID;
}

SDL_Rect Tile::getBox()
Expand Down
18 changes: 12 additions & 6 deletions Tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ class LGame;
class Tile : public Renderable
{
public:
// Initializes position and type
Tile(LTexture &myTexture, LGame &game, int x, int y, int width, int height, int tileType);
// Initializes position and id
Tile(LTexture &myTexture, LGame &game, int x, int y, int width, int height, int tileID);

// Shows the tile
int render(SDL_Renderer *renderer, SDL_Rect &camera);

// Get the tile type
int getType();
// Get the tile id
int getID();

void setType( int type ) ;

int getType( ) ;
// get the tile
// Get the collision box
SDL_Rect getBox();

Expand All @@ -35,8 +39,10 @@ class Tile : public Renderable

LGame &mGame;

// The tile type
int mType;
// The tile id from map
int mID;
// tells the type of each tile ie which entity does it belong to
int mType ;
};

#endif
Loading

0 comments on commit e6307eb

Please sign in to comment.