Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help with Book (SFML Game Development By Example)  (Read 2758 times)

0 Members and 1 Guest are viewing this topic.

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Help with Book (SFML Game Development By Example)
« on: October 16, 2019, 04:34:20 am »
Hello,

I'm assuming there must be some people here who have read this awesome book. I'm towards the end now and I'm trying to do something very simple and it is not coming out right at all. It could be I am not understanding how they are positioning the tiles.

I'm trying to add some simple path-finding. The way I usually do it is grabbing the tile underneath position and adding it to a vector. Thus, creating the path to the destination. But I am having trouble just getting the tile position in general!

I have a simple function that displays values of various things to sf::Text, one being the position of the sprite the tile is on and it is never correct. It seems to only change when a new tile is underneath the player. For example, all the grass tiles say (2016x2016), but when I move to a tile that might be the edge of grass it changes. Yet I am retrieving the tile sprite from TileInfo and using getPosition. I tried to see what happened if I just changed the tiles color instead and no luck.

       
auto posComp = m_stateMgr->getContext()->m_entityManager->getComponent<C_Position>(m_player, Component::Position);
auto gameMap = m_stateMgr->getContext()->m_gameMap;

auto currTile = gameMap->getTileMap()->getTile(floor(posComp->getPosition().x/32), floor(posComp->getPosition().y)/32, 0);

std::cout << currTile->m_properties->m_sprite.getPosition().x << ", " << currTile->m_properties->m_sprite.getPosition().y << "\n";
 

I would really like to continue to use the engine provided in the book, so any help would be appreciated.  Thanks!

PS: For those who would like to help I can provide anything you would need to know!
« Last Edit: October 16, 2019, 04:37:06 am by SFMLNewGuy »

mequint

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Help with Book (SFML Game Development By Example)
« Reply #1 on: December 01, 2019, 01:48:51 am »
What you have done looks correct...let me see if this helps...

The Tile Map has no concept of a "position" itself.  It's merely a buffer of Tile Data.  When it is drawn to the screen, there is predefined Sheet data that provides the tile position and tile size to the Sprite when it's drawn.

So here is what I propose:
1. Get the players position via the ECS
2. Determine the players Tile Coordinates based on the Sheet sizes
(You have this so far)

3. Determine the tile you want to move to.
4. Convert that tile to the game position (x * TileSize, y * TileSize)
5. Set a Target for the player to Move To (Implementation is up to you but you will probably need a Component for Targeting, an Entity State to indicate movement is happening (I think it already exist), and an Entity Message to notify the State system that Target Movement is occurring.  The Movement System will need to update the Mover automatically while the Target is set.

Hope this helps.

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Help with Book (SFML Game Development By Example)
« Reply #2 on: December 02, 2019, 03:35:07 am »
Hello,

Thanks for the response. That makes more sense. I'll try it out!

Much appreciated

 

anything