0
\$\begingroup\$

I have a design problem with my Player and Level class in my game.

So below is a picture of the game. The problem is I don't want to move on the black space and only the white space. I know how to do this as all I need to do is get the check for the sf::Color::Black and I have methods to do this in the Level class. The problem is this piece of code

   void Game::input()
{
    player.input();
}

void Game::update()
{
    (*level).update();
    player.update();
}

void Game::render()
{
    (*level).render();
    player.render();
}

So as you there is a problem in that how do I get the map information from the Level class to the Player class.

Now I was thinking if I made the Player position static and pass it into the Level as parameter in update I could do it. The problem is interaction. I don't know what to do. I could maybe make player go into the Level class. However, what if I want multiple levels?

So I have big design problems that I'm trying to solve.

enter image description here

\$\endgroup\$
3
  • 2
    \$\begingroup\$ It seems unclear to me, from your description, why you need the player object to know anything about the level. The player has a position, right? When the player moves, you can compute what the new position would be, ask the level object if that position is passable (i.e., black or white) and then set the players new position. Please clarify your question a bit. \$\endgroup\$
    – user1430
    Nov 6, 2013 at 16:06
  • \$\begingroup\$ Yeah, please explain a bit more in detail (e.g. with examples) what you try to do... Else we have to guess which causes a lot of answers that do not help you (or people avoiding to answer, that would help you). \$\endgroup\$
    – SDwarfs
    Nov 6, 2013 at 16:12
  • \$\begingroup\$ Josh Petrie. The problem is I cannot get the information from the level into the player class. \$\endgroup\$
    – pangaea
    Nov 6, 2013 at 16:18

1 Answer 1

0
\$\begingroup\$

Now I was thinking if I made the Player position static and pass it into the Level as parameter in update I could do it. The problem is interaction. I don't know what to do. I could maybe make player go into the Level class. However, what if I want multiple levels?

If I understood your problem correctly, your problem is, that your player has no position on the map, if you do this. And this causes your problem with interactions, right?

The players position should never be "static". Only its position on the screen. To achieve this you should set the View port position (= camera position) relative to your players position. So the background scrolls, while the player stays at the middle of the screen.

This way you have a position of your player, relative to the map. You also have positions of things you can interact with, also relative to the map. Thus, you can implement collision / proximity detection or whatever you like easily.

EDIT #1

As you changed your questions title, I assume another possible problem. If you want the player class to get access your map...

Let us have a look at your code here...

void Game::update()
{
    (*level).update();
   player.update();
}

In this case your player.update should have the parameter "level".

void Game::update()
{
    (*level).update();
   player.update(level);
}

That's it.... the player can then access the level data.

\$\endgroup\$
3
  • \$\begingroup\$ Okay I will look into doing that. Passing in level into the player.update. I was thinking it might be easier to make Level class have a static Player player in the header. So I could have multiple levels with the player in them. \$\endgroup\$
    – pangaea
    Nov 6, 2013 at 16:21
  • \$\begingroup\$ Well, if you think about it it's more about the player needing access to the level then the level needing access to the player. And you can still have multiple levels, just pass in a different level. \$\endgroup\$
    – Jonkel
    Nov 6, 2013 at 16:37
  • \$\begingroup\$ You can also pass the player to level.update() or let it store a reference to the player object, if you need (pass it into the level object via the constructor method of level). But, what works best depends on there the game logic is actually implemented... If the logic is within player, it needs the level. If the logic is implemented within the level class, it needs the player object... Another possibility is that there might be multiple players, so you should implement an "add" method to add players to the level. Level.update should then also call the update function of the players. \$\endgroup\$
    – SDwarfs
    Nov 6, 2013 at 16:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .