TileMapComponent now takes a TMX to render tilemap
This commit is contained in:
parent
dc43e59ff6
commit
72b4ac9229
6
Makefile
6
Makefile
@ -21,7 +21,7 @@ LINKER_FLAGS = -lSDL2 -lSDL2_image -lSDL2_mixer -lxml2 -ltmxparser
|
||||
|
||||
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
|
||||
# $(CC) $(OBJS) -o $@ $(LDFLAGS)
|
||||
$(CXX) $(OBJS) $(LINKER_FLAGS) -o $@
|
||||
LD_LIBRARY_PATH=/home/ayoungblood/project/BeagleRescue/libtmx-parser/ $(CXX) $(OBJS) $(LINKER_FLAGS) -o $@
|
||||
|
||||
# assembly
|
||||
$(BUILD_DIR)/%.s.o: %.s
|
||||
@ -31,12 +31,12 @@ $(BUILD_DIR)/%.s.o: %.s
|
||||
# c source
|
||||
$(BUILD_DIR)/%.c.o: %.c
|
||||
$(MKDIR_P) $(dir $@)
|
||||
$(CXX) $(CPPFLAGS) $(CFLAGS) $(LINKER_FLAGS) -c $< -o $@
|
||||
LD_LIBRARY_PATH=/home/ayoungblood/project/BeagleRescue/libtmx-parser/ $(CXX) $(CPPFLAGS) $(CFLAGS) $(LINKER_FLAGS) -c $< -o $@
|
||||
|
||||
# c++ source
|
||||
$(BUILD_DIR)/%.cpp.o: %.cpp
|
||||
$(MKDIR_P) $(dir $@)
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LINKER_FLAGS) -c $< -o $@
|
||||
LD_LIBRARY_PATH=/home/ayoungblood/project/BeagleRescue/libtmx-parser/ $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LINKER_FLAGS) -c $< -o $@
|
||||
|
||||
|
||||
.PHONY: clean
|
||||
|
Binary file not shown.
@ -3,5 +3,5 @@
|
||||
"WindowName":"Beagle Rescue",
|
||||
"WindowSize":{"w":320,"h":240},
|
||||
"WindowFullScreen": 0,
|
||||
"GlobalScale": 1
|
||||
"GlobalScale": 3
|
||||
}
|
||||
|
@ -15,16 +15,20 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include "tmxparser.h"
|
||||
|
||||
class TileMapComponent : public Component
|
||||
{
|
||||
public:
|
||||
TransformComponent *transform;
|
||||
// TransformComponent *transform;
|
||||
SDL_Texture* texture;
|
||||
SDL_Rect srcRect, destRect;
|
||||
Vector2D position;
|
||||
std::tuple <SDL_Rect,SDL_Rect> tile;
|
||||
|
||||
// std::tuple <SDL_Rect,SDL_Rect> tile;
|
||||
tmxparser::TmxMap map;
|
||||
int globalScale;
|
||||
std::vector<SDL_Rect> tileSet;
|
||||
|
||||
TileMapComponent() = default;
|
||||
|
||||
~TileMapComponent()
|
||||
@ -32,19 +36,28 @@ public:
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
|
||||
TileMapComponent(std::string id, int tsize, int tscale)
|
||||
TileMapComponent(tmxparser::TmxMap loadedMap, int gScale)
|
||||
{
|
||||
setTex(id);
|
||||
// position.x = xpos;
|
||||
// position.y = ypos;
|
||||
//
|
||||
// srcRect.x = srcX;
|
||||
// srcRect.y = srcY;
|
||||
srcRect.w = srcRect.h = tsize;
|
||||
|
||||
// destRect.x = xpos;
|
||||
// destRect.y = ypos;
|
||||
destRect.w = destRect.h = tsize * tscale;
|
||||
map = loadedMap;
|
||||
std::string texturePath = "assets/textures/tiles/" + loadedMap.tilesetCollection[0].name + ".png";
|
||||
Game::assets->AddTexture(loadedMap.tilesetCollection[0].name, texturePath.c_str());
|
||||
setTex(map.tilesetCollection[0].name);
|
||||
globalScale = gScale;
|
||||
int totalTiles = map.tilesetCollection[0].colCount*map.tilesetCollection[0].rowCount;
|
||||
// std::cout << "Number of Tiles: " << totalTiles << std::endl;
|
||||
tileSet.resize(totalTiles);
|
||||
for (int r=0;r<map.tilesetCollection[0].rowCount;r++){
|
||||
for (int c=0;c<map.tilesetCollection[0].colCount;c++){
|
||||
srcRect.x = c*map.tilesetCollection[0].tileWidth;
|
||||
srcRect.y = r*map.tilesetCollection[0].tileHeight;
|
||||
srcRect.w = srcRect.h = map.tileWidth;
|
||||
int element = r*map.tilesetCollection[0].colCount+c;
|
||||
// std::cout << "element " << element << std::endl;
|
||||
// std::cout << "srcRect= x: " << srcRect.x << " y: " << srcRect.y << " srcRect.w & h: " << srcRect.w << std::endl;
|
||||
tileSet[element] = srcRect;
|
||||
}
|
||||
}
|
||||
destRect.w = destRect.h = map.tileWidth * gScale;
|
||||
}
|
||||
|
||||
void update() override
|
||||
@ -56,19 +69,19 @@ public:
|
||||
void draw() override
|
||||
{
|
||||
//iterate through rows and columns of the map to draw the tiles
|
||||
|
||||
TextureManager::Draw(texture, srcRect, destRect, SDL_FLIP_NONE);
|
||||
// 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[c].gid-1;
|
||||
destRect.x = c*map.tilesetCollection[0].tileWidth*globalScale;
|
||||
destRect.y = r*map.tilesetCollection[0].tileWidth*globalScale;
|
||||
TextureManager::Draw(texture, tileSet[tileToDraw], destRect, SDL_FLIP_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::tuple<SDL_Rect,SDL_Rect> getTile(char currentTile, int i)
|
||||
{
|
||||
std::tuple<SDL_Rect,SDL_Rect> tileTuple;
|
||||
|
||||
|
||||
tileTuple = std::make_tuple(srcRect,destRect);
|
||||
return tileTuple;
|
||||
}
|
||||
|
||||
|
||||
void setTex(std::string id)
|
||||
{
|
||||
texture = Game::assets->GetTexture(id);
|
||||
|
@ -52,6 +52,8 @@ auto& uiInfo(manager.addEntity());
|
||||
auto& uiJumpInfo(manager.addEntity());
|
||||
auto& uiTextInstructions(manager.addEntity());
|
||||
|
||||
auto& gameScene(manager.addEntity());
|
||||
|
||||
bool Game::debugCollisionBoxes = false;
|
||||
bool Game::gravityOnPlayer = true;
|
||||
bool Game::playerIsGrounded = false;
|
||||
@ -127,11 +129,21 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
|
||||
// current_time = SDL_GetTicks();
|
||||
if (!error)
|
||||
{
|
||||
printf("Yay! Tile map loaded with no errors.\n");
|
||||
std::cout << "Map width: " << map.width << " tiles wide by: " << map.height << " tiles high" << std::endl;
|
||||
// printf("Yay! Tile map loaded with no errors.\n");
|
||||
tmxparser::TmxLayer layer = map.layerCollection[0];
|
||||
// std::cout << map.height;
|
||||
// std::cout << "Map width: " << map.width << " tiles wide by: " << map.height << " tiles high" << std::endl;
|
||||
// for (int t = 0;t<map.width;t++){
|
||||
// std::cout << layer.tiles[t].gid << std::endl;
|
||||
// }
|
||||
}
|
||||
|
||||
assets->AddTexture("terrain", "assets/textures/tiles/br-tiles.png");
|
||||
// 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("font", "assets/textures/ui/ui-font-cloud-sans.png");
|
||||
assets->AddTexture("textBox", "assets/textures/ui/ui-element-cloud.png");
|
||||
@ -147,10 +159,12 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
|
||||
|
||||
// map = new Map("terrain",globalScale,16);
|
||||
|
||||
gameScene.addComponent<TileMapComponent>(map,gScale);
|
||||
gameScene.addGroup(groupMap);
|
||||
// std::string myText = "Find lost puppies!\nThey need your help!";
|
||||
std::string myText = "Press U to Start";
|
||||
|
||||
uiTextInstructions.addComponent<TransformComponent>(18*gScale,22*gScale,138*gScale,20*gScale,gScale);
|
||||
|
||||
uiTextInstructions.addComponent<TransformComponent>(18,22,138,20,gScale);
|
||||
uiTextInstructions.addComponent<UITextComponent>("font",myText,8,12,gScale);
|
||||
uiTextInstructions.addGroup(groupUI_Layer1);
|
||||
|
||||
@ -162,11 +176,11 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
|
||||
my9Slice = new UINineSlice("textBox");
|
||||
my9Slice->MakeSlices("textBox",32,32,14,16,14,16,myDestRect,globalScale,Game::groupUI_Layer0);
|
||||
|
||||
uiInfo.addComponent<TransformComponent>(camera.w-94*gScale,10*gScale,72*gScale,96*gScale,1);
|
||||
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.addGroup(groupUI_Layer3);
|
||||
|
||||
uiJumpInfo.addComponent<TransformComponent>(camera.w-48*gScale,48*gScale,40*gScale,12*gScale,1);
|
||||
uiJumpInfo.addComponent<TransformComponent>(camera.w-48,48,40,12,gScale);
|
||||
uiJumpInfo.addComponent<UITextComponent>("font", "false", 8, 12, 1);
|
||||
uiJumpInfo.addGroup(groupUI_Layer3);
|
||||
// debugJumpText = new UIText(Game::BoolToString(playerIsJumping), "font", 0,0,8,12,1,"debug text",Game::groupUI_Layer3);
|
||||
@ -185,7 +199,8 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
|
||||
|
||||
// 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<SpriteComponent>("player", SpriteComponent::spriteAnimation, "assets/textures/actors/firefighter.json");
|
||||
|
||||
// player.addComponent<PlayerController>(0.0,0.0,false,false,Vector2D().Zero());
|
||||
@ -280,20 +295,20 @@ void Game::update()
|
||||
if(!playerIsGrounded){
|
||||
player.getComponent<SpriteComponent>().Play("Idle");
|
||||
}
|
||||
playerIsGrounded = true;
|
||||
// playerIsGrounded = true;
|
||||
// player.getComponent<TransformComponent>().position.y = player.getComponent<TransformComponent>().lastSafePos.y;
|
||||
gravityOnPlayer = false;
|
||||
// gravityOnPlayer = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Gravity
|
||||
if (gravityOnPlayer){
|
||||
player.getComponent<TransformComponent>().position.y += 3*gScale;
|
||||
uiJumpInfo.getComponent<UITextComponent>().updateString("true");
|
||||
} else {
|
||||
uiJumpInfo.getComponent<UITextComponent>().updateString("false");
|
||||
}
|
||||
// if (gravityOnPlayer){
|
||||
// player.getComponent<TransformComponent>().position.y += 3*gScale;
|
||||
// uiJumpInfo.getComponent<UITextComponent>().updateString("true");
|
||||
// } else {
|
||||
// uiJumpInfo.getComponent<UITextComponent>().updateString("false");
|
||||
// }
|
||||
// for(auto& p: projectiles)
|
||||
// {
|
||||
// if(Collision::AABB(player.getComponent<ColliderComponent>().collider, p->getComponent<ColliderComponent>().collider))
|
||||
|
Loading…
Reference in New Issue
Block a user