0
\$\begingroup\$

I have a 2d map of integers

0000000000
0200000001
1111100011

0 = sky/empty
1 = ground
2 = player

When player falls in a hole/cliff I want the player to go below the map, so it will look like the player had died by falling in the hole/cliff.

Here is what I did:

if(player.getY()+player.getHeight() > map.getHeight()){
   //player has fallen in hole/clifff
}

The code above will test if the player's feet touch the end of map. But how to move him below the map? Because if I move the player down then the array will be out of bounds.

Is there way without changing the map array?

\$\endgroup\$
  • \$\begingroup\$ You should make the underlying array a bit larger, or give it a bottom row which doesn't appear on the screen, but still exists in memory. That way, all you have to do is move the player into a coordinate in that row. They won't appear, but they also won't fall "out of bounds". \$\endgroup\$ – Shotgun Ninja Oct 10 '13 at 18:24
4
\$\begingroup\$

Don't save the player position inside the map, save it external.

You could also lay an simple interface around the raw map access to check for the position where the player is currently. If an access to that position were made you just return a 2.

The design as an simpified UML:

IMap
+ int getObjectAtPosition(int x, int y)
+ Vector2i getSize()

Vector2i is a 2d vector with x and y components. Of course the implementation for this interface must have an method for tetting the player position and getObjectAtPosition should do the above mentioned stuff.

To check for the death of the player because he is outside the map you need to check the height against the minimal height of the map before an access to the array. If it is below the borders just let the player die or do some other fancy stuff.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.