TileMaps now render each row correctly

This commit is contained in:
Alan Youngblood 2022-03-27 23:59:12 -04:00
parent 72b4ac9229
commit 871e6d19c6
9 changed files with 152 additions and 162 deletions

View File

@ -1,21 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.8" tiledversion="1.8.2" name="br-tiles" tilewidth="16" tileheight="16" tilecount="50" columns="10"> <tileset version="1.8" tiledversion="1.8.2" name="br-tiles" tilewidth="16" tileheight="16" tilecount="50" columns="10">
<image source="br-tiles.png" width="160" height="80"/> <image source="br-tiles.png" width="160" height="80"/>
<tile id="8"/>
<tile id="1"/>
<tile id="0"/>
<tile id="2"/>
<tile id="3"/>
<tile id="4"/>
<tile id="5"/>
<tile id="6"/>
<tile id="7"/>
<tile id="10"> <tile id="10">
<objectgroup draworder="index" id="2"> <objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/> <object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup> </objectgroup>
</tile> </tile>
<tile id="9"/>
<tile id="11"> <tile id="11">
<objectgroup draworder="index" id="2"> <objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/> <object id="1" x="0" y="0" width="16" height="16"/>
@ -31,15 +21,11 @@
<object id="1" x="0" y="0" width="16" height="16"/> <object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup> </objectgroup>
</tile> </tile>
<tile id="14"/>
<tile id="15"> <tile id="15">
<objectgroup draworder="index" id="2"> <objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/> <object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup> </objectgroup>
</tile> </tile>
<tile id="16"/>
<tile id="17"/>
<tile id="18"/>
<tile id="19"> <tile id="19">
<objectgroup draworder="index" id="2"> <objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/> <object id="1" x="0" y="0" width="16" height="16"/>
@ -55,31 +41,11 @@
<object id="1" x="0" y="0" width="16" height="16"/> <object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup> </objectgroup>
</tile> </tile>
<tile id="22"/>
<tile id="23"/>
<tile id="24"/>
<tile id="25"/>
<tile id="26"/>
<tile id="27"/>
<tile id="28"> <tile id="28">
<objectgroup draworder="index" id="2"> <objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/> <object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup> </objectgroup>
</tile> </tile>
<tile id="29"/>
<tile id="30"/>
<tile id="31"/>
<tile id="32"/>
<tile id="33"/>
<tile id="34"/>
<tile id="35"/>
<tile id="36"/>
<tile id="37"/>
<tile id="38"/>
<tile id="39"/>
<tile id="40"/>
<tile id="41"/>
<tile id="42"/>
<tile id="43"> <tile id="43">
<objectgroup draworder="index" id="2"> <objectgroup draworder="index" id="2">
<object id="2" x="0" y="0" width="16" height="16"/> <object id="2" x="0" y="0" width="16" height="16"/>
@ -96,8 +62,4 @@
<object id="1" x="0" y="0" width="16" height="16"/> <object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup> </objectgroup>
</tile> </tile>
<tile id="46"/>
<tile id="47"/>
<tile id="48"/>
<tile id="49"/>
</tileset> </tileset>

View File

@ -3,24 +3,24 @@
<tileset firstgid="1" source="br-tiles.tsx"/> <tileset firstgid="1" source="br-tiles.tsx"/>
<layer id="1" name="TileMap" width="42" height="42"> <layer id="1" name="TileMap" width="42" height="42">
<data encoding="csv"> <data encoding="csv">
11,12,13,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 11,12,13,35,35,35,36,36,36,35,36,35,35,9,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,35,35,35,35,36,36,35,35,35,35,10,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,35,35,37,35,35,35,35,41,42,43,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,35,35,35,35,35,35,38,38,35,35,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,35,35,35,35,35,38,38,35,35,35,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,35,35,35,35,38,38,35,35,37,35,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,35,35,35,38,38,35,35,35,37,37,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,35,35,38,38,35,35,35,37,37,37,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,35,38,38,35,35,35,35,35,35,37,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,45,46,44,44,44,45,46,44,45,46,44,45,46,44,45,46,44,45,46,44,45,44,45,44,45,46,45,44,44,44,44,44,44,44,44,14,15,16, 14,15,16,34,45,46,34,34,34,45,46,44,45,46,44,45,46,44,45,46,44,45,46,44,45,44,45,44,45,46,45,44,44,44,44,44,44,44,44,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,34,34,34,34,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,34,34,34,34,34,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,34,34,34,41,42,43,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,34,34,34,34,34,34,34,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,34,34,34,34,34,34,34,34,34,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,34,34,34,34,34,34,34,34,34,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,34,34,34,34,34,34,34,34,34,34,34,34,34,34,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,
14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16, 14,15,16,17,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,14,15,16,

File diff suppressed because one or more lines are too long

View File

Binary file not shown.

View File

@ -3,5 +3,5 @@
"WindowName":"Beagle Rescue", "WindowName":"Beagle Rescue",
"WindowSize":{"w":320,"h":240}, "WindowSize":{"w":320,"h":240},
"WindowFullScreen": 0, "WindowFullScreen": 0,
"GlobalScale": 3 "GlobalScale": 1
} }

View File

@ -41,11 +41,25 @@ public:
{ {
switch (Game::event.key.keysym.sym) switch (Game::event.key.keysym.sym)
{ {
case SDLK_UP: case SDLK_UP:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.y = -1;
// if(Game::playerIsGrounded){
sprite->Play("Walk");
// }
sprite->spriteFlip = SDL_FLIP_NONE;
}
break; break;
case SDLK_DOWN: case SDLK_DOWN:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.y = 1;
// if(Game::playerIsGrounded){
sprite->Play("Walk");
// }
sprite->spriteFlip = SDL_FLIP_NONE;
}
break; break;
case SDLK_LEFT: case SDLK_LEFT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
// if (playerCtrl == NULL){ // if (playerCtrl == NULL){
// printf("No player controller found\n"); // printf("No player controller found\n");
@ -56,13 +70,13 @@ public:
// transform->lastSafePos = Vector2D(transform->position.x,transform->position.y); // transform->lastSafePos = Vector2D(transform->position.x,transform->position.y);
// printf("lastSafePos .x: %g .y: %g \n",transform->lastSafePos.x,transform->lastSafePos.y); // printf("lastSafePos .x: %g .y: %g \n",transform->lastSafePos.x,transform->lastSafePos.y);
transform->velocity.x = -1; transform->velocity.x = -1;
if(Game::playerIsGrounded){ // if(Game::playerIsGrounded){
sprite->Play("Walk"); sprite->Play("Walk");
} // }
sprite->spriteFlip = SDL_FLIP_NONE; sprite->spriteFlip = SDL_FLIP_NONE;
} }
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
// &entity->getComponent<PlayerController>().setLastSafePos(Vector2D(transform->position.x,transform->position.y)); // &entity->getComponent<PlayerController>().setLastSafePos(Vector2D(transform->position.x,transform->position.y));
// playerCtrl->lastSafePosition.x = transform->position.x; // playerCtrl->lastSafePosition.x = transform->position.x;
@ -72,30 +86,30 @@ public:
// ========= // =========
// int intersectionTileX = collider->center.x; // int intersectionTileX = collider->center.x;
transform->velocity.x = 1; transform->velocity.x = 1;
if(Game::playerIsGrounded){ // if(Game::playerIsGrounded){
sprite->Play("Walk"); sprite->Play("Walk");
} // }
sprite->spriteFlip = SDL_FLIP_HORIZONTAL; sprite->spriteFlip = SDL_FLIP_HORIZONTAL;
} }
break; break;
case SDLK_a: // case SDLK_a:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ // if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = -1; // transform->velocity.x = -1;
if(Game::playerIsGrounded){ // if(Game::playerIsGrounded){
sprite->Play("Walk"); // sprite->Play("Walk");
} // }
sprite->spriteFlip = SDL_FLIP_NONE; // sprite->spriteFlip = SDL_FLIP_NONE;
} // }
break; // break;
case SDLK_d: // case SDLK_d:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ // if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = 1; // transform->velocity.x = 1;
if(Game::playerIsGrounded){ // if(Game::playerIsGrounded){
sprite->Play("Walk"); // sprite->Play("Walk");
} // }
sprite->spriteFlip = SDL_FLIP_HORIZONTAL; // sprite->spriteFlip = SDL_FLIP_HORIZONTAL;
} // }
break; // break;
case SDLK_k: case SDLK_k:
if (!Game::debugCollisionBoxes) if (!Game::debugCollisionBoxes)
{ Game::debugCollisionBoxes = true; } { Game::debugCollisionBoxes = true; }
@ -137,44 +151,63 @@ public:
{ {
switch (Game::event.key.keysym.sym) switch (Game::event.key.keysym.sym)
{ {
case SDLK_LEFT: case SDLK_LEFT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = 0; transform->velocity.x = 0;
sprite->Play("Idle"); sprite->Play("Idle");
if (!Game::gravityOnPlayer){ // if (!Game::gravityOnPlayer){
Game::gravityOnPlayer = true; // Game::gravityOnPlayer = true;
// sprite->Play("Fall"); // // sprite->Play("Fall");
} // }
} }
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = 0; transform->velocity.x = 0;
sprite->Play("Idle"); sprite->Play("Idle");
if (!Game::gravityOnPlayer){ // if (!Game::gravityOnPlayer){
Game::gravityOnPlayer = true; // Game::gravityOnPlayer = true;
} // }
} }
break; break;
case SDLK_a: case SDLK_UP:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = 0; transform->velocity.y = 0;
sprite->Play("Idle"); sprite->Play("Idle");
if (!Game::gravityOnPlayer){ // if (!Game::gravityOnPlayer){
Game::gravityOnPlayer = true; // Game::gravityOnPlayer = true;
// sprite->Play("Fall"); // // sprite->Play("Fall");
} // }
} }
break; break;
case SDLK_d: case SDLK_DOWN:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){ if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = 0; transform->velocity.y = 0;
sprite->Play("Idle"); sprite->Play("Idle");
if (!Game::gravityOnPlayer){ // if (!Game::gravityOnPlayer){
Game::gravityOnPlayer = true; // Game::gravityOnPlayer = true;
} // }
} }
break; break;
// case SDLK_a:
// if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
// transform->velocity.x = 0;
// sprite->Play("Idle");
// if (!Game::gravityOnPlayer){
// Game::gravityOnPlayer = true;
// // sprite->Play("Fall");
// }
// }
// break;
// case SDLK_d:
// if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
// transform->velocity.x = 0;
// sprite->Play("Idle");
// if (!Game::gravityOnPlayer){
// Game::gravityOnPlayer = true;
// }
// }
// break;
case SDLK_k: case SDLK_k:
break; break;
case SDLK_j: case SDLK_j:

View File

@ -23,11 +23,13 @@ public:
// TransformComponent *transform; // TransformComponent *transform;
SDL_Texture* texture; SDL_Texture* texture;
SDL_Rect srcRect, destRect; SDL_Rect srcRect, destRect;
Vector2D position; // Vector2D position;
// std::tuple <SDL_Rect,SDL_Rect> tile; // std::tuple <SDL_Rect,SDL_Rect> tile;
tmxparser::TmxMap map; tmxparser::TmxMap map;
int globalScale; int globalScale;
std::vector<SDL_Rect> tileSet; std::vector<SDL_Rect> tileSet;
std::vector<SDL_Rect> destRects;
int totalTiles;
TileMapComponent() = default; TileMapComponent() = default;
@ -40,10 +42,10 @@ public:
{ {
map = loadedMap; map = loadedMap;
std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png"; std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png";
Game::assets->AddTexture(loadedMap.tilesetCollection[0].name, texturePath.c_str()); Game::assets->AddTexture(map.tilesetCollection[0].name, texturePath.c_str());
setTex(map.tilesetCollection[0].name); setTex(map.tilesetCollection[0].name);
globalScale = gScale; globalScale = gScale;
int totalTiles = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount; totalTiles = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount;
// std::cout << "Number of Tiles: " << totalTiles << std::endl; // std::cout << "Number of Tiles: " << totalTiles << std::endl;
tileSet.resize(totalTiles); tileSet.resize(totalTiles);
for (int r=0;r<map.tilesetCollection[0].rowCount;r++){ for (int r=0;r<map.tilesetCollection[0].rowCount;r++){
@ -57,25 +59,37 @@ public:
tileSet[element] = srcRect; tileSet[element] = srcRect;
} }
} }
destRects.resize(totalTiles);
for (int i=0;i<totalTiles-1;i++){
SDL_Rect myRect = SDL_Rect();
myRect.w = myRect.h = map.tileWidth * gScale;
myRect.x = myRect.y = 0;
destRects[i] = myRect;
}
destRect.w = destRect.h = map.tileWidth * gScale; destRect.w = destRect.h = map.tileWidth * gScale;
} }
void update() override void update() override
{ {
destRect.x = position.x - Game::camera.x; // for (int i=0;i<totalTiles-1;i++){
destRect.y = position.y - Game::camera.y; // destRects[i].x = destRects[i].x - Game::camera.x;
// destRects[i].y = destRects[i].y - Game::camera.y;
// }
} }
void draw() override void draw() override
{ {
//iterate through rows and columns of the map to draw the tiles //iterate through rows and columns of the map to draw the tiles
// First cycle through rows // First cycle through rows
for (int r = 0;r<map.height;r++){ for (int r = 0;r<map.height-1;r++){
// Next cycle through each column or tile in that row: // Next cycle through each column or tile in that row:
for (int c = 0;c<map.width;c++){ for (int c = 0;c<map.width-1;c++){
int tileToDraw = map.layerCollection[0].tiles[c].gid-1; int i = r*map.width+c;
int tileToDraw = map.layerCollection[0].tiles[i].gid-1;
destRect.x = c*map.tilesetCollection[0].tileWidth*globalScale; destRect.x = c*map.tilesetCollection[0].tileWidth*globalScale;
destRect.y = r*map.tilesetCollection[0].tileWidth*globalScale; destRect.y = r*map.tilesetCollection[0].tileWidth*globalScale;
// destRects[i].x = 8;
// destRects[i].y = 32;
TextureManager::Draw(texture, tileSet[tileToDraw], destRect, SDL_FLIP_NONE); TextureManager::Draw(texture, tileSet[tileToDraw], destRect, SDL_FLIP_NONE);
} }
} }

View File

@ -36,6 +36,7 @@ SDL_Renderer* Game::renderer = nullptr;
SDL_Event Game::event; SDL_Event Game::event;
SDL_Rect Game::camera; SDL_Rect Game::camera;
SDL_Rect levelMap;
AssetManager* Game::assets = new AssetManager(&manager); AssetManager* Game::assets = new AssetManager(&manager);
@ -59,9 +60,7 @@ bool Game::gravityOnPlayer = true;
bool Game::playerIsGrounded = false; bool Game::playerIsGrounded = false;
bool Game::playerIsJumping = false; bool Game::playerIsJumping = false;
int gScale = 0; int gScale = 0;
int last_time; int last_time;
int current_time; int current_time;
int diff_time; int diff_time;
@ -76,7 +75,6 @@ std::string Game::BoolToString(bool b) {
return myString; return myString;
} }
Game::Game() { Game::Game() {
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
@ -125,25 +123,36 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
// Trying out the halsafar/libtmx-parser // Trying out the halsafar/libtmx-parser
tmxparser::TmxReturn error; tmxparser::TmxReturn error;
tmxparser::TmxMap map; tmxparser::TmxMap map;
error = tmxparser::parseFromFile("assets/maps/testmap.tmx", &map, "assets/textures/tiles/"); error = tmxparser::parseFromFile("assets/maps/testmapb64.tmx", &map, "assets/textures/tiles/");
// current_time = SDL_GetTicks(); // current_time = SDL_GetTicks();
if (!error) if (!error)
{ {
// printf("Yay! Tile map loaded with no errors.\n"); // printf("Yay! Tile map loaded with no errors.\n");
tmxparser::TmxLayer layer = map.layerCollection[0]; tmxparser::TmxLayer layer = map.layerCollection[0];
levelMap.x = 0;
levelMap.y = 0;
levelMap.w = map.width*map.tileWidth*gScale;
levelMap.h = map.height*map.tileHeight*gScale;
// std::cout << "Map Width Tiles: " << map.width << std::endl;
//iterate through rows and columns of the map to draw the tiles
// // First cycle through rows
// for (int r = 0;r<map.height;r++){
// // Next cycle through each column or tile in that row:
// for (int c = 0;c<map.width;c++){
// int tileToDraw = map.layerCollection[0].tiles[r*map.width+c].gid;
// std::cout << tileToDraw << ",";
// }
// std::cout << std::endl;
// }
// std::cout << map.height; // std::cout << map.height;
// std::cout << "Map width: " << map.width << " tiles wide by: " << map.height << " tiles high" << std::endl; // std::cout << "Map width: " << map.width << " tiles wide by: " << map.height << " tiles high" << std::endl;
// for (int t = 0;t<map.width;t++){ // for (int t = 0;t<map.width;t++){
// std::cout << layer.tiles[t].gid << std::endl; // std::cout << layer.tiles[t].gid << std::endl;
// } // }
} else {
std::cout << "Encountered error loading map file: " << error << std::endl;
} }
// std::string mapImage = map.tilesetCollection[0].name;
// std::string tileMapTexture = "assets/textures/tiles/" + map.tilesetCollection[0].name + ".png";
// std::cout << tileMapTexture << std::endl;
// assets->AddTexture("terrain", tileMapTexture.c_str());
// assets->AddTexture("terrain", "assets/textures/tiles/br-tiles.png");
assets->AddTexture("player", "assets/textures/actors/firefighter.png"); assets->AddTexture("player", "assets/textures/actors/firefighter.png");
assets->AddTexture("font", "assets/textures/ui/ui-font-cloud-sans.png"); assets->AddTexture("font", "assets/textures/ui/ui-font-cloud-sans.png");
assets->AddTexture("textBox", "assets/textures/ui/ui-element-cloud.png"); assets->AddTexture("textBox", "assets/textures/ui/ui-element-cloud.png");
@ -177,14 +186,13 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
my9Slice->MakeSlices("textBox",32,32,14,16,14,16,myDestRect,globalScale,Game::groupUI_Layer0); my9Slice->MakeSlices("textBox",32,32,14,16,14,16,myDestRect,globalScale,Game::groupUI_Layer0);
uiInfo.addComponent<TransformComponent>(camera.w-94,10,72,96,gScale); uiInfo.addComponent<TransformComponent>(camera.w-94,10,72,96,gScale);
uiInfo.addComponent<UITextComponent>("font", "CollisionHori: Vert: Jump: P.y : P.dy: YVec: ", 8, 12, 1); // uiInfo.addComponent<UITextComponent>("font", "CollisionHori: Vert: Jump: P.y : P.dy: YVec: ", 8, 12, 1);
uiInfo.addComponent<UITextComponent>("font", "Camera CamX: CamY: Jump: P.y : P.dy: YVec: ", 8, 12, 1);
uiInfo.addGroup(groupUI_Layer3); uiInfo.addGroup(groupUI_Layer3);
uiJumpInfo.addComponent<TransformComponent>(camera.w-48,48,40,12,gScale); uiJumpInfo.addComponent<TransformComponent>(camera.w-48,48,40,12,gScale);
uiJumpInfo.addComponent<UITextComponent>("font", "false", 8, 12, 1); uiJumpInfo.addComponent<UITextComponent>("font", "false", 8, 12, 1);
uiJumpInfo.addGroup(groupUI_Layer3); uiJumpInfo.addGroup(groupUI_Layer3);
// debugJumpText = new UIText(Game::BoolToString(playerIsJumping), "font", 0,0,8,12,1,"debug text",Game::groupUI_Layer3);
// debugJumpText->ParseString(Game::BoolToString(playerIsJumping),camera.w-50*globalScale,44*gScale,1,"debugJumpText",Game::groupUI_Layer3);
// debug UI box // debug UI box
SDL_Rect debugBoxRect = SDL_Rect(); SDL_Rect debugBoxRect = SDL_Rect();
@ -197,8 +205,6 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
//ecs implementation //ecs implementation
// map->LoadMap("assets/maps/br-map-color.txt",70,45, globalScale);
// player.addComponent<TransformComponent>(860*globalScale,640*globalScale,22,42,globalScale); // player.addComponent<TransformComponent>(860*globalScale,640*globalScale,22,42,globalScale);
player.addComponent<TransformComponent>(120*globalScale,120*globalScale,22,42,globalScale); player.addComponent<TransformComponent>(120*globalScale,120*globalScale,22,42,globalScale);
player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json"); player.addComponent<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json");
@ -212,10 +218,6 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
puppy.addComponent<SpriteComponent>("puppy", SpriteComponent::spriteObject); puppy.addComponent<SpriteComponent>("puppy", SpriteComponent::spriteObject);
puppy.addGroup(groupObjects); puppy.addGroup(groupObjects);
// enemy.addComponent<TransformComponent>(180*globalScale,180*globalScale,32,32,globalScale);
// enemy.addComponent<SpriteComponent>("robber", SpriteComponent::spriteAnimation, "assets/textures/actors/robberrodent.json");
// enemy.addGroup(groupEnemies);
} else { } else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't Initialize SDL: %s", SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't Initialize SDL: %s", SDL_GetError());
isRunning = false; isRunning = false;
@ -275,14 +277,6 @@ void Game::update()
// Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0); // Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0);
// } // }
// for (auto& letter : debugText)
// {
// if(letter->tag=="debugJumpText"){
// std::cout<<letter->tag<<std::endl;
// }
// }
// debugJumpText->ParseString(Game::BoolToString(playerIsJumping),260*gScale,44*gScale,1,"debugJumpText",Game::groupUI_Layer3);
manager.refresh(); manager.refresh();
manager.update(); manager.update();
@ -291,10 +285,9 @@ void Game::update()
SDL_Rect cCol = c->getComponent<ColliderComponent>().collider; SDL_Rect cCol = c->getComponent<ColliderComponent>().collider;
if(Collision::AABB(cCol, playerCol)) if(Collision::AABB(cCol, playerCol))
{ {
// printDebug("Collision Detected"); // if(!playerIsGrounded){
if(!playerIsGrounded){ // player.getComponent<SpriteComponent>().Play("Idle");
player.getComponent<SpriteComponent>().Play("Idle"); // }
}
// playerIsGrounded = true; // playerIsGrounded = true;
// player.getComponent<TransformComponent>().position.y = player.getComponent<TransformComponent>().lastSafePos.y; // player.getComponent<TransformComponent>().position.y = player.getComponent<TransformComponent>().lastSafePos.y;
// gravityOnPlayer = false; // gravityOnPlayer = false;
@ -325,10 +318,10 @@ void Game::update()
camera.x = 0; camera.x = 0;
if (camera.y < 0) if (camera.y < 0)
camera.y = 0; camera.y = 0;
// if (camera.x > map->width-camera.w) // if (camera.x > levelMap.w-camera.w)
// camera.x = map->width-camera.w; // camera.x = levelMap.w-camera.w;
// if (camera.y > map->height-camera.h) // if (camera.y > levelMap.h-camera.h)
// camera.y = map->height-camera.h; // camera.y = levelMap.h-camera.h;
if (Game::debugCollisionBoxes) if (Game::debugCollisionBoxes)
{ {
for (auto& c: colliders) for (auto& c: colliders)
@ -417,20 +410,8 @@ void Game::clean()
printf("Game Cleaned\n"); printf("Game Cleaned\n");
} }
std::string previousMessage = "";
void Game::printDebug(std::string debugInfo)
{
if (previousMessage != debugInfo)
{
std::cout << debugInfo;
printf("\n");
}
previousMessage = debugInfo;
}
void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue) void Game::drawLine(Vector2D srcpt, Vector2D destpt, int red, int green, int blue)
{ {
SDL_SetRenderDrawColor(renderer, red, green, blue, 255); SDL_SetRenderDrawColor(renderer, red, green, blue, 200);
SDL_RenderDrawLine(renderer, srcpt.x, srcpt.y, destpt.x, destpt.y); SDL_RenderDrawLine(renderer, srcpt.x, srcpt.y, destpt.x, destpt.y);
} }